import numpy as np import xlrd import matplotlib.pyplot as plt from sklearn.feature_selection import RFE from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.linear_model import LogisticRegression from sklearn.model_selection import cross_val_score def excel2m(path):#读excel数据转为矩阵函数 data = xlrd.open_workbook(path) table = data.sheets()[0] # 获取excel中第一个sheet表 nrows = table.nrows # 行数 ncols = table.ncols # 列数 datamatrix = np.zeros((nrows, ncols)) for x in range(ncols): cols = table.col_values(x) cols1 = np.matrix(cols) # 把list转换为矩阵进行矩阵操作 datamatrix[:, x] = cols1 # 把数据进行存储 return datamatrix x=excel2m("factors.xlsx") x=np.matrix(x) y=excel2m("RON.xlsx") y=np.matrix(y) rfc=RandomForestClassifier(n_estimators=10,random_state=0) score=[] for i in range(1,200,10): rfe = RFE(estimator=rfc, n_features_to_select=i, step=10).fit(x, y.astype('int')) rfe.support_.sum() rfe.ranking_ x_wrapper=rfe.transform(x) once=cross_val_score(rfc,x_wrapper,y.astype('int'),cv=5).mean() score.append(once) plt.figure(figsize=[20,5]) plt.plot(range(1,200,10),score) plt.xticks(range(1,200,10)) plt.show() np.savetxt('score.csv', score, delimiter = ',') # 确定选择特征数量后,看各个特征得分排名 # 每个特征的得分排名,特征得分越低(1最好),表示特征越好 #print(rfe.ranking_) #np.savetxt('ranking.csv', rfe.ranking_, delimiter = ',') # 每次交叉迭代各个特征得分 #print(rfe.grid_scores_) #np.savetxt('grid_scores.csv', rfe.grid_scores_, delimiter = ',')
时间: 2024-03-04 16:53:34 浏览: 36
这段代码主要实现的功能是特征筛选,其中使用了随机森林分类器(RandomForestClassifier)和递归特征消除算法(RFE),以提高模型的准确性和降低过拟合的风险。
具体流程为:首先将读取的excel数据转换为矩阵形式,然后通过循环调整特征数量,利用RFE算法进行特征筛选,得到一个新的特征子集。接着,利用交叉验证计算新特征子集下的模型得分,并将得分保存在score列表中。最后,通过matplotlib库将score列表中的得分绘制成图表,以便直观地查看得分随特征数量的变化情况。
需要注意的是,代码中还将特征得分排名和每次交叉迭代各个特征得分保存到了csv文件中,并注释了相关代码。
相关问题
import numpy as np import matplotlib.pyplot as plt from sklearn import svm, datasets啥意思
这些程序是用来实现一个基于支持向量机(SVM)的分类器,该分类器可以对鸢尾花数据集进行分类。具体来说,该程序使用datasets模块加载鸢尾花数据集,并使用svm模块中的SVC类创建一个支持向量机分类器。接着,使用matplotlib.pyplot库可视化分类结果。这些程序中的numpy库是用来进行数据处理和计算的。
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn import tree
以下是使用import语句导入pandas、numpy、matplotlib.pyplot、seaborn和sklearn.tree的示例代码:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import tree
```
- pandas是一个数据处理库,用于读取、处理和分析数据。
- numpy是一个数学库,用于处理数组和矩阵等数学运算。
- matplotlib.pyplot是一个绘图库,用于绘制各种类型的图表。
- seaborn是一个基于matplotlib的数据可视化库,提供了更高级别的界面和更多的图表类型。
- sklearn.tree是scikit-learn库中的一个模块,用于实现决策树算法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)