我现在有这样的数据: x: [[1,2,3,...,15],[16,17,18,...,30],... * n ] y: [0,1,... * n] 我想对这个 15 维的数组求出来一个范围致使在里面取值得到的结果为 1 ,我搜了很久,想用 SVM 去实现,这是我的代码:
import numpy as np
from matplotlib import colors
from sklearn import svm
from sklearn import model_selection
import matplotlib.pyplot as plt
import matplotlib as mpl
# 加载
data = np.loadtxt('./data.csv',dtype=float,delimiter=',')
# 切分
x, y = np.split(data, (15, ), axis=1)
x_train, x_test, y_train, y_test=model_selection.train_test_split(x, y, random_state=1, test_size=0.2)
# 构建
def classifier():
clf = svm.SVC(C=0.8,kernel='linear',decision_function_shape='ovr')
return clf
# 训练
def train(clf, x_train, y_train):
clf.fit(x_train, y_train.ravel())
# 定义
clf = classifier()
# 调用
train(clf, x_train, y_train)
# 判断 a,b 是否相等 计算 acc 的均值
def show_accuracy(a, b, tip):
acc = a.ravel() == b.ravel()
print('%s Accuracy:%.3f' %(tip, np.mean(acc)))
# 分别打印训练集和测试集的准确率 score(x_train, y_train)表示输出 x_train,y_train 在模型上的准确率
def print_accuracy(clf, x_train, y_train, x_test, y_test):
print('training prediction:%.3f' %(clf.score(x_train, y_train)))
print('test data prediction:%.3f' %(clf.score(x_test, y_test)))
# 原始结果和预测结果进行对比 predict() 表示对 x_train 样本进行预测,返回样本类别
show_accuracy(clf.predict(x_train), y_train, 'traing data')
show_accuracy(clf.predict(x_test), y_test, 'testing data')
print_accuracy(clf, x_train, y_train, x_test, y_test)
佬们可以帮我斧正一下吗,有哪里需要改一下?我一直感觉前面有一层雾蒙蒙的东西我理解不上来
1
kang773371222 314 天前
SVM 支持分类和回归,你确定你用的是支持分类的应该就行
|