我想评估特征选择技术粒子群优化与神经网络。但我一直有这样的错误: “布尔索引与维度1上的索引数组不匹配;维数为1,对应的布尔维数为19。 这是我的代码:
model = Sequential()
model.add(Dense(16, input_dim=19, activation= 'relu'))
model.add(Dense(12, activation= 'relu'))
model.add(Dense(2, activation= 'softmax'))
model.add(Flatten())
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
def f_per_particle(m, alpha):
total_features = 19
if np.count_nonzero(m) == 0:
X_subset = x_train
else:
X_subset = x_train[:,m==1]
P=model.fit(X_subset, y_train, batch_size=64, epochs=100)
j = (alpha * (1.0 - P)+ (1.0 - alpha) * (1 - (X_subset.shape[1] / total_features)))
return j
def f(x, alpha=0.9):
n_particles = x.shape[0]
j = [f_per_particle(x[i], alpha) for i in range(n_particles)]
return np.array(j)
options = {'c1': 0.5, 'c2': 0.5, 'w':0.9, 'k': 30, 'p':2}
dimensions = 19
optimizer = ps.discrete.BinaryPSO(n_particles=100, dimensions=dimensions, options=options)
cost, pos = optimizer.optimize(f, iters=100)
问题来源StackOverflow 地址:/questions/59386403/how-to-match-boolean-index-array-along-with-dimension
After an update of Macports, that I think updated numpy, I'm getting the warning:
VisibleDeprecationWarning: boolean index did not match indexed array along dimension 1; dimension is 2 but corresponding boolean dimension is 1 inliers = n.size(pixels[distances <= self.dst]) that was not raised before. The related code is:
distances = guess_feature.points_distance(pixels)
inliers = n.size(pixels[distances <= self.dst]) self.dst is a single scalar.
guess_feature.points_distance:
def points_distance(self,points): r''' Compute the distance of the points from the feature
:math:`d = \left| \sqrt{(x_i - x_c)^2 + (y_i-y_c)^2} - r \right|`
Args:
points (numpy.ndarray): a (n,2) numpy array, each row is a 2D Point.
Returns:
d (numpy.ndarray): the computed distances of the points from the feature.
'''
xa = n.array([self.xc,self.yc]).reshape((1,2))
d = n.abs(dist.cdist(points,xa) - self.radius)
return d
Any ideas?
解决方案 I started getting a similar error after going up to numpy 1.10.1. I think you can get rid of the warning just by wrapping the boolean array in a numpy.where().
inliers = n.size(pixels[n.where(distances <= self.dst)]) Since you're just taking the size, there's no need to use the pixels array, so this should work:
inliers = n.size(n.where(distances <= self.dst])[0]) 机译 在Macports更新之后,我认为已经更新了numpy,我得到了警告:
VisibleDeprecationWarning:布尔值索引沿维度1与索引数组不匹配;维为2,但对应的布尔维为1 inliers = n.size(pixels [distances< = self.dst])
以前没有提出过。相关代码为:
#计算所有非零点到圆周的距离 的距离= guess_feature.points_distance (像素)
#检查哪些点是inliers(即圆附近) inliers = n.size(pixels [distances <= self.dst])
self.dst 是单个标量。
guess_feature.points_distance :
def points_distance(self,points): r''' 计算点距特征
的距离:math:d = \ \左| \sqrt {(x_i-x_c)^ 2 +(y_i-y_c)^ 2}-r \right |
Args: 分(numpy.ndarray):a( n,2)numpy数组,每一行都是一个2D点。
返回: d(numpy.ndarray):点到要素的计算距离。
'''
xa = n.array([self.xc,self.yc])。reshape((1,2)) d = n .abs(dist.cdist(points,xa)-self.radius) return d
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。