标签:style blog http color io os 使用 ar for
1、k-近邻算法
算法原理:
存在一个样本数据集(训练样本集),并且我们知道样本集中的每个数据与其所属分类的对应关系。输入未知类别的数据后将新数据的每个特征与样本集中数据对应的特征进行比较,然后算法提取样本集中特征最相似(最近邻)的k组数据。然后将k组数据中出现次数最多的分类,来作为新数据的分类。
算法步骤:
python代码实现:
def classify0(inX, dataSet, labels, k): dataSetSize = dataSet.shape[0] diffMat = tile(inX, (dataSetSize,1)) - dataSet #当前点的值矩阵(列表复制成矩阵)与训练样本集特征值矩阵差 sqDiffMat = diffMat**2 sqDistances = sqDiffMat.sum(axis=1) #将矩阵列的值相加 distances = sqDistances**0.5 sortedDistIndicies = distances.argsort() classCount={} for i in range(k): voteIlabel = labels[sortedDistIndicies[i]] classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) return sortedClassCount[0][0]
输入:
inX:当前点的特征值列表 dataSet、label:训练集的特征值矩阵及分类值 k:kNN算法k的取值
输出:
sortedClassCount[0][0]:k组数据中出现频率最高的分类
sortedClassCount 是一个字典,存储的是k组数据中各个分类对应的出现次数
2、决策树(ID3)
原理:
在当前数据集上选择哪个特征来划分数据分类。原始数据划分为几个数据子集,这些数据子集会分布在决策点的所有分支上。如果分支下的数据属于同一类型,则无需进一步对数据划分。否则,需要重复划分数据子集的过程。(递归)
结束条件:程序遍历完所有的划分数据集的数学,或者每一个分支下的所有实例都具有相同的分类(得到叶子节点或终止块)。
选择特征划分数据原则:将无序的数据变得更加有序。
组织杂乱无章数据的一种方法是使用信息论度量信息。(熵)
信息增益:划分数据集前后的信息变化。获得信息增益最高的特征是最好的选择。
熵:H = -∑pi*log2pi
注:还有一种度量信息无序程度的方法是基尼不纯度(Gini impurity)。
python代码计算数据集的熵:
def calcShannonEnt(dataSet): numEntries = len(dataSet) labelCounts = {} for featVec in dataSet: #the the number of unique elements and their occurance 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) #log base 2 return shannonEnt
获得最好的数据集划分方式:
def splitDataSet(dataSet, axis, value): #根据给定特征及其值来划分数据 retDataSet = [] for featVec in dataSet: if featVec[axis] == value: reducedFeatVec = featVec[:axis] #chop out axis used for splitting reducedFeatVec.extend(featVec[axis+1:]) retDataSet.append(reducedFeatVec) return retDataSet def chooseBestFeatureToSplit(dataSet): numFeatures = len(dataSet[0]) - 1 #the last column is used for the labels baseEntropy = calcShannonEnt(dataSet) bestInfoGain = 0.0; bestFeature = -1 for i in range(numFeatures): #iterate over all the features featList = [example[i] for example in dataSet]#create a list of all the examples of this feature uniqueVals = set(featList) #get a set of unique values 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 #calculate the info gain; ie reduction in entropy if (infoGain > bestInfoGain): #compare this to the best gain so far bestInfoGain = infoGain #if better than current best, set to best bestFeature = i return bestFeature #returns an integer
递归构建决策树
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] if classList.count(classList[0]) == len(classList): return classList[0]#stop splitting when all of the classes are equal if len(dataSet[0]) == 1: #stop splitting when there are no more features in dataSet return majorityCnt(classList)
bestFeat = chooseBestFeatureToSplit(dataSet) bestFeatLabel = labels[bestFeat] myTree = {bestFeatLabel:{}} del(labels[bestFeat]) featValues = [example[bestFeat] for example in dataSet] uniqueVals = set(featValues) for value in uniqueVals: subLabels = labels[:] #copy all of labels, so trees don‘t mess up existing labels myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value),subLabels) return myTree
2014-10-12
本文内容遵从CC3.0版权协议,转载请注明:转自学而优,思则通
本文链接地址:机器学习实战学习笔记(一)
标签:style blog http color io os 使用 ar for
原文地址:http://www.cnblogs.com/ddblog/p/4020922.html