機器學習100天---day02 簡單線性迴歸模型
阿新 • • 發佈:2018-12-09
資料集:
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
# 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()