Python 反向消除:';numpy.ndarray和#x27;对象没有属性';列';

Python 反向消除:';numpy.ndarray和#x27;对象没有属性';列';,python,pandas,numpy,dataframe,while-loop,Python,Pandas,Numpy,Dataframe,While Loop,我正在运行一个while循环以进行反向消除,该循环将删除/迭代删除具有最大值的属性(建议:使用p值1列=列表(X列) 2 pmax=1 3而(len(cols)>0): 4 p=[] 5 X_1=X[cols] AttributeError:'numpy.ndarray'对象没有属性'columns' 什么编程语言?请编辑您的问题并添加相应的语言标记,同时将代码和错误消息作为单独的块放置。已编辑。如您所见,它无法识别column函数。您可以发布df.head()的输出吗?您可以看到我在这里工作的

我正在运行一个while循环以进行反向消除,该循环将删除/迭代删除具有最大值的属性(建议:使用p值<0.05的while循环)

回溯(最近一次呼叫上次)
在()
---->1列=列表(X列)
2 pmax=1
3而(len(cols)>0):
4 p=[]
5 X_1=X[cols]
AttributeError:'numpy.ndarray'对象没有属性'columns'

什么编程语言?请编辑您的问题并添加相应的语言标记,同时将代码和错误消息作为单独的块放置。已编辑。如您所见,它无法识别column函数。您可以发布
df.head()
的输出吗?您可以看到我在这里工作的内容:
X
是来自数据帧的
.values
。它是一个
numpy
数组。错误非常明显-这样的对象没有
columns
属性。之前您成功地使用了
dataframe.columns
。在Python中,属性和方法是特定对象类的属性。仅仅因为
dataframe
具有
columns
属性,并不意味着
ndarray
也具有属性。您需要注意类的文档,并跟踪每个变量的对象类型。
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)