scipy中的csv到矩阵
我无法对数据进行简单的矩阵运算,我一生都无法弄清楚我做错了什么:
data = np.genfromtxt(dataset1, names=True, delimiter=",", dtype=float)
X = np.matrix(data)
print(X.T*X)
Traceback (most recent call last):
File "genfromtxt.py", line 11, in <module>
print(X.T*X)
File "/usr/lib/pymodules/python2.6/numpy/matrixlib/defmatrix.py", line 319, in __mul__
return N.dot(self, asmatrix(other))
TypeError: can't multiply sequence by non-int of type 'tuple'
print(data) 给出:
[ (3.0, 32.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.5606799999999996, 9.0)
(4.0, 43.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.7203099999999996, 16.0)
(5.0, 40.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.9964500000000003, 25.0)
...,
(5.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.2146100000000004, 25.0)
(6.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.2915700000000001, 36.0)
(7.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.3716100000000004, 49.0)]
编辑:
此外,此代码
reader = csv.reader(open(dataset1, 'r'))
header = reader.next()
X = np.array([[float(col) for col in row] for row in reader])
print(X.shape)
print(X.T.shape)
print(X * X.T)
给出了以下输出:
(4165, 13)
(13, 4165)
Traceback (most recent call last):
File "genfromtxt.py", line 17, in <module>
print(X * X.T)
ValueError: shape mismatch: objects cannot be broadcast to a single shape
>>>
I can't get simple matrix operations to work on data, for the life of me I haven't been able to figure out what I'm doing incorrectly:
data = np.genfromtxt(dataset1, names=True, delimiter=",", dtype=float)
X = np.matrix(data)
print(X.T*X)
Traceback (most recent call last):
File "genfromtxt.py", line 11, in <module>
print(X.T*X)
File "/usr/lib/pymodules/python2.6/numpy/matrixlib/defmatrix.py", line 319, in __mul__
return N.dot(self, asmatrix(other))
TypeError: can't multiply sequence by non-int of type 'tuple'
print(data) gives:
[ (3.0, 32.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.5606799999999996, 9.0)
(4.0, 43.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.7203099999999996, 16.0)
(5.0, 40.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.9964500000000003, 25.0)
...,
(5.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.2146100000000004, 25.0)
(6.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.2915700000000001, 36.0)
(7.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.3716100000000004, 49.0)]
EDIT:
Further, this code
reader = csv.reader(open(dataset1, 'r'))
header = reader.next()
X = np.array([[float(col) for col in row] for row in reader])
print(X.shape)
print(X.T.shape)
print(X * X.T)
gives this output:
(4165, 13)
(13, 4165)
Traceback (most recent call last):
File "genfromtxt.py", line 17, in <module>
print(X * X.T)
ValueError: shape mismatch: objects cannot be broadcast to a single shape
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第二个示例的问题似乎是运算符
*
对 NumPy 数组执行逐元素乘法。想必您想执行矩阵乘法。有两个选项可以做到这一点:使用
numpy.matrix
而不是numpy.array
- 那么乘法将是矩阵乘法,整数指数的幂将作为使用
numpy.dot(A, B)
而不是A*B
——这将为数组和矩阵执行矩阵乘法。The problem with the second example seems to be that the operator
*
performs element-wise multilpication for NumPy arrays. Presumably you would like to perform a matrix multiplication. There are two options to do this:Use
numpy.matrix
instead ofnumpy.array
-- then multiplication will be matrix multiplication and powers by integer exponents will work as expected.Use
numpy.dot(A, B)
instead ofA*B
-- this will perform matrix multiplication for both arrays and matrices.如果您有使用 Matlab 和/或 Octave 的经验,此页面提供了许多有用的提示:
链接
If you have any experience with Matlab and/or Octave this page gives a bunch of useful hints:
Link