在python中,如何使用循环向numpy数组追加/添加行,而不删除前一行?

2024-04-24 20:11:55 发布

您现在位置:Python中文网/ 问答频道 /正文

给出这样的数据样本

3,12.2,3.03,2.32,19,96,1.25,.49,.4,.73,5.5,.66,1.83,510
3,12.77,2.39,2.28,19.5,86,1.39,.51,.48,.64,9.899999,.57,1.63,470
3,14.16,2.51,2.48,20,91,1.68,.7,.44,1.24,9.7,.62,1.71,660
3,13.71,5.65,2.45,20.5,95,1.68,.61,.52,1.06,7.7,.64,1.74,740
3,13.4,3.91,2.48,23,102,1.8,.75,.43,1.41,7.3,.7,1.56,750
3,13.27,4.28,2.26,20,120,1.59,.69,.43,1.35,10.2,.59,1.56,835
3,13.17,2.59,2.37,20,120,1.65,.68,.53,1.46,9.3,.6,1.62,840
3,14.13,4.1,2.74,24.5,96,2.05,.76,.56,1.35,9.2,.61,1.6,560

还有我的密码

import numpy as np
with open("wine.txt","r") as f:
    stuff=f.readlines()
#np.genfromtxt("wine.txt", delimiter=",")
z=np.empty((0,14),float)
for hello in stuff:
    firstbook=hello.strip().split(",")
    x=[float(i) for i in firstbook]
    y=np.array(x)
    b=np.append(b,y)
print b[1:2]

我很难得到一个由整个数据集组成的numpy数组(我只得到集的最后一行作为数组),这样当我打印时它会给我整个元素列(就像在最后一行代码中一样)。当我到达最后一行时,我才得到[14.13]


Tags: 数据innumpytxt密码helloforas
3条回答

为什么不使用^{}将分隔符作为逗号传递:

Load data from a text file. Each row in the text file must have the same number of values.

你的数据看起来不错:

import numpy as np

with open("wine.txt","r") as f:
    b = np.loadtxt(f, delimiter=',')
print b[1:2]
# [[3,12.77,2.39,2.28,19.5,86,1.39,.51,.48,.64,9.899999,.57,1.63,470]]

最好在一个列表中累积行值,并生成一个数组。你知道吗

alist = []
for hello in stuff:
    firstbook=hello.strip().split(",")
    x=[float(i) for i in firstbook]
    alist.append(x)
b = np.array(alist)

假设x每行有相同数量的术语,alist将是一个长度相等的列表。np.array将其转换为2d数组,就像在原型数组构造表达式中所做的那样:

np.array([[1,2],[3,4]])

重复列表附加比重复数组堆栈/附加快得多。你知道吗

使用文件示例(作为行列表)

In [1826]: data=np.genfromtxt(txt, dtype=float, delimiter=',')
In [1827]: data
Out[1827]: 
array([[  3.00000000e+00,   1.22000000e+01,   3.03000000e+00,
          2.32000000e+00,   1.90000000e+01,   9.60000000e+01,
          1.25000000e+00,   4.90000000e-01,   4.00000000e-01,
          7.30000000e-01,   5.50000000e+00,   6.60000000e-01,
          1.83000000e+00,   5.10000000e+02],
       [  3.00000000e+00,   1.27700000e+01,   2.39000000e+00,
          ...
          1.35000000e+00,   9.20000000e+00,   6.10000000e-01,
          1.60000000e+00,   5.60000000e+02]])
In [1828]: data.shape
Out[1828]: (8, 14)

第2列(作为1d数组):

In [1829]: data[:,1]
Out[1829]: array([ 12.2 ,  12.77,  14.16,  13.71,  13.4 ,  13.27,  13.17,  14.13])

In [1830]: data[:,1:2]
Out[1830]: 
array([[ 12.2 ],
       [ 12.77],
       [ 14.16],
       [ 13.71],
       [ 13.4 ],
       [ 13.27],
       [ 13.17],
       [ 14.13]])

你可以使用vstack()

import numpy as np

data = '''3,12.2,3.03,2.32,19,96,1.25,.49,.4,.73,5.5,.66,1.83,510
3,12.77,2.39,2.28,19.5,86,1.39,.51,.48,.64,9.899999,.57,1.63,470
3,14.16,2.51,2.48,20,91,1.68,.7,.44,1.24,9.7,.62,1.71,660
3,13.71,5.65,2.45,20.5,95,1.68,.61,.52,1.06,7.7,.64,1.74,740
3,13.4,3.91,2.48,23,102,1.8,.75,.43,1.41,7.3,.7,1.56,750
3,13.27,4.28,2.26,20,120,1.59,.69,.43,1.35,10.2,.59,1.56,835
3,13.17,2.59,2.37,20,120,1.65,.68,.53,1.46,9.3,.6,1.62,840
3,14.13,4.1,2.74,24.5,96,2.05,.76,.56,1.35,9.2,.61,1.6,560'''

stuff = data.split('\n')

z = np.empty((0,14), float)

for hello in stuff:
    firstbook = hello.strip().split(",")
    x = [float(i) for i in firstbook]

    z = np.vstack([z, x])

print(z[1:2])

相关问题 更多 >