簡體   English   中英

你能幫我解決這個錯誤嗎? AttributeError: 'numpy.ndarray' object 沒有屬性 'keys'

[英]Can you help me in this error? AttributeError: 'numpy.ndarray' object has no attribute 'keys'

all_features = X_train.keys()

AttributeError: 'numpy.ndarray' object 沒有屬性 'keys'

這是我的代碼:

    cor_list = []

    #calculate the correlation with y for each feature
    for i in all_features:
        cor = np.corrcoef(X_train[i], y_train, rowvar=False)[0, 1]
        cor_list.append(cor)
     #replace NaN with 0
    cor_list = [0 if np.isnan(i) else i for i in cor_list]
    #feature_name
    cor_feature = X_train.iloc[:np.argsort(np.abs(cor_list))[-num_feats:]].columns.tolist()
    #feature selection? 0 for not select, 1 for select
    cor_support = [True if i in cor_feature else False for i in all_features]
    X_train_selected = pd.DataFrame(X_train, columns=cor_feature)
    X_test_selected = pd.DataFrame(X_test, column = cor_feature)
    # Correlation with output variable
    return X_train_selected, X_test_selected


**This when i called in main:**

``` all_features = X_train.keys()

X_train_selected, X_test_selected = FS.Feature_cor_selector(X_train, X_test, y_train, 10, all_features) ```

[在此處輸入圖片描述][1]

ndarray 沒有“鍵”屬性。 您可以找到文檔和所有屬性https://numpy.org/doc/stable/reference/arrays.ndarray.html# 您可能應該使用 X_train.flags()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM