开发者社区> 问答> 正文

如何匹配布尔索引数组与维度?

我想评估特征选择技术粒子群优化与神经网络。但我一直有这样的错误: “布尔索引与维度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

展开
收起
kun坤 2019-12-25 21:59:44 533 0
1 条回答
写回答
取消 提交回答
  • 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:

    Compute distance of all non-zero points from the circumference

    distances = guess_feature.points_distance(pixels)

    Check which points are inliers (i.e. near the circle)

    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

    2021-02-20 10:37:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载