数据挖掘领域十大经典算法之—SVM算法(超详细附代码) (3)


# encoding=utf-8

import time

import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
from sklearn import datasets
from sklearn import svm

if __name__ == \'__main__\':

    print(\'prepare datasets...\')
    # Iris数据集
    # iris=datasets.load_iris()
    # features=iris.data
    # labels=iris.target

    # MINST数据集
    raw_data = pd.read_csv(\'../data/train_binary.csv\', header=0)  # 读取csv数据,并将第一行视为表头,返回DataFrame类型
    data = raw_data.values
    features = data[::, 1::]
    labels = data[::, 0]    # 选取33%数据作为测试集,剩余为训练集

    train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size=0.33, random_state=0)

    time_2=time.time()
    print(\'Start training...\')
    clf = svm.SVC()  # svm class   
    clf.fit(train_features, train_labels)  # training the svc model
    time_3 = time.time()
    print(\'training cost %f seconds\' % (time_3 - time_2))

    print(\'Start predicting...\')
    test_predict=clf.predict(test_features)
    time_4 = time.time()
    print(\'predicting cost %f seconds\' % (time_4 - time_3))

    score = accuracy_score(test_labels, test_predict)
print("The accruacy score is %f" % score)

测试数据集为经过二分类处理后的MNIST数据集,获取地址train_binary.csv
————————————————
版权声明:本文为CSDN博主「fuqiuai」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fuqiuai/article/details/79483057

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgjwxz.html