import os
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import svm
path = 'D:/AC_xlsx/training_set'
fs_all_file= os.listdir(path)
for file_name in fs_all_file:
complete_path = os.path.join(path, file_name)
#依序讀取檔案路徑
dataX = pd.read_excel(complete_path,
sheet_name = ' X',
header = None)
dataY = pd.read_excel(complete_path,
sheet_name = 'Y',
header = None)
dataZ = pd.read_excel(complete_path,
sheet_name = 'Z',
header = None)
x= pd.concat([dataX, dataY, dataZ], axis=1)
dataTR = pd.read_excel(complete_path,
sheet_name = 'trrrrr',
header = None)
y = dataTR.copy()
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2,random_state=0)
clf=svm.SVC(kernel='linear',gamma='auto',C=10)
clf.fit(x_train,y_train)
clf.predict(x_test)
print(clf.score(x_train,y_train))
print(clf.score(x_test, y_test))
我跑出來結果是y should a 1d array,got an array of shape (376,3100) instead,請教要怎弄感謝觀看及回覆
要丟入SVM需要np.array一維的資料
而上面
a = np.array([[1,2,3], [4,5,6]])
np.reshape(a, (1,6))
array([[1, 2, 3, 4, 5, 6]]) --結果依舊是二維
a = np.array([[1,2,3], [4,5,6]])
np.reshape(a, (6))
array([1, 2, 3, 4, 5, 6]) --這樣才是一維
依這篇來看
就是「錯誤地把 2D array 的資料傳給需要 1D array 的參數」
至於要如何把 2D array 的資料「轉」成 1D array
可以參考這篇
或Google 這一堆