python贝叶斯分类
常见的分类算法有:
K近邻算法
决策树
朴素贝叶斯
SVM
Logistic Regression
2. python scikit-learn 有什么算法
1,前言
很久不发文章,主要是Copy别人的总感觉有些不爽,所以整理些干货,希望相互学习吧。不啰嗦,进入主题吧,本文主要时说的为朴素贝叶斯分类算法。与逻辑回归,决策树一样,是较为广泛使用的有监督分类算法,简单且易于理解(号称十大数据挖掘算法中最简单的算法)。但其在处理文本分类,邮件分类,拼写纠错,中文分词,统计机器翻译等自然语言处理范畴较为广泛使用,或许主要得益于基于概率理论,本文主要为小编从理论理解到实践的过程记录。
2,公式推断
一些贝叶斯定理预习知识:我们知道当事件A和事件B独立时,P(AB)=P(A)(B),但如果事件不独立,则P(AB)=P(A)P(B|A)。为两件事件同时发生时的一般公式,即无论事件A和B是否独立。当然也可以写成P(AB)=P(B)P(A|B),表示若要两件事同事发生,则需要事件B发生后,事件A也要发生。
由上可知,P(A)P(B|A)= P(B)P(A|B)
推出P(B|A)=
其中P(B)为先验概率,P(B|A)为B的后验概率,P(A|B)为A的后验概率(在这里也为似然值),P(A)为A的先验概率(在这也为归一化常量)。
由上推导可知,其实朴素贝叶斯法就是在贝叶斯定理基础上,加上特征条件独立假设,对特定输入的X(样本,包含N个特征),求出后验概率最大值时的类标签Y(如是否为垃圾邮件),理解起来比逻辑回归要简单多,有木有,这也是本算法优点之一,当然运行起来由于得益于特征独立假设,运行速度也更快。
8. Python代码
# -*-coding: utf-8 -*-
importtime
fromsklearn import metrics
fromsklearn.naive_bayes import GaussianNB
fromsklearn.naive_bayes import MultinomialNB
fromsklearn.naive_bayes import BernoulliNB
fromsklearn.neighbors import KNeighborsClassifier
fromsklearn.linear_model import LogisticRegression
fromsklearn.ensemble import RandomForestClassifier
fromsklearn import tree
fromsklearn.ensemble import GradientBoostingClassifier
fromsklearn.svm import SVC
importnumpy as np
importurllib
# urlwith dataset
url ="-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
#download the file
raw_data= urllib.request.urlopen(url)
#load the CSV file as a numpy matrix
dataset= np.loadtxt(raw_data, delimiter=",")
#separate the data from the target attributes
X =dataset[:,0:7]
#X=preprocessing.MinMaxScaler().fit_transform(x)
#print(X)
y =dataset[:,8]
print(" 调用scikit的朴素贝叶斯算法包GaussianNB ")
model= GaussianNB()
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
print(" 调用scikit的朴素贝叶斯算法包MultinomialNB ")
model= MultinomialNB(alpha=1)
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
print(" 调用scikit的朴素贝叶斯算法包BernoulliNB ")
model= BernoulliNB(alpha=1,binarize=0.0)
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
print(" 调用scikit的KNeighborsClassifier ")
model= KNeighborsClassifier()
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
print(" 调用scikit的LogisticRegression(penalty='l2')")
model= LogisticRegression(penalty='l2')
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
print(" 调用scikit的RandomForestClassifier(n_estimators=8) ")
model= RandomForestClassifier(n_estimators=8)
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
print(" 调用scikit的tree.DecisionTreeClassifier()")
model= tree.DecisionTreeClassifier()
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
print(" 调用scikit的GradientBoostingClassifier(n_estimators=200) ")
model= GradientBoostingClassifier(n_estimators=200)
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
print(" 调用scikit的SVC(kernel='rbf', probability=True) ")
model= SVC(kernel='rbf', probability=True)
start_time= time.time()
model.fit(X,y)
print('training took %fs!' % (time.time() - start_time))
print(model)
expected= y
predicted= model.predict(X)
print(metrics.classification_report(expected,predicted))
print(metrics.confusion_matrix(expected,predicted))
"""
# 预处理代码集锦
importpandas as pd
df=pd.DataFrame(dataset)
print(df.head(3))
print(df.describe())##描述性分析
print(df.corr())##各特征相关性分析
##计算每行每列数据的缺失值个数
defnum_missing(x):
return sum(x.isnull())
print("Missing values per column:")
print(df.apply(num_missing, axis=0)) #axis=0代表函数应用于每一列
print(" Missing values per row:")
print(df.apply(num_missing, axis=1).head()) #axis=1代表函数应用于每一行"""