comparing a string 1-d numpy array elementwise

  • Last Update :
  • Techknowledgy :

Just use the ndarray's __eq__ method, i.e. ==

>>> a = array(['S', 'S', 'D', 'S', 'N', 'S', 'A', 'S', 'M'], dtype = '|S1') >>>
   b = array(['T', 'I', 'D', 'N', 'G', 'B', 'A', 'J', 'M'], dtype = '|S1') >>>
   a == b
array([False, False, True, False, False, False, True, False, True], dtype = bool)

You can use numpy.equal :

import numpy as np
c = np.equal(a, b)
c = np.core.defchararray.equal(a, b)

np.equal has been deprecated in the last numpy's releases and now raises a FutureWarning:

>>> c = np.equal(a, b)
__main__: 1: FutureWarning: elementwise comparison failed;
returning scalar instead, but in the future will perform elementwise comparison >>>
   c
NotImplemented

Suggestion : 2

❓ How to compare each element of the NumPy array arr against the scalar x using any of the greater (>), greater equal (>=), smaller (<), smaller equal (<=), or equal (==) operators? ,To compare each element of a NumPy array arr against the scalar x using any of the greater (>), greater equal (>=), smaller (<), smaller equal (<=), or equal (==) operators, use the broadcasting feature with the array as one operand and the scalar as another operand. For example, the greater comparison arr > x results in an array of Boolean values from the element-wise comparisons.,The following handy NumPy feature will prove useful throughout your career. You can use comparison operators directly on NumPy arrays. The result is an equally-sized NumPy array with Boolean values. Each Boolean indicates whether the comparison evaluates to True for the respective value in the original array.,The desired result is a NumPy array of Boolean values respresenting the element-wise comparison results.

To compare each element of a NumPy array arr against the scalar x using any of the greater (>), greater equal (>=), smaller (<), smaller equal (<=), or equal (==) operators, use the broadcasting feature with the array as one operand and the scalar as another operand. For example, the greater comparison arr > x results in an array of Boolean values from the element-wise comparisons.

array > scalar
array >= scalar
array < scalar
array <= scalar
array == scalar

# yields a new Boolean array[True / False...True / False]

For example, consider the following pseudocode of what you’re trying to accomplish:

# Given
arr = [1 10 100]
x = 3

# Desired
res = [1 > x 10 > x 100 > x] = [False True True]

NumPy will automatically bring both operands into the same shape (a feature called “broadcasting“).

import numpy as np

# Given
arr = np.array([1, 10, 100])
x = 3

# Greater:
   print(arr > x)
#[False True True]

# Greater or equal:
   print(arr >= x)
#[False True True]

# Smaller:
   print(arr < x)
#[True False False]

# Smaller or equal:
   print(arr <= x)
#[True False False]

# Equal:
   print(arr == x)
#[False False False]

Programmer Humor

Q: What is the object - oriented way to become wealthy ? 💰

   A : Inheritance.

Suggestion : 3

Last Updated : 03 Jun, 2022

Output:

True

Method 2: We can also use greater than, less than and equal to operators to compare. To understand, have a look at the code below.

Syntax: numpy.greater(x1, x2[, out])
Syntax: numpy.greater_equal(x1, x2[, out])
Syntax: numpy.less(x1, x2[, out])
Syntax: numpy.less_equal(x1, x2[, out])

Syntax:

numpy.array_equal(arr1, arr2)

Suggestion : 4

Once again, it is safer to use the np.array_equal() function to compare the two arrays. It is because this function is designed to handle these cases to produce the correct results.,To overcome this issue, you should use the built-in array_equal method for comparing arrays.,This is because when comparing arrays of different sizes, the comparison returns a single boolean value, False in this case.,Thus, you should use the dedicated np.array_equal() function to make the comparison reliable.

For example:

(A == B).all()

Instead, you should consider using the built-in np.array_equal() function for good measure.

np.array_equal(A, B)

Here is an example:

import numpy as np

A = np.array([
   [1, 1],
   [2, 2]
])
B = np.array([
   [1, 1],
   [2, 2]
])

equal_arrays = (A == B).all()

print(equal_arrays)

Let’s try to compare two NumPy arrays like you would compare two lists:

import numpy as np

A = np.array([
   [1, 1],
   [2, 2]
])
B = np.array([
   [1, 1],
   [2, 2]
])

print(A == B)

As you can see, the result is a matrix, not a boolean:

[
   [True True]
   [True True]
]

Suggestion : 5

Output array, element-wise comparison of x1 and x2. Typically of type bool, unless dtype=object is passed. This is a scalar if both x1 and x2 are scalars.,What is compared are values, not types. So an int (1) and an array of length one can evaluate as True:,Input arrays. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).,A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

>>> np.equal([0, 1, 3], np.arange(3))
array([True, True, False])
>>> np.equal(1, np.ones(1))
array([True])
>>> a = np.array([2, 4, 6]) >>>
   b = np.array([2, 4, 2]) >>>
   a == b
array([True, True, False])

Suggestion : 6

Suppose each name corresponds to a row in the data array and we wanted to select all the rows with corresponding name 'Bob'. Like arithmetic operations, comparisons (such as ==) with arrays are also vectorized. Thus, comparing names with the string 'Bob' yields a boolean array:,One of the key features of NumPy is its N-dimensional array object, or ndarray, which is a fast, flexible container for large data sets in Python. Arrays enable you to perform mathematical operations on whole blocks of data using similar syntax to the equivalent operations between scalar elements:,Whenever you see “array”, “NumPy array”, or “ndarray” in the text, with few exceptions they all refer to the same thing: the ndarray object.,In multidimensional arrays, if you omit later indices, the returned object will be a lower-dimensional ndarray consisting of all the data along the higher dimensions. So in the 2 × 2 × 3 array arr3d

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]
   ])