我正在为我的数据建立一个相关矩阵,看起来像这样
df <- structure(list(V1 = c(56, 123, 546, 26, 62, 6, NA, NA, NA, 15
), V2 = c(21, 231, 5, 5, 32, NA, 1, 231, 5, 200), V3 = c(NA,
NA, 24, 51, 53, 231, NA, 153, 6, 700), V4 = c(2, 10, NA, 20,
56, 1, 1, 53, 40, 5000)), .Names = c("V1", "V2", "V3", "V4"), row.names = c(NA,
10L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
这给出了以下数据框:
V1 V2 V3 V4
1 56 21 NA 2
2 123 231 NA 10 …Run Code Online (Sandbox Code Playgroud) 用 python 编写的项目,带有一些 C 扩展(不使用 SWIG 等)。我正在尝试弄清楚如何构建我的项目,以便:
当前的结构如下所示:
Project\
docs\ # mainly documentation, right?
bin\ # empty
setup.py # setup for the project, as suggested in the above link
project\
__init__.py
module.py
tests\
bla_test.py
C\ # the package of C files
file.c
file.so
other_c_stuff.c
header.h
setup.py # setup to compile the C files and create .so files
build\ # contaisn a bunch of (hopefully) irrelevant stuf
Run Code Online (Sandbox Code Playgroud)
它可以在 PyDev 中运行,但不能在 shell 中运行。理想的答案将解决以下问题:
我正在包装一个C文件,所以我可以在python中使用它.C函数的输出是双精度数组.我希望这是python中的一个numpy数组.我得到了垃圾.这是一个生成错误的示例.
首先,C文件(专注于最后一个函数定义,其他一切应该没问题):
#include <Python.h>
#include <numpy/arrayobject.h>
#include <stdio.h>
static char module_docstring[] =
"docstring";
static char error_docstring[] =
"generate the error";
static PyObject *_aux_error(PyObject *self, PyObject *args);
static PyMethodDef module_methods[] = {
{"error", _aux_error, METH_VARARGS, error_docstring},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC init_tmp(void) {
PyObject *m = Py_InitModule3("_tmp", module_methods, module_docstring);
if (m == NULL)
return;
/* Load `numpy` functionality. */
import_array();
}
static PyObject *_aux_error(PyObject *self ,PyObject *args) {
double vector[2] = {1.0 , 2.0};
npy_intp dims[1] = { 2 }; …Run Code Online (Sandbox Code Playgroud) 我想将一个函数应用于滚动窗口。我在这里看到的所有答案都集中在应用于单行/列,但我想将我的功能应用于整个窗口。这是一个简化的示例:
import pandas as pd
data = [ [1,2], [3,4], [3,4], [6,6], [9,1], [11,2] ]
df = pd.DataFrame(columns=list('AB'), data=data)
Run Code Online (Sandbox Code Playgroud)
这是df:
A B
0 1 2
1 3 4
2 3 4
3 6 6
4 9 1
5 11 2
Run Code Online (Sandbox Code Playgroud)
取一些函数应用于整个窗口:
df.rolling(3).apply(lambda x: x.shape)
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我想得到类似的东西:
some_name
0 NA
1 NA
2 (3,2)
3 (3,2)
4 (3,2)
5 (3,2)
Run Code Online (Sandbox Code Playgroud)
当然,这里以形状为例说明f将整个窗口作为计算对象,而不仅仅是一行/列。我尝试使用axis关键字 forrolling以及raw关键字 forapply但没有成功。其他方法 ( agg, transform) 似乎也没有提供。 …
我上课了.该类包含一个函数.我希望每隔一段时间以相同的方式更改此功能.如果我使用lambda,我会得到无限的递归.我理解为什么我会这样,我想找到一个优雅的解决方案.
def func(s):
return 1 # some not interesting function
class cls: # a class
def __init__(self , f):
self.f = f
c = cls(func)
c.f = lambda x: c.f(x) + 1 # i want c.f to return c.f(x) + 1
print(c.f(1)) # causes infinite recursion
Run Code Online (Sandbox Code Playgroud)
我不想这样做
c.f = lambda x: func(x) + 1
Run Code Online (Sandbox Code Playgroud)
因为我想以c.f同样的方式不止一次地改变.
以下代码(从此处复制)在纽约市和新德里之间生成一条漂亮的测地线。仔细检查后,测地线看起来并不平滑。怎样才能让它看起来光滑呢?
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
linewidth=2, marker='o', transform=ccrs.Geodetic())
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)
以下给出错误:
print numpy.linalg.norm(2) # returns 2
print numpy.linalg.norm(2, np.inf) # returns error,
print numpy.linalg.norm(2, np.inf) # returns the same error:
ValueError: Improper number of dimensions to norm.
Run Code Online (Sandbox Code Playgroud)
如何在非 numpy 数组输入中使用上述规范?