2016-06-01 10 views
-3

この質問には多くの回答があります(Python Math - TypeError: 'NoneType' object is not subscriptable参照)。 np.genfromtxt(...)が配列を返すと正しく期待しているため、私の質問は異なります(つまり、np.genfromtxt(...)は場所機能ではありません)。'NoneType'オブジェクトはサブスクリプトできません - `np.fromregex`を使用

私は1次元配列に次のように解析し、保存しようとしています。そうするために

http://pastie.org/10860707#2-3

、私が試した:

pattern = re.compile(b'[\s,]') 
theta = np.fromregex("RegLogTheta", regexp = pattern, dtype = float) 

これは(どのようにすべきトレースバックですフォーマットされていますか?):

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 is not subscriptable 

これを実行するには、http://pastie.org/10860707#2-3からテキストファイルをダウンロードし、上記のコードを実行してください。

+1

完全なトレースバックを投稿してください。 – kindall

+1

投稿したコードに 'np.genfromtxt'を使用していません。 – user2357112

+0

外部のウェブサイトにデータを投稿しないでください。それをあなたの質問にコピーしてください。 – MattDMo

答えて

1

ファイルが最後を除いて唯一の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 the new array as a single data-type and then 
    1241    # re-interpret as a 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 is not subscriptable 

は、単純な 'BR' を搭載している、コンマ分離、3つの数字のperlineで、複数の行を持つファイルは次のようになり、読み:

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' 

最後の行に番号がないと、genfromtxtの問題が発生します。

パターンの選択が間違っています。デリミタパターンのように見えます。あなたがに変換することができタプルのリストを作成したパターンを考え出す必要があるfromregexを使用したい場合は

regexp = r"(\\d+)\\s+(...)" 

fromregex

seq = regexp.findall(file.read()) # read whole file and group it 
output = np.array(seq, dtype=dtype) # make array from seq 

を行います。しかし、fromregexドキュメント内のパターンは、グループを作成します直接配列。私は、当面の問題はdtypeであることがわかり、エラーmesssageで再び見

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

けれども。 dtype=floatは、この関数の有効なdtype仕様ではありません。複合dtype(構造化)が必要です。

エラーがfloatがあなたのdtypeパラメータであり、この作用により生成されます

In [189]: np.dtype(float).names[0] 
... 
TypeError: 'NoneType' object is not subscriptable 

しかし、パターンが

In [194]: pattern.findall(txt) 
Out[194]: 
[b',', 
b',', 
b',', 
b'\n', 
b',', 
b' ', 
b' ', 
....] 

ないというタプルのリストを作成したので、これをやろうとしていますそれは期待された。

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

私は一時的に問題を回避するためにusecolsを使用してい

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]]) 

を使用してファイルを読み込むことができます最後の行には2つの数字しかありません。

\nを削除し、カンマで分割すると、結果のテキストフィールドを直接np.arrayで解析できます。

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]) 

今、私は構造化された配列を作成し、そのフィールドを抽出てる場合:

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]) 

このパターンは、小数点と科学的表記を含んでいます。その周りに道があるかもしれません。しかし、fromregexは、構造化されたdtypesの使用を好むようです。

+0

これは答えになっていますか? –

+0

コメントは長すぎます!加えて、私は編集を終える前にあなたの質問に飛び乗った。私はまだ編集が完了していません。 – hpaulj

+0

@hpaulj次のように動作するはずです: 'theta = np.fromregex(" RegLogTheta "、regexp = r" \ s +、(\ d +)\ s +、 "、dtype = [(np.float128)])'。唯一の問題は、[(np.float128)]が認識されたdtypeではないことです。私はそれを理解できません。 – Muno