简体   繁体   English

使用矩阵作为字典值并在python中执行numpy矩阵运算

[英]Using a matrix as a dictionary value and performing numpy matrix operations in python

I have a dictionary with key and value (it was given to me). 我有一本包含键和值的字典(这是给我的)。 When I read a key to extract the value, I get something like this: matrix([[1.234, -4.056]]) 当我读取一个用于提取值的键时,我得到的是这样的:matrix([[1.234,-4.056]])

I call this value A. 我将此值称为A。

I define a variable as below 我定义一个变量如下

B = np.matrix([0, 0])

B is a running sum of A times a group of scalars, B是A乘以一组标量的总和,

for i in range(0, n):

     B =+ A*scalar[i]

The problem is that the output has the format 问题是输出具有格式

matrix([[xxx , yyy]]) 矩阵([[xxx,yyy]])

and I need 我需要

matrix([xxx, yyy]) 矩阵([xxx,yyy])

that is, I do not want the double brackets. 也就是说,我不需要双括号。

You want a numpy.array not a numpy.matrix . 您想要一个numpy.array而不是numpy.matrix The np.matrix docs is a 2D data structure, np.array is an nd dimensional structure. np.matrix docs是2D数据结构, np.arraynd维结构。

If you look at B.shape immediately after creating you will discover that it is (1,2) , not (2,) as you intended. 如果在创建后立即查看B.shape ,您会发现它是(1,2) ,而不是您想要的(2,)

B.A.reshape(2,)  # or B.A1

will give you a np.array that is 1-dimensional. 将为您提供一维的np.array

NumPy arrays ARE NOT equivalent to NumPy matrices, the meaning of operations is different between the two, for instance * is a dot product for matrix but an element wise product for an array . NumPy数组不等同于NumPy矩阵,两者的运算含义不同,例如*matrix的点积,而array是元素明智的积。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM