多元线性回归
Day 3的任务是多元线性回归. 开始任务~
Step1 Data Preprocessing
首先我们import numpy, pandas, matplotlib. 使用pandas来read数据集. 使用sklearn来分配训练集和测试集. test_size为五分之一. 注意, 这里有必要的话, 我们需要编辑虚拟向量并注意避免虚拟变量陷阱.
code如下:
# Step1 Data Preprocessing
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Import Datasets
dataset = pd.read_csv('../datasets/50_Startups.csv')
X = dataset.iloc[ : , :-1].values
Y = dataset.iloc[ : , 4 ].values
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder = LabelEncoder()
X[: , 3] = labelencoder.fit_transform(X[ : , 3])
onehotencoder = OneHotEncoder(categorical_features= [3])
X = X[: , 1:]
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
print("X_train")
print(X_train)
print("X_test")
print(X_test)
print("Y_train")
print(Y_train)
print("Y_test")
print(Y_test)
Step2 train the model by using multiple linear regression
线性回归来训练我们的数据集.
code如下:
# Step2 train by using multiple linear regression
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, Y_train)
Step3 Prediction Outcome
我们可以使用predict来预测输出, 将输出保存到Y_pred中, 然后打印出来.
code如下:
#Step 3: Prediction Outcome
Y_pred = regressor.predict(X_test)
print('Y_pred')
print(Y_pred)
Step4 Visulization
最后一步, 我们使用matplotlib来可视化我们的结果. 这里我们可以把X_train和X_test可视化出来.
code如下:
#Step 4: Visulization
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.show()
plt.plot(X_test, regressor.predict(X_test), color = 'blue')
plt.show()