<jupyter_start><jupyter_text>2.3.3　線形モデル<jupyter_code>from sklearn.linear_model import LinearRegression
X, y = mglearn.datasets.make_wave(n_samples=60)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

lr = LinearRegression().fit(X_train, y_train)

print(lr.coef_)
print(lr.intercept_)
print("訓練セットスコア: {:.2f}".format(lr.score(X_train, y_train)))
print("テストセットスコア: {:.2f}".format(lr.score(X_test, y_test)))
#boston_housingデータセット
X,y= mglearn.datasets.load_extended_boston()
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
lr = LinearRegression().fit(X_train, y_train)
print("訓練セットスコア: {:.2f}".format(lr.score(X_train, y_train)))
print("テストセットスコア: {:.2f}".format(lr.score(X_test, y_test)))
from sklearn.linear_model import  Ridge

ridge = Ridge().fit(X_train, y_train)
print("訓練セットスコア: {:.2f}".format(ridge.score(X_train, y_train)))
print("テストセットスコア: {:.2f}".format(ridge.score(X_test, y_test)))
ridge10 = Ridge(alpha=10).fit(X_train,y_train)
print("訓練セットスコア: {:.2f}".format(ridge10.score(X_train, y_train)))
print("テストセットスコア: {:.2f}".format(ridge10.score(X_test, y_test)))
ridge01 = Ridge(alpha=0.1).fit(X_train,y_train)
print("訓練セットスコア: {:.2f}".format(ridge01.score(X_train, y_train)))
print("テストセットスコア: {:.2f}".format(ridge01.score(X_test, y_test)))
#plt.plot(ridge.coef_, 's')
from sklearn.linear_model import Lasso

lasso = Lasso().fit(X_train, y_train)
print("訓練セットスコア: {:.2f}".format(lasso.score(X_train, y_train)))
print("テストセットスコア: {:.2f}".format(lasso.score(X_test, y_test)))
print("使用された特徴量数: {}".format(np.sum(lasso.coef_ != 0)))
#max_iterを増やさないと警告
lasso001 = Lasso(alpha=0.01, max_iter=100000).fit(X_train, y_train)
print("訓練セットスコア: {:.2f}".format(lasso001.score(X_train, y_train)))
print("テストセットスコア: {:.2f}".format(lasso001.score(X_test, y_test)))
print("使用された特徴量数: {}".format(np.sum(lasso001.coef_ != 0)))<jupyter_output>訓練セットスコア: 0.90
テストセットスコア: 0.77
使用された特徴量数: 33<jupyter_text>2.3.3.5 クラス分類のための線形モデル<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC

# X, y = mglearn.datasets.make_forge()

#axisの複数形がaxes
# fig, axes = plt.subplots(1,2, figsize=(10, 3))
#ロジスティック回帰をcancerデータセットで解析
from sklearn.datasets import  load_breast_cancer
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
    cancer.data, cancer.target, stratify=cancer.target, random_state=42)
logreg = LogisticRegression().fit(X_train, y_train)

print("訓練セットスコア: {:.3f}".format(logreg.score(X_train, y_train)))
print("テストセットスコア: {:.3f}".format(logreg.score(X_test, y_test)))
logreg100 = LogisticRegression(C=100).fit(X_train, y_train)
print("訓練セットスコア: {:.3f}".format(logreg100.score(X_train, y_train)))
print("テストセットスコア: {:.3f}".format(logreg100.score(X_test, y_test)))
logreg.coef_
logreg.coef_.T
cancer.data
#cancerデータポイントの特徴量は30
cancer.data.shape[1]
#rotation=文字の角度？<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17869415807560138, 'fraction_numerical': 0.03402061855670103, 'mean_word_length': 8.544262295081968, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '123301', 'license': 'MIT', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1388, 'n_words': 168, 'path': 'introduction_with_python_practice/chap2.ipynb', 'repo_name': 'kaito0223/shakyou', 'size': '2910'}