'''
K = 5
kf = StratifiedKFold(n_splits=K, shuffle=True, random_state=42)
for fold, (train_indices, test_indices) in enumerate(kf.split(X,y)):
X_train, X_test = X[train_indices], X[test_indices]
y_train, y_test = y[train_indices], y[test_indices]
ランダムフォレストのモデル構築
K = 5
kf = StratifiedKFold(n_splits=K, shuffle=True, random_state=42)
for fold, (train_indices, test_indices) in enumerate(kf.split(X,y)):
X_train, X_test = X[train_indices], X[test_indices]
y_train, y_test = y[train_indices], y[test_indices]
# ランダムフォレストのモデル構築
model = RandomForestClassifier(n_estimators=100,
n_jobs=-1,random_state=42,max_samples=None)
model.fit(X_train, y_train)
#特徴量の重要度
feature = model.feature_importances_
#特徴量の名前
label = df.columns[1:]
#特徴量の重要度順(降順)
indices = np.argsort(feature)[::1]
プロット
#特徴量の重要度
feature = model.feature_importances_
#特徴量の名前
label = df.columns[1:]
#特徴量の重要度順(降順)
indices = np.argsort(feature)[::1]
# プロット
x = range(len(feature))
y = feature[indices]
y_label = label[indices]
plt.figure(figsize=(30,42))
plt.barh(x, y, align = 'center')
plt.yticks(x, y_label)
plt.xlabel("importance_num")
plt.ylabel("label")
plt.rcParams["font.size"] = 9
plt.show()
#特徴量の名前
label = df.columns[1:]
#特徴量の重要度順(降順)
indices = np.argsort(feature)[::-1]
for i in range(len(feature)):
print(str(i + 1) + " " + str(label[indices[i]]) + " " + str(feature[indices[i]]))
#特徴量の名前
label = df.columns[1:]
#特徴量の重要度順(降順)
indices = np.argsort(feature)[::-1]
for i in range(len(feature)):
print(str(i + 1) + " " + str(label[indices[i]]) + " " + str(feature[indices[i]]))
'''