<jupyter_start><jupyter_text>策略 0.实现工具scikit-learn 1.数据特征特征数量少，样本数量正常 2.实现方法由于确定数据的特征空间分布，先使用线性核函数实现，观察误差然后再使用RBF核函数实现 3.后期扩展深入了解SVM的理论推导，最好是自己推导一遍；尝试更多的核函数以及参数，考虑这些函数的融合；实现可视化  resouce[SVM分类方案](https://www.cnblogs.com/CheeseZH/p/5265959.html)[SVM核函数](https://blog.csdn.net/batuwuhanpei/article/details/52354822)[SVM概括](https://www.cnblogs.com/hhh5460/p/5186197.html)[SVM example](http://scikit-learn.org/stable/modules/svm.htmlsvm)[SVM API](http://scikit-learn.org/stable/modules/classes.html)<jupyter_code>from sklearn import svm
import pandas as pd
import numpy as np
train_set_num = 35000
test_set_num = 15000
batch_size = 500
iteration = int(train_set_num / batch_size) #　这个地方注意一下
# extract data from the file and store it in the dataframe
def extract_data(filename):
    data = pd.read_table(filename, delim_whitespace = True)
    # handle the exception
    #shuffle 
    data = data.sample(frac = 1).reset_index(drop = True)
    
    # seperate the dataset
    # train/test = 7/3
    train_set = data[0:train_set_num]
    test_set = data[train_set_num:]
    return train_set, test_set

# seperate the data and label
# return the ndarray
def split_data_label(data):
    data_label = data.as_matrix(['Label'])
    data_input = data.as_matrix(['K1', 'K2', 'K3', 'K4'])
    print(data_label)
    print(data_input)
    return data_input, data_label

# next_batch operation
start = 0
def next_batch(data_input, data_label, batch_size, set_num):
    global start
    begin = start
    end = begin + batch_size
    # better improve this part
    if end >= set_num:
        print("This set is done")
    start = end
    return data_input[begin:end, : ], data_label[begin:end, : ]<jupyter_output><empty_output><jupyter_text>shuffle:https://blog.csdn.net/qq_22238533/article/details/70917102slice:https://blog.csdn.net/yoonhee/article/details/76168253<jupyter_code>filename = "/home/rick/Documents/data/CB550_features_targets.dat"
train_set, test_set = extract_data(filename)
train_input, train_label = split_data_label(train_set)
test_input, test_label = split_data_label(test_set)
clf = svm.SVC(decision_function_shape = 'ovo')

for i in range(iteration):
        batch_X, batch_Y = next_batch(train_input, train_label, batch_size, train_set_num)
        clf.fit(batch_X, batch_Y.T.ravel())


'''
def run(input, label ,batch_size, set_num):
    # 注意python３取消了xrange()
    for i in range(iteration):
        batch_X, batch_Y = next_batch(input, label, batch_size, set_num)
        clf.fit(batch_X, batch_Y.T.ravel())

run(train_input, train_label, batch_size, train_set_num)
'''
result = clf.predict(test_input)
test_label = test_label
#print(test_label)
#print(result)
num = len(result)
bingo = 0
for i in range(num - 1):
    if result[i] == test_label[i][0]:
        bingo += 1
accuracy = bingo / num
print("the accuracy of precition is %f"% accuracy)<jupyter_output>the accuracy of precition is 0.797400
{'fraction_non_alphanumeric': 0.11884257664485015, 'fraction_numerical': 0.031346882535308304, 'mean_word_length': 4.819639278557114, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '223712', 'license': 'Apache-2.0', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1187, 'n_words': 203, 'path': 'SVM/SVM.ipynb', 'repo_name': 'vsc-hvdc/srtp_GIS', 'size': '2903'}