关于 SVM 一些疑问

144 天前
 shuangxunian

我现在有这样的数据: 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)

佬们可以帮我斧正一下吗,有哪里需要改一下?我一直感觉前面有一层雾蒙蒙的东西我理解不上来

856 次点击
所在节点    程序员
1 条回复
kang773371222
142 天前
SVM 支持分类和回归,你确定你用的是支持分类的应该就行

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1014724

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX