🍨 本文为[🔗365天深度学习训练营]中的学习记录博客
🍖 原作者:[K同学啊]
一、简单线性回归模型
1、数据预处理
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dataset = pd.read_csv(r"D:\资料\365deep_learning\1.机器学习\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)
dataset
2、简单线性回归模型
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor = regressor.fit(X_train, Y_train)
3、预测结果
Y_pred = regressor.predict(X_test)
4、可视化
1)训练集可视化
2)测试集预测结果可视化
5、鸢尾花数据集导入
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['花萼-length', '花萼-width', '花瓣-length', '花瓣-width', 'class']
dataset = pd.read_csv(url, names=names)
dataset
二、多元线性回归模型
1、数据预处理
1)导入数据集
import pandas as pd
import numpy as np
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['花萼-length', '花萼-width', '花瓣-length', '花瓣-width', 'class']
dataset = pd.read_csv(url, names=names)
dataset
2)数据分析
import matplotlib.pyplot as plt
plt.plot(dataset['花萼-length'], dataset['花瓣-width'], 'x', label="marker='x'")
plt.plot(dataset['花萼-width'], dataset['花瓣-width'], 'o', label="marker='o'")
plt.plot(dataset['花瓣-length'], dataset['花瓣-width'], 'v', label="marker='v'")
plt.legend(numpoints=1)
plt.show()
X = dataset.iloc[ : ,[1,2]].values
Y = dataset.iloc[ : , 3 ].values
3)构建训练集、测试集
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)
2、训练多元线性回归模型
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, Y_train)
3、在测试集上预测结果
y_pred = regressor.predict(X_test)
4、测试集预测结果可视化
plt.scatter(Y_test,y_pred, color='red')
plt.xlabel("Prediction")
plt.ylabel("True");
plt.show()