numpy的阵列获取行索引由行搜索

编程入门 行业动态 更新时间:2024-10-10 03:22:55
本文介绍了numpy的阵列获取行索引由行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新来numpy的,我随机森林使用Python实现集群。我的问题是:

我怎么能找到确切的行的数组索引?例如:

[0。5。2] [0 0 3] [0 0 0]

和我期待 [0。 0.3。] 并获得尽可能结果1(第二行的索引)。

任何建议?遵循code(不工作...)

对于指数,在历数(leaf_node.x)元素:        对于index_second_element,element_two在历数(leaf_node.x):            如果(指数< = index_second_element):                index_row = np.where(X ==元素)                index_column = np.where(X == element_two)                self.similarity_matrix [index_row] [index_column] + = 1

解决方案

为什么不直接做这样的事?

>>>一个阵列([0,5,2]       [0,0,3]       [0,0,0。]])>>> b阵列([0,0,3])>>>一个== b阵列([真,假,假]       [真,真,真]       [真,真,假],DTYPE =布尔)>>> np.all(A == B,轴= 1)阵列([假,真,假],DTYPE =布尔)>>> np.where(np.all(A == B,轴= 1))(阵列([1]))

I am new to numpy and I am implementing clustering with random forest in python. My question is:

How could I find the index of the exact row in an array? For example

[[ 0. 5. 2.] [ 0. 0. 3.] [ 0. 0. 0.]]

and I look for [0. 0. 3.] and get as result 1(the index of the second row).

Any suggestion? Follows the code (not working...)

for index, element in enumerate(leaf_node.x): for index_second_element, element_two in enumerate(leaf_node.x): if (index <= index_second_element): index_row = np.where(X == element) index_column = np.where(X == element_two) self.similarity_matrix[index_row][index_column] += 1

解决方案

Why not simply do something like this?

>>> a array([[ 0., 5., 2.], [ 0., 0., 3.], [ 0., 0., 0.]]) >>> b array([ 0., 0., 3.]) >>> a==b array([[ True, False, False], [ True, True, True], [ True, True, False]], dtype=bool) >>> np.all(a==b,axis=1) array([False, True, False], dtype=bool) >>> np.where(np.all(a==b,axis=1)) (array([1]),)

更多推荐

numpy的阵列获取行索引由行搜索

本文发布于:2023-11-22 00:29:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   索引   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!