嵌套Python numpy数组维度混乱 [英] nested Python numpy arrays dimension confusion
问题描述
假设我有一个如下构造的numpy数组c
:
Suppose I have a numpy array c
constructed as follows:
a = np.zeros((2,4))
b = np.zeros((2,8))
c = np.array([a,b])
我希望c.shape
是(2,1)
或(2,)
,但是它是(2,2)
.另外,我想做的是将一个列向量连接到a
上,但是可以通过以下方式通过c
访问它:
I would have expected c.shape
to be (2,1)
or (2,)
but instead it is (2,2)
. Additionally, what I want to do is concatenate a column vector of ones onto a
, but by accessing it through c
in the following way:
c0 = c[0] # I would have expected this to be 'a'
np.concatenate((np.ones((c0.shape[0], 1)), c0), axis=1)
这当然是行不通的,因为c[0]
不等于我期望的a
,我得到
This of course doesn't work because c[0]
does not equal a
as I expected, and I get
ValueError: all the input arrays must have same number of dimensions
我需要某种方式来创建一个成对的数组(或列表),每个成对组件都是一个numpy数组,并且我需要访问该对中的第一个数组以将一个成对的列向量连接起来.我的应用程序是机器学习,我的数据将以所描述的格式提供给我,但是我需要在开始时修改数据,以便为它添加一个bias元素.
I need some way to have an array (or list) of pairs, each pair component being a numpy array, and I need to access the first array in the pair in order to concatenate a column vector of ones to it. My application is machine learning and my data will be coming to me in the format described, but I need to modify the data at the start in order to add a bias element to it.
编辑:我正在使用Python 2.7和Numpy 1.8.2
I'm using Python 2.7 and Numpy 1.8.2
推荐答案
通常,NumPy数组的嵌套NumPy数组不是很有用. 如果您使用NumPy来提高速度,通常最好坚持使用NumPy数组 具有相同的基本数字dtype.
Generally, nested NumPy arrays of NumPy arrays are not very useful. If you are using NumPy for speed, usually it is best to stick with NumPy arrays with a homogenous, basic numeric dtype.
要将两个项目放置在数据结构中,以使c[0]
返回第一个项目,
而c[1]
是第二个,将使用诸如c = [a, b]
之类的列表(或元组).
To place two items in a data structure such that c[0]
returns the first item,
and c[1]
the second, a list (or tuple) such as c = [a, b]
will do.
顺便说一句,如果您使用的是statemodels
包,则可以使用sm.add_constant
添加常量列:
By the way, if you are using the statemodels
package, then you can add a constant column with sm.add_constant
:
import numpy as np
import statsmodels.api as sm
a = np.random.randint(10, size=(2,4))
print(a)
# [[2 3 9 6]
# [0 2 1 1]]
print(sm.add_constant(a))
[[ 1. 2. 3. 9. 6.]
[ 1. 0. 2. 1. 1.]]
但是请注意,如果a
已经包含一个常量列,则不会添加任何额外的列:
Note however that if a
already contains a constant column, no extra column is added:
In [126]: sm.add_constant(np.zeros((2,4)))
Out[126]:
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
这篇关于嵌套Python numpy数组维度混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!