最后的pre_text 不应该是一样的嘛,为什么结果会不同呢?
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
def load_data(filename): # 加载数据
f = open(filename, encoding='utf-8-sig')
data = np.genfromtxt(f, delimiter=',')
f.close()
y = data[:, 0].astype(int)
x = data[:, 1:]
x_std = StandardScaler().fit_transform(x)
x_train, x_test, y_train, y_test = train_test_split(x_std, y, test_size=.3)
return x_train, x_test, y_train, y_test
def svm_c(x_train, x_test, y_train, y_test):
svc = SVC(kernel='rbf', class_weight='balanced', probability=True)
clf = svc.fit(x_train, y_train) # 训练模型,
pre_test = clf.predict(x_test)
pre_test_proba = clf.predict_proba(x_test) # 计算测试集精度
return y_test, pre_test, pre_test_proba
if __name__ == '__main__':
y_test, pre_test, pre_test_proba = svm_c(*load_data('./files/Data.csv'))
print(pre_test)
x = svm_c(*load_data('./files/Data.csv'))[1]
print(x)