2.4. NumPy Operations
2.4. NumPy Operations
2.4. NumPy Operations
3
• Example 2
import numpy as np
a = np.array([1, 2, 3, 4])
a = 2 ** a
print (a)
4
numpy.ones() in Python
The numpy.ones() function returns a new array of given shape and
type, with ones.
Syntax: numpy.ones(shape, dtype = None, order = 'C’)
Creating one-dimensional array with ones
import numpy as np
array_1d = np.ones(3)
print(array_1d)
Output:
[1. 1. 1.]
5
numpy.ones() in Python
Creating Multi-dimensional array
import numpy as np
array_2d = np.ones((2, 3))
print(array_2d)
Output:
[[1. 1. 1.]
[1. 1. 1.]]
6
numpy.ones() in Python
NumPy ones array with int data type
import numpy as np
array_2d_int = np.ones((2, 3), dtype=int)
print(array_2d_int)
Output:
[[1 1 1]
[1 1 1]]
7
numpy.ones() in Python
The numpy.ones() function returns a new array of given shape and
type, with ones.
Syntax: numpy.ones(shape, dtype = None, order = 'C')
import numpy as np
a = np.array([1,2,3,4])
b = np.ones(4) + 1
print (a - b)
8
numpy.ones() in Python
• Example 2
import numpy as np
a = np.array([1,2,3,4])
b = np.ones(4) + 1
print (a * b)
9
numpy.ones() in Python
• Example 3
• import numpy as np
• j = np.arange(5)
• j = 2**(j + 1) - j
• print (j)
10
Array multiplication
• Example 1
import numpy as np
c = np.ones((3, 3))
print (c * c)
output:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
11
Matrix multiplication
• Example 1
import numpy as np
c = np.ones((3, 3))
print (c.dot(c))
12
Comparison Operations
• Example 1
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
print (a == b)
13
Comparison Operations
Example 2
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
print (a > b)
output:
14
Array Comparison
• Example 1
import numpy as np
a = np.array([1,2,3,4,5])
b = np.array ([1,2,3,4,5])
c = np.array ([1,2,3,4,5])
print (np.array_equal(a,b))
15
Array Comparison
• Example 2
import numpy as np
a = np.array([1,2,3,4,5])
b = np.array ([1,2,3,4,5])
c = np.array ([2,2,3,4,5])
print (np.array_equal(a,c))
16
Logical Operations
• Example 1
import numpy as np
a = np.array([1, 1, 0, 0], dtype=bool)
b = np.array([1, 0, 1, 0], dtype=bool)
print (np.logical_or(a, b))
17
Logical Operations
• Example 2
import numpy as np
a = np.array([1, 1, 0, 0], dtype=bool)
b = np.array([1, 0, 1, 0], dtype=bool)
print (np.logical_and(a, b))
18
Mathematical Functions
Arithmetic Functions
19
Mathematical Functions
import numpy as np
20
Mathematical Functions
import numpy as np
24
Mathematical Functions
import numpy as np
first_array = np.array([9, 10, 20])
second_array = np.array([2, 5, 7])
# using the % operator
result1 = first_array % second_array
print("Using the % operator:",result1)
# using the mod() function
result2 = np.mod(first_array, second_array)
print("Using the mod() function:",result2)
25
Mathematical Functions
Rounding Functions
26
Mathematical Functions
import numpy as np
print(rounded_array)
27
Mathematical Functions
import numpy as np
28
Mathematical Functions
Trigonometric Functions
29
Mathematical Functions
import numpy as np
Output
Angles: [0 1 2]
Sine values: [0. 0.84147098 0.90929743]
30
Computing Sums
• Example 1
import numpy as np
x = np.array([1, 2, 3, 4])
print (np.sum(x))
31
Sum by rows and columns
• Example 1
import numpy as np
x = np.array([[1, 1], [2, 2]])
print (x.sum(axis=0)) # column first axis
32
Computing Sums
import numpy as np
x = np.array([[1, 1], [2, 2]])
print (x.sum(axis=1)) # row second axis
33
Logical operations
• Example
import numpy as np
print (np.all([True, True, False]))
output: False
• Example
import numpy as np
print (np.any([True, True, False]))
output: True
34
Statistical operations
• Example 1
import numpy as np
import numpy as np
x = np.array([1, 2, 3, 1])
y = np.array([[1, 2, 3], [5, 6, 1]])
print (x.mean())
output: 1.75
35
Statistical operations
Example
import numpy as np
x = np.array([1, 2, 3, 1])
y = np.array([[1, 2, 3], [5, 6, 1]])
print (np.medain(x))
output: 1.5
36
Statistical operations
Example
import numpy as np
marks = np.array([76, 78, 81, 66, 85])
std_marks = np.std(marks)
print(std_marks)
# Output: 6.803568381206575
37
Statistical operations
Example
import numpy as np
marks = np.array([76, 78, 81, 66, 85])
std_marks = np.std(marks)
print(std_marks)
# Output: 6.803568381206575
38
Statistical operations
import numpy as np
array1 = np.array([2,6,9,15,17,22,65,1,62])
min_val = np.min(array1)
max_val = np.max(array1)
print("Minimum value:", min_val)
print("Maximum value:", max_val)
39
Statistics functions
• Flatening
Example 1
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print (a.ravel())
output: [1 2 3 4 5 6]
40
Reshaping
The inverse operation of flattening
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = a.ravel()
b = b.reshape((2, 3))
print (b)
output
[[1 2 3]
[4 5 6]]
41
Dimension shuffling
• Example 1
import numpy as np
a = np.arange(4*3*2).reshape(4, 3, 2)
print (a)
42
Resizing
43
Sorting along the axis:
• Example 1
import numpy as np
a = np.array([[4, 3, 5], [1, 2, 1]])
b = np.sort(a, axis=1)
print (b)
output:
[[3 4 5]
[1 1 2]]
44
In-place sort
• Example 1
import numpy as np
a = np.array([[4, 3, 5], [1, 2, 1]])
a.sort(axis=1)
print (a)
45
slice a 2D array (or matrix) using NumPy or a
nested list
•array[:, :2]: Slices all rows (:) and the first two columns (:2).
•array[:2, :]: Slices the first two rows (:2) and all columns (:).
•array[1, :2]: Slices the second row (1) and the first two columns (:2).
•array[1:, 1:]: Slices the last two rows (1:) and the last two columns (1:).
46
import numpy as np
# Create a 2D array (3x3 matrix)
array = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Slice all rows and first two columns
slice1 = array[:, :2]
# Slice first two rows and all columns
slice2 = array[:2, :]
# Slice second row and first two columns
slice3 = array[1, :2]
# Slice last two rows and last two columns
slice4 = array[1:, 1:]
# Print the slices
print("Slice 1 (all rows, first two columns):\n", slice1)
print("Slice 2 (first two rows, all columns):\n", slice2)
print("Slice 3 (second row, first two columns):\n", slice3)
print("Slice 4 (last two rows, last two columns):\n", slice4)
47
Summary
48
THANK YOU
18/10/2024 49