



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of numerical operations on NumPy arrays, focusing on elementwise operations, comparisons, logical operations, transcendental functions, and extrema. It covers basic arithmetic operations, matrix multiplication, and various other operations.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!
Basic operation with scalars:
In [1]:
In [2]:
In [3]:
All arithmetic operates elementwise:
In [4]:
In [5]:
In [6]:
Warning : 2D array multiplication is not matrix multiplication:
Out[1]: array([2, 3, 4, 5])
Out[2]: array([ 0.5, 1. , 1.5, 2. ])
Out[3]: (array([ 2, 4, 8, 16]), array([ 1, 4, 9, 16]))
Out[4]: array([-1., 0., 1., 2.])
Out[5]: array([^ 2.,^ 4.,^ 6.,^ 8.])
Out[6]: array([ 2, 3, 6, 13, 28])
# sum of a scalar import numpy as np a = np.array([1, 2, 3, 4]) a + 1
# division by a scalar a / 2
# exponentiation 2 ****** a, a ****** 2
# difference of 2 arrays b = np.ones(4) + 1 # b = array([2.,2.,2.,2.]) a - b
# multiplication of 2 arrays a ***** b
# a more complex operation j = np.arange(5) 2 ****** (j + 1) - j
In [7]:
Matrix multiplication:
In [8]:
Comparisons:
In [9]:
In [10]:
Logical operations:
In [11]:
In [12]:
Transcendental functions:
Out[7]: array([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]])
Out[8]: array([[ 3., 3., 3.], [ 3., 3., 3.], [ 3., 3., 3.]])
Out[9]: array([False, True, False, True], dtype=bool)
Out[10]: array([False, False, True, False], dtype=bool)
Out[11]: array([ True, True, True, False], dtype=bool)
Out[12]: array([ True, False, False, False], dtype=bool)
c = np.ones((3, 3)) c ***** c # NOT matrix multiplication!
c.dot(c)
a = np.array([1, 2, 3, 4]) b = np.array([4, 2, 2, 4]) a == b
a > b
# the truth value of a OR b element-wise a = np.array([1, 1, 0, 0], dtype=bool) b = np.array([1, 0, 1, 0], dtype=bool) np.logical_or(a, b)
# the truth value of a AND b element-wise np.logical_and(a, b)
In [21]:
In [22]:
In [23]:
Can be used for array comparisons:
In [24]:
In [25]:
In [26]:
Sorting along an axis:
In [27]:
Out[21]: 1
Out[22]: False
Out[23]: True
Out[24]: False
Out[25]: True
Out[26]: True
Out[27]: array([[4, 3, 5], [1, 2, 1]])
x.argmax() # index of maximum
np.all([ True , True , False ])
np.any([ True , True , False ])
a = np.zeros((100, 100)) np.any(a! = 0)
np.all(a == a)
a = np.array([1, 2, 3, 2]) b = np.array([2, 2, 3, 2]) c = np.array([6, 4, 4, 5]) ((a < = b) & (b < = c)).all()
a = np.array([[4, 3, 5], [1, 2, 1]]) a
In [28]:
Out[28]: array([[3, 4, 5], [1, 1, 2]])
# Sort inplace a.sort(axis=1) a