使用matplotlib画一条直线
import numpy as np
import matplotlib.pyplot as pltx = np.linspace(-5, 5, 100)
y = 0.5*x + 3plt.plot(x, y, c="orange")
plt.title("Straight Line")
plt.show()
线性模型的直线表示
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegressionX = [[1], [4]]
y = [3,5]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")
plt.title("Straight Line")
plt.show()
获取线性模型的方程
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegressionX = [[1], [4]]
y = [3,5]lr = LinearRegression().fit(X, y)
print(f"y = {lr.coef_[0]:.3f}x + {lr.intercept_:.3f}")z = np.linspace(0, 5, 20)plt.scatter(X, y, s=80)
plt.plot(z, lr.predict(z.reshape(-1,1)), c="k")
plt.title("Straight Line")
plt.show()
绘制三个点的线性模型图
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegressionX = [[1], [4], [3]]
y = [3,5,3]lr = LinearRegression().fit(X, y)
print(f"y = {lr.coef_[0]:.3f}x + {lr.intercept_:.3f}")z = np.linspace(0, 5, 20)plt.scatter(X, y, s=80)
plt.plot(z, lr.predict(z.reshape(-1,1)), c="k")
plt.title("Straight Line")
plt.show()
生成数据集并绘制线性模型的图
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regressionX, y = make_regression(n_samples=50, n_features=1, n_informative=1, noise=50, random_state=1)lr = LinearRegression().fit(X, y)
print(f"y = {lr.coef_[0]:.3f}x + {lr.intercept_:.3f}")z = np.linspace(-3, 3, 200).reshape(-1, 1)plt.scatter(X, y, c="b", s=60)
plt.plot(z, lr.predict(z), c="k")
plt.title("Linear Regression")
plt.show()
训练和测试模型
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_splitX, y = make_regression(n_samples=100, n_features=2, n_informative=2, noise=50, random_state=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)lr = LinearRegression().fit(X_train, y_train)
print(f"y = {lr.coef_[0]:.3f}X1 + {lr.coef_[1]:.3f}X2 + {lr.intercept_:.3f}")
print(f"score = {lr.score(X_test, y_test)}")
实战:糖尿病预测
完整代码:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_diabetesdata = load_diabetes()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)lr = LinearRegression().fit(X_train, y_train)
print(f"y = {lr.coef_[0]:.3f}X1 + {lr.coef_[1]:.3f}X2 + {lr.intercept_:.3f}")
print(f"score = {lr.score(X_test, y_test)}")
输出:
y = 11.512X1 + -282.514X2 + 152.563
score = 0.4593422174874441