"""
y = 0.5x+3
"""
import numpy as np
import matplotlib.pyplot as plt
# 令x为-5到5之间,元素数为100的等差数列
x = np.linspace(-5, 5, 100)
# 输入直线方程
y = 0.5 * x + 3
plt.plot(x, y, c='orange')
# 图题设为"straight line"
plt.title("Straight line")
plt.show()
"""
线性模型的图形表示
"""
# 导入线性回归模型
from sklearn.linear_model import LinearRegression
# 输入两个点的横坐标
X = [[1], [4], [3]]
# 输入两个点的纵坐标
y = [3, 5, 3]
# 用线性模型拟合这两个点
lr = LinearRegression().fit(X, y)
# 画出两个点和直线的图形
z = np.linspace(0, 5, 20)
plt.scatter(X, y, s=80)
plt.plot(z, lr.predict(z.reshape(-1, 1)), c='k')
# 设定图题为Straight line
plt.title("Straight line")
plt.show()
# 打印直线方程
print('y = {:.3f}'.format(lr.coef_[0]), 'x', '+ {:.3f}'.format(lr.intercept_))
y = 0.571 x + 2.143
from sklearn.datasets import make_regression
# 生成用于回归分析的数据集
X, y = make_regression(n_samples=50, n_features=1, n_informative=1, noise=50, random_state=1)
# 使用线性模型对数据进行拟合
reg = LinearRegression()
reg.fit(X, y)
# z是我们生成的等差数列,用来画出线性模型的图形
z = np.linspace(-3, 3, 200).reshape(-1, 1)
plt.scatter(X, y, c='b', s=60)
plt.plot(z, reg.predict(z), c='k')
plt.title('Linear Regression')
plt.show()
# 打印直线的系数和截距
print("直线的系数是:{:.2f}".format(reg.coef_[0]))
print("直线的截距是:{:.2f}".format(reg.intercept_))
直线的系数是:79.52
直线的截距是:10.92