A. python sklearn 怎样用
SVM既可以用来分类,就是SVC;又可以用来预测,或者成为回归,就是SVR。sklearn中的svm模块中也集成了SVR类。
我们也使用一个小例子说明SVR怎么用。
X = [[0, 0], [1, 1]] y = [0.5, 1.5] clf = svm.SVR() clf.fit(X, y) result = clf.predict([2, 2]) print result
B. 如何以Python代码实例展示kNN算法的实际运用
给样本数据集T={2,4,10,12,3,20,22,21,11,24} t={18},K=4 1. N={2,4,10,12},d1=16,d2=14,d3=8,d4=6 2.d={3},比较,N={4,10,12,3},d1=14,d2=8,d3=6,d4=15 3.d={20},比较,N={4,10,12,20},d1=14,d2=8,d3=6,d4=2 4.d={22},比较,N={10,12,20,22},
C. python knn能够用来干吗
如果你是在校生,你可以加入相关实验室。如果不是的话,有些python论坛或者编程论坛你可以进去看看,有相关项目练手。像码云,github上有很多python项目,你可以申请加入,当然要求较高。也可以把python2的程序用python3写(网上大多是用2写的爬...
D. 如何用python实现knn算法
1. 数据分类:离散型标签 2. 数据回归:连续型标签 近邻算法的准则是:寻找接近新数据点的训练样本的数目,根据训练样本的信息来预测新数据点的某些信息。
E. 新手学习PYTHON中KNN算法的手写识别出现问题 求助
参考了其他博主的代码 想试着运行 然后去理解。结果一直报错,希望大神帮帮忙。
import numpy as np
import os
import kNN
def img2vector(filename):
"""函数将以文本格式出现的32*32的0-1图片,转变成一维特征数组,返回一维数组
Keyword argument:
filename -- 文本格式的图片文件
"""
imgvect = np.zeros((1, 1024))
fr = open(filename)
for i in range(32):
linestr = fr.readline()
for j in range(32):
imgvect[0, 32*i + j] = int(linestr[j])
return imgvect
def handwriteClassfiy(testfile, trainfile, k):
"""函数将trainfile中的文本图片转换成样本特征集和样本类型集,用testfile中的测试样本测试,无返回值
Keyword argument:
testfile -- 测试图片目录
trainfile -- 样本图片目录
"""
trainFileList = os.listdir(trainfile)
trainFileSize = len(trainFileList)
labels = []
trainDataSet = np.zeros((trainFileSize, 1024))
for i in range(trainFileSize):
filenameStr = trainFileList[i]
digitnameStr = filenameStr.split('.')[0]
digitLabels = digitnameStr.split('_')[0]
labels.append(digitLabels)
trainDataSet[i, :] = img2vector(trainfile + '/' + filenameStr)
testFileList = os.listdir(testfile)
testNumber = len(testFileList)
errorcount = 0.0
for testname in testFileList:
testdigit = img2vector(testfile + '/' + testname)
classifyresult = kNN.classfiy(testdigit, trainDataSet, labels, k)
testStr = testname.split('.')[0]
testDigitLabel = testStr.split('_')[0]
if classifyresult != testDigitLabel:
errorcount += 1.0
#print('this test real digit is:%s, and the result is: %s' % (testDigitLabel, classifyresult))
print('k = %d, errorRatio is: %f' % (k, errorcount/float(testNumber)))
return
if __name__ == '__main__':
filename = 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/testDigits/0_1.txt'
traindir= 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/trainingDigits'
testdir = 'C:/Users/lx/Desktop/MachineLearning-master/kNN/use Python and NumPy/testDigits'
handwriteClassfiy(testdir, traindir, 3)
错误提示Traceback (most recent call last):
File "kNN.py", line 56, in <mole>
handwriteClassfiy(testdir, traindir, 3)
File "kNN.py", line 43, in handwriteClassfiy
classifyresult = kNN.classfiy(testdigit, trainDataSet, labels, k)
AttributeError: mole 'kNN' has no attribute 'classfiy'
你这个文件是不是就叫 kNN.py ?如果是的话那你这个里面根本就没有 classfiy 这个属性,当然会报错。
另外,import kNN 是 import 自己?
F. knn算法算是一种python模型吗
“算法”不能算是“模型”,更不能说是“python模型”,因为python能实现的,c++、java等通用语言也能实现。
G. 谁可以提供Python环境中用KNN手写识别数据MNIST的读取代码
其实就是python怎么读取binnary
file
mnist的结构如下,选取train-images
TRAINING
SET
IMAGE
FILE
(train-images-idx3-ubyte):
[offset]
[type]
[value]
[description]
0000
32
bit
integer
0x00000803(2051)
magic
number
0004
32
bit
integer
60000
number
of
images
0008
32
bit
integer
28
number
of
rows
0012
32
bit
integer
28
number
of
columns
0016
unsigned
byte
??
pixel
0017
unsigned
byte
??
pixel
........
xxxx
unsigned
byte
??
pixel
也就是之前我们要读取4个
32
bit
integer
试过很多方法,觉得最方便的,至少对我来说还是使用
struct.unpack_from()
filename
=
'train-images.idx3-ubyte'binfile
=
open(filename
,
'rb')buf
=
binfile.read()
先使用二进制方式把文件都读进来
index
=
0magic,
numImages
,
numRows
,
numColumns
=
struct.unpack_from('>IIII'
,
buf
,
index)index
+=
struct.calcsize('>IIII')
然后使用struc.unpack_from
'>IIII'是说使用大端法读取4个unsinged
int32
然后读取一个图片测试是否读取成功
im
=
struct.unpack_from('>784B'
,buf,
index)index
+=
struct.calcsize('>784B')
im
=
np.array(im)im
=
im.reshape(28,28)
fig
=
plt.figure()plotwindow
=
fig.add_subplot(111)plt.imshow(im
,
cmap='gray')plt.show()
'>784B'的意思就是用大端法读取784个unsigned
byte
完整代码如下
import
numpy
as
npimport
structimport
matplotlib.pyplot
as
plt
filename
=
'train-images.idx3-ubyte'binfile
=
open(filename
,
'rb')buf
=
binfile.read()
index
=
0magic,
numImages
,
numRows
,
numColumns
=
struct.unpack_from('>IIII'
,
buf
,
index)index
+=
struct.calcsize('>IIII')
im
=
struct.unpack_from('>784B'
,buf,
index)index
+=
struct.calcsize('>784B')
im
=
np.array(im)im
=
im.reshape(28,28)
fig
=
plt.figure()plotwindow
=
fig.add_subplot(111)plt.imshow(im
,
cmap='gray')plt.show()
只是为了测试是否成功所以只读了一张图片
H. 在Python上Dry Beans用knn分类训练集为70%测试集为30%的代码怎么写
摘要
处理数据集数据 清洗,采用留出法hold-out拆分数据集:训练集、测试集
I. python,knn算法的笔迹识别,总有地方报错,求大神帮忙
你想把这个参数的值打印出来,但参数并不存在,最简单的方法,把这句打印的语句注释掉