0% found this document useful (0 votes)
15 views10 pages

Zeta Numpy 2024

Numpy study material

Uploaded by

r3752842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Zeta Numpy 2024

Numpy study material

Uploaded by

r3752842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

8/12/24, 9:27 AM zeta_numpy.

ipynb - Colab

keyboard_arrow_down What is numpy?


NumPy stands for numerical python which is a python package List item

NumPy is an open source library available in Python, which helps in mathematical, scientific, engineering, and data science programming.

It works perfectly for multi-dimensional arrays , matrix multiplication and reshaping.

Installation of NumPy

pip install numpy

Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (1.25.2)

NumPy as np

NumPy is usually imported under the np alias.

Now the NumPy package can be referred to as np instead of numpy.

import numpy as np

Checking NumPy Version

import numpy as np

print(np.__version__)

1.25.2

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 1/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab

keyboard_arrow_down Dimensions in Arrays

0-Dimentional array

import numpy as np

a= np.array(1000)

print(a)

1000

1-Dimensional Arrays

import numpy as np

a = np.array([10, 20, 30, 40, 50])

print(a)
print(a[0])
print(id(a[0]))

[10 20 30 40 50]
10
140120889803472

2-Dimensional Arrays

import numpy as np
a = np.array([[10, 20], [30, 40], [50, 60]])
print(a)

[[10 20]
[30 40]
[50 60]]

3-Dimensional Arrays

import numpy as np

a = np.array([[[10, 20, 30], [40, 50, 60]], [[10, 20, 30], [40, 50, 60]]])

print(a)

[[[10 20 30]
[40 50 60]]

[[10 20 30]
[40 50 60]]]

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 2/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab

Check Number of Dimensions?

import numpy as np

a = np.array(1000)
b = np.array([10, 20, 30, 40, 50])
c = np.array([[10, 20, 30], [40, 50, 60]])
d = np.array([[[10, 20, 30], [40, 50, 60]], [[10, 20, 30], [40, 50, 60]]])

print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)

0
1
2
3

Higher Dimensional Arrays

you can define the number of dimensions by using the ndmin argument.

import numpy as np

a = np.array([1, 2, 3, 4], ndmin=2)


b = np.array([1, 2, 3, 4], ndmin=32)
print(a)
print(b)
print('number of dimensions :', a.ndim)
print('number of dimensions :', b.ndim)

[[1 2 3 4]]
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[1 2 3 4]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
number of dimensions : 2
number of dimensions : 32

Indexing in numpy array

import numpy as np

a = np.array([10, 20, 30, 40])

print(a[0])
print(a[1])
print(a[2] + a[3])
print(a[2] + a[3] * a[2])

10
20
70
1230

import numpy as np

a= np.array([[10,20,30,40,50], [60,70,80,90,100]])

print('1st element on 1st row: ', a[0,4])


print('5th element on 1st row: ', a[1,0])
print('3rd element on 2nd row: ', a[1, 2])
print('5th element on 2st row: ', a[1, 4])

1st element on 1st row: 50


5th element on 1st row: 60
3rd element on 2nd row: 80
5th element on 2st row: 100

import numpy as np

a = np.array([[[10, 20, 30], [40, 50, 60]], [[70, 80, 90], [100, 110, 120]]])

print(a[0, 0, 0])
print(a[1, 1, 1])

print(a[0, 1, 2])

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 3/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab

10
110
60

Negative Indexing

import numpy as np

a = np.array([[10,20,30,40,50], [60,70,80,90,100]])

print('Last element from 2nd dim: ', a[1, -1])

Last element from 2nd dim: 100

Start coding or generate with AI.

keyboard_arrow_down Slicing arrays


We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

import numpy as np

a = np.array([10, 20, 30, 40, 50, 60, 70])

print(a[1:5])
print(a[:4])
print(a[4:])

[20 30 40 50]
[10 20 30 40]
[50 60 70]

keyboard_arrow_down NUMPY FUNCTIONS


Shape of an Array

import numpy as np

a = np.array([[10, 20, 30, 40],


[50, 60, 70, 80]])

print(a.shape)

(2, 4)

import numpy as np

a = np.array([10, 20, 30, 40], ndmin=5)

print(a)
print('shape of array :', a.shape)

[[[[[10 20 30 40]]]]]
shape of array : (1, 1, 1, 1, 4)

Reshape From 1-Dimensional Array to 2-Dimensional Array

import numpy as np

a = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120])

b = a.reshape(4, 3)

print(b)

[[ 10 20 30]
[ 40 50 60]
[ 70 80 90]

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 4/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab
[100 110 120]]

numpy.concatenate() or Joining Arrays Using Stack Functions

import numpy as np
a=np.array([[10,20],[30,40]])
b=np.array([[50,60]])
c=np.concatenate((b,a))
print(c)

[[50 60]
[10 20]
[30 40]]

HORIZONTAL STACKING: hstack() to stack along rows.

import numpy as np

a = np.array([10, 20, 30])

b = np.array([40, 50, 60])


b = np.array([70, 80, 90])

d = np.hstack((a,b,c))

print(d)

[10 20 30 70 80 90 40 50 60 10 20 30]

VERTICAL STACKING: vstack() to stack along columns.

import numpy as np

a = np.array([10, 20, 30])

b = np.array([40, 50, 60])

c = np.vstack((a, b))

print(c)

[[10 20 30]
[40 50 60]]

array_split: Splitting breaks one array into multiple

import numpy as np

a = np.array([10, 20, 30, 40, 50, 60,70])

b = np.array_split(a, 3)

print(b)

[array([10, 20, 30]), array([40, 50]), array([60, 70])]

Searching Arrays: where()

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 5/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab
import numpy as np

a = np.array([10, 20, 30, 40, 50, 60,70])


b = np.where(a == 4)
c = np.where(a == 40)
print(b)
print(c)

(array([], dtype=int64),)
(array([3]),)

Sorting Arrays: sort()

import numpy as np

a = np.array([70, 30, 10, 40, 20, 60,50])


b = print(np.sort(a))

[10 20 30 40 50 60 70]

import numpy as np

a = np.array([True, False,True, True])

print(np.sort(a))

[False True True True]

average()

import numpy as np
a = list(range(1,6))
b =np.average(a)
print(a)
print(b)

[1, 2, 3, 4, 5]
3.0

import numpy as np
a=np.arange(12).reshape((4,3))
b = np.average(a)
print(a)
print()

print(b)

[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]

5.5

Numpy ceil()

import numpy as np

a= [0.99999999999, 0.000000000001, 1.20, 5.24, 9.99,145.23, 0.12, 12.34, 123]

print("Input array:",a)

b = np.ceil(a)

print("Output array:",b)

Input array: [0.99999999999, 1e-12, 1.2, 5.24, 9.99, 145.23, 0.12, 12.34, 123]
Output array: [ 1. 1. 2. 6. 10. 146. 1. 13. 123.]

Numpy fix()

This function is used to round the array values to the nearest integers towards zero.

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 6/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab
import numpy as np

a = [0.33, 0.08, 12.2, 18.42, 9.99, 20.35, 21.9999999]

print("Input array:",a)

b = np.fix(a)

print("Output array:",b)

Input array: [0.33, 0.08, 12.2, 18.42, 9.99, 20.35, 21.9999999]


Output array: [ 0. 0. 12. 18. 9. 20. 21.]

Numpy floor()

This function returns the floor value of the input array elements.

import numpy as np

a = [0.43, 0.08, 1.24, 41.24, 99.999999999999999]

print("Input array:",a)

b = np.floor(a)

print("Output array:",b)

Input array: [0.43, 0.08, 1.24, 41.24, 100.0]


Output array: [ 0. 0. 1. 41. 100.]

NumPy Array Manipulation Functions

import numpy as np

# create a 1D array
a = np.array([1, 3, 5, 7, 9, 11])

# reshape the 1D array into a 2D array


b = np.reshape(a, (2, 3))

# transpose the 2D array


c = np.transpose(b)

print("Original array:\n", a)
print("\nReshaped array:\n",b)
print("\nTransposed array:\n",c)

Original array:
[ 1 3 5 7 9 11]

Reshaped array:
[[ 1 3 5]
[ 7 9 11]]

Transposed array:
[[ 1 7]
[ 3 9]
[ 5 11]]

NumPy Array Mathematical Functions

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 7/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab
import numpy as np

# create two arrays


a = np.array([1, 2, 3, 4, 5])
b = np.array([4, 9, 16, 25, 36])

# add the two arrays element-wise


arr_sum = np.add(a, b)

# subtract the array2 from array1 element-wise


arr_diff = np.subtract(a, b)

# compute square root of array2 element-wise


arr_sqrt = np.sqrt(b)

print("\nSum of arrays:\n", arr_sum)


print("\nDifference of arrays:\n", arr_diff)
print("\nSquare root of first array:\n", arr_sqrt)

Sum of arrays:
[ 5 11 19 29 41]

Difference of arrays:
[ -3 -7 -13 -21 -31]

Square root of first array:


[2. 3. 4. 5. 6.]

NumPy Array Statistical Functions

import numpy as np

# create a numpy array


marks = np.array([76, 78, 81, 66, 85])

# compute the mean of marks


mean_marks = np.mean(marks)
print("Mean:",mean_marks)

# compute the median of marks


median_marks = np.median(marks)
print("Median:",median_marks)

# find the minimum and maximum marks


min_marks = np.min(marks)
print("Minimum marks:", min_marks)

max_marks = np.max(marks)
print("Maximum marks:", max_marks)

Mean: 77.2
Median: 78.0
Minimum marks: 66
Maximum marks: 85

Scalar multiplication

The simple form of matrix multiplication is called scalar multiplication

import numpy as np
a = 7
b = [[1,2],
[3,4]]
np.dot(a,b)

array([[ 7, 14],
[21, 28]])

Matrix Multiplications

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 8/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab

a = [[1,2],
[3,4]]
b = [[5,6],
[7,8]]
np.dot(a,b)

array([[19, 22],
[43, 50]])

a = [[1,2],
[3,4]]
b = [[5,6],
[7,8]]
np.dot(b,a)

array([[23, 34],
[31, 46]])

keyboard_arrow_down NumPy - String Functions


add()

import numpy as np
print('Concatenate two strings:')
print(np.char.add(['EAST'],['WEST']))

Concatenate two strings:


['EASTWEST']

import numpy as np
print('Concatenate two strings:')
print(np.char.add(['EAST'],['WEST'],['POLYTECHNIC']))

Concatenate two strings:


---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-4d515120e6bb> in <cell line: 3>()
1 import numpy as np
2 print('Concatenate two strings:')
----> 3 print(np.char.add(['EAST'],['WEST'],['POLYTECHNIC']))

TypeError: add() takes 2 positional arguments but 3 were given

Next steps: Explain error

multiply()

import numpy as np
print (np.char.multiply('Hello world ',300))

Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world

center()

import numpy as np
# np.char.center(arr, width,fillchar)
print (np.char.center('hello', 25,fillchar = '*'))

**********hello**********

capitalize()

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 9/10
8/12/24, 9:27 AM zeta_numpy.ipynb - Colab
import numpy as np
print (np.char.capitalize('hello world'))

Hello world

title()

import numpy as np
print (np.char.title('hello world'))

Hello World

lower() & upper()

import numpy as np
print (np.char.lower(['HELLO','WORLD']) )

['hello' 'world']

import numpy as np
print (np.char.upper(['hello','world']) )

['HELLO' 'WORLD']

numpy.char.encode() function

The numpy.char.encode() is used to encode the elements of a string array using a specified encoding.

numpy.char.decode() function

The numpy.char.decode() is used to decode the elements of a byte array into strings using a specified encoding.

import numpy as np

a = np.char.encode('hello', 'cp500')
print(a)
print (np.char.decode(a,'cp500'))

b'\x88\x85\x93\x93\x96'
hello

https://colab.research.google.com/drive/155N1MYCLdV-SqqPW3I4wuSn2eCcPFVEC#scrollTo=zq3FWQHl5srS&printMode=true 10/10

You might also like