iT邦幫忙

0

python svr輸出圖及準確度問題

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

https://ithelp.ithome.com.tw/upload/images/20210220/20135268mwZK9UIcjI.jpg
我想用SVR建立三個X(紅色框)一個Y(藍色框)的迴歸模型
目前寫出下面的程式碼,可以執行但不知道對不對,想麻煩大家幫我看看
程式檔案下載(會跳出廣告,再按一下可以下載):https://www.sendspace.com/file/z3qyw3

import tensorflow as tf
import numpy as np  # 導入numpy
import pandas as pd # 導入pandas
from sklearn.svm import SVR  # 使用SVM中的SVR
from google.colab import drive #連接雲端硬碟
import matplotlib.pyplot as plt #畫圖

drive.mount('/content/drive',force_remount=True)
data_path = '/content/drive/MyDrive/drill_parameter.csv' #導入data
drill = pd.read_csv(data_path) #drill為原始資料檔
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 是做相同的事?
https://yanwei-liu.medium.com/python%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%8D%81%E4%B8%80-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E7%9A%84%E8%B3%87%E6%96%99%E5%89%8D%E8%99%95%E7%90%86%E6%8A%80%E8%A1%93-4dbd27560743

2.表示你作圖時x和y的數量不同...
把你的input叫出來看,搞不好根本不是你要的。

尚未有邦友回答

立即登入回答