gpt4 book ai didi

python - RandomForest IndexError : only integers, slices (`:` ), ellipsis (`...` ), numpy.newaxis (`None` ) 和整数或 bool 数组是有效索引

转载 作者:行者123 更新时间:2023-11-28 18:21:49 30 4
gpt4 key购买 nike

我在 RandomForestClassifier 上使用 sklearn:

class RandomForest(RandomForestClassifier):

def fit(self, x, y):
self.unique_train_y, y_classes = transform_y_vectors_in_classes(y)
return RandomForestClassifier.fit(self, x, y_classes)

def predict(self, x):
y_classes = RandomForestClassifier.predict(self, x)
predictions = transform_classes_in_y_vectors(y_classes, self.unique_train_y)
return predictions

def transform_classes_in_y_vectors(y_classes, unique_train_y):
cyr = [unique_train_y[predicted_index] for predicted_index in y_classes]
predictions = np.array(float(cyr))
return predictions

我收到此错误消息:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

最佳答案

y_classes 似乎包含不是有效索引的值。

当您尝试使用 predicted_index 访问 unique_train_y 时,您会得到异常,因为 predicted_index 不是您认为的那样。

尝试执行以下代码:

cyr = [unique_train_y[predicted_index] for predicted_index in range(len(y_classes))] 
# assuming unique_train_y is a list and predicted_index should be integer.

关于python - RandomForest IndexError : only integers, slices (`:` ), ellipsis (`...` ), numpy.newaxis (`None` ) 和整数或 bool 数组是有效索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44679165/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com