2.2 數(shù)據(jù)挖掘
PLA算法
Python代碼
import numpy as np
from numpy import *
import random
def pla():
W=np.ones(4)#initial all weight with 1
count=0
dataset=[[1,0.10723,0.64385, 0.29556 ,1],
[1 ,0.2418, 0.83075, 0.42741, 1],
[1 ,0.23321 ,0.81004 ,0.98691, 1],
[1 ,0.36163, 0.14351 ,0.3153, -1],
[1, 0.46984, 0.32142, 0.00042772, -1],
[1, 0.25969, 0.87208 ,0.075063, -1],
]
while True:
count+=1
iscompleted=True
for i in range(0,len(dataset)):
X=dataset[i][:-1]
Y=np.dot(W,X)#matrix multiply
if sign(Y)==sign(dataset[i][-1]):
continue
else:
iscompleted=False
W=W+(dataset[i][-1])*np.array(X)
if iscompleted:
break
print("final W is :",W)
print("count is :",count)
return W
def main():
pla()
if __name__ == '__main__':
main()
程序運(yùn)行結(jié)果如下:
('final W is :', array([-1. , -1.20451 , 1.12317 , 1.50704028]))
('count is :', 9)
kNN算法
#coding:utf-8
from numpy import *
import operator
##給出訓(xùn)練數(shù)據(jù)以及對(duì)應(yīng)的類(lèi)別
def createDataSet():
group = array([[1.0,2.0],[1.2,0.1],[0.1,1.4],[0.3,3.5]])
labels = ['A','A','B','B']
return group,labels
###通過(guò)KNN進(jìn)行分類(lèi)
def classify(input,dataSet,label,k):
dataSize = dataSet.shape[0]
####計(jì)算歐式距離
diff = tile(input,(dataSize,1)) - dataSet
sqdiff = diff ** 2
squareDist = sum(sqdiff,axis = 1)###行向量分別相加,從而得到新的一個(gè)行向量
dist = squareDist ** 0.5
##對(duì)距離進(jìn)行排序
sortedDistIndex = argsort(dist)##argsort()根據(jù)元素的值從大到小對(duì)元素進(jìn)行排序挠蛉,返回下標(biāo)
classCount={}
for i in range(k):
voteLabel = label[sortedDistIndex[i]]
###對(duì)選取的K個(gè)樣本所屬的類(lèi)別個(gè)數(shù)進(jìn)行統(tǒng)計(jì)
classCount[voteLabel] = classCount.get(voteLabel,0) + 1
###選取出現(xiàn)的類(lèi)別次數(shù)最多的類(lèi)別
maxCount = 0
for key,value in classCount.items():
if value > maxCount:
maxCount = value
classes = key
return classes
dataSet,labels = createDataSet()
input = array([1.1,0.3])
K = 3
output = classify(input,dataSet,labels,K)
print("測(cè)試數(shù)據(jù)為:",input,"分類(lèi)結(jié)果為:",output)
決策樹(shù)
def createDataSet():
dataSet = [[1,1,'yes'],
[1,1, 'yes'],
[1,0,'no'],
[0,1,'no'],
[0,1,'no']]
labels = ['no surfacing','flippers']
return dataSet, labels
def calcShannonEnt(dataSet):
#calculate the shannon value
numEntries = len(dataSet)
labelCounts = {}
for featVec in dataSet: #create the dictionary for all of the data
currentLabel = featVec[-1]
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel] = 0
labelCounts[currentLabel] += 1
shannonEnt = 0.0
for key in labelCounts:
prob = float(labelCounts[key])/numEntries
shannonEnt -= prob*log(prob,2) #get the log value
return shannonEnt
def chooseBestFeatureToSplit(dataSet):
numFeatures = len(dataSet[0])-1
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0; bestFeature = -1
for i in range(numFeatures):
featList = [example[i] for example in dataSet]
uniqueVals = set(featList)
newEntropy = 0.0
for value in uniqueVals:
subDataSet = splitDataSet(dataSet, i , value)
prob = len(subDataSet)/float(len(dataSet))
newEntropy +=prob * calcShannonEnt(subDataSet)
infoGain = baseEntropy - newEntropy
if(infoGain > bestInfoGain):
bestInfoGain = infoGain
bestFeature = i
return bestFeature
def majorityCnt(classList):
classCount = {}
for vote in classList:
if vote not in classCount.keys(): classCount[vote] = 0
classCount[vote] += 1
sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0]
def createTree(dataSet, labels):
classList = [example[-1] for example in dataSet]
# the type is the same, so stop classify
if classList.count(classList[0]) == len(classList):
return classList[0]
# traversal all the features and choose the most frequent feature
if (len(dataSet[0]) == 1):
return majorityCnt(classList)
bestFeat = chooseBestFeatureToSplit(dataSet)
bestFeatLabel = labels[bestFeat]
myTree = {bestFeatLabel:{}}
del(labels[bestFeat])
#get the list which attain the whole properties
featValues = [example[bestFeat] for example in dataSet]
uniqueVals = set(featValues)
for value in uniqueVals:
subLabels = labels[:]
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels)
return myTree
myDat, labels = trees.createDataSet()
myTree = trees.createTree(myDat,labels)
print myTree