美文网首页
绘图常用

绘图常用

作者: 一只当归 | 来源:发表于2020-08-17 21:15 被阅读0次

    matplotlib

    分类变量
    import matplotlib.pyplot as plt
    data = {'apple': 10, 'orange': 15, 'lemon': 5, 'lime': 20}
    names = list(data.keys())
    values = list(data.values())
    #直方图,散点图和折线图
    fig, axs = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    axs[0].bar(names, values)
    axs[1].scatter(names, values)
    axs[2].plot(names, values)
    fig.suptitle('Categorical Plotting')
    
    #双线
    cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
    dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
    activity = ["combing", "drinking", "feeding", "napping", "playing", "washing"]
    
    fig, ax = plt.subplots()
    ax.plot(activity, dog, label="dog")
    ax.plot(activity, cat, label="cat")
    ax.legend()
    
    plt.show()
    
    image.png
    image.png
    饼图
    import pandas as pd
    import matplotlib.pyplot as plt
    %matplotlib inline
    df1 = pd.Series(3 * np.random.rand(4),index = ('a','b','c','d'),name = 'pie')
    df1.plot.pie(figsize = (6,6))
    plt.show()
    
    image.png
    箱型图
    df3 = pd.DataFrame(np.random.rand(10,4),columns=('a','b','c','d'))
    df3.plot.box()
    plt.show()
    
    image.png
    lgb
    import lightgbm as lgb
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    print('数据...')
    x_train = np.random.random((1000,10))
    y_train = np.random.rand(1000)>0.5
    x_test = np.random.random((100,10))
    y_test = np.random.randn(100)>0.5
    
    # 导入到lightgbm矩阵
    lgb_train = lgb.Dataset(x_train, y_train)
    lgb_test = lgb.Dataset(x_test, y_test, reference=lgb_train)
    
    # 设置参数
    params = {
        'num_leaves': 5,
        'metric': ('auc', 'loss'),#可以设置多个评价指标
        'verbose': 0
    }
    # if (evals_result and gbm) not in locbals():
        # global evals_result,gbm 
    #如果是局部变量的话,推荐把他们变成全局变量,这样plot的代码位置不受限制
    evals_result = {}  #记录训练结果所用
    
    print('开始训练...')
    # train
    gbm = lgb.train(params,
                    lgb_train,
                    num_boost_round=100,
                    valid_sets=[lgb_train, lgb_test],
                    evals_result=evals_result,#非常重要的参数,一定要明确设置
                    verbose_eval=10)
    
    print('画出训练结果...')
    ax = lgb.plot_metric(evals_result, metric='auc')
    #metric的值与之前的params里面的值对应
    plt.show()
    
    image.png
    print('画特征重要性排序...')
    ax = lgb.plot_importance(gbm, max_num_features=10)#max_features表示最多展示出前10个重要性特征,可以自行设置
    plt.show()
    
    image.png

    相关文章

      网友评论

          本文标题:绘图常用

          本文链接:https://www.haomeiwen.com/subject/btccjktx.html