Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Numpy Array Operations: Elementwise, Comparisons, Logical, Transcendental, and Extrema, Study notes of Advanced Computer Programming

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

2021/2022

Uploaded on 09/12/2022

francyne
francyne 🇺🇸

4.7

(21)

268 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
numerical operations on numpy arrays
Elementwise operations
Basic operation with scalars:
In[1]:
In[2]:
In[3]:
Elementwise operations (2)
All arithmetic operates elementwise:
In[4]:
In[5]:
In[6]:
Elementwise operations (3)
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
pf3
pf4
pf5

Partial preview of the text

Download Numpy Array Operations: Elementwise, Comparisons, Logical, Transcendental, and Extrema and more Study notes Advanced Computer Programming in PDF only on Docsity!

numerical operations on numpy arrays

Elementwise operations

Basic operation with scalars:

In [1]:

In [2]:

In [3]:

Elementwise operations (2)

All arithmetic operates elementwise:

In [4]:

In [5]:

In [6]:

Elementwise operations (3)

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

Other elementwise operations

Comparisons:

In [9]:

In [10]:

Other elementwise operations (2)

Logical operations:

In [11]:

In [12]:

Other elementwise operations (3)

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

Logical operations:

In [22]:

In [23]:

Can be used for array comparisons:

In [24]:

In [25]:

Array comparisons: another example

In [26]:

Sorting data

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