0%

Simple Linear Regression | Day 2

这边还是直接贴上原图吧,手打实在是比较累,而且我按图打字的时候容易分心,很容易变成机械运动,不如直接上图。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataset = pd.read_csv('F:\\dataset\\100-Days-Of-ML-Code-master\\datasets\\studentscores.csv')
X = dataset.iloc[ : , : 1 ].values
Y = dataset.iloc[ : , 1 ].values

from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split( X, Y, test_size = 1/4, random_state = 0)

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor = regressor.fit(X_train, Y_train)

Y_pred = regressor.predict(X_test)

plt.scatter(X_train , Y_train, color = 'red')
plt.plot(X_train , regressor.predict(X_train), color ='blue')
plt.show()

1
2
3
plt.scatter(X_test , Y_test, color = 'red')
plt.plot(X_test , regressor.predict(X_test), color ='blue')
plt.show()

理解

第二天的内容比较简单,没有需要特别结实的内容

值得注意的是,最后两个测试结果可视化和训练结果可视化内容里面的向量其实是一样的,Y_pred = regressor.predict(X_test)这一步其实类似于保存结果,但是后面不知道为什么没有直接使用起来,有点奇怪。