scipy中的csv到矩阵

发布于 2024-10-02 18:34:52 字数 1515 浏览 1 评论 0原文

我无法对数据进行简单的矩阵运算,我一生都无法弄清楚我做错了什么:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-10-09 18:34:52

第二个示例的问题似乎是运算符 * 对 NumPy 数组执行逐元素乘法。想必您想执行矩阵乘法。有两个选项可以做到这一点:

  1. 使用numpy.matrix而不是numpy.array - 那么乘法将是矩阵乘法,整数指数的幂将作为

  2. 使用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:

  1. Use numpy.matrix instead of numpy.array -- then multiplication will be matrix multiplication and powers by integer exponents will work as expected.

  2. Use numpy.dot(A, B) instead of A*B -- this will perform matrix multiplication for both arrays and matrices.

雾里花 2024-10-09 18:34:52

如果您有使用 Matlab 和/或 Octave 的经验,此页面提供了许多有用的提示:
链接

If you have any experience with Matlab and/or Octave this page gives a bunch of useful hints:
Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文