V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shuangxunian
V2EX  ›  程序员

关于 SVM 一些疑问

  •  
  •   shuangxunian · 84 天前 · 801 次点击
    这是一个创建于 84 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我现在有这样的数据: 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 条回复    2024-02-08 10:16:38 +08:00
    kang773371222
        1
    kang773371222  
       82 天前
    SVM 支持分类和回归,你确定你用的是支持分类的应该就行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1846 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:27 · PVG 08:27 · LAX 17:27 · JFK 20:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.