AttributeError:'numpy.ndarray'对象没有属性'columns'

时间:2016-03-14 06:20:14

标签: python numpy pandas

我正在尝试创建一个功能来删除彼此高度相关的功能。但是,我收到错误''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')

1 个答案:

答案 0 :(得分:4)

查看Pandas文档,但我认为

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

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

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