1.一元线性回归
对鸢尾花数据集中petal_length和petal_width两列数据进行一元线性回归分析。对上述两列数据进行预处理并进行回归分析,画出散点图和回归线。打印回归方程的截距和斜率。根据回归模型,给定花萼长度为4.0的花,预测其花萼宽度
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.linear_model import LinearRegression
iris = load_iris()
data = pd.DataFrame(iris.data)
data.columns = ["sepal_length","sepal_width","petal_length","petal_width"]
x = data["petal_length"].values
y = data["petal_width"].values
x = x.reshape(len(x),1)
y = y.reshape(len(y),1)
lmodel = LinearRegression()
lmodel.fit(x,y)
plt.scatter(x,y)
pre_y = lmodel.predict(x)
plt.plot(x,pre_y,'r-',linewidth=2)
print("截距:",lmodel.intercept_)
print("斜率:",lmodel.coef_)
print("predict petal_length=4.0:",lmodel.predict([[4.0]]))