下記の設問に対する答えとして相応しものを選択肢から選び、次のコードの空欄(##########)を埋めてください.
ロジスティック回帰、ランダムフォレスト、SVM をそれぞれ分類器として用いてハード投票によるアンサブル学習を実行してください.
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_moons
X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
#Q1:RandomForestClassifier クラスをインポートしてください.
from ########## import RandomForestClassifier
#Q2:VotingClassifier クラスをインポートしてください.
from ########## import VotingClassifier
#Q3:LogisticRegresson クラスをインポートしてください.
from ########## import LogisticRegression
#Q4:SVC クラスをインポートしてください.
from ########## import SVC
from sklearn.metrics import accuracy_score
[Q1–4 選択肢]
1. sklearn.linear_model
2. sklearn.svm
3. sklearn.ensemble
4. sklearn.tree
log_c = LogisticRegression(solver=”liblinear”, random_state=42)
rnd_c = RandomForestClassifier(n_estimators=10, random_state=42)
svm_c = SVC(gamma=”auto”, random_state=42)
# Q5:VotingClassifier() クラスを用いてソフト投票による投票分類器を訓練してください.( 1 行 )
voting_c = ##########
voting_c.fit(X_train, y_train)
for c in (log_c, rnd_c, svm_c, voting_c):
c.fit(X_train, y_train)
y_pred = c.predict(X_test)
print(c.__class__.__name__, accuracy_score(y_test, y_pred))
[Q5 の選択肢]
1.
VotingClassifier(
estimators=[(‘rf’, rnd_c), (‘lr’, log_c), (‘svc’, svm_c)],
voting=’soft’)
2.
VotingClassifier(
estimators=[(‘rf’, rnd_c), (‘lr’, log_c), (‘svc’, “soft”)],
voting=True)
3.
VotingClassifier(
estimators=[(‘rf’, log_c), (‘lr’, rnd_c), (‘svc’, svm_c)],
voting=’hard’)