千锋扣丁学堂Python培训之使用sklearn库实现各种分类算法
今天千锋扣丁学堂Python培训老师给大家分享一篇关于介绍了Python使用sklearn库实现的各种分类算法,结合实例形式分析了Python使用sklearn库实现的KNN、SVM、LR、决策树、随机森林等算法实现技巧,下面我们一起来看一下吧。
KNN
from sklearn.neighbors import KNeighborsClassifier import numpy as np def KNN(X,y,XX):#X,y 分别为训练数据集的数据和标签,XX为测试数据 model = KNeighborsClassifier(n_neighbors=10)#默认为5 model.fit(X,y) predicted = model.predict(XX) return predicted
SVM
from sklearn.svm import SVC def SVM(X,y,XX): model = SVC(c=5.0) model.fit(X,y) predicted = model.predict(XX) return predicted
SVM Classifier using cross validation
def svm_cross_validation(train_x, train_y): from sklearn.grid_search import GridSearchCV from sklearn.svm import SVC model = SVC(kernel='rbf', probability=True) param_grid = {'C': [1e-3, 1e-2, 1e-1, 1, 10, 100, 1000], 'gamma': [0.001, 0.0001]} grid_search = GridSearchCV(model, param_grid, n_jobs = 1, verbose=1) grid_search.fit(train_x, train_y) best_parameters = grid_search.best_estimator_.get_params() for para, val in list(best_parameters.items()): print(para, val) model = SVC(kernel='rbf', C=best_parameters['C'], gamma=best_parameters['gamma'], probability=True) model.fit(train_x, train_y) return model
LR
from sklearn.linear_model import LogisticRegression def LR(X,y,XX): model = LogisticRegression() model.fit(X,y) predicted = model.predict(XX) return predicted
决策树(CART)
from sklearn.tree import DecisionTreeClassifier def CTRA(X,y,XX): model = DecisionTreeClassifier() model.fit(X,y) predicted = model.predict(XX) return predicted
随机森林
from sklearn.ensemble import RandomForestClassifier def CTRA(X,y,XX): model = RandomForestClassifier() model.fit(X,y) predicted = model.predict(XX) return predicted
GBDT (Gradient Boosting Decision Tree)
from sklearn.ensemble import GradientBoostingClassifier def CTRA(X,y,XX): model = GradientBoostingClassifier() model.fit(X,y) predicted = model.predict(XX) return predicted
朴素贝叶斯:一个是基于高斯分布求概率,一个是基于多项式分布求概率,一个是基于伯努利分布求概率。
from sklearn.naive_bayes import GaussianNB from sklearn.naive_bayes import MultinomialNB from sklearn.naive_bayes import BernoulliNB def GNB(X,y,XX): model =GaussianNB() model.fit(X,y) predicted = model.predict(XX) return predicted def MNB(X,y,XX): model = MultinomialNB() model.fit(X,y) predicted = model.predict(XX return predicted def BNB(X,y,XX): model = BernoulliNB() model.fit(X,y) predicted = model.predict(XX return predicted
以上就是关于千锋扣丁学堂Python培训之使用sklearn库实现各种分类算法的全部内容,希望本文所述对大家Python程序设计有所帮助,想要了解更多关于Python开发方面内容的小伙伴,请关注扣丁学堂Python培训官网、微信等平台,扣丁学堂IT职业在线学习教育有专业的Python讲师为您指导,此外扣丁学堂老师精心推出的Python视频教程定能让你快速掌握Python从入门到精通开发实战技能。扣丁学堂Python技术交流群:279521237。
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16