NUMPY ARRAYS
BASIC OPERATIONS IN NUMPY
Jaume Boguñá
Dive into Python
What is NumPy?
NumPy is a Python library used for numerical and matrix computations.
Main Features
I. Fast Array Processing
NumPy arrays are more efficient than Python lists for large data.
II. Vectorized Operations
Perform element-wise operations without writing loops.
III. Multidimensional Arrays
Support for arrays of any dimension.
IV. Integration
Works with other libraries like Pandas, Matplotlib, and Scikit-learn.
import numpy as np
Jaume Boguñá
Dive into Python 2
Creating Arrays
There are several ways to create arrays in NumPy:
1. [Link]()
Purpose:
Converts input data (lists, tuples) into a NumPy array.
import numpy as np
ages = [16, 22, 39, 86]
array = [Link](ages)
[16 22 39 86]
Jaume Boguñá
Dive into Python 3
Creating Arrays
2. [Link]()
Purpose:
Creates an array filled with zeros.
Parameters:
`shape`: A tuple defining the shape of the array.
import numpy as np
zeros_array = [Link]((3,4))
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Jaume Boguñá
Dive into Python 4
Creating Arrays
3. [Link]()
Purpose:
Creates an array filled with ones.
Parameters:
`shape`: A tuple defining the shape of the array.
import numpy as np
ones_array = [Link]((3,4))
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
Jaume Boguñá
Dive into Python 5
Creating Arrays
4. [Link]()
Purpose:
Generates an array with values from start to stop (exclusive) with a step.
Parameters:
`start`: Starting value.
`stop`: Stopping value (excluded).
`step`: Step size (default is 1).
import numpy as np
arange_array = [Link](5, 25, 5)
[5 10 15 20]
Jaume Boguñá
Dive into Python 6
Creating Arrays
5. [Link]()
Purpose:
Creates an array of evenly spaced values between start and stop.
Parameters:
`start`: Starting value.
`stop`: Stopping value (included).
`num`: Number of values to generate.
import numpy as np
linspace_array = [Link](5, 25, 10)
[ 5. 7.22222222 9.44444444 11.66666667 13.88888889
16.11111111 18.33333333 20.55555556 22.77777778 25. ]
Jaume Boguñá
Dive into Python 7
Creating Arrays
Summary
[Link]() Converts lists/tuples to arrays.
[Link]() Creates zero-filled arrays.
[Link]() Creates one-filled arrays.
[Link]() Creates arrays with a range of values.
[Link]() Creates arrays with evenly spaced values.
Jaume Boguñá
Dive into Python 8
Array Attributes
1. shape
Purpose:
Returns the dimensions (size along each axis) of the array.
import numpy as np
array = [Link]([[9, 8, 7, -3], [-1, 2, 0, 5]])
print([Link])
(2, 4)
Jaume Boguñá
Dive into Python 9
Array Attributes
2. dtype
Purpose:
Returns the data type of the array elements.
array = [Link]([[9, 8, 7], [-1, 2, 0]])
print([Link])
int32
array = [Link](-8, 8, 16)
[-8. -6.93333333 -5.86666667 -4.8 -3.73333333 -
2.66666667 -1.6 -0.53333333 0.53333333 1.6
2.66666667 3.73333333 4.8 5.86666667 6.93333333 8. ]
print([Link])
float64
Jaume Boguñá
Dive into Python 10
Array Attributes
3. size
Purpose:
Returns the total number of elements in the array.
array = [Link]([[9, 8, 7], [-1, 2, 0]])
print([Link])
6
array = [Link](1,9,3)
[1 4 7]
print([Link])
3
Jaume Boguñá
Dive into Python 11
Array Attributes
4. ndim
Purpose:
Returns the number of dimensions (axes) of the array.
array = [Link]([[9, 8, 7], [-1, 2, 0]])
print([Link])
2
Jaume Boguñá
Dive into Python 12
Array Attributes
Summary
shape Dimensions of the array.
dtype Data type of array elements.
size Total number of elements.
ndim Number of dimensions.
Jaume Boguñá
Dive into Python 13
Basic Operations
Element-wise
1. Addition (+)
d = [Link]((2,3))
a = [Link]([1, 2, 3])
e = [Link]([[1,-1,1], [0,-2,-1]])
b = [Link]([4, 5, 6])
f = d + e
c = a + b
print(f)
print(c)
[[ 2. 0. 2.]
[5 7 9]
[ 1. -1. 0.]]
Jaume Boguñá
Dive into Python 14
Basic Operations
Element-wise
2. Subtraction (-)
d = [Link]((2,3))
a = [Link]([1, 2, 3])
e = [Link]([[1,-1,1], [0,-2,-1]])
b = [Link]([4, 5, 6])
f = d - e
c = a - b
print(f)
print(c)
[[0. 2. 0.]
[-3 -3 -3]
[1. 3. 2.]]
Jaume Boguñá
Dive into Python 15
Basic Operations
Element-wise
3. Multiplication (*)
d = [Link]((2,3))
a = [Link]([1, 2, 3])
e = [Link]([[1,-1,1], [0,-2,-1]])
b = [Link]([4, 5, 6])
f = d * e
c = a * b
print(f)
print(c)
[[ 1. -1. 1.]
[ 4 10 18]
[ 0. -2. -1.]]
Jaume Boguñá
Dive into Python 16
Basic Operations
Element-wise
4. Division (/)
d = [Link]((2,3))
a = [Link]([1, 2, 3])
e = [Link]([[1,-1,1], [0,-2,-1]])
b = [Link]([4, 5, 6])
f = d / e
c = a / b
print(f)
print(c)
[[ 1. -1. 1. ]
[0.25 0.4 0.5 ]
[ inf -0.5 -1. ]]
Jaume Boguñá
Dive into Python 17
Basic Operations
Element-wise
5. Exponentiation (**)
d = [Link]((2,3))
a = [Link]([1, 2, 3])
e = [Link]([[1,-1,1], [0,-2,-1]])
b = [Link]([4, 5, 6])
f = d ** e
c = a ** b
print(f)
print(c)
[[1. 1. 1.]
[ 1 32 729]
[1. 1. 1.]]
Jaume Boguñá
Dive into Python 18
Basic Operations
Element-wise
Summary
Addition +
Subtraction -
Multiplication *
Division /
Exponentiation **
Jaume Boguñá
Dive into Python 19
Basic Operations
Unary
1. Sum: [Link]()
x = [Link]([[-2, 3, 9], [Link](3)])
v = [Link]([-2, 3, 9]) [[-2. 3. 9.]
[ 1. 1. 1.]]
[Link](v)
10 [Link](x)
13.0
Jaume Boguñá
Dive into Python 20
Basic Operations
Unary
2. Minimum: [Link]()
x = [Link]([[-2, 3, 9], [Link](3)])
v = [Link]([-2, 3, 9]) [[-2. 3. 9.]
[ 1. 1. 1.]]
[Link](v)
-2 [Link](x)
-2.0
Jaume Boguñá
Dive into Python 21
Basic Operations
Unary
3. Maximum: [Link]()
x = [Link]([[-2, 3, 9], [Link](3)])
v = [Link]([-2, 3, 9]) [[-2. 3. 9.]
[ 1. 1. 1.]]
[Link](v)
9 [Link](x)
-9.0
Jaume Boguñá
Dive into Python 22
Basic Operations
Unary
4. Mean: [Link]()
x = [Link]([[-2, 3, 9], [Link](3)])
v = [Link]([-2, 3, 9]) [[-2. 3. 9.]
[ 1. 1. 1.]]
[Link](v)
3.3333333333333335 [Link](x)
2.1666666666666665
Jaume Boguñá
Dive into Python 23
Basic Operations
Unary
5. Standard Deviation: [Link]()
x = [Link]([[-2, 3, 9], [Link](3)])
v = [Link]([-2, 3, 9]) [[-2. 3. 9.]
[ 1. 1. 1.]]
[Link](v)
4.4969125210773475 [Link](x)
3.387066905483596
Jaume Boguñá
Dive into Python 24
Basic Operations
Unary
Summary
Sum [Link]()
Minimum [Link]()
Maximum [Link]()
Mean [Link]()
Standard
[Link]()
Deviation
Jaume Boguñá
Dive into Python 25
Advanced Manipulations
1. Reshaping Arrays: reshape()
Purpose:
Change the shape of an array without changing its data.
import random
array = [Link]([[Link](1,10) for _ in range(3)])
[2 1 4]
reshaped = [Link]((3,1))
[[2]
[1]
[4]]
Jaume Boguñá
Dive into Python 26
Advanced Manipulations
2. Transposing Arrays: T
Purpose:
Flip the dimensions of an array.
import random
array = [Link]([[[Link](1,10) for _ in range(3)],
[[Link](11,20) for _ in range(3)]])
[[ 5 3 8]
[13 20 12]]
transposed = array.T
[[ 5 13]
[ 3 20]
[ 8 12]]
Jaume Boguñá
Dive into Python 27
Advanced Manipulations
3. Concatenation: [Link]()
Purpose:
Combine multiple arrays into one.
a = [Link](1,12,4)
[1 5 9]
b = [Link](5,9,6)
[5. 5.8 6.6 7.4 8.2 9. ]
combined = [Link]((a,b))
[1. 5. 9. 5. 5.8 6.6 7.4 8.2 9. ]
Jaume Boguñá
Dive into Python 28
Advanced Manipulations
4. Splitting: [Link]()
Purpose:
Split an array into multiple sub-arrays.
array = [Link](8.0)
[0. 1. 2. 3. 4. 5. 6. 7.]
splitted = [Link](array, 2)
[array([0., 1., 2., 3.]), array([4., 5., 6., 7.])]
Jaume Boguñá
Dive into Python 29
Advanced Manipulations
Summary
reshape() Reshapes an array
T Transposes an array
[Link]() Merges arrays
[Link]() Divides an array
Jaume Boguñá
Dive into Python 30
Like Comment Share
Jaume Boguñá
Aerospace Engineer | Data Scientist