1
我試圖創建一個函數來刪除相互之間高度相關的功能。但是,我得到錯誤''AttributeError: 'numpy.ndarray' object has no attribute 'columns' '' ...
AttributeError:'numpy.ndarray'對象沒有屬性'列'
我只是想調用熊貓閱讀列號。接下來我可以做什麼?
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')