intersection of two numpy arrays of different dimensions by column

  • Last Update :
  • Techknowledgy :

You can use np.in1d(array1, array2) to search in array1 each value of array2. In your case you just have to take the first column of the first array:

mask = np.in1d(a[: , 0], b)
#array([False, False, False, False, False, False, False, False, True, True], dtype = bool)

You can use this mask to obtain the encountered values:

a[: , 0][mask]
#array([17.64705882, 21.17647059])

Suggestion : 2

Return the sorted, unique values that are in both of the input arrays.,To return the indices of the values common to the input arrays along with the intersected values:,If True, the indices which correspond to the intersection of the two arrays are returned. The first instance of a value is used if there are multiple. Default is False.,Find the intersection of two arrays.

>>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
array([1, 3])
>>> from functools
import reduce
   >>>
   reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
array([3])
>>> x = np.array([1, 1, 2, 3, 4]) >>>
   y = np.array([2, 1, 4, 6]) >>>
   xy, x_ind, y_ind = np.intersect1d(x, y, return_indices = True) >>>
   x_ind, y_ind(array([0, 2, 4]), array([1, 0, 2])) >>>
   xy, x[x_ind], y[y_ind]
   (array([1, 2, 4]), array([1, 2, 4]), array([1, 2, 4]))

Suggestion : 3

I have two different numpy arrays given. First one is two-dimensional array which looks like (first ten points):,the second array is just one-dimensional which looks like (first ten points):,I want to get an array from the two-dimension one where values of the first column match values in the second (one-dimension) array. How to do that?, 1 week ago Jan 22, 2016  · Values from the second (one-dimension) array could occur in first (two-dimension) one in the first column. F.e. 17.64705882. I want to get an array from the two-dimension one where values of the first column match values in the second (one-dimension) array. How to do that?


[
   [0. 0.][12.54901961 18.03921569][13.7254902 17.64705882][14.11764706 17.25490196][14.90196078 17.25490196][14.50980392 17.64705882][14.11764706 17.64705882][14.50980392 17.25490196][17.64705882 18.03921569][21.17647059 34.11764706]
]

mask = np.in1d(a[: , 0], b) #array([False, False, False, False, False, False, False, False, True, True], dtype = bool)
1._
# Concatenating 1 - dimensional NumPy Arrays
import numpy as np array1 = np.array([1, 2, 3, 4]) array2 = np.array([5, 6, 7, 8]) joined = np.concatenate((array1, array2)) print(joined) # Returns: [1 2 3 4 5 6 7 8]
# Concatenating 1 - dimensional NumPy Arrays
import numpy as np array1 = np.array([1, 2, 3, 4]) array2 = np.array([5, 6, 7, 8]) joined = np.vstack((array1, array2)) print(joined) # Returns: #[[1 2 3 4] #[5 6 7 8]]

Suggestion : 4

Post date November 1, 2021

For instance, we write:

import numpy as np

A = np.array([
   [1, 4],
   [2, 5],
   [3, 6]
])
B = np.array([
   [1, 4],
   [3, 6],
   [7, 8]
])
aset = set([tuple(x) for x in A])
bset = set([tuple(x) for x in B])
intersection = np.array([x
   for x in aset & bset
])
print(intersection)

Therefore, intersection is:

[
   [1 4]
   [3 6]
]

Suggestion : 5

Last Updated : 29 Aug, 2020

Output:

[1, 3, 4]

Suggestion : 6

I have two different numpy arrays given. First one is two-dimensional array which looks like (first ten points):,I want to get an array from the two-dimension one where values of the first column match values in the second (one-dimension) array. How to do that?,→ What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?,Values from the second (one-dimension) array could occur in first (two-dimension) one in the first column. F.e. 17.64705882

I have two different numpy arrays given. First one is two-dimensional array which looks like (first ten points):

[
   [0. 0.]
   [12.54901961 18.03921569]
   [13.7254902 17.64705882]
   [14.11764706 17.25490196]
   [14.90196078 17.25490196]
   [14.50980392 17.64705882]
   [14.11764706 17.64705882]
   [14.50980392 17.25490196]
   [17.64705882 18.03921569]
   [21.17647059 34.11764706]
]

the second array is just one-dimensional which looks like (first ten points):

[18.03921569 17.64705882 17.25490196 17.25490196 17.64705882
   17.64705882 17.25490196 17.64705882 21.17647059 22.35294118
]

You can use np.in1d(array1, array2) to search in array1 each value of array2. In your case you just have to take the first column of the first array:

mask = np.in1d(a[: , 0], b)
#array([False, False, False, False, False, False, False, False, True, True], dtype = bool)

You can use this mask to obtain the encountered values:

a[: , 0][mask]
#array([17.64705882, 21.17647059])

Suggestion : 7

The easiest way to create an array is to use the array function. This accepts any sequence-like object (including other arrays) and produces a new NumPy array containing the passed data. For example, a list is a good candidate for conversion:,Whenever you see “array”, “NumPy array”, or “ndarray” in the text, with few exceptions they all refer to the same thing: the ndarray object.,NumPy array indexing is a rich topic, as there are many ways you may want to select a subset of your data or individual elements. One-dimensional arrays are simple; on the surface they act similarly to Python lists:,As a simple example, suppose we wished to evaluate the function sqrt(x^2 + y^2) across a regular grid of values. The np.meshgrid function takes two 1D arrays and produces two 2D matrices corresponding to all pairs of (x, y) in the two arrays:

In[13]: data1 = [6, 7.5, 8, 0, 1]

In[14]: arr1 = np.array(data1)

In[15]: arr1
Out[15]: array([6., 7.5, 8., 0., 1.])
In[27]: arr1 = np.array([1, 2, 3], dtype = np.float64)

In[28]: arr2 = np.array([1, 2, 3], dtype = np.int32)

In[29]: arr1.dtype In[30]: arr2.dtype
Out[29]: dtype('float64') Out[30]: dtype('int32')
In[45]: arr = np.array([
   [1., 2., 3.],
   [4., 5., 6.]
])

In[46]: arr
Out[46]:
   array([
      [1., 2., 3.],
      [4., 5., 6.]
   ])

In[47]: arr * arr In[48]: arr - arr
Out[47]: Out[48]:
   array([
      [1., 4., 9.], array([
         [0., 0., 0.],
         [16., 25., 36.]
      ])[0., 0., 0.]
   ])
In[51]: arr = np.arange(10)

In[52]: arr
Out[52]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In[53]: arr[5]
Out[53]: 5

In[54]: arr[5: 8]
Out[54]: array([5, 6, 7])

In[55]: arr[5: 8] = 12

In[56]: arr
Out[56]: array([0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
In[75]: arr[1: 6]
Out[75]: array([1, 2, 3, 4, 64])
In[83]: names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])

In[84]: data = np.random.randn(7, 4)

In[85]: names
Out[85]:
   array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'],
      dtype = '|S4')

In[86]: data
Out[86]:
   array([
      [-0.048, 0.5433, -0.2349, 1.2792],
      [-0.268, 0.5465, 0.0939, -2.0445],
      [-0.047, -2.026, 0.7719, 0.3103],
      [2.1452, 0.8799, -0.0523, 0.0672],
      [-1.0023, -0.1698, 1.1503, 1.7289],
      [0.1913, 0.4544, 0.4519, 0.5535],
      [0.5994, 0.8174, -0.9297, -1.2564]
   ])