[英]Error when broadcasting to numpy matrix
I know this is a relatively common topic on stackoverflow but I couldn't find the answer I was looking for. 我知道这是关于stackoverflow的相对常见的话题,但是我找不到想要的答案。 Basically, I am trying to make very efficient code (I have rather large data sets) to get certain columns of data from a matrix.
基本上,我正在尝试编写非常有效的代码(我有相当大的数据集)以从矩阵中获取某些数据列。 Below is what I have so far.
以下是到目前为止的内容。 It gives me this error: could not broadcast input array from shape (2947,1) into shape (2947)
它给了我这个错误:无法将输入数组从形状(2947,1)广播到形状(2947)
def get_data(self, colHeaders):
temp = np.zeros((self.matrix_data.shape[0],len(colHeaders)))
for col in colHeaders:
index = self.header2matrix[col]
temp[:,index:] = self.matrix_data[:,index]
data = np.matrix(temp)
return temp
Maybe this simple example will help: 也许这个简单的例子会有所帮助:
In [70]: data=np.arange(12).reshape(3,4)
In [71]: header={'a':0,'b':1,'c':2}
In [72]: col=['c','a']
In [73]: index=[header[i] for i in col]
In [74]: index
Out[74]: [2, 0]
In [75]: data[:,index]
Out[75]:
array([[ 2, 0],
[ 6, 4],
[10, 8]])
data
is some sort of 2D array, header
is a dictionary mapping names to column numbers. data
是某种2D数组, header
是将名称映射到列号的字典。 Using the input col
, I construct a column index list. 使用输入
col
,我构造了一个列索引列表。 You can select all columns at once, rather than one by one. 您可以一次选择所有列,而不是一个一个地选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.