<jupyter_start><jupyter_text>[作業重點]使用 Sklearn 中的 Lasso, Ridge 模型，來訓練各種資料集，務必了解送進去模型訓練的**資料型態**為何，也請了解模型中各項參數的意義。機器學習的模型非常多種，但要訓練的資料多半有固定的格式，確保你了解訓練資料的格式為何，這樣在應用新模型時，就能夠最快的上手開始訓練！  練習時間試著使用 sklearn datasets 的其他資料集 (boston, ...)，來訓練自己的線性迴歸模型，並加上適當的正則化來觀察訓練情形。<jupyter_code>import numpy as np
from sklearn import datasets, linear_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
boston = datasets.load_boston()
# 正常的 regression
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, random_state=4)

regr = linear_model.LinearRegression()

regr.fit(x_train, y_train)

y_pred = regr.predict(x_test)

print(regr.coef_)

print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred))
# Lasso
lasso = linear_model.Lasso(alpha=1.0)

lasso.fit(x_train, y_train)

y_pred = lasso.predict(x_test)

print(lasso.coef_)

print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred))
# Ridge
ridge = linear_model.Ridge(alpha=1.0)

ridge.fit(x_train, y_train)

y_pred = regr.predict(x_test)

print(ridge.coef_)

print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred))<jupyter_output>[-1.12499445e-01  4.79562332e-02 -2.40438147e-02  2.96199458e+00
 -9.33966118e+00  3.93079015e+00 -1.73821202e-02 -1.43347691e+00
  2.75239392e-01 -1.38920708e-02 -8.31116943e-01  9.15637729e-03
 -4.66460539e-01]
Mean squared error: 25.42
{'fraction_non_alphanumeric': 0.12351709699930216, 'fraction_numerical': 0.10956036287508723, 'mean_word_length': 6.5473684210526315, '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': '34299', 'license': 'MIT', 'n_tokens_mistral': 850, 'n_tokens_neox': 776, 'n_words': 97, 'path': 'D40_regression model_程式碼撰寫/Day_040_HW.ipynb', 'repo_name': 'jacky0405/100Days_ML_Marathon', 'size': '1433'}