假设我有以下内容numpy.array:
In[]: x
Out[]:
array([[1, 2, 3, 4, 5],
[5, 2, 4, 1, 5],
[6, 7, 2, 5, 1]], dtype=int16)
In[]: y
Out[]:
array([[-3, -4],
[-4, -1]], dtype=int16)
Run Code Online (Sandbox Code Playgroud)
我想替换xby的子数组y并尝试了以下操作:
In[]: x[[0,2]][:,[1,3]]= y
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望这种情况发生:
In[]: x
Out[]:
array([[1, -3, 3, -4, 5],
[5, 2, 4, 1, 5],
[6, -4, 2, -1, 1]], dtype=int16)
Run Code Online (Sandbox Code Playgroud)
赋值行没有给我任何错误,但是当我检查输出时x
In[]: x
Run Code Online (Sandbox Code Playgroud)
我发现这x没有改变,即作业没有发生。
我怎样才能完成这项任务?为什么任务没有完成?
新手RCpp问题:如何让NumericVector返回NAR?例如,假设我有一个RCpp代码,它分配NA给向量的第一个元素.
// [[RCpp::export]]
NumericVector myFunc(NumericVector x) {
NumericVector y=clone(x);
y[0]=NA; // <----- what's the right expression here?
return y;
}
Run Code Online (Sandbox Code Playgroud) 我是C++和Rcpp的新手.假设,我有一个向量
t1<-c(1,2,NA,NA,3,4,1,NA,5)
Run Code Online (Sandbox Code Playgroud)
我想得到一个t1元素的索引NA.我可以写:
NumericVector retIdxNA(NumericVector x) {
// Step 1: get the positions of NA in the vector
LogicalVector y=is_na(x);
// Step 2: count the number of NA
int Cnt=0;
for (int i=0;i<x.size();i++) {
if (y[i]) {
Cnt++;
}
}
// Step 3: create an output matrix whose size is same as that of NA
// and return the answer
NumericVector retIdx(Cnt);
int Cnt1=0;
for (int i=0;i<x.size();i++) {
if (y[i]) {
retIdx[Cnt1]=i+1;
Cnt1++;
}
}
return retIdx;
} …Run Code Online (Sandbox Code Playgroud) 因为NumericVector,我可以NumericVector使用IntegerVector包含位置到子集的包含较小的子集.
例如假设x<-c(1,2,2,3,4,5),idx<-c(1,3,4)和xsub<-x[idx]这是1 2 3.
在RCpp内,我可以简单地使用xsub=x[idx].
有没有类似的方法来NumericMatrix使用IntegerVector?的行的子集?
例如,以下代码xmatsub=xmat(idx,_)对我不起作用.
在pandas的read_csv函数中是否有任何选项可以自动将objectdtype的每个项目转换为str.
例如,在尝试读取CSV文件时,我得到以下信息:
mydata = pandas.read_csv(myfile, sep="|", header=None)
C:\...\pandas\io\parsers.py:1159: DtypeWarning: Columns (6,635) have mixed types. Specify dtype option on import or set low_memory=False.
data = self._reader.read(nrows)
是否存在这样的方式:(i)警告被禁止打印,但(ii)我可以从字符串中捕获警告消息,从中我可以提取特定列,例如在这种情况下为6和635(这样我就可以修复dtype后续)?或者,或者,如果我可以指定何时存在mixed types,该read_csv函数应该将该列中的值转换为str?
我使用的是Python 3.4.2和Pandas 0.15.2
假设我有一个班级:
import copy
class TestClass:
def __init__(self, value):
if isinstance(value, TestClass):
self = value.deepcopy()
else:
self.data = value
def deepcopy(self):
return copy.deepcopy(self)
Run Code Online (Sandbox Code Playgroud)
我想编写代码,以便如果一个类的实例由同一个类的另一个实例初始化,它将成为deepcopy第二个类的一个.
现在,如果我尝试
In []: x = TestClass(3)
In []: x.data
Out[]: 3
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试
In []: y = TestClass(x)
Out[]: y.data
...
AttributeError: 'TestClass' object has no attribute 'data'
Run Code Online (Sandbox Code Playgroud)
为什么没有deepcopy在实例x传递给它时发生y?
这可能是一个非常愚蠢的问题,但我一直在努力,并且在文档中找不到它.
我正在尝试使用此处给出的描述进行二次规划.这里的文档仅涵盖了将2维numpy数组转换为cvxopt数组,而不是1维numpy数组.
我q的目标函数(1/2)x' P x + q' x向量是一个numpy向量,比如大小n.
我尝试q通过以下方式从numpy 转换为cvxopt:
import cvxopt as cvx
cvx_q = cvx.matrix(q) # didn't work
cvx_q = cvx.matrix(q, (n, 1)) # didn't work
cvx_q = cvx.matrix(np.array([q])) # didn't work
cvx_q = cvx.matrix(np.array([q]), (1, n)) # didn't work
cvx_q = cvx.matrix(np.array([q]), (n, 1)) # didn't work
Run Code Online (Sandbox Code Playgroud)
在所有情况下,我都会得到答案TypeError: buffer format not supported.
然而,numpy矩阵似乎工作正常,例如
cvx_p = cvx.matrix(p) # works fine, …Run Code Online (Sandbox Code Playgroud) 什么是Matlab typecast在R中的功能?在Python?在朱莉娅?这里给出了Matlab的类型转换函数的描述:类型转换
例如,在Matlab中
X = uint32([1 255 256])
X =
1 255 256
Y = typecast(X, 'uint8') # little endian
Y =
1 0 0 0 255 0 0 0 0 1 0 0
Run Code Online (Sandbox Code Playgroud)
谢谢
请注意:我不是在寻找R/Python的/朱莉娅相当于Matlab的的cast功能(例如我不找as.integer,as.character在R)
编辑:
感谢Julia/R/Python的答案.StackOverflow允许我选择一个答案,但我投了所有答案.
是否有任何简单的语法来提取RCpp中的一行DataFrame(仅包含numeric)作为DataFrame类的一部分或作为其他类的一部分?
目前我一直在做以下事情:
DataFrame myFunc(DataFrame& x) {
...
// Suppose I need to get the 10th row
int nCols=x.size();
std::vector<double> y;
y.reserve(nCols);
for (int j=0; j<nCols;j++) {
y.push_back((as<vector<double> >(x[j]))[9]); // index in C++ starts at 0
}
...
}
Run Code Online (Sandbox Code Playgroud)