鸢尾花案例的多维度分析:
1、bayes分类
import numpy as np
from sklearn import datasets ##导入包中的数据
iris=datasets.load_iris() ##加载数据
print(iris.feature_names) ##显示特征名字
# ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
print(iris.data) ##显示数据
# array([[ 5.1, 3.5, 1.4, 0.2],[ 4.9, 3. , 1.4, 0.2],[ 4.7, 3.2, 1.3, 0.2]............
print(iris.data.size) ##数据大小 ---600个
print(iris.target_names) ##显示分类的名字
# array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
from sklearn.naive_bayes import GaussianNB ##导入高斯朴素贝叶斯算法
clf = GaussianNB() ##给算法赋一个变量,主要是为了方便使用
clf.fit(iris.data, iris.target) ##开始分类。对于量特别大的样本,可以使用函数partial_fit分类,避免一次加载过多数据到内存
clf.predict(iris.data[0].reshape(1,-1)) ##验证分类。标红部分特别说明:因为predict的参数是数组,data[0]是列表,所以需要转换一下
#array([0])
data=np.array([6,4,6,2]) ##验证分类
print(clf.predict(data.reshape(1,-1)))
2、import numpy as np
from sklearn.datasets import load_iris
#加载数据集
iris=load_iris()
iris.keys()
#dict_keys(['target', 'DESCR', 'data', 'target_names', 'feature_names'])
#数据的条数和维数
n_samples,n_features=iris.data.shape
print("Number of sample:",n_samples) #Number of sample: 150
print("Number of feature",n_features)
#Number of feature 4
#第一个样例
print(iris.data[0])
#[ 5.1 3.5 1.4 0.2]
print(iris.data.shape)
#(150, 4)
print(iris.target.shape)
#(150,)
print(iris.target)
"""
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2]
"""
import numpy as np
print(iris.target_names)
#['setosa' 'versicolor' 'virginica']
np.bincount(iris.target)
#[50 50 50]
import matplotlib.pyplot as plt
#以第3个索引为划分依据,x_index的值可以为0,1,2,3
x_index=3
color=['blue','red','green']
for label,color in zip(range(len(iris.target_names)),color):
plt.hist(iris.data[iris.target==label,x_index],label=iris.target_names[label],color=color)
plt.xlabel(iris.feature_names[x_index])
plt.legend(loc="Upper right")
plt.show()
#画散点图,第一维的数据作为x轴和第二维的数据作为y轴
x_index=0
y_index=1
colors=['blue','red','green']
for label,color in zip(range(len(iris.target_names)),colors):
plt.scatter(iris.data[iris.target==label,x_index],
iris.data[iris.target==label,y_index],
label=iris.target_names[label],
c=color)
plt.xlabel(iris.feature_names[x_index])
plt.ylabel(iris.feature_names[y_index])
plt.legend(loc='upper left')
plt.show()
3、KNN
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
iris = datasets.load_iris()
iris_X = iris.data
iris_y = iris.target
X_train, X_test, y_train, y_test = train_test_split(
iris_X, iris_y, test_size=0.15
)
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
print(knn.predict(X_test)) #训练后预测结果
print(y_test) #真实的结果
plt.plot(X_test,y_test)
plt.show()
4、决策树
# -*- coding: UTF-8 -*-
from math import log
"""
函数说明:创建测试数据集
Returns:
data - 数据集
labels - 分类属性
Modify:
2017-11-08
"""
def creatData():
data = [[0, 0, 0, 0, 'no'], #数据集
[0, 0, 0, 1, 'no'],
[0, 1, 0, 1, 'yes'],
[0, 1, 1, 0, 'yes'],
[0, 0, 0, 0, 'no'],
[1, 0, 0, 0, 'no'],
[1, 0, 0, 1, 'no'],
[1, 1, 1, 1, 'yes'],
[1, 0, 1, 2, 'yes'],
[1, 0, 1, 2, 'yes'],
[2, 0, 1, 2, 'yes'],
[2, 0, 1, 1, 'yes'],
[2, 1, 0, 1, 'yes'],
[2, 1, 0, 2, 'yes'],
[2, 0, 0, 0, 'no']]
labels = ['年龄', '有工作', '有自己的房子', '信贷情况'] #分类属性
return data,labels
"""
函数说明:计算给定数据集的经验熵(香农熵)
data - 数据集
Returns:
shannonEnt - 经验熵(香农熵)
"""
def calcShannonEnt(data):
num = len(data) #返回数据集的行数
labelscount = {} #保存每个标签出现的次数
for feature in data: #对每组特征向量进行统计
currentLabel = feature[-1] #提取标签(Label)信息
if currentLabel not in labelscount.keys(): #如果标签(Label)没有放入统计次数的字典,添加进去
labelscount[currentLabel] = 0
labelscount[currentLabel] += 1 #Label计数
shannonEnt = 0.0 #经验熵(香农熵)
for key in labelscount: #计算香农熵
prob = float(labelscount[key]) / num #选择该标签(Label)的概率
shannonEnt -= prob * log(prob, 2) #利用公式计算
return shannonEnt #返回经验熵(香农熵)
if __name__ == '__main__':
data, features = creatData()
print(data)
print(calcShannonEnt(data))
5、扩展运用
(1)、如果import graphviz不能正常使用,参考
https://blog.csdn.net/qq_41682302/article/details/105348027?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2
(2)、import numpy as np
from sklearn import tree
X = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1],
[1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]])
y = [0, 1, 1, 1, 2, 3, 3, 4]
clf = tree.DecisionTreeClassifier() # 创建决策树分类器
clf.fit(X, y) # 拟合
'''DecisionTreeClassifier(class_weight=None, criterion='gini',
max_depth=None,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort=False,
random_state=None, splitter='best')'''
clf.predict([[1, 0, 0]]) # 分类
#array([2])
import graphviz
dot_data = tree.export_graphviz(clf, out_file=None) # 导出决策树
graph = graphviz.Source(dot_data) # 创建图形
graph.render('result') # 输出PDF文件
(3)、购买电脑案例
from math import log
import operator
import pandas as pd
import numpy as np
def calcShannonEnt(dataSet): # 计算数据的熵(entropy)
numEntries=len(dataSet) # 数据条数
labelCounts={}
for featVec in dataSet:
currentLabel=featVec[-1] # 每行数据的最后一个字(类别)
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel]=0
labelCounts[currentLabel]+=1 # 统计有多少个类以及每个类的数量
shannonEnt=0
for key in labelCounts:
prob=float(labelCounts[key])/numEntries # 计算单个类的熵值
shannonEnt-=prob*log(prob,2) # 累加每个类的熵值
return shannonEnt
def createDataSet1(): # 创造数据集
dataSet = [['<=30', 'high', 'no', 'fair', 'no'],
['<=30', 'high', 'no', 'excellent', 'no'],
['31…40', 'high', 'no', 'fair', 'yes'],
['>40', 'medium', 'no', 'fair', 'yes'],
['>40', 'low', 'yes', 'fair', 'yes'],
['>40', 'low', 'yes', 'excellent', 'no'],
['31…40', 'low', 'yes', 'excellent', 'yes'],
['<=30', 'medium', 'no', 'fair', 'no'],
['<=30', 'low', 'yes', 'fair', 'yes'],
['>40', 'medium', 'yes', 'fair', 'yes'],
['<=30', 'medium', 'yes', 'excellent', 'yes'],
['31…40', 'medium', 'no', 'excellent ', 'yes'],
['31…40', 'high', 'yes', 'fair', 'yes'],
['>40', 'medium', 'no', 'excellent', 'no']
]
labels = ['age', 'income', 'student', 'credit_rating']
return dataSet,labels
def splitDataSet(dataSet,axis,value): # 按某个特征分类后的数据
retDataSet=[]
for featVec in dataSet:
if featVec[axis]==value:
reducedFeatVec =featVec[:axis]
reducedFeatVec.extend(featVec[axis+1:])
retDataSet.append(reducedFeatVec)
return retDataSet
def chooseBestFeatureToSplit(dataSet): # 选择最优的分类特征
numFeatures = len(dataSet[0])-1
baseEntropy = calcShannonEnt(dataSet) # 原始的熵
bestInfoGain = 0
bestFeature = -1
for i in range(numFeatures):
featList = [example[i] for example in dataSet]
uniqueVals = set(featList)
newEntropy = 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): #按分类后类别数量排序,比如:最后分类为2好瓜1坏瓜,则判定为好瓜;
classCount={}
for vote in classList:
if vote not in classCount.keys():
classCount[vote]=0
classCount[vote]+=1
sortedClassCount = sorted(classCount.items(),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]
if len(dataSet[0])==1:
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[:]
myTree[bestFeatLabel][value]=createTree(splitDataSet\
(dataSet,bestFeat,value),subLabels)
return myTree
if __name__=='__main__':
dataSet, labels=createDataSet1() # 创造示列数据
print(createTree(dataSet, labels)) # 输出决策树模型结果
#输出结果:{'age': {'>40': {'credit_rating': {'fair': 'yes', 'excellent': 'no'}}, '31…40': 'yes', '<=30': {'student': {'no': 'no', 'yes': 'yes'}}}}
4、
from math import log
import operator
import pandas as pd
import numpy as np
def calcShannonEnt(dataSet): # calculate entropy
numEntries=len(dataSet) # the data length
labelCounts={}
for featVec in dataSet:
currentLabel=featVec[-1] # the catalogy of each row of the data(the last one)
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel]=0
labelCounts[currentLabel]+=1 # count the catalogies and the num of each catalogy
shannonEnt=0
for key in labelCounts:
prob=float(labelCounts[key])/numEntries # caculate the entropy of each catalogy
shannonEnt-=prob*log(prob,2) # sum the entropy of each catalogy
return shannonEnt
def createDataSet1(): # initialize the input datasets
dataSet = [['晴', '热', '高', '否', '否'],
['晴', '热', '高', '是', '否'],
['阴', '热', '高', '否', '是'],
['雨', '温', '高', '否', '是'],
['雨', '凉爽', '中', '否', '是'],
['雨', '凉爽', '中', '是', '否'],
['阴', '凉爽', '中', '是', '是'],
['晴', '温', '高', '否', '否'],
['晴', '凉爽', '中', '否', '是'],
['雨', '温', '中', '否', '是'],
['晴', '温', '中', '是', '是'],
['阴', '温', '高', '是', '是'],
['阴', '热', '中', '否', '是'],
['雨', '温', '高', '是', '否']
]
labels = ['weather', 'temp', 'humidity', 'wind']
return dataSet,labels
def splitDataSet(dataSet,axis,value): # the data of classifies by a feature
retDataSet=[]
for featVec in dataSet:
if featVec[axis]==value:
reducedFeatVec =featVec[:axis]
reducedFeatVec.extend(featVec[axis+1:])
retDataSet.append(reducedFeatVec)
return retDataSet
def chooseBestFeatureToSplit(dataSet): # choose the best classifying feature
numFeatures = len(dataSet[0])-1
baseEntropy = calcShannonEnt(dataSet) # base entropy
bestInfoGain = 0
bestFeature = -1
for i in range(numFeatures):
featList = [example[i] for example in dataSet]
uniqueVals = set(featList)
newEntropy = 0
for value in uniqueVals:
subDataSet = splitDataSet(dataSet,i,value)
prob =len(subDataSet)/float(len(dataSet))
newEntropy +=prob*calcShannonEnt(subDataSet) # the entropy of classified
infoGain = baseEntropy - newEntropy # the difference entropy
if (infoGain>bestInfoGain): # the second best feature choose
bestInfoGain=infoGain
bestFeature = i
return bestFeature
def majorityCnt(classList): #sort the classified data
classCount={}
for vote in classList:
if vote not in classCount.keys():
classCount[vote]=0
classCount[vote]+=1
sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
return sortedClassCount[0][0]
def classify(inputTree, featLabels, testVec):
firstStr = list(inputTree.keys())[0]
secondDict = inputTree[firstStr]
featIndex = featLabels.index(firstStr)
for key in secondDict.keys():
if testVec[featIndex] == key:
if type(secondDict[key]).__name__ == 'dict':
classLabel = classify(secondDict[key], featLabels, testVec)
else:
classLabel = secondDict[key]
return classLabel
def createTree(dataSet,labels):
classList=[example[-1] for example in dataSet] # classify
if classList.count(classList[0])==len(classList):
return classList[0]
if len(dataSet[0])==1:
return majorityCnt(classList)
bestFeat=chooseBestFeatureToSplit(dataSet)
bestFeatLabel=labels[bestFeat]
myTree={bestFeatLabel:{}} #store the result in dict
del(labels[bestFeat])
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
if __name__=='__main__':
dataSet, labels=createDataSet1() #create the dataset
labels_tmp=labels[:]
mytree=createTree(dataSet, labels)
# print(labels_tmp)
print(classify(mytree,labels_tmp,['雨','热','中','否']))

