Python sklearn 中的SVM示例
# -*- coding: utf-8 -*-
import pandas as pd
from numpy.random import shuffle
from sklearn import svm
import joblib
from sklearn import metrics
inputfile = '../Data/moment.csv'
outputfile1 = '../Data/WaterEval_train.csv'
outputfile2 = '../Data/WaterEval_test.csv'
def readData():
"""
读取数据
:return:
"""
data = pd.read_csv(inputfile, encoding='utf8')
data = data.as_matrix().astype(float)
shuffle(data) # 随机打乱
data_train = data[:int(0.8 * len(data)), :] # 训练集矩阵
data_test = data[int(0.8 * len(data)):, :] # 测试集矩阵
return data_train, data_test
def train(data_train, data_test):
"""
训练
:param data_train:
:param data_test:
:return:
"""
x_train = data_train[:, 2:] * 30 # 放大特征,矩阵2维
y_train = data_train[:, 0].astype(int) # 矩阵1维
x_test = data_test[:, 2:] * 30 # 放大特征
y_test = data_test[:, 0].astype(int)
# C:错误项的惩罚系数。C越大,即对分错样本的惩罚程度越大,