<jupyter_start><jupyter_text>[作業重點]確保你了解隨機森林模型中每個超參數的意義，並觀察調整超參數對結果的影響  作業1. 試著調整 RandomForestClassifier(...) 中的參數，並觀察是否會改變結果？2. 改用其他資料集 (boston, wine)，並與回歸模型與決策樹的結果進行比較<jupyter_code>from sklearn import datasets, metrics
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.model_selection import train_test_split
wine = datasets.load_wine()
x = wine.data
y = wine.target
print('x sahpe: ', x.shape)
print('y sample: ', y[: 6]) # classification
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=4)

# baseline logistic regression
logreg = LogisticRegression(solver='newton-cg')
logreg.fit(x_train, y_train)

print('params: ', logreg.coef_)
print('acc: ', logreg.score(x_test, y_test))

clf = RandomForestClassifier(n_estimators=10, max_depth=4)
clf.fit(x_train, y_train)

print('acc: ', clf.score(x_test, y_test))
print('feature importances: ', {name:value for (name, value) in zip(wine.feature_names, clf.feature_importances_)})
# boston
boston = datasets.load_boston()
x = boston.data
y = boston.target
print('x sahpe: ', x.shape)
print('y sample: ', y[: 6]) # linear regression

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=5)
# baseline linear regression
linear = LinearRegression()
linear.fit(x_train, y_train)

print('params: ', linear.coef_)
print('R2: ', linear.score(x_test, y_test))
clf2 = RandomForestRegressor(n_estimators=10, max_depth=4)
clf2.fit(x_train, y_train)

print('R2: ', clf2.score(x_test, y_test))
print('feature importances: ', {name:value for (name, value) in zip(boston.feature_names, clf2.feature_importances_)})<jupyter_output>R2:  0.837963895356755
feature importances:  {'CRIM': 0.03946751128402972, 'ZN': 0.0, 'INDUS': 0.002276176307010874, 'CHAS': 0.0, 'NOX': 0.009136104068026727, 'RM': 0.3577398862093797, 'AGE': 0.0029516843209303235, 'DIS': 0.08636260460188355, 'RAD': 0.002480758878488432, 'TAX': 0.00436853956189145, 'PTRATIO': 0.0034113614528555186, 'B': 0.0033432546939453226, 'LSTAT': 0.4884621186215584}
{'fraction_non_alphanumeric': 0.13543516873889877, 'fraction_numerical': 0.11012433392539965, 'mean_word_length': 7.80078125, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '10694', 'license': 'MIT', 'n_tokens_mistral': 1091, 'n_tokens_neox': 966, 'n_words': 168, 'path': 'Day_044_HW.ipynb', 'repo_name': 'semishen/ML100Days', 'size': '2252'}