第四届工业大数据创新大赛代码2(自写)

这段代码展示了如何使用LightGBM库进行多目标回归任务。首先导入了必要的库,然后加载了训练和测试数据。数据预处理过程中删除了'Id'列。定义了一个自定义损失函数my_loss,用于计算预测与实际值之间的差距。接着创建了一个LGBMRegressor模型,并设置了参数。模型分别针对三个目标变量进行了训练,并对测试集进行预测。最后,使用自定义损失函数计算了预测的准确性。
摘要由CSDN通过智能技术生成
import datetime
import numpy as np
import pandas as pd
import lightgbm as lgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import numpy as np
from lightgbm.sklearn import LGBMRegressor
    df_x_train = pd.read_csv('new/new_my_train.csv')
    df_x_test = pd.read_csv('new/new_my_test.csv')
    df_y_train = pd.read_csv('new/new_label.csv')
    df_y_test = pd.read_csv('new/new_test_label.csv')
    df_x_train.drop('Id', axis=1, inplace=True)
    df_x_test.drop('Id', axis=1, inplace=True)
    df_y_train.drop('Id', axis=1, inplace=True)
    df_y_test.drop('Id', axis=1, inplace=True)
    
    x_train = np.array(df_x_train)
    y_train = np.array(df_y_train)
    x_test = np.array(df_x_test)
    y_test = np.array(df_y_test)
def my_loss(st, sp):
    num_example = sp.shape[0]
    num_size = sp.shape[1]
    w = np.ones(sp.shape)
    b = np.zeros(sp.shape)
    e = np.exp(abs(st - sp)/0.012) - 1
    for j in range(num_size):
        if j == 0:
            LL = 299.85
            UL = 300.15
        else:
            LL = 199.925
            UL = 200.075
        for i in range(num_example):
            if st[i][j] >= LL and st[i][j] <= UL:
                if sp[i][j] < LL or sp[i][j] > UL:
                    w[i][j] = 10
            if st[i][j] <LL:
                b[i][j] = abs(st[i][j] - LL)
                if sp[i][j] >= LL:
                    w[i][j] = 10
            else:
                b[i][j] = abs(st[i][j] - UL)
                if sp[i][j] <= UL:
                    w[i][j] = 10
    a=100*b+1
    score = np.sum(w*e*a) / float(num_example)
    return score
model = LGBMRegressor(boosting_type= 'gbdt',  # 设置提升类型
    objective= 'regression',  # 目标函数
#     metric= 'l2',  # 评估函数
    num_leaves=31,  # 叶子节点数
    learning_rate=0.1,  # 学习速率
    feature_fraction=0.9,  # 建树的特征选择比例
    bagging_fraction= 0.8,  # 建树的样本采样比例
    bagging_freq= 5,  # k 意味着每 k 次迭代执行bagging
    verbose= 1
    
    
    )# <0 显示致命的, =0 显示错误 (警告), >0 显示信息)
model.fit(x_train, y_train[:,0])
[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.9
[LightGBM] [Warning] bagging_fraction is set=0.8, subsample=1.0 will be ignored. Current value: bagging_fraction=0.8
[LightGBM] [Warning] bagging_freq is set=5, subsample_freq=0 will be ignored. Current value: bagging_freq=5
[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.9
[LightGBM] [Warning] bagging_fraction is set=0.8, subsample=1.0 will be ignored. Current value: bagging_fraction=0.8
[LightGBM] [Warning] bagging_freq is set=5, subsample_freq=0 will be ignored. Current value: bagging_freq=5
[LightGBM] [Warning] Auto-choosing col-wise multi-threading, the overhead of testing was 0.004365 seconds.
You can set `force_col_wise=true` to remove the overhead.
[LightGBM] [Info] Total Bins 7308
[LightGBM] [Info] Number of data points in the train set: 16000, number of used features: 39
[LightGBM] [Info] Start training from score 300.064592





LGBMRegressor(bagging_fraction=0.8, bagging_freq=5, feature_fraction=0.9,
              objective='regression', verbose=1)
y_pred1 = model.predict(x_test)
model.fit(x_train, y_train[:,1])
[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.9
[LightGBM] [Warning] bagging_fraction is set=0.8, subsample=1.0 will be ignored. Current value: bagging_fraction=0.8
[LightGBM] [Warning] bagging_freq is set=5, subsample_freq=0 will be ignored. Current value: bagging_freq=5
[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.9
[LightGBM] [Warning] bagging_fraction is set=0.8, subsample=1.0 will be ignored. Current value: bagging_fraction=0.8
[LightGBM] [Warning] bagging_freq is set=5, subsample_freq=0 will be ignored. Current value: bagging_freq=5
[LightGBM] [Warning] Auto-choosing col-wise multi-threading, the overhead of testing was 0.006047 seconds.
You can set `force_col_wise=true` to remove the overhead.
[LightGBM] [Info] Total Bins 7308
[LightGBM] [Info] Number of data points in the train set: 16000, number of used features: 39
[LightGBM] [Info] Start training from score 200.000605





LGBMRegressor(bagging_fraction=0.8, bagging_freq=5, feature_fraction=0.9,
              objective='regression', verbose=1)
y_pred2 = model.predict(x_test)
model.fit(x_train, y_train[:,2])
[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.9
[LightGBM] [Warning] bagging_fraction is set=0.8, subsample=1.0 will be ignored. Current value: bagging_fraction=0.8
[LightGBM] [Warning] bagging_freq is set=5, subsample_freq=0 will be ignored. Current value: bagging_freq=5
[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=1.0 will be ignored. Current value: feature_fraction=0.9
[LightGBM] [Warning] bagging_fraction is set=0.8, subsample=1.0 will be ignored. Current value: bagging_fraction=0.8
[LightGBM] [Warning] bagging_freq is set=5, subsample_freq=0 will be ignored. Current value: bagging_freq=5
[LightGBM] [Warning] Auto-choosing col-wise multi-threading, the overhead of testing was 0.004993 seconds.
You can set `force_col_wise=true` to remove the overhead.
[LightGBM] [Info] Total Bins 7308
[LightGBM] [Info] Number of data points in the train set: 16000, number of used features: 39
[LightGBM] [Info] Start training from score 200.016679





LGBMRegressor(bagging_fraction=0.8, bagging_freq=5, feature_fraction=0.9,
              objective='regression', verbose=1)
y_pred3 = model.predict(x_test)
y_pred=np.zeros((600,3))
y_pred[:,0]=y_pred1
y_pred[:,1]=y_pred2
y_pred[:,2]=y_pred3
accuracy = my_loss(y_test, y_pred)
print('accuracy:'+str(accuracy))
accuracy:9920.103339264759
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值