【问题标题】:Can you help me in this error? AttributeError: 'numpy.ndarray' object has no attribute 'keys'你能帮我解决这个错误吗? AttributeError:“numpy.ndarray”对象没有属性“键”
【发布时间】:2021-06-09 22:57:52
【问题描述】:

all_features = X_train.keys()

AttributeError: 'numpy.ndarray' 对象没有属性 '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) ```

【问题讨论】:

    标签: python pandas correlation feature-extraction feature-selection


    【解决方案1】:

    [在此处输入图片描述][1]

    ndarray 没有“keys”属性。您可以找到文档和所有属性,https://numpy.org/doc/stable/reference/arrays.ndarray.html#。您可能应该使用 X_train.flags()

    【讨论】:

    • 我使用X_train.flags(),但出现另一个错误:TypeError: 'numpy.flagsobj' object is not callable
    • 对不起,不是函数,只是X_train.flags
    猜你喜欢
    • 2022-07-20
    • 1970-01-01
    • 2021-07-15
    • 2020-12-03
    • 2020-11-29
    • 2020-10-06
    • 2018-01-25
    • 2016-06-29
    • 2020-03-25
    相关资源
    最近更新 更多