【问题标题】:only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组有效
【发布时间】:2019-09-26 20:33:10
【问题描述】:

我从我的数据集中选择了特征,然后当我尝试从我的数据集中选择这些特征时,我收到了这个错误。为什么会这样?

    dataset = pd.read_csv('Banking Dataset.csv')
    LabelEncoder1 = LabelEncoder()
    independent_variables[:,1] = LabelEncoder1.fit_transform(independent_variables[:,1])
    LabelEncoder2 = LabelEncoder()
    independent_variables[:,2] = LabelEncoder2.fit_transform(independent_variables[:,2])


    onehotencoder = OneHotEncoder(categorical_features=[1])
    independent_variables = onehotencoder.fit_transform(independent_variables).toarray()

    X_train, X_test, Y_train,Y_test = train_test_split(independent_variables,target_values  ,test_size=0.25,random_state=0)

    c = DecisionTreeClassifier(min_samples_split=100)
    features =["CreditScore","Geography","Gender","Age","Tenure","Balance","NumOfProducts","HasCrCard","IsActiveMember","EstimatedSalary"]
    X = X_train(features)

输出:

FutureWarning:不推荐使用非元组序列进行多维索引;使用arr[tuple(seq)] 而不是arr[seq]。将来这将被解释为数组索引arr[np.array(seq)],这将导致错误或不同的结果。 X_train=X_train[特征] Traceback(最近一次调用最后一次):

X_train=X_train[features]

IndexError:只有整数、切片 (:)、省略号 (...)、numpy.newaxis (None) 和整数或布尔数组是有效的索引

Process finished with exit code 1

【问题讨论】:

  • 编辑问题时请多加注意。阅读此How to create a Minimal, Complete, and Verifiable example
  • 您在编辑之前遇到的问题仍然存在于您的代码中。如果您有进一步的警告或错误,请将其作为编辑提供或将其作为评论。以便方便回答问题。否则,它可能会被否决或标记。祝兄弟好运

标签: python deep-learning artificial-intelligence classification decision-tree


【解决方案1】:

使用下面的

X=X_train[features]

而不是

X=X_train(features)

调用numpy数组时使用[]

【讨论】:

  • 更新(未来警告) 未来警告表明您的功能是 list 而不是 tuple
【解决方案2】:

错误代码是说具体的行

X=X_train(features)

应该有方括号 [] 围绕特征而不是 ()。即

X=X_train[features]

【讨论】:

  • FutureWarning:不推荐使用非元组序列进行多维索引;使用arr[tuple(seq)] 而不是arr[seq]。将来这将被解释为数组索引arr[np.array(seq)],这将导致错误或不同的结果。 X_train=X_train[features] Traceback(最近一次调用最后一次):文件“C:/Users/tejus/PycharmProjects/Bankingproject/BankDecisionTree.py”,第 60 行,在 X_train=X_train[features] IndexError: only integers,切片 (:)、省略号 (...)、numpy.newaxis (None) 和整数或布尔数组是有效的索引
猜你喜欢
  • 2016-04-29
  • 2017-12-08
  • 2019-03-13
  • 2017-11-24
  • 2019-12-15
  • 2018-03-11
  • 2021-08-29
  • 2019-12-12
  • 1970-01-01
相关资源
最近更新 更多