SVM对红酒数据集分类实验

#  # -*- coding: utf-8 -*-
import os
import numpy as np
from sklearn.svm import SVC
from sklearn import metrics
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA

    #第一步 加载数据集
path = u"C:\\Users\\Lenovo\\Downloads\\wine.txt"
data = np.loadtxt(path,dtype=float,delimiter=",")
print(data)

    #第二步 划分数据集
yy, x = np.split(data, (1,), axis=1) #第一列类标yy,后面13列特征为x
print(yy.shape, x.shape)
y = []
for n in yy:
    y.append(int(n))
y =  np.array(y, dtype = int) #list转换数组
    #划分数据集 测试集40%
train_data, test_data, train_target, test_target = train_test_split(x, y, test_size=0.4, random_state=42)
print( train_data.shape, train_target.shape)
print (test_data.shape, test_target.shape)

    #第三步 SVC训练
clf = SVC()
clf.fit(train_data, train_target)
result = clf.predict(test_data)
print(result)
print(test_target)

    #第四步 评价算法
print(np.sum(result==test_target) )#预测结果与真实结果比对
print(metrics.classification_report(test_target, result))  #准确率 召回率 F值

    #第五步 降维
pca = PCA(n_components=2)
newData = pca.fit_transform(test_data)

    #第六步 绘图可视化
plt.figure()
cmap_bold = ListedColormap(['#000000', '#00FF00', '#FFFFFF'])
plt.scatter(newData[:,0], newData[:,1], c=test_target, cmap=cmap_bold, s=50)
plt.show()

 

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值