codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Only one value in file, numpy.loadtxt() just returns the value instead of array?


By : , Category : python

Use the ndmin parameter of numpy.loadtxt. Set it to 1, and you will always have an array with at least one dimension.

ReLated :

I suspect that there is a problem with your delimiter character at least in the first file. try to set the delimiter argument.

Take a look to this explanation

You can't create a numpy array with shape (n, 3) where each column has a different type. What you can create (and what you did when you used loadtxt with dtype=[('ts','i10'), ('sndr','|S14'), ('recr', '|S14')]) is create a structured array, where each element in the array is a structure composed of several fields. In your case, you have three fields: one is an integer and two are strings. The array created by loadtxt is a one-dimensional array. Each element in the array is a structure with three fields. You can access the fields (which you can interpret as "columns") as network['ts'], network['sndr'] and network['recr'].

See http://docs.scipy.org/doc/numpy/user/basics.rec.html for more information. There is probably a lot of related information here on SO, too. For example, Access Columns of Numpy Array? Errors Trying to Do by Transpose or by Column Access

You are right that np.loadtxt returns a 1D array, but you can still access the 'columns', which are actually fields in a structured array:

array([(2011, 12, 25, b'AAA', b'AAA', b'AAA'),
       (2011, 12, 26, b'BBB', b'BBB', b'BBB'),
       (2011, 12, 27, b'CCC', b'CCC', b'CCC')], 
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', 'S4'), ('f4', 'S4'), ('f5', 'S4')])

It does let you index the fields, but you need to do so by the names (f0, f1, f2...) and not indexes:

nt['f3']
#>>> array([b'AAA', b'BBB', b'CCC'], 
#>>>       dtype='|S4')

You can of course specify the dtype names:

dtype=[('MEAT', '<i4'), ('CHEESE', '<i4'), ('TOAST', '<i4'), ('BIRD', 'S4'), ('PLANE', 'S4'), ('SOCK', 'S4')]
nt = numpy.loadtxt('/home/joshua/file.csv', dtype=dtype, delimiter=',')

nt['SOCK']
#>>> array([b'AAA', b'BBB', b'CCC'], 
#>>>       dtype='|S4')

This is done to simplify a lot of complications that arise from non-homogeneous arrays.

If you have pandas you could parse it with pd.read_fwf:

import pandas as pd
df = pd.read_fwf('data', colspecs='infer', header=None, parse_dates=[[0]])
print(df)

yields

                     0   1   2   3   4    5    6    7    8
0  2012-01-27 16:51:26  19  42  54  91  113  147  188  284
1  2012-01-27 16:51:27  19  42  54  91  113  147  188  284
...
11 2012-01-27 16:51:37  19  42  54   0  NaN    0    2   25
12 2012-01-27 16:51:38  19  42  53   0  NaN    0    1   22
13 2012-01-27 16:51:39  19  42  54   0  NaN    0    1   19

[36 rows x 9 columns]

Or, thanks to DSM, using np.genfromtxt you can parse fixed-width data by passing a list of widths to the delimiter parameter:

import numpy as np
np.set_printoptions(formatter={'float':'{:g}'.format})
arr = np.genfromtxt('data', delimiter=[18]+[7]*8)
print(arr)

yields

[[2.01201e+13 19 42 54 91 113 147 188 284]
 [2.01201e+13 19 42 54 91 113 147 188 284]
 [2.01201e+13 19 42 54 90 113 147 188 284]
...
 [2.01201e+13 19 42 54 0 nan 0 2 25]
 [2.01201e+13 19 42 53 0 nan 0 1 22]
 [2.01201e+13 19 42 54 0 nan 0 1 19]
...]
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .