首页 > 编程知识 正文

b树和b+树都能有效的支持随机检索,各种树的名称和特点

时间:2023-05-03 23:29:39 阅读:30530 作者:401

从文章目录数据集读取和准备系数获取特征重要性从树模型获取特征重要性从PCA分数获取特征重要性

加载和准备数据集

为了简单起见,这里使用' load_breast_cancer '数据集。 此数据内置于Scikit-Learn中。

importnumpyasnpimportpandasaspdfromsklearn.datasetsimportload _ breast _ cancerimportmatplotlib.pyplotaspltfrommmatplotliotlib standardscalerfromsklearn.model _ selectionimporttrain _ test _ splitrcparams [ ' figure.figure ]7rcparams [ ' axes.]=falsercparams [ ' axes.spines.right ' ]=false #加载数据data=load _ breast _ cancer columns=data.feature _ names

上述数据有30个特征变量和一个目标变量。 所有的值都是数值,没有缺失的值。 在解决缩放问题之前,必须执行培训并测试分割。

x=df.drop(y ),axis=1) y=df ) ) y_train,X_test,y_train,y_test=train_test_split(X ) random_state=42 ) ss=standardscaler (x _ train _ scaled=ss.fit _ transform ) x_test_scaled

简而言之,如果分配的系数是较大的(负或正)数字,则会对预测产生某些影响。 相反,如果系数为零,则对预测没有任何影响。

逻辑非常简单。 逻辑回归是一种合适的算法。 拟合模型时,系数将作为coef_存储在属性中。

froms klearn.linear _ modelimportlogisticregressionmodel=logistic regression () model.fit ) x_train_scaled, y _ train (importance s=PD.data frame (data={ ' attribute ' : x _ train.columns, ' importance ' : model.coef _ [0] } importance s=importance s.sort _ values (by=' importance ',ascending=ffaf ) color='#087E8B ' ) PLT.title (featureimportancesobtainedfromcoeficients '

该方法的最大特点是简单、高效。 系数越大(正方向和负方向),越影响预测效果。

从树模型中获取特征的重要性并训练任何树模型后,都可以访问feature_importances属性。 这是获得工作特征重要性的最快方法之一。

这里使用XG提升

fromxgboostimportxgbclassifiermodel=xgbclassifier () model.fit ) x_train_scaled,y _ train (importance s=PD.DD ) ' importance ' : model.feature _ importance s _ } (importance s.sort _ values (by=' importance ' ), ascending=false (# PLT.bar (x=importance s )、height=importances(importance )、color=' # 087 e8b ' (PLT.title

从PCA分数获得特征重要性的主成分分析(PCA )是一种优秀的降维技术,也可用于确定特征的重要性。

PCA不像前两种技术那样直接显示最重要的特征。 相反,它返回n个主程序集。 其中n与原始特征的数量相同。

froms klearn.decompositionimportpcapca=PCA (.fit ) x_train_scaled ) #PLT.plot ) PCA.explained_variance可视化087 e8b ' (PLT.title (cumulativeexplainedvariancebynumberofprincipalcomponents (,size=20 ) plt.show () ) ) )

但是这是什么意思? 换句话说,前五个主要组件可以用来说明源数据集90%的方差。 同样,如果你不知道那意味着什么,请继续往下看。

loadings=PD.data frame (data=PCA.com ponents _.t * NP.sqrt ) PCA.explained_variance_ )、columns=[f'PC]

第一个主要组成部分很重要。 它只是一个元素,但它描述了超过60%的数据集方差。 由上图可知,与平均半径特性的相关系数接近0.8,这被认为是强正相关。

让我们可视化所有输入元素与第一个主部件之间的相关性。 以下是整个代码段,包括可视化。

pc1 _ loadings=loadings.sort _ values (by=' pc1 ',ascending=false(['PC1'] ) pc1 _ loadings=pc1 _ loadindings ' correlationwithpc1' ] PLT.bar (x=pc1 _ loadings [ ' attribute ' ],height=pc1 _ loadings [ ' correlationwionwithpc1]

原件很难,转载请注明出处

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。