我遇到了numpy数组形状不匹配错误。在StackOverflow上有几十个这样的问题,但没有一个能帮助我解决这个问题。我正在努力调试,因为错误是由库代码引发的。下面是相关的部分:
from modAL.models import ActiveLearner
# ... fetch data
print("Beginning debugging logs:")
print(f"classifier: {clf}")
print(f"X_train shape: {X_train.shape}")
print(f"y_train shape: {y_train.shape}")
print(f"X_pool shape: {X_POOL.shape}")
learner = ActiveLearner(
estimator=clf,
X_training=X_train,
y_training=y_train
)
result = learner.query(X_POOL)下面是回溯:
classifier: DecisionTreeClassifier(max_depth=4)
X_train shape: (84, 4926)
y_train shape: (84, 51)
X_pool shape: (997, 4926)
File "rpc_server.py", line 139, in <module>
result = learner.query(X_POOL)
File "/home/ubuntu/venv/lib/python3.8/site-packages/modAL/models/base.py", line 261, in query
query_result = self.query_strategy(self, X_pool, *query_args, **query_kwargs)
File "/home/ubuntu/venv/lib/python3.8/site-packages/modAL/uncertainty.py", line 152, in uncertainty_sampling
uncertainty = classifier_uncertainty(classifier, X, **uncertainty_measure_kwargs)
File "/home/ubuntu/venv/lib/python3.8/site-packages/modAL/uncertainty.py", line 82, in classifier_uncertainty
uncertainty = 1 - np.max(classwise_uncertainty, axis=1)
File "/home/ubuntu/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 2504, in amax
return _wrapreduction(a, np.maximum, 'max', axis, None, out, keepdims=keepdims,
File "/home/ubuntu/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 86, in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: could not broadcast input array from shape (997,1) into shape (997)该环境为Python3.8,具有:
pandas==1.1.4
numpy==1.16.0
sklearn==0.0
modAL==0.4.0我相信(997,1)只是一个普通的嵌套(997)数组?它似乎离它所需要的并不太远。
如何调试此错误?谢谢!
发布于 2021-04-20 09:23:36
您正在接收的ValueError似乎与线路有关:
uncertainty = 1 - np.max(classwise_uncertainty, axis=1)也许您可以使用以下命令完全避免在ufunc.reduce级别进行广播:
uncertainty = 1 - (np.max(classwise_uncertainty, axis=1, keepdims=1)).flatten()但是我应该注意,我无法在我的机器上重现这个错误。在最坏的情况下,您可以简单地手动重新制定代码行(使用一两个循环),以反映它所编码的相同数学。
https://stackoverflow.com/questions/67170008
复制相似问题