资源简介
用SVM解决兵王问题,是比较好的机器学习svm入门项目,包括训练测试数据集、libsvm包以及程序代码。其中数据集总样本数为28056个,取其中5000个作为训练样本,其余的为测试样本。libsvm包中有各个属性的使用说明,代码为python版本,测试的准确率为99.36%
代码片段和文件信息
import sys
import os
import datetime
import pickle
import numpy as np
import scipy.io as sio
import matplotlib
import matplotlib.pyplot as plt
sys.path.append(‘.\libsvm-master\python‘)
from svmutil import *
from numpy import *
tStart = datetime.datetime.now()
# 处理数据
rawdata = ‘krkopt.data‘
rd = open(rawdata)
arrayOLines = rd.readlines()
del arrayOLines[0]
numbersOfLines = len(arrayOLines)
featureDimension = 6
data = zeros((numbersOfLines featureDimension))
label = zeros(numbersOfLines)
for i in range(len(arrayOLines)):
line = arrayOLines[i]
listFromLine = line.split(‘‘)
data[i 0] = ord(listFromLine[0]) - 96
data[i 1] = ord(listFromLine[1]) - 48
data[i 2] = ord(listFromLine[2]) - 96
data[i 3] = ord(listFromLine[3]) - 48
data[i 4] = ord(listFromLine[4]) - 96
data[i 5] = ord(listFromLine[5]) - 48
if listFromLine[6] == ‘draw\n‘:
label[i] = 1
else:
label[i] = -1
# 随机取5000个数据作为训练样本 剩下数据作为测试样本
permutatedData = zeros((numbersOfLines featureDimension))
permutatedLabel = zeros(numbersOfLines)
p = random.permutation(numbersOfLines)
for i in range(numbersOfLines):
permutatedData[i :] = data[p[i] :]
permutatedLabel[i] = label[p[i]]
numbersOfTrainData = 5000
xTrain = permutatedData[:numbersOfTrainData]
yTrain = permutatedLabel[:numbersOfTrainData]
xTest = permutatedData[numbersOfTrainData:]
yTest = permutatedLabel[numbersOfTrainData:]
# 样本均一化
averageData = zeros((1 featureDimension))
for i in range(len(xTrain)):
averageData += xTrain[i :]
averageData = averageData / len(xTrain)
standardDiviation = zeros((1 featureDimension))
for i in range(len(xTrain)):
standardDiviation = standardDiviation + (xTrain[i] - averageData) ** 2
standardDiviation = (standardDiviation/(len(xTrain)-1))**0.5
for i in range(len(xTrain)):
xTrain[i] = (xTrain[i] - averageData)/standardDiviation
for i in range(len(xTest)):
xTest[i] = (xTest[i] - averageData)/standardDiviation
# 粗略搜素最佳的C、gamma参数
CScale = list(range(-5 16 2))
gammaScale = list(range(-15 4 2))
maxRecognitionRate = 0
arr = np.array(xTrain)
newX = arr.tolist()
arr = np.array(yTrain)
newY = arr.tolist()
for i in range(len(CScale)):
testC = 2 ** CScale[i]
for j in range(len(gammaScale)):
cmd = ‘-t 2 -c ‘
cmd += str(testC)
cmd += ‘ -g ‘
testGamma = 2**gammaScale[j]
cmd += str(testGamma)
cmd += ‘ -v 5‘
cmd += ‘ -h 0‘
print(‘rude search‘)
recognitionRate = svm_train(newY newX cmd)
if recognitionRate > maxRecognitionRate:
maxRecognitionRate = recognitionRate
print(maxRecognitionRate)
maxCIndex = i
maxGammaIndex = j
#精确搜索C、gamma
n = 10
minCScale = 0.5 * (CScale[max(0 maxCIndex - 1)]+CScale[maxCIndex])
maxCScale = 0.5 * (CScale[min(len(CScale) - 1 maxCIndex + 1)] + CScale[maxCIndex])
newCScale =
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2020-07-29 21:58 SVM\
文件 0 2020-07-27 23:22 SVM\__init__.py
文件 184648 2020-07-29 21:58 SVM\decisionValues.mat
文件 559878 2020-03-02 17:56 SVM\krkopt.data
目录 0 2020-07-28 20:23 SVM\libsvm-master\
文件 1497 2018-07-15 22:15 SVM\libsvm-master\COPYRIGHT
文件 83238 2018-07-15 22:15 SVM\libsvm-master\FAQ.html
文件 27670 2018-07-15 22:15 SVM\libsvm-master\heart_scale
目录 0 2020-07-28 20:23 SVM\libsvm-master\java\
文件 55181 2018-07-15 22:15 SVM\libsvm-master\java\libsvm.jar
目录 0 2020-07-28 20:23 SVM\libsvm-master\java\libsvm\
文件 64084 2018-07-15 22:15 SVM\libsvm-master\java\libsvm\svm.java
文件 63281 2018-07-15 22:15 SVM\libsvm-master\java\libsvm\svm.m4
文件 868 2018-07-15 22:15 SVM\libsvm-master\java\libsvm\svm_model.java
文件 115 2018-07-15 22:15 SVM\libsvm-master\java\libsvm\svm_node.java
文件 1285 2018-07-15 22:15 SVM\libsvm-master\java\libsvm\svm_parameter.java
文件 87 2018-07-15 22:15 SVM\libsvm-master\java\libsvm\svm_print_interface.java
文件 136 2018-07-15 22:15 SVM\libsvm-master\java\libsvm\svm_problem.java
文件 659 2018-07-15 22:15 SVM\libsvm-master\java\Makefile
文件 4945 2018-07-15 22:15 SVM\libsvm-master\java\svm_predict.java
文件 8937 2018-07-15 22:15 SVM\libsvm-master\java\svm_scale.java
文件 12262 2018-07-15 22:15 SVM\libsvm-master\java\svm_toy.java
文件 8354 2018-07-15 22:15 SVM\libsvm-master\java\svm_train.java
文件 81 2018-07-15 22:15 SVM\libsvm-master\java\test_ap
文件 732 2018-07-15 22:15 SVM\libsvm-master\Makefile
文件 1135 2018-07-15 22:15 SVM\libsvm-master\Makefile.win
目录 0 2020-07-28 20:23 SVM\libsvm-master\matlab\
文件 4060 2018-07-15 22:15 SVM\libsvm-master\matlab\libsvmread.c
文件 2326 2018-07-15 22:15 SVM\libsvm-master\matlab\libsvmwrite.c
文件 888 2018-07-15 22:15 SVM\libsvm-master\matlab\make.m
文件 1240 2018-07-15 22:15 SVM\libsvm-master\matlab\Makefile
............此处省略52个文件信息
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论