gpt4 book ai didi

python - 属性错误 : 'numpy.ndarray' object has no attribute 'columns'

转载 作者:行者123 更新时间:2023-11-28 21:47:48 26 4
gpt4 key购买 nike

我正在尝试创建一个函数来删除彼此高度相关的特征。但是,我收到错误 ''AttributeError: 'numpy.ndarray' object has no attribute 'columns' '' ...

我只想调用 pandas 来读取列号。接下来我能做什么?

import pandas as pd
import numpy as np

def remove_features_identical(DataFrame,data_source):
n=len(DataFrame.columns)
print 'dealing with %d features of %s data......... \n' % (n,data_source)
remove_ind = []
R = np.corrcoef(DataFrame.T)
for i in range(n-1):
for j in range(i+1,n):
if R[i,j]==1:
remove_ind.append(j)

DataFrame.drop(remove_ind, axis=1, inplace=True)
DataFrame.drop(remove_ind, axis=1, inplace=True)
print ('deleting %d columns with correration factor >0.99') % ( len(remove_ind))
return DataFrame

if __name__ == "__main__":
# load data and initialize y and x from train set and test set
df_train = pd.read_csv('train.csv')
df_test = pd.read_csv('test.csv')
y_train=df_train['TARGET'].values
X_train =df_train.drop(['ID','TARGET'], axis=1).values
y_test=[]
X_test = df_test.drop(['ID'], axis=1).values

# delete identical feartures in raw data
X_train = remove_features_identical(X_train,'train set')
X_test = remove_features_identical(X_test,'test set')

最佳答案

查看 Pandas 文档,但我认为

X_train = df_train.drop(['ID','TARGET'], axis=1).values

.values 返回一个 numpy 数组,而不是 Pandas 数据帧。数组没有 columns 属性。

remove_features_identical - 如果您将数组传递给它,请确保您只使用数组,而不是数据框,功能。否则,请确保将数据框传递给它。并且不要使用像 DataFrame 这样的变量名。

关于python - 属性错误 : 'numpy.ndarray' object has no attribute 'columns' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35980747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com