版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangyang_yangqi/article/details/82691480
数据集:
Hours,Scores
2.5,21
5.1,47
3.2,27
8.5,75
3.5,30
1.5,20
9.2,88
5.5,60
8.3,81
2.7,25
7.7,85
5.9,62
4.5,41
3.3,42
1.1,17
8.9,95
2.5,30
1.9,24
6.1,67
7.4,69
2.7,30
4.8,54
3.8,35
6.9,76
7.8,86
数据预处理常规步骤:
导入相关库
导入数据集
检查缺失值
划分数据集
特征缩放
#_*_coding:utf-8_*_
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dataset = pd.read_csv('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.0/4, random_state = 0)
#注意:Python2.x 1/4=0 1.0/4=0.25 Python3.x 1/4=0.25
# Fitting Simple Linear Regression Model to the training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
#拟合模型
regressor = regressor.fit(X_train, Y_train)
# Predecting the Result
Y_pred = regressor.predict(X_test)
# Visualising the Training results
plt.scatter(X_train , Y_train, color = 'red')
plt.plot(X_train , regressor.predict(X_train), color ='blue')
# Visualizing the test results
plt.scatter(X_test , Y_test, color = 'yellow')
plt.plot(X_test , regressor.predict(X_test), color ='green')
plt.show()