0

## python svr輸出圖及準確度問題

rui_ 2021-02-20 16:34:181340 瀏覽

``````import tensorflow as tf
import numpy as np  # 導入numpy
import pandas as pd # 導入pandas
from sklearn.svm import SVR  # 使用SVM中的SVR
import matplotlib.pyplot as plt #畫圖

drive.mount('/content/drive',force_remount=True)
data_path = '/content/drive/MyDrive/drill_parameter.csv' #導入data
df=pd.DataFrame(drill)

x=df.loc[:,['Spindle speed', 'Feed rate', 'Point angle']]
y=df['Thrust force']

# 資料分割
from sklearn.model_selection import train_test_split #分割資料的函式
x_train, x_test, y_train, y_test = train_test_split(x.values, y.values, random_state=10, train_size=0.8)

#正規化
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler #標準化
ss_x = StandardScaler()
ss_y = StandardScaler()
x_train = ss_x.fit_transform(x_train)
x_test = ss_x.fit_transform(x_test)
y_train = ss_y.fit_transform(y_train.reshape(-1, 1))
y_test = ss_y.fit_transform(y_test.reshape(-1, 1))

#建立SVR
svr_rbf = SVR(C=1e3, kernel='rbf', gamma='auto')
svr_rbf.fit(x_train,y_train)
svr_predict=svr_rbf.predict(x_test)

#模型評估
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
print('\nThe value of default measurement of rbf SVR is', svr_rbf.score(x_test, y_test))
print('R-squared value of rbf SVR is', r2_score(y_test, svr_predict))
print('The mean squared error of rbf SVR is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(svr_predict)))
print('The mean absolute error of rbf SVR is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(svr_predict)))
print(svr_rbf.intercept_) #迴歸模型的截距

#輸出圖
plt.scatter(svr_predict, y_test, color= 'black', label= 'Data') #數據點
plt.plot(svr_predict, svr_predict, color= 'red', label= 'RBF model') #迴歸線
plt.xlabel('data')
plt.ylabel('quality')
plt.title('Support Vector Regression')
plt.legend()
plt.show()
``````

1.正規化→跟用EXCEL算不一樣
2.輸出圖→在網路上有看到放x_train 跟 svr_predict的程式,可是我放了x_train就會顯示x and y must be same size
3.重新切資料或調SVR裡的C,r square都上不去了,想問是不是資料筆數太少或應該調整其他地方(我共有75筆資料,這裡只放上40筆)

froce iT邦大師 1 級 ‧ 2021-02-22 09:12:21 檢舉
1.你確定你在excel做的和StandardScaler 是做相同的事？