<jupyter_start><jupyter_text>[作業重點]使用 Sklearn 中的線性迴歸模型，來訓練各種資料集，務必了解送進去模型訓練的**資料型態**為何，也請了解模型中各項參數的意義  作業試著使用 sklearn datasets 的其他資料集 (wine, boston, ...)，來訓練自己的線性迴歸模型。  HINT: 注意 label 的型態，確定資料集的目標是分類還是回歸，在使用正確的模型訓練！<jupyter_code>import sklearn.datasets as dsets
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score, accuracy_score

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
boston = dsets.load_boston()
x = boston.data
y = boston.target

print('x shape: ', x.shape)
print('y shape: ', y.shape)

df = pd.DataFrame(x, columns=boston.feature_names)
df['PRICE'] = y
# df.head()

print(df.dtypes.value_counts())
df.corr()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=4)
print('x_train shape: ', x_train.shape)
print('x_test shape: ', x_test.shape)
print('y_train shape: ', y_train.shape)
print('y_test shape: ', y_test.shape)

# use linear regression model
reg = LinearRegression().fit(x_train, y_train)

y_train_hat = reg.predict(x_train)
y_test_hat = reg.predict(x_test)

print('\ntrain')
print('MSE: ', mean_squared_error(y_train, y_train_hat))
print('R2 core: ', r2_score(y_train, y_train_hat)) 

print('\ntest')
print('MSE: ', mean_squared_error(y_test, y_test_hat))
print('R2 core: ', r2_score(y_test, y_test_hat)) 
reg.coef_
wine = dsets.load_wine()
x = wine.data
y = wine.target

print('x shape: ', x.shape)
print('y shape: ', y.shape)

df = pd.DataFrame(x, columns=wine.feature_names)
df['CLASS'] = y
df.head()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=5)
print('x_train shape: ', x_train.shape)
print('x_test shape: ', x_test.shape)
print('y_train shape: ', y_train.shape)
# print('y_test shape: ', y_test.shape)
# use logistic regression model
logreg = LogisticRegression(multi_class='multinomial', solver='newton-cg').fit(x_train, y_train)

y_train_hat = logreg.predict(x_train)
y_test_hat = logreg.predict(x_test)

print('train')
print('acc: ', accuracy_score(y_train,y_train_hat))
print('\ntest')
print('acc: ', accuracy_score(y_test,y_test_hat))

logreg.coef_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.004910714285714286, 'mean_word_length': 7.149090909090909, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '12705', 'license': 'MIT', 'n_tokens_mistral': 989, 'n_tokens_neox': 990, 'n_words': 169, 'path': 'Day_038_HW.ipynb', 'repo_name': 'semishen/ML100Days', 'size': '2240'}