0
次のcsvファイルの最初の2行を見てください。最初の行はフィールド名で、2行目は実際のデータの最初の行です。ループ反復のためのPythonループが期待どおりでない
私は最初の行を反復処理して、元の順序で値を配列に格納しようとしています。
age workclass fnlwgt education education-num marital-status occupation relationship race sex capital-gain capital-loss hours-per-week native-country label
59 Private 307423 9th 5 Never-married Other-service Not-in-family Black Male 0 0 50 United-States 0
reader = csv.DictReader(csvfile)
train_x = []
train_y = []
dic = {}
for row in reader:
row_x = []
for title in row.keys():
l = ['workclass','education','marital-status','occupation', 'relationship', 'race', 'sex', 'native-country']
if title in l:
value = get_dict[title][row[title]]
row_x.append(value)
elif title == 'label':
train_y.append(float(row['label']))
else:
row_x.append(float(row[title]))
train_x.append(row_x)
これは私が最初のラインについて得るものである:
[3、5、59.0、0.0、0、50.0、4、35、5.0、0.0、8、307423.0、4、3]
ご覧のとおり、フィールドの順序は間違っています。
age workclass fnlwgt education education-num marital-status occupation relationship race sex capital-gain capital-loss hours-per-week native-country label
59 Private 307423 9th 5 Never-married Other-service Not-in-family Black Male 0 0 50 United-States 0