以下面的格式读取csv文件到numpy数组中 [英] reading csv file into numpy array in below format

查看:197
本文介绍了以下面的格式读取csv文件到numpy数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


[array([0.33333333,0.75,0.5,1.,0.


0.,0.,0.,1.,0 。,
0.,0.,0.,0.,0.,
0.,0.,0.,0.,0.,
0.,0.5,0.2 ,0.25,0.6,
0.8,0.5]),数组([1.,0.25,1,1,1,0,0,0,0,0,0,1,
0。 ,0.,0.,0.,0.,0.,0.,0.,0.b $ b 0.,0.,0.1,1.,0.5,0.6,0.4, 0.25]),阵列([1.,0.25,0.5,0.5,0.,0,0,0,0,1,,
0.,0,0,0,0,0, ([0.66666667,0.25,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 1.,0.


0.,0.,0.,1.,0.,
0.,0.,0.,0.,0。 ,
0.,0.,0.,0.,0.,
0.,1.,0.4,0.25,0.4,
0.6,0.75]),array([0.66666667 ,0.5,1.,1.,0.,
0.,0.,0.,1.,0.,
0.,0,0,0,0,
0.,0,0,0,0,0,0
0.,0.5,1.,0.5,0.6,
0.2,0.75]),数组([0.33333333, 0.5,1.,1.,0.,
0.,0.,0.,1.,0.,
0.,0.,0.,0.,0.,
0.,0,0,0,0,0,0,
0.,0.5,0.2,0.75,0.4,
0.8,0.75]),array([0.66666667,0.5, 0.5,1.,0.,
0.,0.,0.,1.,0.,
0.,0.,0.,0.,0.,
0.,0,0,0,0,0,0,0,
0.,0.5,1.,0.5,0.8,
1.,0.25]),数组([0.33333333,0.25,1 。,1.,0.,
0.,0.,0.,1.,0.,
0.,0.,0.,0.,0.,
0.,0,0,0,0,0,0,0,
0.,1.,1.,0.25,0.2,
0.6,0.5]),数组([0.66666667,0.5,0.5 ,1.,0.,
0.,0.,0.,1.,0.,
0.,0.,0.,0.,0.,
0 ,...,0.,0,0,0,0,0,
0,1,0.8,0.75,1,
0.2,0.75]),array([1.,0.5,1 0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0.,0,0.,0.1,
0.8,1.,0.4,0.2,1]],数组([0.33333333,0.25,1.,1.,0.,
0.,0.,0.,1.,0.,
0.,0.,0.,0.,0. b $ b 0.,0.,0.,0。 ,0.,
0,1,0.6,0.25,0.2,
0.4,0.5]),数组([0.33333333,0.75,0.5,0.5,0.8,0b $ b 0。 ,0.,0.1,0.,0.,
0.,0.,0.,0.,0.,
0.,0.,0.,0.,0。 ,
0.,0.5,1,0.25,0.8,
0.4,0.75]),数组([0.66666667,0.75,0.5,0.5,0.,0b $ b 0.,0。 ,0.,1.,0.,
0.,0.,0.,0.,0.,
0.,0.,0.,0.,0.,
0.,0.5,0.6,0.25,0.4,
0.2,0.75]),数组([0.66666667,0.1,1.,0.,
0., 0.,1.,0.,
0.,0.,0.,0.,0.,
0.,0.,0.,0.,
0.,0.5,0.6,0.25,0.8,
1,1,1]],数组([0.33333333,1.,1.,1.,0.,
0., ,0.,1.,0.,
0.,0.,0.,0.,0.,
0.,0.,0.,0.,0.,
0,1,0.4,0.25,0.6,
0.4,1]],数组([1.,0.75,0.5,1。 1.,
0.,0.,0.,0.,0.,0.,0.,0.,0.,
0.,0.,0.,0.5,0.6 ,0.25,1.,1.,0.5])]


我有文件读取,但我想在 [[0.33333333,0.75,0.5,1。,0.,0.,0.,0.1,0.,0.,0.,0., ,0.,0,0,0,0,0,0,0,0.5,0.2,0.25,0.6,0.8,0.5],[....],[....],[ ....]] 以这种格式



是我的代码,请检查一下...谢谢您的帮助

  import csv 
import numpy as nm
my_data = nm.genfromtxt('ta.csv',dtype = float,delimiter = ',',usecols = range(4,31))

data = list(my_data)
#input_data = data
print(data)



请给我建议....再次感谢

方案

我认为问题是 list()命令。



一个2d数组如:

 在[322]:data 
Out [322]:
array [[120258560,3],
[167772160,339],
[118948100,1],
[50331904,1]])

并将其包装在 list()中,我得到一个数组列表 - 原始。

 在[323]:list(data)
Out [323]:
[ array([120258560,3]),
array([167772160,339]),
array([118948100,1]),
array $ b

为什么要使用 list



有时候 genfromtxt 给出一个结构化数组,1d和命名列。但它看起来像读取您的文件作为所有浮点数。因此, my_data 应以您需要的形式显示。



============= =================



数组(和列表)可以以多种方式显示 - 好。让我用一个简单的数组说明 - 在交互式ipython shell中工作

 在[6]:x = np.array 1,2],[3,4]])

In [7]:x
Out [7]:
array [[1,2],
[3,4]])

您的原始方法 - 2 array:

 在[8]:list(x)
Out [8]:[array ,2]),array([3,4])]

使用 tolist 方法:

 在[9]:x.tolist )
Out [9]:[[1,2],[3,4]]

在[10]中:print x
[[1 2]
[3 4]]

注意 x print x 。后者使用数组的 str 格式化方法。前者是 repr 方法。这些方法是为每个类定义的,但通常 repr 给出了更详细的视图。

  in [11]:str(x)
Out [11]:'[[1 2] \\\
[3 4]]'

在[12] repr(x)
Out [12]:'array([[1,2],\\\
[3,4]])'

数组的 str 格式省略逗号,而列表的格式包括它们。



对于结构化数组(如使用 names = True 创建),() replace []

 在[15]中:y = np.array ([(a',1),('b',2)],dtype ='str,int')

In [16]:y
Out [16]
array([('',1),('',2)],
dtype = [('f0','S'),('f1','< i4' ]

注意,这是一个带有 dtype



打印( str )省略了 dtype ,看起来像元组列表。但请注意缺少的

  
[('',1)('',2)]

code> list 看起来一样:

  
Out [18]:[('',1),('',2)]

在[19]:y.tolist :[('',1),('',2)]

区别

 在[23]:type(list(y)[0])
Out [23]:numpy .void

在[24]:type(y.tolist()[0])
Out [24]:tuple

一个是 numpy 对象(结构化'记录')的列表,另一个是元组列表。



请注意,当我定义 y 时,我使用元组列表。这是一个重要的细节。


[array([ 0.33333333, 0.75 , 0.5 , 1. , 0.
, 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 0.2 , 0.25 , 0.6 , 0.8 , 0.5 ]), array([ 1. , 0.25, 1. , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 1. , 0.5 , 0.6 , 0.4 , 0.25]), array([ 1. , 0.25, 0.5 , 0.5 , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 0.4 , 0.75, 0.4 , 0.2 , 1. ]), array([ 0.66666667, 0.25 , 0.5 , 1. , 0.
, 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 0.4 , 0.25 , 0.4 , 0.6 , 0.75 ]), array([ 0.66666667, 0.5 , 1. , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 1. , 0.5 , 0.6 , 0.2 , 0.75 ]), array([ 0.33333333, 0.5 , 1. , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 0.2 , 0.75 , 0.4 , 0.8 , 0.75 ]), array([ 0.66666667, 0.5 , 0.5 , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 1. , 0.5 , 0.8 , 1. , 0.25 ]), array([ 0.33333333, 0.25 , 1. , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 1. , 0.25 , 0.2 , 0.6 , 0.5 ]), array([ 0.66666667, 0.5 , 0.5 , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 0.8 , 0.75 , 1. , 0.2 , 0.75 ]), array([ 1. , 0.5, 1. , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 0.8, 1. , 0.4, 0.2, 1. ]), array([ 0.33333333, 0.25 , 1. , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 0.6 , 0.25 , 0.2 , 0.4 , 0.5 ]), array([ 0.33333333, 0.75 , 0.5 , 0.5 , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 1. , 0.25 , 0.8 , 0.4 , 0.75 ]), array([ 0.66666667, 0.75 , 0.5 , 0.5 , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 0.6 , 0.25 , 0.4 , 0.2 , 0.75 ]), array([ 0.66666667, 0. , 1. , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 0.6 , 0.25 , 0.8 , 1. , 1. ]), array([ 0.33333333, 1. , 1. , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 0.4 , 0.25 , 0.6 , 0.4 , 1. ]), array([ 1. , 0.75, 0.5 , 1. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5 , 0.6 , 0.25, 1. , 1. , 0.5 ])]

i got file as reading this but i want in [[0.33333333,0.75,0.5,1.,0.,0., 0., 0.,1.,0.,0.,0.,0.,0. ,0.,0.,0.,0.,0.,0.,0.,0.5,0.2,0.25, 0.6,0.8 ,0.5 ],[....],[....],[....]] in that format

below is my code please review it..thanks for your help

import csv
import numpy as nm
my_data = nm.genfromtxt('ta.csv',dtype=float,delimiter=',',usecols=range(4,31))

data=list(my_data)
#input_data=data
print(data)

please give me suggestions....thanks once again

解决方案

I think the issue is the list() command.

If I take a 2d array like:

In [322]: data
Out[322]: 
array([[120258560,         3],
       [167772160,       339],
       [118948100,         1],
       [ 50331904,         1]])

and wrap it in list(), I get a list of arrays - one array per row of the original.

In [323]: list(data)
Out[323]: 
[array([120258560,         3]),
 array([167772160,       339]),
 array([118948100,         1]),
 array([50331904,        1])]

Why are you using list?

Sometimes genfromtxt gives a structured array, 1d with named columns. But it looks like it's reading your file as all floats. So my_data should in the form you want.

==============================

Arrays (and lists) can be displayed in various ways - and custom ones are possible as well. Let me illustrate with a simple array - working in interactive ipython shell

In [6]: x=np.array([[1,2],[3,4]])

In [7]: x
Out[7]: 
array([[1, 2],
       [3, 4]])

Your original approach - which turns the array into a list of 2 arrays:

In [8]: list(x)
Out[8]: [array([1, 2]), array([3, 4])]

For a complete conversion to nested lists use tolist method:

In [9]: x.tolist()
Out[9]: [[1, 2], [3, 4]]

In [10]: print x
[[1 2]
 [3 4]]

Notice the difference between x and print x. The latter uses the str formatting method of array. The former the repr method. Those methods are defined for each class, but generally repr gives a more detailed view.

In [11]: str(x)
Out[11]: '[[1 2]\n [3 4]]'

In [12]: repr(x)
Out[12]: 'array([[1, 2],\n       [3, 4]])'

The str format of arrays omits commas, while the format for lists includes them.

For a structured array (such as created with names=True), () replace [].

In [15]: y=np.array([('a',1),('b',2)],dtype='str,int')

In [16]: y
Out[16]: 
array([('', 1), ('', 2)], 
      dtype=[('f0', 'S'), ('f1', '<i4')])

Note, this is a 1d array with a compound dtype. You do not index columns, instead you use strings to index fields.

The print (str) omits the dtype, and looks like a list of tuples. But note the missing ,:

In [17]: print y
[('', 1) ('', 2)]

These 2 list look the same:

In [18]: list(y)
Out[18]: [('', 1), ('', 2)]

In [19]: y.tolist()
Out[19]: [('', 1), ('', 2)]

but there is a subtle difference

In [23]: type(list(y)[0])
Out[23]: numpy.void

In [24]: type(y.tolist()[0])
Out[24]: tuple

One's a list of numpy objects (structured 'records') the other a list of tuples.

Note that when I defined y, I use the list of tuples. That's an important detail.

这篇关于以下面的格式读取csv文件到numpy数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆