Python 关于Scikit学习的特征选择回归

Python 关于Scikit学习的特征选择回归,python,machine-learning,scikit-learn,neural-network,feature-selection,Python,Machine Learning,Scikit Learn,Neural Network,Feature Selection,我有一个excel数据文件,包括预测值和目标/响应。我将目标称为“接近”,每个预测器都有自己的名称。为了研究特征选择,我使用了这段代码,但出现了一些问题,我无法意识到每个预测因素对目标的影响。我写的代码如下。一切帮助都将不胜感激 #importing libraries from sklearn.datasets import load_boston import pandas as pd import numpy as np import matplotlib import matplotli

我有一个excel数据文件,包括预测值和目标/响应。我将目标称为“接近”,每个预测器都有自己的名称。为了研究特征选择,我使用了这段代码,但出现了一些问题,我无法意识到每个预测因素对目标的影响。我写的代码如下。一切帮助都将不胜感激

#importing libraries
from sklearn.datasets import load_boston
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.api as sm
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.feature_selection import RFE
from sklearn.linear_model import RidgeCV, LassoCV, Ridge, Lasso

# reading files
x = pd.read_excel("train.xlsx")

X = x.iloc[:, 0:5]

y = x.iloc[:,5]

#Using Pearson Correlation
plt.figure(figsize=(12,10))
cor = X.corr()
sns.heatmap(cor, annot=True, cmap=plt.cm.Reds)
plt.show()
#Correlation with output variable
cor_target = abs(cor["NEAR"])
#Selecting highly correlated features
relevant_features = cor_target[cor_target > 0.5]
relevant_features 
但是我犯了这个错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-28-3468dfbea7e1> in <module>
      1 #Correlation with output variable
----> 2 cor_target = abs(cor[y])
      3 #Selecting highly correlated features
      4 relevant_features = cor_target[cor_target > 0.5]
      5 relevant_features

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2984             if is_iterator(key):
   2985                 key = list(key)
-> 2986             indexer = self.loc._convert_to_indexer(key, axis=1, raise_missing=True)
   2987 
   2988         # take() does not accept boolean indexers

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter, raise_missing)
   1283                 # When setting, missing keys are not allowed, even with .loc:
   1284                 kwargs = {"raise_missing": True if is_setter else raise_missing}
-> 1285                 return self._get_listlike_indexer(obj, axis, **kwargs)[1]
   1286         else:
   1287             try:

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1090 
   1091         self._validate_read_indexer(
-> 1092             keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
   1093         )
   1094         return keyarr, indexer

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1175                 raise KeyError(
   1176                     "None of [{key}] are in the [{axis}]".format(
-> 1177                         key=key, axis=self.obj._get_axis_name(axis)
   1178                     )
   1179                 )

KeyError: "None of [Float64Index([-0.00398046,  0.00205926, -0.00304156,  0.00206342,  0.00797852,\n               0.00619195,  0.00368038,  0.00415858,  0.00454432,  0.00536623,\n              ...\n               0.00201033,  0.00184575,  0.00165407,  0.00148248,  0.00131221,\n               0.00103276,  0.00084394,  0.00078347,  0.00069564,  0.00058571],\n             dtype='float64', length=209076)] are in the [columns]"

---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在里面
1#与输出变量的相关性
---->2 cor_目标=绝对值(cor[y])
3#选择高度相关的特征
4相关特征=相关目标[相关目标>0.5]
5相关的功能
~\Anaconda3\lib\site packages\pandas\core\frame.py in\uuuu\getitem\uuuuuuu(self,key)
2984如果是迭代器(键):
2985键=列表(键)
->2986索引器=self.loc.\u将\u转换为\u索引器(键,轴=1,上升\u缺失=True)
2987
2988#take()不接受布尔索引器
~\Anaconda3\lib\site packages\pandas\core\index.py in\u convert\u to\u indexer(self、obj、axis、is\u setter、raise\u缺失)
1283#设置时,即使使用.loc:
1284 kwargs={“raise_missing”:如果SEUTER else raise_missing}
->1285返回自我。获取列表式索引器(obj,轴,**kwargs)[1]
1286其他:
1287尝试:
~\Anaconda3\lib\site packages\pandas\core\index.py in\u get\u listlike\u索引器(self、key、axis、raise\u缺失)
1090
1091自我验证读取索引器(
->1092 keyarr,索引器,o._获取_轴编号(轴),raise_missing=raise_missing
1093         )
1094返回键,索引器
~\Anaconda3\lib\site packages\pandas\core\index.py in\u validate\u read\u indexer(self、key、indexer、axis、raise\u缺失)
1175上升键错误(
1176“所有[{key}]都不是[{axis}]”格式(
->1177键=键,轴=自身。对象。\u获取\u轴\u名称(轴)
1178                     )
1179                 )
KeyError:“没有[Float64Index]([-0.00398046,0.00205926,-0.00304156,0.00206342,0.00797852,n 0.00619195,0.00368038,0.00415858,0.00454432,0.00536623,n…\n 0.0020033,0.00184575,0.00165407,0.00148248,0.00131221,0.00103276,0.00084394,0.00078347,0.00069564,0.00058571],\n dtype='float64',length=209076)]在[列]中。“

问题似乎出在以下行中(与您发布的代码不同):

cor_target = abs(cor[y])
您似乎已将其更改为:

cor_target = abs(cor["NEAR"])
代码的问题在于,您正在向数据帧
cor
传递一个用作数组的变量
y
。执行此操作时,pandas会尝试选择值等于数组
y
的列名。这意味着它将搜索名为
-0.00398046、0.00205926、-0.00304156
等的列c、 这是不存在的,因此是错误的

如果目标变量是
附近的
,则新代码应该可以工作