ランダムフォレストで特徴量の重要度を表示する際のplotで、エラーが発生して苦慮しています。
競馬データで統計の勉強をしている初心者でございます。
今回のソースコードは以下の通りでございます。
# -*- coding: utf-8 -*- import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import LeaveOneOut from sklearn.ensemble import RandomForestClassifier import scipy as sp import sklearn import seaborn as sns from matplotlib import pyplot as plt import sklearn.cross_validation #訓練データ gakusyuu_data = pd.read_csv("data_kyoto_test.csv", sep=",") # gakusyuu_data から NaN を含む列を削除する gakusyuu_data_d = gakusyuu_data.drop(gakusyuu_data.columns[np.isnan(gakusyuu_data).any()], axis=1) # 特徴データとラベルデータを取り出す gakusyuu_data_d_except_arrival = gakusyuu_data_d.drop("arrival", axis=1) neta = gakusyuu_data_d_except_arrival.as_matrix() targets = gakusyuu_data_d['arrival'].as_matrix() model = RandomForestClassifier() model.fit(neta, targets) #それぞれの変数の重要度を記述する print (model.feature_importances_) ############################################# ここから下以降のスクリプトがうまく動きません #特徴量の重要度 feature = model.feature_importances_ #特徴量の重要度を上から順に出力する f = pd.DataFrame({'number': range(0, len(feature)),'feature': feature[:]}) f2 = f.sort_values('feature',ascending=False) f3 = f2.ix[:, 'number'] #特徴量の名前 label = gakusyuu_data.columns[0:] #特徴量の重要度順(降順) indices = np.argsort(feature)[::-1] for i in range(len(feature)): print (str(i + 1) + " " + str(label[indices[i]]) + " " + str(feature[indices[i]]) plt.title('Feature Importance') plt.bar(range(len(feature)),feature[indices], color='lightblue', align='center') plt.xticks(range(len(feature)), label[indices], rotation=90) plt.xlim([-1, len(feature)]) plt.tight_layout() plt.show()
訓練データは以下の通りでございます。
season race_num age kinryo sex tousuu baba arrival 1 6 3 54 2 10 1 3 2 6 3 54 2 16 1 4 10 2 3 55 1 15 1 2 11 3 3 55 1 15 1 1 1 9 3 56 1 10 1 2 4 8 3 56 1 14 2 1 5 7 3 56 1 10 1 2 11 7 4 55 1 17 1 3 1 7 4 56 1 15 1 2 1 7 4 56 1 16 1 1 2 9 4 54 1 9 2 2 1 9 6 55 1 15 3 2 2 9 6 55 1 12 1 1 4 11 6 57 1 13 1 3 2 10 7 57 1 11 2 3 4 11 7 53 1 14 1 3 10 11 8 57 1 11 1 3 11 9 8 57 1 10 1 3 1 11 8 53 1 16 1 3 2 10 8 57 1 10 1 3 1 1 3 54 2 14 1 3 10 3 3 55 1 11 1 1 11 8 3 55 1 8 2 2 10 4 6 60 1 11 1 3 11 4 7 60 1 11 1 2 1 5 3 54 2 16 1 3 5 2 3 54 2 18 1 4 11 1 3 55 1 16 1 4 5 2 3 56 1 16 1 4
ソースコードの下方にございます、
plt.title('Feature Importance') この箇所におきまして、
plt.title('Feature Importance')
^
SyntaxError: invalid syntax
このようなエラーが発生しました。
構文エラーなのですが、参考にさせて頂きました
http://aiweeklynews.com/archives/50653819.html
ここでは、同じ箇所では、SyntaxErrorは発生しておりません。
特に複雑なことをしている訳でもございません。
いろいろ調べましたが、解決出来ません。
先輩方の御教示、よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー