python - 向后消除:“numpy.ndarray”对象没有属性“列”
问题描述
我正在运行一个用于向后消除的 while 循环,它将删除/迭代删除具有最大值的属性(建议:使用 p 值 < 0.05 的 while 循环)
thefile=input("Enter filename:")
separator=input("Enter symbol of separator (comma, semicolon etc):")
targetvar=input("Enter name of target variable for prediction "
"(note: this is case sensitive): ") # MEDV for Boston.csv
# Test with boston.csv and other CSVs
# Load Dataset
dataframe=pd.read_csv(thefile,sep=separator)
# Show first few data points of the dataset
print("This is the preview of the loaded data:")
df = pd.read_csv(thefile,sep=separator)
X = dataframe.loc[:,dataframe.columns != targetvar].values
y = dataframe.iloc[:,targetvarloc].values
df.head()
#Backward Elimination
cols = list(X.columns)
pmax = 1
while (len(cols)>0):
p= []
X_1 = X[cols]
X_1 = sm.add_constant(X_1)
model = sm.OLS(y,X_1).fit()
p = pd.Series(model.pvalues.values[1:],index = cols)
pmax = max(p)
feature_with_p_max = p.idxmax()
if(pmax>0.05):
cols.remove(feature_with_p_max)
else:
break
selected_features_BE = cols
print(selected_features_BE)
Traceback (most recent call last)
<ipython-input-96-3f41adb6e314> in <module>()
----> 1 cols = list(X.columns)
2 pmax = 1
3 while (len(cols)>0):
4 p= []
5 X_1 = X[cols]
AttributeError: 'numpy.ndarray' object has no attribute 'columns'
解决方案
推荐阅读
- javascript - 如何在 dae 文件中为对象提供纹理,就像在 SAP UI5 中使用 3D 查看器组件一样?
- amazon-ec2 - 消费者偏移主题在 kafka 集群中的一个领导者上损坏
- javascript - 选择某个选项时,如何将值传递给隐藏的表单字段?
- flutter - 颤振:如何从对象列表中填充下拉框
- python - Tabula 未正确排序提取的表
- javascript - 我有不会添加的数字(我必须保留为字符串)。他们只是连接数字
- git - 新签出时 cygwin 文件上的 git 已更改
- regex - 第 N 个非空白正则表达式匹配
- c# - C#如何从包含具有不同编码的多个文件的文件夹中读取文本
- java - 当我向它添加 actionListener 时,为什么我的按钮会从边框布局中消失?