如何在 scipy 中将 ndarray 转换为矩阵?
如何将 ndarray 转换为 numpy 中的矩阵?我正在尝试从 csv 导入数据并将其转换为矩阵。
from numpy import array, matrix, recfromcsv
my_vars = ['docid','coderid','answer1','answer2']
toy_data = matrix( array( recfromcsv('toy_data.csv', names=True)[my_vars] ) )
print toy_data
print toy_data.shape
但我明白了:
[[(1, 1, 3, 3) (1, 2, 4, 1) (1, 3, 7, 2) (2, 1, 3, 3) (2, 2, 4, 4)
(2, 4, 3, 1) (3, 1, 3, 3) (3, 2, 4, 3) (3, 3, 3, 4) (4, 4, 5, 1)
(4, 5, 6, 2) (4, 2, 4, 3) (5, 2, 5, 4) (5, 3, 3, 1) (5, 4, 7, 2)
(6, 1, 3, 3) (6, 5, 4, 1) (6, 2, 5, 2)]]
(1, 18)
我需要做什么才能从这段代码中得到 4 x 18 矩阵?这个问题一定有一个简单的答案,但我就是找不到。
How can I convert an ndarray to a matrix in numpy? I'm trying to import data from a csv and turn it into a matrix.
from numpy import array, matrix, recfromcsv
my_vars = ['docid','coderid','answer1','answer2']
toy_data = matrix( array( recfromcsv('toy_data.csv', names=True)[my_vars] ) )
print toy_data
print toy_data.shape
But I get this:
[[(1, 1, 3, 3) (1, 2, 4, 1) (1, 3, 7, 2) (2, 1, 3, 3) (2, 2, 4, 4)
(2, 4, 3, 1) (3, 1, 3, 3) (3, 2, 4, 3) (3, 3, 3, 4) (4, 4, 5, 1)
(4, 5, 6, 2) (4, 2, 4, 3) (5, 2, 5, 4) (5, 3, 3, 1) (5, 4, 7, 2)
(6, 1, 3, 3) (6, 5, 4, 1) (6, 2, 5, 2)]]
(1, 18)
What do I have to do to get a 4 by 18 matrix out of this code? There's got to be an easy answer to this question, but I just can't find it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果最终目标是创建一个矩阵,则无需创建具有命名列的重新数组。您可以使用
np.loadtxt
将csv加载到ndarray中,然后使用np.asmatrix
将其转换为矩阵:yields
注意:skiprows参数用于跳过在 csv 的标题上。
If the ultimate goal is to make a matrix, there's no need to create a recarray with named columns. You could use
np.loadtxt
to load the csv into an ndarray, then usenp.asmatrix
to convert it to a matrix:yields
Note: the skiprows argument is used to skip over the header in the csv.
您可以将所有值读入向量,然后重新调整它的形状。
但使用较新的 numpy 可能有一种更直接的方法。
You can just read all your values into a vector, then reshape it.
But there may be an even more direct way with newer numpy.