2.4. NumPy Operations

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 49

UNIVERSITY INSTITUTE OF COMPUTING

MASTERS OF COMPUTER APPLICATIONS


PYTHON PROGRAMMING
21CAH-645/635/604

DISCOVER . LEARN . EMPOWER


1
2.2.3. NumPy Operations
• More speed: NumPy uses algorithms written in C that complete in
nanoseconds rather than seconds.
• Fewer loops: NumPy helps you to reduce loops and keep from getting
tangled up in iteration indices.
• Clearer code: Without loops, your code will look more like the
equations you’re trying to calculate.
• Better quality: There are thousands of contributors working to keep
NumPy fast, friendly, and bug. free.
Because of these benefits, NumPy is the de-facto standard for
multidimensional arrays in Python data analytics, and many of the
most popular libraries are built on top of it.
2
Scalar
• Example 1
import numpy as np
a = np.array([1, 2, 3, 4])
a=a+1
print (a)
output: [2 3 4 5]

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

first_array = np.array([1, 3, 5, 7])


second_array = np.array([2, 4, 6, 8])

# using the add() function


result2 = np.add(first_array, second_array)
print("Using the add() function:",result2)

20
Mathematical Functions
import numpy as np

first_array = np.array([3, 9, 27, 81])


second_array = np.array([2, 4, 8, 16])

# using the - operator


result1 = first_array - second_array
print("Using the - operator:",result1)

# using the subtract() function


result2 = np.subtract(first_array, second_array)
print("Using the subtract() function:",result2)
21
Mathematical Functions
import numpy as np

first_array = np.array([1, 3, 5, 7])


second_array = np.array([2, 4, 6, 8])

# using the * operator


result1 = first_array * second_array
print("Using the * operator:",result1)

# using the multiply() function


result2 = np.multiply(first_array, second_array)
print("Using the multiply() function:",result2)
22
Mathematical Functions
import numpy as np

first_array = np.array([1, 2, 3])


second_array = np.array([4, 5, 6])

# using the / operator


result1 = first_array / second_array
print("Using the / operator:",result1)

# using the divide() function


result2 = np.divide(first_array, second_array)
print("Using the divide() function:",result2)
23
Mathematical Functions
import numpy as np
array1 = np.array([1, 2, 3])
# using the ** operator
result1 = array1 ** 2
print("Using the ** operator:",result1)
# using the power() function
result2 = np.power(array1, 2)
print("Using the power() function:",result2)

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

numbers = np.array([1.23456, 2.34567, 3.45678, 4.56789])

# round the array to two decimal places


rounded_array = np.round(numbers, 2)

print(rounded_array)

27
Mathematical Functions
import numpy as np

array1 = np.array([1.23456, 2.34567, 3.45678, 4.56789])

print("Array after floor():", np.floor(array1))

print("Array after ceil():", np.ceil(array1))

28
Mathematical Functions
Trigonometric Functions

29
Mathematical Functions
import numpy as np

# array of angles in radians


angles = np.array([0, 1, 2])
print("Angles:", angles)
# compute the sine of the angles
sine_values = np.sin(angles)
print("Sine values:", sine_values)

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

• Size of the array can be changed with ndarray.resize


• Example 1
import numpy as np
a = np.arange(4)
a.resize((8,))
print (a)
output: [0 1 2 3 0 0 0 0]

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

• In this chapter we have seen some basic operations /manipulations


on N-dimensional NumPy array.

48
THANK YOU

18/10/2024 49

You might also like