あるテキストの変数増加法のコードについてわからない点があります。
featuresには特徴量が、XにはX_trainがyにはy_trainが入ります。
def get_gfs_feature_indices(X, y, features, clf): X_train_, X_test_, y_train_, y_test_ = \ train_test_split(X, y, test_size=0.3, random_state=1234) feature_indices = {feature: idx for idx, feature in enumerate(features)} features = set(features) last_mse = np.inf chosen_features = set() while len(chosen_features) < len(features): mse_features = [] for feature in (features - chosen_features): candidates = chosen_features.union(set([feature])) indices = [feature_indices[feature] for feature in candidates] clf.fit(X_train_[:, indices], y_train_) y_pred = clf.predict(X_test_[:, indices]) mse = mean_squared_error(y_test_, y_pred) mse_features += [(mse, feature)] mse, feature = min(mse_features) if mse >= last_mse: break last_mse = mse print('Newly Added Feature: {},\tMSE Score: {}'.format(feature, mse)) chosen_features.add(feature) return [feature_indices[feature] for feature in chosen_features]
六行目の
last_mse=np.inf
は無限大を変数に入れているのでしょうか。
while文では、特徴量を一つ一つ試すために、空のセット(chosen_features)をつくり、それに試したものから入れていき、すべて試し終わったとき、chosen_featuresもfeaturesと同じ数になっているため、whileループから抜けるという処理を行いたいのだと思います。
9行目のmse_feautures=[]はどういう役割なのでしょうか。
while文にネストしてfor文をつくる意味は何でしょうか。
10行目のcandidatesはどういう役割なのでしょうか。
11行目のindicesはどういう役割なのでしょうか。
12行目のclf.fit(X_train[:,indices],y_train)のX_train[:,indices]はどういう意味でしょうか。
一つでもいいので、よろしくお願いします。
あなたの回答
tips
プレビュー