如何在 scipy 中将 ndarray 转换为矩阵?

发布于 2024-11-04 02:46:35 字数 644 浏览 4 评论 0原文

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

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

发布评论

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

评论(2

欢你一世 2024-11-11 02:46:35

如果最终目标是创建一个矩阵,则无需创建具有命名列的重新数组。您可以使用np.loadtxt将csv加载到ndarray中,然后使用np.asmatrix将其转换为矩阵:

import numpy as np
toy_data = np.asmatrix(np.loadtxt('toy_data.csv',delimiter=','skiprows=1))
print toy_data
print toy_data.shape

yields

[[ 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.]]
(18, 4)

注意: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 use np.asmatrix to convert it to a matrix:

import numpy as np
toy_data = np.asmatrix(np.loadtxt('toy_data.csv',delimiter=','skiprows=1))
print toy_data
print toy_data.shape

yields

[[ 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.]]
(18, 4)

Note: the skiprows argument is used to skip over the header in the csv.

明天过后 2024-11-11 02:46:35

您可以将所有值读入向量,然后重新调整它的形状。

fo = open("toy_data.csv")

def _ReadCSV(fileobj):
  for line in fileobj:
    for el in line.split(","):
      yield float(el)


header = map(str.strip, fo.readline().split(","))
a = numpy.fromiter(_ReadCSV(fo), numpy.float64)
a.shape = (-1, len(header))

但使用较新的 numpy 可能有一种更直接的方法。

You can just read all your values into a vector, then reshape it.

fo = open("toy_data.csv")

def _ReadCSV(fileobj):
  for line in fileobj:
    for el in line.split(","):
      yield float(el)


header = map(str.strip, fo.readline().split(","))
a = numpy.fromiter(_ReadCSV(fo), numpy.float64)
a.shape = (-1, len(header))

But there may be an even more direct way with newer numpy.

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