程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses numpy to find the union, intersection and difference (image) of two arrays

編輯:Python

List of articles

  • 1. Union of regular arrays 、 intersection 、 Difference set
  • 2. Intersection and union difference of probability graph
    • 2.1 Concept description
    • 2.2 Realization

1. Union of regular arrays 、 intersection 、 Difference set

import numpy as np
array1=np.array([3,2,-1,100])
array2=np.array([0,-5,100,20])
""" Combine """
union=np.union1d(array1,array2)
print(union)
> [ -5 -1 0 2 3 20 100]
""" intersection """
inter=np.intersect1d(array1,array2)
print(inter)
> [100]
""" Difference set """
diff=np.setdiff1d(array1,array2)
print(diff)
> [-1 2 3]
diff=np.setdiff1d(array2,array1)
print(diff)
> [-5 0 20]
""" One additional thing about difference sets : The function prototype :numpy.setdiff1d(ar1, ar2, assume_unique=False) Return results :return 1D array of values in ar1 that are not in ar2. The return is ar1 Are there in ,ar2 There's nothing in . therefore ,ar1 and ar2 The order of results will make a difference """

Be careful ,np.union1d Function and subsequent np.intersect1dnp.setdiff1d They only support 1d Array of , If it's a multidimensional array , It needs to be done first flatten.

for example :

import numpy as np
array1=np.array([[3,2],[-1,100]])
array2=np.array([[0,-5],[100,20]])
union=np.union1d(array1.flatten(), array2.flatten())
print(union)
> [ -5 -1 0 2 3 20 100]

Reference resources :

  • numpy.union1d
  • numpy.intersect1d
  • numpy.setdiff1d
  • 【 Crystal sugar Python】numpy Difference set 、 A vision or set of 、 Combine 、 intersection setdiff1d() setxor1d() union1d() intersect1d()
  • How to find intersection between two Numpy arrays?
  • NumPy Set Operations – A Detailed Guide!

2. Intersection and union difference of probability graph

2.1 Concept description

  • It is different from the event probability in the serious statistical sense : Speed statistics tutorial 8 | Probability and set
  • Here's the probability graph , It refers to the split task , The probability that each point is a former scenic spot .
  • The intersection here is not good ( and ) operation :
    • Target : The probability graph of a segmentation object of the same graph obtained by the two algorithms
    • Premise : Suppose the model training is sigmoid function , Just split the foreground and background , With 0.3 As the threshold of foreground background probability segmentation .
    • Cross operation : For the same graph , Two segmented probability graphs obtained by using two algorithms ; Compare the two probability graphs element by element , Who takes who .
    • And operate : For the same graph , Two segmented probability graphs obtained by using two algorithms ; Compare the two probability graphs element by element , Who takes who .
    • And operation : about ensemble Come on , It doesn't make sense to find the difference between two probability graphs , So let's change it to the sum of the probability graph /2.

2.2 Realization

# and 
if op_type=="add":
prob_rs=(prob1_array+prob2_array)/2
# and 
elif op_type=="union":
prob_rs=np.where(prob1_array > prob2_array, prob1_array, prob2_array)
# hand over 
elif op_type=="inter":
prob_rs=np.where(prob1_array < prob2_array, prob1_array, prob2_array)

Reference resources :

  • Python Of Numpy operation : How to get the maximum value one by one , New composition array?

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved