简体   繁体   English

Numpy 如何用另一个向量分配矩阵列的值?

[英]Numpy How to assign values of a column of a matrix with another vector?

I'm trying to make an empty 3x2 matrix, then replace each column into randomly generated vectors.我正在尝试制作一个空的 3x2 矩阵,然后将每一列替换为随机生成的向量。

Therefore, I tried to run the following code:因此,我尝试运行以下代码:


import numpy as np

A = np.empty(shape=(3,2))

x1 = np.random.rand(3,1)
x2 = np.random.rand(3,1)

A[:,1] = x1
A[:,2] = x2

However, when I tried to run the code, I got the following error message:但是,当我尝试运行代码时,我收到以下错误消息:


    A[:,1] = x1

ValueError: could not broadcast input array from shape (3,1) into shape (3)

How can I fix the error?如何修复错误?

Thank you.谢谢你。

There are several things wrong here.这里有几件事是错误的。 First you're trying to assign to a slice a higher dimensional array:首先,您尝试为切片分配更高维的数组:

A[:,0].shape
# (3,)

x1.shape
#(3, 1)

On another side, you're wrongly indexing, indices in numpy (and python more generally) begin at position 0 .另一方面,您错误地索引, numpy (以及更一般的 python )中的索引从 position 0开始。 So taking these aspects into consideration, you could either assign as:因此,考虑到这些方面,您可以指定为:

A = np.empty(shape=(3,2))

x1 = np.random.rand(3,1)
x2 = np.random.rand(3,1)

A[:,0] = x1.ravel()
A[:,1] = x2.ravel()

A
array([[0.2331048 , 0.2974727 ],
       [0.6789782 , 0.9680256 ],
       [0.0151457 , 0.05476883]])

Or note that np.random.rand can generate arrays of multiple dimensions:或者注意np.random.rand可以生成多个维度的arrays:

np.random.rand(3,2)
array([[0.10108146, 0.14859229],
       [0.55174044, 0.7399697 ],
       [0.38104021, 0.32287851]])
​
  • A is of size 3 X 2 ie it has 3 rows and 2 columns A的大小为3 X 2 ,即它有3行和2
  • A[:,1] implies all rows of A and second column. A[:,1]表示A所有行和第二列。 Array are 0 indexed in python数组在 python 中索引为 0
  • A[:,1] is a column vector so you can assign any vector of size 3 into it A[:,1]是一个列向量,因此您可以将任何大小为 3 的向量分配给它
  • np.random.rand(3,1) returns a numpy array (matrix) or size 3 X 1 . np.random.rand(3,1)返回一个 numpy 数组(矩阵)或大小3 X 1 But what you want is a vector ie np.random.rand(3)但是你想要的是一个向量,即np.random.rand(3)
A = np.empty(shape=(3,2))

x1 = np.random.rand(3)
x2 = np.random.rand(3)

A[:,0] = x1
A[:,1] = x2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将(行)numpy.matrix 分配给 numpy.matrix 中的列 - How to assign a (row) numpy.matrix to a column in a numpy.matrix Python,Numpy:无法将numpy数组的值分配给矩阵的列 - Python, Numpy: Cannot assign the values of a numpy array to a column of a matrix Numpy根据另一个数组的值分配一个数组值,并根据向量选择列 - Numpy assign an array value based on the values of another array with column selected based on a vector NumPy矩阵加列向量 - NumPy matrix plus column vector 如何在大型numpy数组中分配列值? - How to assign column values in large numpy array? 如何将元素值元素分配给theano矩阵? Numpy和Theano的区别? - How to assign values elementwise to theano matrix ? Difference between Numpy and Theano? 如何根据另一个矩阵的行和列的乘积分配一个 python 矩阵? - How to assign a python matrix based on the product of the row and column of another matrix? 如何将 numpy 向量转换为矩阵,其中矩阵中的每一列都包含初始向量中各个元素周围的范围? - How to turn a numpy vector into a matrix, where each column in the matrix contains a range around the respective element in the initial vector? 如何将变量分配给numpy矩阵 - How to assign variables to numpy matrix 如何用另一个数组中的值替换 numpy 矩阵列中的值? - How to replace values in numpy matrix columns with values from another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM