将数据从csv加载到numpy数组中 [英] Load data from csv into numpy array
本文介绍了将数据从csv加载到numpy数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将csv文件(带有定界符',')中的数据加载到numpy数组中.行的示例是:81905.75578271,81906.6205052,50685.487931,....(1000列). 我有这段代码,但是它似乎无法正常工作,因为在函数的退出端,调试器无法识别数据,当我调用xtrain.shape时,它返回0:
I am trying to load data in a csv file (with delimiter ',') into a numpy array. Example of a line is: 81905.75578271,81906.6205052,50685.487931,.... (1000 columns). I have this code but it seems to not be working properly as in the exit of the function the debugger cannot recognize the data, and when I call the xtrain.shape it returns 0:
def load_data(path):
# return np.loadtxt(path,dtype=int,delimiter=',')
file = open(path,'r')
data = []
for line in file:
array_vals = line.split(",")
array = []
for val in array_vals:
if not val:
array.append(float(val))
data.append(np.asarray(array))
return np.asarray(data)
x_train = load_data(path)
推荐答案
这应该为您提供所需的输出.
This should give you your required output.
import numpy as np
def load_data(path):
return np.loadtxt(path,delimiter=',')
这篇关于将数据从csv加载到numpy数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文