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演算法的筆跡識別,總有地方報錯,求大神幫忙
你想把這個參數的值列印出來,但參數並不存在,最簡單的方法,把這句列印的語句注釋掉