-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathStreamlit_Bk7_Ch04_06.py
More file actions
83 lines (66 loc) · 2.14 KB
/
Copy pathStreamlit_Bk7_Ch04_06.py
File metadata and controls
83 lines (66 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
###############
# Authored by Weisheng Jiang
# Book 1 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import streamlit as st
p = plt.rcParams
p["font.sans-serif"] = ["Roboto"]
p["font.weight"] = "light"
p["ytick.minor.visible"] = True
p["xtick.minor.visible"] = True
p["axes.grid"] = True
p["grid.color"] = "0.5"
p["grid.linewidth"] = 0.5
# 生成随机数据
np.random.seed(0)
num = 30
X = np.random.uniform(0,4,num)
y = np.sin(0.4*np.pi * X) + 0.4 * np.random.randn(num)
data = np.column_stack([X,y])
x_array = np.linspace(0,4,101).reshape(-1,1)
degree_array = [1,2,3,4,7,8]
with st.sidebar:
st.title('Polynomial Regression')
degree = st.slider('Degree',
min_value = 1,
max_value = 9,
value = 2, step = 1)
fig, ax = plt.subplots(figsize=(5,5))
poly = PolynomialFeatures(degree = degree)
X_poly = poly.fit_transform(X.reshape(-1, 1))
# 训练线性回归模型
poly_reg = LinearRegression()
poly_reg.fit(X_poly, y)
y_poly_pred = poly_reg.predict(X_poly)
data_ = np.column_stack([X,y_poly_pred])
y_array_pred = poly_reg.predict(
poly.fit_transform(x_array))
# 绘制散点图
ax.scatter(X, y, s=20)
ax.scatter(X, y_poly_pred, marker = 'x', color='k')
ax.plot(([i for (i,j) in data_], [i for (i,j) in data]),
([j for (i,j) in data_], [j for (i,j) in data]),
c=[0.6,0.6,0.6], alpha = 0.5)
ax.plot(x_array, y_array_pred, color='r')
# 提取参数
coef = poly_reg.coef_
intercept = poly_reg.intercept_
# 回归解析式
equation = '$y = {:.1f}'.format(intercept)
for j in range(1, len(coef)):
equation += ' + {:.1f}x^{}'.format(coef[j], j)
equation += '$'
equation = equation.replace("+ -", "-")
# ax.text(0.05, -1.8, equation)
st.write(equation)
ax.set_aspect('equal', adjustable='box')
ax.set_xlim(0,4)
ax.grid(False)
ax.set_ylim(-2,2)
st.pyplot(fig)