#coding=utf-8
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import classification_report, recall_score, confusion_matrix,auc
from sklearn.preprocessing import StandardScaler
import xgboost as xgb
import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics
import seaborn as sns
from sklearn.linear_model import LogisticRegression
doo = pd.read_excel('C://Users/chickendaddy/Desktop/期末信息汇总/金融风险管理/doable.xlsx')
def delete_nan(data):
data.fillna(0, inplace=True)
return
delete_nan(doo)
std = StandardScaler()
def standardize(data):
data_x_stand = std.fit_transform(data.iloc[:, :-2])
data_x_stand = pd.DataFrame(data_x_stand)
return data_x_stand
std_doo = standardize(doo)
x_train, x_test, y_train, y_test = train_test_split(std_doo, doo.iloc[:,[-1]], test_size=0.25)
dtrain = xgb.DMatrix( x_train, y_train, missing = -999.0)
dtest = xgb.DMatrix(x_test, y_test)
alg = xgb.XGBClassifier(booster='gbtree', objective='binary:logistic', eval_metric='error',use_label_encoder=False)
clf = GridSearchCV(alg,{ 'eta': [0.1,0.05,0.001,0.005], 'max_depth':[10,11,12,13,14,15],'subsample':[0.3,0.5,0.7]})
clf.fit(x_train,y_train)
num_round = 190
param = {'base_score':0.5, 'booster':'gbtree', 'colsample_bylevel':1,
'colsample_bynode':1, 'colsample_bytree':1, 'eta':0.1, 'gamma':0,
'gpu_id':-1, 'importance_type':'gain', 'interaction_constraints':'',
'learning_rate':'0.1', 'max_delta_step':0, 'max_depth':13,
'min_child_weight':1, 'missing':'nan', 'monotone_constraints':'()',
'n_estimators':100, 'n_jobs':12, 'num_parallel_tree':1,
'objective':'binary:logistic', 'random_state':0, 'reg_alpha':0,
'reg_lambda':1, 'scale_pos_weight':1, 'subsample':0.7,
'tree_method':'exact', 'use_label_encoder':'False' }
bst = xgb.train(param, dtrain, num_round)
preds = bst.predict(dtest)
confusion = confusion_matrix(c.round(),y_test)
print(confusion)
plt.figure(figsize=(8, 6))
sns.heatmap(confusion, annot=True, cmap='Blues')
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.show()
precision, recall, threshold = metrics.precision_recall_curve(y_test, preds)
print(precision)
print(recall)
pr_auc = metrics.auc(recall, precision)
print(pr_auc)
plt.figure(figsize=(6,8))
plt.plot(precision)
plt.plot(recall)
lg = LogisticRegression(C=1.0)
lg.fit(x_train, y_train)
print('系数', lg.coef_)
y_predict = lg.predict(x_test)
print("准确率:", lg.score(x_test, y_test))
print("召回率:", classification_report(y_test, y_predict, labels=[0, 1], target_names=["0", "1"]))
fmg
最新推荐文章于 2024-05-23 10:17:44 发布