Python 矩阵问题
这是从这个线程继续的: Python 矩阵,有解决方案吗?
输入
from numpy import *
import numpy
x=[['1','7'],
['1.5', '8'],
['2', '5.5'],
['2','9']]
代码
y = x[:, :, None] * x[:, None]
print y.sum(axis=0)
我收到错误:
“列表索引必须是整数,而不是 元组”
但是如果 x 是 x = numpy.array([[1, 7], [1.5, 8], [2, 5.5], [2, 9]])
那么就可以了,但是我没有 <强>这样的输入。
This is continued from this thread: Python matrix, any solution?
Input
from numpy import *
import numpy
x=[['1','7'],
['1.5', '8'],
['2', '5.5'],
['2','9']]
Code
y = x[:, :, None] * x[:, None]
print y.sum(axis=0)
I received error:
"list indices must be integers, not
tuple"
But if x isx = numpy.array([[1, 7], [1.5, 8], [2, 5.5], [2, 9]])
then it's ok, but I don't have such input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:
我并不是 100% 清楚你在这里要求/试图实现什么。回应有关 [['1','7'] ...] 的评论:当前您的列表中有字符串元素;您可以轻松地转换为数字元素:
原始帖子:
通过在列表元素之间放置逗号来定义列表:
当我没有这样做时,我收到了错误,但通过这样做我避免了错误。
Edit:
It's not 100% clear to me what you're asking/trying to achieve here. In response to the comment about having [['1','7'] ...]: Currently you have string elements in your list; you can easily enough convert to numeric elements with:
Original post:
Define your list by putting commas between your list elements:
When I didn't do this, I got your error, but by doing this I avoided the error.
将
x
转换为 numpy 数字数组:Convert
x
to a numpy array of numbers: