-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVR支持向量机回归.py
112 lines (93 loc) · 2.8 KB
/
SVR支持向量机回归.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from sklearn.svm import SVR
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
Dict = {'x1':[1,4,3,2,5,6,6,7],'x2':[4,8,6,3,5,6,13,14]}
X_Data = np.array([1,4,3,2,5,6]).reshape(-1,1)
Y_Data = np.array([4,8,6,3,5,6]).reshape(-1,1)
model1 = SVR(kernel='linear',epsilon=1)
model1.fit(X_Data,Y_Data)
X_test = np.linspace(0,8,100).reshape(-1,1)
y = model1.predict(X_test)
model1_score = model1.score(X_Data,Y_Data)
print(model1_score)
model3 = SVR(kernel='rbf')
model3.fit(X_Data,Y_Data)
X_test = np.linspace(0,8,100).reshape(-1,1)
y2 = model3.predict(X_test)
model3_score = model3.score(X_Data,Y_Data)
model2 = LinearRegression()
model2.fit(X_Data,Y_Data)
y1 = model2.predict(X_test)
model2_score = model2.score(X_Data,Y_Data)
print(model2_score)
model4 = SVR(kernel='sigmoid')
model4.fit(X_Data,Y_Data)
y3 = model4.predict(X_test)
model4_score = model4.score(X_Data,Y_Data)
plt.subplot(221)
plt.plot(X_test,y)
plt.scatter(X_Data,Y_Data)
plt.title('SVR(Linear)')
plt.grid()
plt.subplot(222)
plt.plot(X_test,y1)
plt.scatter(X_Data,Y_Data)
i = model2_score
plt.title('Linear Regression')
plt.grid()
plt.subplot(223)
plt.title('SVR(RBF)',fontsize=8)
plt.scatter(X_Data,Y_Data)
plt.plot(X_test,y2)
plt.grid()
plt.subplot(224)
plt.scatter(X_Data,Y_Data)
plt.plot(X_test,y3)
plt.title('SVR(Sigmoid)',fontsize=8)
plt.grid()
plt.show()
def Draw_plot_SVR_Sigmoid(x:list,y:list):
from sklearn.svm import SVR
main_model = SVR(kernel='sigmoid')
x_data = np.array(x).reshape(-1,1)
y_data = np.array(y).reshape(-1,1)
plt.scatter(x_data,y_data)
main_model.fit(x_data,y_data)
X_test = np.linspace(0, 8, 100).reshape(-1, 1)
predict_data = main_model.predict(X_test)
plt.plot(X_test,predict_data)
plt.grid()
plt.title('SVR(Sigmoid)')
plt.show()
def Draw_plot_SVR_Linear(x:list,y:list):
from sklearn.svm import SVR
main_model = SVR(kernel='linear')
x_data = np.array(x).reshape(-1,1)
y_data = np.array(y).reshape(-1,1)
plt.scatter(x_data,y_data)
main_model.fit(x_data,y_data)
X_test = np.linspace(0, 8, 100).reshape(-1, 1)
predict_data = main_model.predict(X_test)
plt.plot(X_test,predict_data)
plt.grid()
plt.title('SVR(Linear)')
plt.show()
print(Draw_plot_SVR_Sigmoid([1,2,3,4,5,6,7],[4,5,4,6,3,2,6]))
print(Draw_plot_SVR_Linear([1,2,3,4,5,6,7],[4,5,4,6,3,8,6]))
#统计拟合度
score_list = []
for i in np.linspace(0,2,20):
model1 = SVR(kernel='linear', epsilon=i)
model1.fit(X_Data, Y_Data)
X_test = np.linspace(0, 8, 100).reshape(-1, 1)
y = model1.predict(X_test)
model1_score = model1.score(X_Data, Y_Data)
score_list.append(model1_score)
print(score_list)
plt.plot(score_list)
plt.title('SVR Linear Model Scores')
plt.xlabel('$\epsilon$',fontsize=20)
plt.ylabel('Scores')
plt.grid()
plt.show()