python sklearn常用分类算法模型的调用


Posted in Python onOctober 16, 2019

本文实例为大家分享了python sklearn分类算法模型调用的具体代码,供大家参考,具体内容如下

实现对'NB', 'KNN', 'LR', 'RF', 'DT', 'SVM','SVMCV', 'GBDT'模型的简单调用。

# coding=gbk
 
import time 
from sklearn import metrics 
import pickle as pickle 
import pandas as pd
 
 
# Multinomial Naive Bayes Classifier 
def naive_bayes_classifier(train_x, train_y): 
  from sklearn.naive_bayes import MultinomialNB 
  model = MultinomialNB(alpha=0.01) 
  model.fit(train_x, train_y) 
  return model 
 
 
# KNN Classifier 
def knn_classifier(train_x, train_y): 
  from sklearn.neighbors import KNeighborsClassifier 
  model = KNeighborsClassifier() 
  model.fit(train_x, train_y) 
  return model 
 
 
# Logistic Regression Classifier 
def logistic_regression_classifier(train_x, train_y): 
  from sklearn.linear_model import LogisticRegression 
  model = LogisticRegression(penalty='l2') 
  model.fit(train_x, train_y) 
  return model 
 
 
# Random Forest Classifier 
def random_forest_classifier(train_x, train_y): 
  from sklearn.ensemble import RandomForestClassifier 
  model = RandomForestClassifier(n_estimators=8) 
  model.fit(train_x, train_y) 
  return model 
 
 
# Decision Tree Classifier 
def decision_tree_classifier(train_x, train_y): 
  from sklearn import tree 
  model = tree.DecisionTreeClassifier() 
  model.fit(train_x, train_y) 
  return model 
 
 
# GBDT(Gradient Boosting Decision Tree) Classifier 
def gradient_boosting_classifier(train_x, train_y): 
  from sklearn.ensemble import GradientBoostingClassifier 
  model = GradientBoostingClassifier(n_estimators=200) 
  model.fit(train_x, train_y) 
  return model 
 
 
# SVM Classifier 
def svm_classifier(train_x, train_y): 
  from sklearn.svm import SVC 
  model = SVC(kernel='rbf', probability=True) 
  model.fit(train_x, train_y) 
  return model 
 
# 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 
 
def read_data(data_file): 
  data = pd.read_csv(data_file)
  train = data[:int(len(data)*0.9)]
  test = data[int(len(data)*0.9):]
  train_y = train.label
  train_x = train.drop('label', axis=1)
  test_y = test.label
  test_x = test.drop('label', axis=1)
  return train_x, train_y, test_x, test_y
   
if __name__ == '__main__': 
  data_file = "H:\\Research\\data\\trainCG.csv" 
  thresh = 0.5 
  model_save_file = None 
  model_save = {} 
  
  test_classifiers = ['NB', 'KNN', 'LR', 'RF', 'DT', 'SVM','SVMCV', 'GBDT'] 
  classifiers = {'NB':naive_bayes_classifier,  
         'KNN':knn_classifier, 
          'LR':logistic_regression_classifier, 
          'RF':random_forest_classifier, 
          'DT':decision_tree_classifier, 
         'SVM':svm_classifier, 
        'SVMCV':svm_cross_validation, 
         'GBDT':gradient_boosting_classifier 
  } 
   
  print('reading training and testing data...') 
  train_x, train_y, test_x, test_y = read_data(data_file) 
   
  for classifier in test_classifiers: 
    print('******************* %s ********************' % classifier) 
    start_time = time.time() 
    model = classifiers[classifier](train_x, train_y) 
    print('training took %fs!' % (time.time() - start_time)) 
    predict = model.predict(test_x) 
    if model_save_file != None: 
      model_save[classifier] = model 
    precision = metrics.precision_score(test_y, predict) 
    recall = metrics.recall_score(test_y, predict) 
    print('precision: %.2f%%, recall: %.2f%%' % (100 * precision, 100 * recall)) 
    accuracy = metrics.accuracy_score(test_y, predict) 
    print('accuracy: %.2f%%' % (100 * accuracy))  
 
  if model_save_file != None: 
    pickle.dump(model_save, open(model_save_file, 'wb'))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python正则表达式如何进行字符串替换实例
Dec 28 Python
Django与JS交互的示例代码
Aug 23 Python
轻松理解Python 中的 descriptor
Sep 15 Python
利用python打开摄像头及颜色检测方法
Aug 03 Python
Python读写zip压缩文件的方法
Aug 29 Python
python 使用pandas计算累积求和的方法
Feb 08 Python
django解决订单并发问题【推荐】
Jul 31 Python
python实现复制文件到指定目录
Oct 16 Python
python输出pdf文档的实例
Feb 13 Python
python模拟实现分发扑克牌
Apr 22 Python
opencv 图像轮廓的实现示例
Jul 08 Python
python 实现的IP 存活扫描脚本
Dec 10 Python
Python使用selenium + headless chrome获取网页内容的方法示例
Oct 16 #Python
使用python实现kNN分类算法
Oct 16 #Python
Python生成验证码、计算具体日期是一年中的第几天实例代码详解
Oct 16 #Python
python可视化实现KNN算法
Oct 16 #Python
python实现KNN分类算法
Oct 16 #Python
python子线程退出及线程退出控制的代码
Oct 16 #Python
python Pillow图像处理方法汇总
Oct 16 #Python
You might like
php发送邮件的问题详解
2015/06/22 PHP
yii2中LinkPager增加总页数和总记录数的实例
2017/08/28 PHP
JavaScript Base64编码和解码,实现URL参数传递。
2006/09/18 Javascript
JQuery里选择超链接的实现代码
2011/05/22 Javascript
JavaScript 学习笔记之一jQuery写法图片等比缩放以及预加载
2012/06/28 Javascript
JavaScript常用判断写法大全(推荐)
2016/05/30 Javascript
基于代数方程库Algebra.js解二元一次方程功能示例
2017/06/09 Javascript
Vue项目使用CDN优化首屏加载问题
2018/04/01 Javascript
js 计算图片内点个数的示例代码
2019/04/04 Javascript
Vue编程式跳转的实例代码详解
2019/07/10 Javascript
微信小程序class封装http代码实例
2019/08/24 Javascript
Vue中el-form标签中的自定义el-select下拉框标签功能
2020/04/20 Javascript
javascript设计模式 ? 访问者模式原理与用法实例分析
2020/04/26 Javascript
简单了解JavaScript弹窗实现代码
2020/05/07 Javascript
通过实例解析vuejs如何实现调试代码
2020/07/16 Javascript
[22:20]初生之犊-TI4第5名LGD战队纪录片
2014/08/13 DOTA
Swift 3.0在集合类数据结构上的一些新变化总结
2016/07/11 Python
python机器学习实战之最近邻kNN分类器
2017/12/20 Python
Python给定一个句子倒序输出单词以及字母的方法
2018/12/20 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
2019/08/28 Python
python如何从文件读取数据及解析
2019/09/19 Python
python解析命令行参数的三种方法详解
2019/11/29 Python
Pytorch实现的手写数字mnist识别功能完整示例
2019/12/13 Python
jupyter notebook 的工作空间设置操作
2020/04/20 Python
Python configparser模块封装及构造配置文件
2020/08/07 Python
Django-silk性能测试工具安装及使用解析
2020/11/28 Python
解决pycharm 格式报错tabs和space不一致问题
2021/02/26 Python
中国梦的演讲稿
2014/01/08 职场文书
2014社区三八妇女节活动总结
2014/03/01 职场文书
合伙协议书范本
2014/04/21 职场文书
婚前协议书标准版
2014/10/19 职场文书
节水倡议书
2015/01/19 职场文书
2015年惩防体系建设工作总结
2015/05/22 职场文书
教师师德工作总结2015
2015/07/22 职场文书
聊聊Python中关于a=[[]]*3的反思
2021/06/02 Python
基于Redis的List实现特价商品列表功能
2021/08/30 Redis