Python使用sklearn库实现的各种分类算法简单应用小结
本文实例讲述了Python使用sklearn库实现的各种分类算法简单应用。分享给大家供大家参考,具体如下:
KNN
fromsklearn.neighborsimportKNeighborsClassifier importnumpyasnp defKNN(X,y,XX):#X,y分别为训练数据集的数据和标签,XX为测试数据 model=KNeighborsClassifier(n_neighbors=10)#默认为5 model.fit(X,y) predicted=model.predict(XX) returnpredicted
SVM
fromsklearn.svmimportSVC defSVM(X,y,XX): model=SVC(c=5.0) model.fit(X,y) predicted=model.predict(XX) returnpredicted
SVMClassifierusingcrossvalidation
defsvm_cross_validation(train_x,train_y): fromsklearn.grid_searchimportGridSearchCV fromsklearn.svmimportSVC 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() forpara,valinlist(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) returnmodel
LR
fromsklearn.linear_modelimportLogisticRegression defLR(X,y,XX): model=LogisticRegression() model.fit(X,y) predicted=model.predict(XX) returnpredicted
决策树(CART)
fromsklearn.treeimportDecisionTreeClassifier defCTRA(X,y,XX): model=DecisionTreeClassifier() model.fit(X,y) predicted=model.predict(XX) returnpredicted
随机森林
fromsklearn.ensembleimportRandomForestClassifier defCTRA(X,y,XX): model=RandomForestClassifier() model.fit(X,y) predicted=model.predict(XX) returnpredicted
GBDT(GradientBoostingDecisionTree)
fromsklearn.ensembleimportGradientBoostingClassifier defCTRA(X,y,XX): model=GradientBoostingClassifier() model.fit(X,y) predicted=model.predict(XX) returnpredicted
朴素贝叶斯:一个是基于高斯分布求概率,一个是基于多项式分布求概率,一个是基于伯努利分布求概率。
fromsklearn.naive_bayesimportGaussianNB fromsklearn.naive_bayesimportMultinomialNB fromsklearn.naive_bayesimportBernoulliNB defGNB(X,y,XX): model=GaussianNB() model.fit(X,y) predicted=model.predict(XX) returnpredicted defMNB(X,y,XX): model=MultinomialNB() model.fit(X,y) predicted=model.predict(XX returnpredicted defBNB(X,y,XX): model=BernoulliNB() model.fit(X,y) predicted=model.predict(XX returnpredicted
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。