如何使用numpy读取数组以从文件形成矩阵

时间:2017-12-05 22:55:32

标签: python numpy

我有一个数据文件: 每行2个数组。总计= 10,000行。

x=[
    [1,2,3,4,5]
    [3,6,9,12,24]
    ....]

y=
  [[2,4,6,8,10]
   [6,12,18,24,48]
    ....]

我打算在fit命令中将此输入提供给Linear Regression。 我有问题如何构建带条目的矩阵。

我正在寻找构建Array(2 by x),如:

minname = ''
minscore = 0
 for i in range(total-1):
    if bowlerscorelist[i] < bowlerscorelist[i+1]:
        minscore = bowlerscorelist[i]
        minname = bowlernamelist[i]
print(minname, "got the lowest score with: ", minscore)

这样我就可以将fit命令作为输入。

我看到numpy.fromfile用于获取二进制数据。 我可以将它用于列表吗?

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.fromfile.html

2 个答案:

答案 0 :(得分:0)

这是解决方案

f = open("test.txt","r")
x = []
y = []
for i in f.readlines():
    split_values = i.split("][")
    s1 = split_values[0].replace('[','')
    s2 = split_values[1].replace(']','')
    s1_split = s1.split(",")
    s2_split = s2.split(",")
    s1_split = map(int, s1_split)
    s2_split = map(int, s2_split)
    x.append(s1_split)
    y.append(s2_split)
print(x)
print(y)

答案 1 :(得分:0)

使用pandas的解决方案

import pandas as pd

df = pd.read_csv('input.txt', delimiter="\]\[", header=None, engine='python')
df[0] = (df[0] + ']')
df[1] = ('[' + df[1])
x = df[0].tolist()
y = df[1].tolist()