zoukankan      html  css  js  c++  java
  • CatBoost算法和GPU测试(python代码实现)

    python风控建模实战lendingClub(博主录制,catboost,lightgbm建模,2K超清分辨率)

    https://study.163.com/course/courseMain.htm?courseId=1005988013&share=2&shareId=400000000398149

    catboost官网文档

    https://catboost.ai/

    catboost GPU官方文档

    https://catboost.ai/docs/features/training-on-gpu.html

    catboost特点:

    少量或无需调参,默认参数效果非常好

    支持分类变量

    支持GPU

    catboost代码举例

    # -*- coding: utf-8 -*-
    """
    Created on Sun Jul  1 12:24:21 2018
    
    @author: Administrator
    """
    from sklearn import metrics
    from sklearn.model_selection import train_test_split
    from sklearn.grid_search import GridSearchCV
    import catboost as cb
    from sklearn.datasets import load_breast_cancer
    
    cancer=load_breast_cancer()
    X, y = cancer.data,cancer.target
    train_x, test_x, y_train, y_test=train_test_split(X,y,test_size=0.3,random_state=0)
    
    cat_features_index = [0,1,2,3,4,5,6]
    
    def auc(m, train, test): 
        return (metrics.roc_auc_score(y_train,m.predict_proba(train)[:,1]),
                                metrics.roc_auc_score(y_test,m.predict_proba(test)[:,1]))
    
    params = {'depth': [4, 7, 10],
              'learning_rate' : [0.03, 0.1, 0.15],
             'l2_leaf_reg': [1,4,9],
             'iterations': [300]}
    cb = cb.CatBoostClassifier()
    #cb_model = GridSearchCV(cb, params, scoring="roc_auc", cv = 5)
    cb.fit(train_x, y_train)
    
    
    print("accuracy on the training subset:{:.3f}".format(cb.score(train_x,y_train)))
    print("accuracy on the test subset:{:.3f}".format(cb.score(test_x,y_test)))
    '''
    accuracy on the training subset:1.000
    accuracy on the test subset:0.982
    '''
    

      

    摘要: 俄罗斯搜索巨头Yandex宣布,将向开源社区提交一款梯度提升机器学习库CatBoost。它能够在数据稀疏的情况下“教”机器学习。特别是在没有像视频、文本、图像这类感官型数据的时候,CatBoost也能根据事务型数据或历史数据进行操作。


      现在,人工智能正在为越来越多的计算功能提供支持,今天,俄罗斯搜索巨头Yandex宣布,将向开源社区提交一款梯度提升机器学习库CatBoost。它能够在数据稀疏的情况下“教”机器学习。特别是在没有像视频、文本、图像这类感官型数据的时候,CatBoost也能根据事务型数据或历史数据进行操作。
     
      今天,CatBoost以两种方式进行了亮相。
     
      首先,Yandex宣布,将在自有服务中使用这款新的框架替换原来的机器学习算法MatrixNet。MatrixNet一直被应用在公司的很多业务上,比如排名、天气预报、出租车和推荐业务。现在,业务正在逐步从MatrixNet切换到CatBoost上来,并将延续几个月。
     
      其次,Yandex将免费提供CatBoost库,任何希望在自己的程序中使用梯度提升技术的人员都可以在Apache许可证下使用这个库。 Yandex机器智能研究主管Misha Bilenko在接受采访时表示:“CatBoost是Yandex多年研究的巅峰之作。我们自己一直在使用大量的开源机器学习工具,所以是时候向社会作出回馈了。” 他提到,Google在2015年开源的Tensorflow以及Linux的建立与发展是本次开源CatBoost的原动力。
     
      Bilenko补充说到,暂时还没有计划将CatBoost商业化,或以任何专利的形式将其闭源。 “这和竞争对手无关,”他说,“我们很高兴有竞争对手使用它”
     
      长期以来,随着Yandex的不断发展,它一直在寻求提升俄语世界之外的国际地位。本次开源举动不仅仅是Yandex对开源社区的承诺,而且也展示了Yandex希望成为大型科技公司与开发者社区发展中心的决心。
     
      就像Google持续地扩展和更新Tensorflow一样,今天的CatBoost版本是其第一个版本,以后将持续更新迭代。目前,这个库主要有三个特点:
     
      “减少过度拟合”:这可以帮助你在训练计划中取得更好的成果。它基于一种构建模型的专有算法,这种算法与标准的梯度提升方案不同。
     
      “类别特征支持”:这将改善你的训练结果,同时允许你使用非数字因素,“而不必预先处理数据,或花费时间和精力将其转化为数字。”
     
      “API??接口支持”:可以通过命令行或者基于Python或R的API接口来使用CatBoost,包括公式分析和训练可视化工具。
     
      虽然目前有大量的库可以利用梯度提升或其他解决方案来训练机器学习系统,但Bilenko认为,CatBoost相较其他大型公司使用的框架(如Yandex)的最大优点是测试精准度高。
     
      “有很多机器学习库的代码质量比较差,需要做大量的调优工作,”他说,“而CatBoost只需少量调试,就可以实现良好的性能。这是一个关键性的区别。”
     
     
     

    CatBoost参数解释和实战
     
    https://blog.csdn.net/linxid/article/details/80723811

    据开发者所说超越Lightgbm和XGBoost的又一个神器,不过具体性能,还要看在比赛中的表现了。

    整理一下里面简单的教程和参数介绍,很多参数不是那种重要,只解释部分重要的参数,训练时需要重点考虑的。

    import numpy as np
    import catboost as cb
    
    train_data = np.random.randint(0, 100, size=(100, 10))
    train_label = np.random.randint(0, 2, size=(100))
    test_data = np.random.randint(0,100, size=(50,10))
    
    model = cb.CatBoostClassifier(iterations=2, depth=2, learning_rate=0.5, loss_function='Logloss',
                                  logging_level='Verbose')
    model.fit(train_data, train_label, cat_features=[0,2,5])
    preds_class = model.predict(test_data)
    preds_probs = model.predict_proba(test_data)
    print('class = ',preds_class)
    print('proba = ',preds_probs)
    

      

    参数
    CatBoostClassifier/CatBoostRegressor
    通用参数
    learning_rate(eta)=automatically

    depth(max_depth)=6: 树的深度

    l2_leaf_reg(reg_lambda)=3 L2正则化系数

    n_estimators(num_boost_round)(num_trees=1000)=1000: 解决ml问题的树的最大数量
    one_hot_max_size=2: 对于某些变量进行one-hot编码
    loss_function=’Logloss’:

    RMSE
    Logloss
    MAE
    CrossEntropy

    custom_metric=None

    RMSE
    Logloss
    MAE
    CrossEntropy
    Recall
    Precision
    F1
    Accuracy
    AUC
    R2
    

    eval_metric=Optimized objective

    RMSE
    Logloss
    MAE
    CrossEntropy
    Recall
    Precision
    F1
    Accuracy
    AUC
    R2
    

    nan_mode=None:处理NAN的方法
    Forbidden
    Min
    Max
    1
    2
    3
    leaf_estimation_method=None:迭代求解的方法,梯度和牛顿
    Newton
    Gradient
    1
    2
    random_seed=None: 训练时候的随机种子
    ---------------------

    性能参数
    thread_count=-1:训练时所用的cpu/gpu核数
    used_ram_limit=None:CTR问题,计算时的内存限制
    gpu_ram_part=None:GPU内存限制
    处理单元设置
    task_type=CPU:训练的器件
    devices=None:训练的GPU设备ID

    counter_calc_method=None,

    leaf_estimation_iterations=None,
    use_best_model=None,
    verbose=None,
    model_size_reg=None,
    rsm=None,
    logging_level=None,
    metric_period=None,
    ctr_leaf_count_limit=None,
    store_all_simple_ctr=None,
    max_ctr_complexity=None,
    has_time=None,
    classes_count=None,
    class_weights=None,

    random_strength=None,

    name=None,
    ignored_features=None,
    train_dir=None,
    custom_loss=None,

    bagging_temperature=None

    border_count=None

    feature_border_type=None,

    save_snapshot=None,
    snapshot_file=None,
    fold_len_multiplier=None,

    allow_writing_files=None,

    final_ctr_computation_mode=None,
    approx_on_full_history=None,
    boosting_type=None,
    simple_ctr=None,
    combinations_ctr=None,
    per_feature_ctr=None,

    device_config=None,

    bootstrap_type=None,

    subsample=None,

    colsample_bylevel=None,

    random_state=None,
    objective=None,

    max_bin=None,

    scale_pos_weight=None,
    gpu_cat_features_storage=None,
    data_partition=None

     

    CatBoostClassifier
    属性(attribute):
    is_fitted_
    tree_count_
    feature_importances_
    random_seed_
    方法(method):
    fit
    X: 输入数据数据类型可以是,list; pandas.DataFrame; pandas.Series

    y=None

    cat_features=None: 拿来做处理的类别特征
    sample_weight=None: 输入数据的样本权重
    logging_level=None: 控制是否输出日志信息,或者何种信息
    plot=False: 训练过程中,绘制,度量值,所用时间等
    eval_set=None: 验证集合,数据类型list(X, y)tuples
    baseline=None
    use_best_model=None
    verbose=None
    predict
    返回验证样本所属类别,数据类型为np.array

    predict_proba
    返回验证样本所属类别的概率,数据类型为np.array

    get_feature_importance
    eval_metrics
    save_model
    load_model
    get_params
    score
    教程(tutorial)


    image.png

     catboost GPU版本

    https://catboost.ai/docs/features/training-on-gpu.html

    Training on GPU

    CatBoost supports training on GPUs.

    Choose the implementation for more details on the parameters that are required to start training on GPU.

     

    Note. Other training parameters are also available. Some of them are CPU-specific or GPU-specific. See the Python package training parameters section for more details.

    For example, use the following code to train a classification model on GPU:

    GPU版本测试

    # -*- coding: utf-8 -*-
    """
    Created on Wed May 29 10:34:20 2019
    
    @author: Administrator
    GPU
    1m -1m 2s 
    CPU
    28.7s-28.8s
    """
    
    from catboost import CatBoostClassifier
    
    train_data = [[0, 3],
                  [4, 1],
                  [8, 1],
                  [9, 1]]
    train_labels = [0, 0, 1, 1]
    
    model = CatBoostClassifier(task_type = "GPU")
    #model = CatBoostClassifier()
    model.fit(train_data,
              train_labels)
    

      

    # -*- coding: utf-8 -*-
    """
    Created on Wed May 22 10:50:59 2019
    
    @author: Administrator
    CPU版本:3m 30s-3m 40s 
    GPU版本:3m 33s-3m 34s
    """
    
    from sklearn import metrics
    from sklearn.model_selection import train_test_split
    import catboost as cb
    from sklearn.datasets import load_breast_cancer
     
    cancer=load_breast_cancer()
    X, y = cancer.data,cancer.target
    train_x, test_x, y_train, y_test=train_test_split(X,y,test_size=0.3,random_state=0)
    cb = cb.CatBoostClassifier() 
    #cb = cb.CatBoostClassifier(task_type="CPU") 
    #cb = cb.CatBoostClassifier(task_type="GPU")
    cb.fit(train_x, y_train)
    

      

    Python package installation

    Restriction.

    Installation is only supported by the 64-bit version of Python.

    Dependencies:
    To install the Python package:
    1.  
    2.  

      (Optionally) Install additional packages for data visualization support.

    3.  
      (Optionally) Test CatBoost.

    Note that there are additional system requirements if training on GPU is required.

    GPU system requirements

    The versions of CatBoost available from pip install and conda install have GPU support out-of-the-box.

    Devices with compute capability 3.0 and higher are supported in compiled packages.

    Training on GPU requires NVIDIA Driver of version 390.xx or higher.

    The Python version of CatBoost for CUDA of compute capability 2.0 can be built from source. In this case the following steps are obligatory:

      

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频,含catboost算法讲解)

    https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campaign=commission&utm_source=cp-400000000398149&utm_medium=share


    欢迎扫描和关注博主主页,学习python视频资源

  • 相关阅读:
    Git的安装
    报错Invalid character found in method name. HTTP method names must be tokens|the HTTP protoco
    Spring Cloud(二)—— Eureka注册与发现
    spring-boot swagger2 设置全局token,说明页面接口无法带入token
    c# 结构体中包含结构体数组的使用
    百度地图api热力图时报错Cannot read property 'y' of undefined
    springboot使用freemaker导出word文档
    c# 同时运行两个相同的程序
    idea maven的pom文件已导入依赖,但是无法引入该包中class
    bootstrap Table 导出时时间格式显示秒 科学计数法显示
  • 原文地址:https://www.cnblogs.com/webRobot/p/9249906.html
Copyright © 2011-2022 走看看