# 导入函数包 import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns
# 导入数据 data = pd.read_csv(r"D:备份OneDrive桌面资料train (1).csv", sep=',')
## 利用.info()查看数据的整体信息 data.info()
RangeIndex: 106644 entries, 0 to 106643 Data columns (total 23 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Date 106644 non-null object 1 Location 106644 non-null object 2 MinTemp 106183 non-null float64 3 MaxTemp 106413 non-null float64 4 Rainfall 105610 non-null float64 5 Evaporation 60974 non-null float64 6 Sunshine 55718 non-null float64 7 WindGustDir 99660 non-null object 8 WindGustSpeed 99702 non-null float64 9 WindDir9am 99166 non-null object 10 WindDir3pm 103788 non-null object 11 WindSpeed9am 105643 non-null float64 12 WindSpeed3pm 104653 non-null float64 13 Humidity9am 105327 non-null float64 14 Humidity3pm 103932 non-null float64 15 Pressure9am 96107 non-null float64 16 Pressure3pm 96123 non-null float64 17 Cloud9am 66303 non-null float64 18 Cloud3pm 63691 non-null float64 19 Temp9am 105983 non-null float64 20 Temp3pm 104599 non-null float64 21 RainToday 105610 non-null object 22 RainTomorrow 106644 non-null object dtypes: float64(16), object(7) memory usage: 18.7+ MB
## 进行简单的数据查看,我们可以利用 .head() 头部.tail()尾部 data.head()
''' 这里我们发现数据集中存在NaN,一般的我们认为NaN在数据集中代表了缺失值,可能是数据采集或处理时产生的一种错误。 这里我们采用-1将缺失值进行填补,还有其他例如“中位数填补、平均数填补”的缺失值处理方法有兴趣的同学也可以尝试。 ''' data = data.fillna(-1) print(data.head())
## 利用value_counts函数查看训练集标签的数量 pd.Series(data['RainTomorrow']).value_counts() # 对比发现,数据中正样本远远大于负样本,在一些情况下需要进行数据均衡化处理
No 82786 Yes 23858 Name: RainTomorrow, dtype: int64
## 对于特征进行一些统计描述 data.describe()
# 为了后续方便,将数字特征与非数据特征分开 numerical_features = [x for x in data.columns if data[x].dtype == np.float] category_features = [x for x in data.columns if data[x].dtype != np.float and data[x].dtype != ['RainTomorrow']]
## 选取三个特征与标签组合的散点可视化 sns.pairplot(data=data[['Rainfall', 'Evaporation', 'Sunshine'] + ['RainTomorrow']], diag_kind='hist', hue= 'RainTomorrow') plt.show()
for col in data[numerical_features].columns: if col != 'RainTomorrow': sns.boxplot(x='RainTomorrow', y=col, saturation=0.5, palette='pastel', data=data) plt.title(col) plt.show() ''' 利用箱型图我们也可以得到不同类别在不同特征上的分布差异情况。我们可以发现Sunshine,Humidity3pm,Cloud9am,Cloud3pm的区分能力较强 '''
tlog = {} for i in category_features: tlog[i] = data[data['RainTomorrow'] == 'Yes'][i].value_counts() flog = {} for i in category_features: flog[i] = data[data['RainTomorrow'] == 'No'][i].value_counts()
plt.figure(figsize=(10,10)) plt.subplot(1,2,1) plt.title('RainTomorrow') sns.barplot(x = pd.Dataframe(tlog['Location']).sort_index()['Location'], y = pd.Dataframe(tlog['Location']).sort_index().index, color = "red") plt.subplot(1,2,2) plt.title('Not RainTomorrow') sns.barplot(x = pd.Dataframe(flog['Location']).sort_index()['Location'], y = pd.Dataframe(flog['Location']).sort_index().index, color = "blue") plt.show()
plt.figure(figsize=(10,2)) plt.subplot(1,2,1) plt.title('RainTomorrow') sns.barplot(x = pd.Dataframe(tlog['RainToday'][:2]).sort_index()['RainToday'], y = pd.Dataframe(tlog['RainToday'][:2]).sort_index().index, color = "red") plt.subplot(1,2,2) plt.title('Not RainTomorrow') sns.barplot(x = pd.Dataframe(flog['RainToday'][:2]).sort_index()['RainToday'], y = pd.Dataframe(flog['RainToday'][:2]).sort_index().index, color = "blue") plt.show()
## 把所有的相同类别的特征编码为同一个值 def get_mapfunction(x): mapp = dict(zip(x.unique().tolist(), range(len(x.unique().tolist())))) # tolist是将数组转化为列表,zip是转化为元组,dict是转化为字典 def mapfunction(y): if y in mapp: return mapp[y] else: return -1 return mapfunction for i in category_features: data[i] = data[i].apply(get_mapfunction(data[i]))
## 编码后的字符串特征变成了数字 data['Location'].unique() # unique函数是去除重复值
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48], dtype=int64)利用 XGBoost 进行训练与预测
## 为了正确评估模型性能,将数据划分为训练集和测试集,并在训练集上训练模型,在测试集上验证模型性能。 from sklearn.model_selection import train_test_split ## 选择其类别为0和1的样本 (不包括类别为2的样本) data_target_part = data['RainTomorrow'] data_features_part = data[[x for x in data.columns if x != 'RainTomorrow']] ## 测试集大小为20%, 80%/20%分 x_train,x_test,y_train,y_test = train_test_split(data_features_part, data_target_part, test_size=0.2, random_state=2022)
pip install --user xgboost==1.2.0 # 由于1.3运行有问题,把版本降到1.2.0
## 导入XGBoost模型 from xgboost.sklearn import XGBClassifier ## 定义 XGBoost模型 clf = XGBClassifier() # 在训练集上训练XGBoost模型 clf.fit(x_train, y_train)
## 在训练集和测试集上分布利用训练好的模型进行预测 train_predict = clf.predict(x_train) test_predict = clf.predict(x_test) from sklearn import metrics ## 利用accuracy(准确度)【预测正确的样本数目占总预测样本数目的比例】评估模型效果 print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_train,train_predict)) print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test,test_predict)) ## 查看混淆矩阵 (预测值和真实值的各类情况统计矩阵) confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test) print('The confusion matrix result:n',confusion_matrix_result) # 利用热力图对于结果进行可视化 plt.figure(figsize=(8, 6)) sns.heatmap(confusion_matrix_result, annot=True, cmap='Blues') plt.xlabel('Predicted labels') plt.ylabel('True labels') plt.show()
The accuracy of the Logistic Regression is: 0.8985407021039676 The accuracy of the Logistic Regression is: 0.8599559285479863 The confusion matrix result: [[15696 2099] [ 888 2646]]
# XGBoost的特征选择属于特征选择中的嵌入式方法,在XGboost中可以用属性feature_importances_去查看特征的重要度。 clf.feature_importances_
array([0.00940117, 0.01830212, 0.01106981, 0.01072195, 0.02148786, 0.01028645, 0.03462758, 0.00981146, 0.03634984, 0.0143293 , 0.01152112, 0.0099855 , 0.01216236, 0.01464254, 0.15441626, 0.01318689, 0.02900443, 0.01352073, 0.03188516, 0.01406286, 0.01174629, 0.50747836], dtype=float32)
sns.barplot(y=data_features_part.columns, x=clf.feature_importances_)
''' 初此之外,我们还可以使用XGBoost中的下列重要属性来评估特征的重要性。 weight:是以特征用到的次数来评价 gain:当利用特征做划分的时候的评价基尼指数 cover:利用一个覆盖样本的指标二阶导数(具体原理不清楚有待探究)平均值来划分。 total_gain:总基尼指数 total_cover:总覆盖 ''' from sklearn.metrics import accuracy_score from xgboost import plot_importance def estimate(model,data): #sns.barplot(data.columns,model.feature_importances_) ax1=plot_importance(model,importance_type="gain") ax1.set_title('gain') ax2=plot_importance(model, importance_type="weight") ax2.set_title('weight') ax3 = plot_importance(model, importance_type="cover") ax3.set_title('cover') plt.show() def classes(data,label,test): model=XGBClassifier() model.fit(data,label) ans=model.predict(test) estimate(model, data) return ans ans=classes(x_train,y_train,x_test) pre=accuracy_score(y_test, ans) print('acc=',accuracy_score(y_test,ans))
acc= 0.8599559285479863通过调整参数获得更好的效果
XGBoost中包括但不限于下列对模型影响较大的参数:
learning_rate: 有时也叫作eta,系统默认值为0.3。每一步迭代的步长,很重要。太大了运行准确率不高,太小了运行速度慢。 subsample:系统默认为1。这个参数控制对于每棵树,随机采样的比例。减小这个参数的值,算法会更加保守,避免过拟合, 取值范围零到一。 colsample_bytree:系统默认值为1。我们一般设置成0.8左右。用来控制每棵随机采样的列数的占比(每一列是一个特征)。 max_depth: 系统默认值为6,我们常用3-10之间的数字。这个值为树的最大深度。这个值是用来控制过拟合的。max_depth越大,模型学习的更加具体。
## 从sklearn库中导入网格调参函数 from sklearn.model_selection import GridSearchCV # 定义参数范围 learning_rate = [0.1, 0.3, 0.6] subsample = [0.8, 0.9] colsample_bytree = [0.6, 0.8] max_depth = [3,5,8] parameters={'learning_rate': learning_rate, 'subsample': subsample, 'colsample_bytree':colsample_bytree, 'max_depth': max_depth} model = XGBClassifier(n_estimators = 50) ## 进行网格搜索 clf = GridSearchCV(model, parameters, cv=3, scoring='accuracy',verbose=1,n_jobs=-1) clf.fit(x_train, y_train)
## 网格搜索后的最好参数为 clf.best_estimator_
XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1, colsample_bynode=1, colsample_bytree=0.6, enable_categorical=False, gamma=0, gpu_id=-1, importance_type=None, interaction_constraints='', learning_rate=0.1, max_delta_step=0, max_depth=3, min_child_weight=1, missing=nan, monotone_constraints='()', n_estimators=50, n_jobs=8, num_parallel_tree=1, predictor='auto', random_state=0, reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=0.8, tree_method='exact', validate_parameters=1, verbosity=None)
## 在训练集和测试集上分布利用最好的模型参数进行预测 # 定义带参数的模型 clf = XGBClassifier(learning_rate=0.1, subsample=0.8, colsample_bytree=0.6, max_depth=3) # 在训练集上训练XGBoost模型 clf.fit(x_train, y_train) train_predict = clf.predict(x_train) test_predict = clf.predict(x_test) ## 利用accuracy(准确度)【预测正确的样本数目占总预测样本数目的比例】评估模型效果 print('The accuracy of the Logistic Regression is:', metrics.accuracy_score(y_train, train_predict)) print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test, test_predict)) ## 查看混淆矩阵 (预测值和真实值的各类情况统计矩阵) confusion_matrix_result = metrics.confusion_matrix(test_predict, y_test) print('The confusion matrix result:n',confusion_matrix_result) # 利用热力图对于结果进行可视化 plt.figure(figsize=(8, 6)) sns.heatmap(confusion_matrix_result, annot=True, cmap='Greens') plt.xlabel('The Predict labels') plt.ylabel('The True labels') plt.show()
The accuracy of the Logistic Regression is: 0.8506007149973627 The accuracy of the Logistic Regression is: 0.8509072155281542 The confusion matrix result: [[15824 2420] [ 760 2325]]xgboost的重要参数
1.eta[默认0.3] 通过为每一颗树增加权重,提高模型的鲁棒性。 典型值为0.01-0.2。
2.min_child_weight[默认1] 决定最小叶子节点样本权重和。 这个参数可以避免过拟合。当它的值较大时,可以避免模型学习到局部的特殊样本。 但是如果这个值过高,则会导致模型拟合不充分。
3.max_depth[默认6] 这个值也是用来避免过拟合的。max_depth越大,模型会学到更具体更局部的样本。 典型值:3-10
4.max_leaf_nodes 树上最大的节点或叶子的数量。 可以替代max_depth的作用。 这个参数的定义会导致忽略max_depth参数。
5.gamma[默认0] 在节点分裂时,只有分裂后损失函数的值下降了,才会分裂这个节点。Gamma指定了节点分裂所需的最小损失函数下降值。 这个参数的值越大,算法越保守。这个参数的值和损失函数息息相关。
6.max_delta_step[默认0] 这参数限制每棵树权重改变的最大步长。如果这个参数的值为0,那就意味着没有约束。如果它被赋予了某个正值,那么它会让这个算法更加保守。 但是当各类别的样本十分不平衡时,它对分类问题是很有帮助的。
7.subsample[默认1] 这个参数控制对于每棵树,随机采样的比例。 减小这个参数的值,算法会更加保守,避免过拟合。但是,如果这个值设置得过小,它可能会导致欠拟合。 典型值:0.5-1
8.colsample_bytree[默认1] 用来控制每棵随机采样的列数的占比(每一列是一个特征)。 典型值:0.5-1
9.colsample_bylevel[默认1] 用来控制树的每一级的每一次分裂,对列数的采样的占比。 subsample参数和colsample_bytree参数可以起到相同的作用,一般用不到。
10.lambda[默认1] 权重的L2正则化项。(和Ridge regression类似)。 这个参数是用来控制XGBoost的正则化部分的。虽然大部分数据科学家很少用到这个参数,但是这个参数在减少过拟合上还是可以挖掘出更多用处的。
11.alpha[默认1] 权重的L1正则化项。(和Lasso regression类似)。 可以应用在很高维度的情况下,使得算法的速度更快。
12.scale_pos_weight[默认1] 在各类别样本十分不平衡时,把这个参数设定为一个正值,可以使算法更快收敛。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)