python - 'NoneType' object is not subscriptable -- using `np.fromregex` -
this question has many answers (see python math - typeerror: 'nonetype' object not subscriptable). question different, because correctly expect np.genfromtxt(...)
return array (i.e np.genfromtxt(...)
not in place function).
i trying parse , store following single dimensional array:
http://pastie.org/10860707#2-3
to so, tried:
pattern = re.compile(b'[\s,]') theta = np.fromregex("reglogtheta", regexp = pattern, dtype = float)
this traceback (how should formatted?):
traceback (most recent call last): file "/users/ahanagrawal/documents/java/machl/chap3/examscorevisual2.py", line 36, in <module> theta = np.fromregex("reglogtheta", regexp = pattern, dtype = float) file "/library/frameworks/python.framework/versions/3.5/lib/python3.5/site-packages/numpy/lib/npyio.py", line 1240, in fromregex newdtype = np.dtype(dtype[dtype.names[0]]) typeerror: 'nonetype' object not subscriptable
if run this, please download text file from: http://pastie.org/10860707#2-3 , run code above.
the file has multiple lines, comma separation, 3 numbers perline, except last has 2
in [182]: fname='../downloads/pastie-10860707.txt' in [183]: np.fromregex(fname,regexp=pattern,dtype=float) ... np.fromregex(fname,regexp=pattern,dtype=float) /usr/lib/python3/dist-packages/numpy/lib/npyio.py in fromregex(file, regexp, dtype) 1240 # create new array single data-type , 1241 # re-interpret single-field structured array. -> 1242 newdtype = np.dtype(dtype[dtype.names[0]]) 1243 output = np.array(seq, dtype=newdtype) 1244 output.dtype = dtype typeerror: 'nonetype' object not subscriptable
loaded simple 'br' read, file looks like:
in [184]: txt out[184]: b'2.75386225e+00,1.80508078e+00,2.95729122e+00,\n-4.21413726e+00, -3.38139076e+00, -4.22751379e+00,\n ... 4.23010784e-01, -1.14839331e+00, -9.56098910e-01,\n -1.15019836e+00, 1.13845303e-06'
that missing number on last line give genfromtxt
problems.
your choice of pattern wrong. looks delimiter pattern. pattern in fromregex
docs produces groups:
regexp = r"(\\d+)\\s+(...)"
fromregex
does
seq = regexp.findall(file.read()) # read whole file , group output = np.array(seq, dtype=dtype) # make array seq
if want use fromregex
need come pattern produces list of tuples can turned array directly.
================
though looking again @ error messsage see immediate problem dtype
. dtype=float
not valid dtype spec function. expects compound dtype (structured).
the error produced action, float
dtype
parameter:
in [189]: np.dtype(float).names[0] ... typeerror: 'nonetype' object not subscriptable
but it's trying because pattern has produced
in [194]: pattern.findall(txt) out[194]: [b',', b',', b',', b'\n', b',', b' ', b' ', ....]
not list of tuples expected.
==================
i can load file with
in [213]: np.genfromtxt(txt.splitlines(),delimiter=',',usecols=[0,1]) out[213]: array([[ 2.75386225e+00, 1.80508078e+00], [ -4.21413726e+00, -3.38139076e+00], [ 7.46991792e-01, -1.08010066e+00], ... [ 4.23010784e-01, -1.14839331e+00], [ -1.15019836e+00, 1.13845303e-06]])
i'm using usecols
temporarily around problem 2 numbers on last line.
if remove \n
, split on commas, can parse resulting text fields directly np.array
.
in [231]: txt1=txt.replace(b'\n',b'').split(b',') in [232]: np.array(txt1,float) out[232]: array([ 2.75386225e+00, 1.80508078e+00, 2.95729122e+00, -4.21413726e+00, -3.38139076e+00, -4.22751379e+00, ... 4.23010784e-01, -1.14839331e+00, -9.56098910e-01, -1.15019836e+00, 1.13845303e-06])
this pattern includes decimal , scientific notation:
in [266]: pattern=re.compile(br"(\d+\.\d+e[\+\-]\d+)") in [267]: np.fromregex(fname,regexp=pattern,dtype=np.dtype([('f0',float)]))['f0'] out[267]: array([ 2.75386225e+00, 1.80508078e+00, 2.95729122e+00, 4.21413726e+00, 3.38139076e+00, 4.22751379e+00, ... 4.23010784e-01, 1.14839331e+00, 9.56098910e-01, 1.15019836e+00, 1.13845303e-06])
for i'm creating structured array , extracting field. there may way around that. fromregex
seems favor use of structured dtypes.