從零開始人工智能AI(一)-k-nn-矩陣

參考資料:人工智能

https://baike.baidu.com/item/%E7%9F%A9%E9%98%B5/18069?fr=aladdinspa

http://blog.csdn.net/c406495762/article/details/75172850.net

基礎概念:code

#0取行數,1取得列數blog

import numpy as np排序

dataSetSize = dataSet.shape[0]get

 

 

diffMat = np.tile(inX, (dataSetSize, 1)) - dataSetit

tile第二個參數爲元組,元組第一個參數表明複製的個數,縱(list的個數), 第二個參數爲橫向(單個list中的元素複製的份數)

例如:class

>>> a=[0,1,2] 
>>> b=tile(a,9)
>>> b
array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1,
       2, 0, 1, 2])
>>> 
>>> b=tile(a,(9,2))
>>> b
array([[0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2]])
>>> b=tile(a,(9,1))
>>> b
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])
>>> b=tile(a,(9,12))
>>> b
array([[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]])

 

矩陣減法import

>>> a
array([[101,  20],
       [101,  20],
       [101,  20],
       [101,  20]])
>>> b
array([[  1, 101],
       [  5,  89],
       [108,   5],
       [115,   8]])
>>> a-b
array([[100, -81],
       [ 96, -69],
       [ -7,  15],
       [-14,  12]])

 

#二維特徵相減後平方
sqDiffMat = diffMat**2

>>> c
array([[100, -81],
       [ 96, -69],
       [ -7,  15],
       [-14,  12]])
>>> c**2
array([[10000,  6561],
       [ 9216,  4761],
       [   49,   225],
       [  196,   144]])

#sum()全部元素相加,sum(0)列相加,sum(1)行相加
sqDistances = sqDiffMat.sum(axis=1)

>>> d=c**2
>>> d.sum(axis=1)
array([16561, 13977,   274,   340])
 
>>> d.sum(axis=0)
array([19461, 11691])

#開方,計算出距離
distances = sqDistances**0.5

>>> f=e**0.5
>>> f
array([ 128.68954892,  118.22436297,   16.55294536,   18.43908891])

sortedDistIndices = distances.argsort()
#定一個記錄類別次數的字典 ,按大小排序,打印index

 
>>> f
array([ 128.68954892,  118.22436297,   16.55294536,   18.43908891])
>>> g=f.argsort()
>>> g
array([2, 3, 1, 0])

取出前k個元素的類別

>>> for i  in range(3):
...     print(i) 
...     labels[sortedDistIndices[i]]
... 
0
'動做片'
1
'動做片'
2
'愛情片'

 

>>> for i  in range(3):
...     voteIlabel = labels[sortedDistIndices[i]]
...     classCount.get(voteIlabel,0) 
... 
0
0
0
>>> for i  in range(3):
...     voteIlabel = labels[sortedDistIndices[i]]
...     classCount.get(voteIlabel,0) + 1
... 
1
1
1
 #key=operator.itemgetter(1)根據字典的值進行排序
    #key=operator.itemgetter(0)根據字典的鍵進行排序
    #reverse降序排序字典
    sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)

    print('#######',sortedClassCount)
classCount~~~~~~ {}
classCount~~~~~~ {'動做片': 2, '愛情片': 1}
####### [('動做片', 2), ('愛情片', 1)]
動做片

 

第一段代碼總算跑通了,雖然到如今爲止還不理解什麼事人工智能,哈哈,

相關文章
相關標籤/搜索