【sklearn学习】LightGBM

# lightgbm原生接口
import lightgbm as lgb
# 基于scikit-learn接口
from lightgbm import LGBMClassifier
from lightgbm import LGBMRegressor

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_absolute_error, mean_squared_error
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold,StratifiedKFold
from sklearn.datasets import load_boston, load_breast_cancer, load_wine
import warnings
warnings.simplefilter("ignore")
bonston = load_boston()
cancer = load_breast_cancer()
wine = load_wine()
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
data_train, data_test, target_train, target_test = train_test_split(cancer.data, cancer.target, test_size = 0.2, random_state = 0)
lgbm = LGBMClassifier()
lgbm.fit(data_train, target_train)
train_score = lgbm.score(data_train, target_train)
print('train score:', train_score)
test_score = lgbm.score(data_test, target_test)
print('test score:', test_score)
data_train, data_test, target_train, target_test = train_test_split(cancer.data, cancer.target, test_size = 0.2, random_state = 0)


lgb_train = lgb.Dataset(data_train, target_train)
lgb_test = lgb.Dataset(data_test, target_test)


params = {
    'learning_rate': 0.1,
    'lambda_l1': 0.1,
    'lambda_l2': 0.2,
    'max_depth': 4,
    'objective': 'multiclass',  # 目标函数
    'num_class': 3,
}

# lgb_train
lgb_model = lgb.train(params, lgb_train, valid_sets=lgb_test)

# 模型预测
y_pred = lgb_model.predict(data_test)
y_pred = [list(x).index(max(x)) for x in y_pred]
print(y_pred)
 
 
# 模型评估
print(accuracy_score(target_test, y_pred))

你可能感兴趣的:(sklearn学习,sklearn,学习,python)