<jupyter_start><jupyter_text>Import libraries<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error<jupyter_output><empty_output><jupyter_text>Import linear regression data<jupyter_code>df = pd.read_csv("../../data/homework.csv")

# Compare every value is Null and summaries them, calculates as number eventually.
print(df.isnull().values.sum())

print(f"Shape: {df.shape}")
print(f"Variables: {df.keys()}")
print(f"The first data content:\n{df.iloc[0,::]}")

# Becaues we are going to predict the future Income, so we set the target as "Income".
print(f'The first awaited predict target: {df["Income"][0]}')<jupyter_output>0
Shape: (133, 2)
Variables: Index(['MileagePerPerson', 'Income'], dtype='object')
The first data content:
MileagePerPerson    150073966
Income              333641788
Name: 0, dtype: int64
The first awaited predict target: 333641788<jupyter_text>Setup figures<jupyter_code>plt.scatter(df.MileagePerPerson, df.Income, color="b")
plt.xlabel("MileagePerPerson")
plt.ylabel("Income")
plt.savefig(f"./figures/homework/Original_Data.jpg")<jupyter_output><empty_output><jupyter_text>Prepare train data and test data for model<jupyter_code>x = df.drop("Income", axis=1)
y = df["Income"]

train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.2, random_state=42)
print(f"Original data shape: {df.shape}")
print(f"Tranning data shape: {train_x.shape}")
print(f"Testing data shape: {test_x.shape}")<jupyter_output>Original data shape: (133, 2)
Tranning data shape: (106, 1)
Testing data shape: (27, 1)<jupyter_text>Trainning<jupyter_code>model = LinearRegression()
model.fit(train_x, train_y)
print(f"Coefficients: {model.coef_}")    # 模型係數
print(f"Intercept: {model.intercept_}")  # 截距<jupyter_output>Coefficients: [2.3248781]
Intercept: -966454.5677380264<jupyter_text>Import test data to model for predictions and calculate MSEUsing formula `MSE(Mean Square Error)` to fine a least `Loss` line$$MSE = \frac{\sum_{i=1}^{n}(yi-\hat{y}i)^2}{n}$$<jupyter_code>pred = model.predict(test_x)
score = mean_squared_error(pred, test_y)
print(f"MSE: {score}")<jupyter_output>MSE: 27636230501588.547<jupyter_text>Linear regression<jupyter_code>plt.scatter(x, y, color="b")
plt.plot(test_x.values.tolist(), pred, color="r")
plt.xlabel("MileagePerPerson")
plt.ylabel("Income")
plt.savefig(f"./figures/homework/Regression_Data.jpg")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.03778040141676505, 'mean_word_length': 7.7353951890034365, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '628293', 'license': 'MIT', 'n_tokens_mistral': 1030, 'n_tokens_neox': 952, 'n_words': 192, 'path': 'src/3.LinearRegression/PRACTICE_3_LR_02.ipynb', 'repo_name': 'LTurret/NFU-Biotech-MachineLearning', 'size': '2541'}