0% found this document useful (0 votes)
15 views

Numpy Basics Part 1

The document demonstrates how to use various NumPy functions and operations to create, manipulate, and analyze multi-dimensional arrays. It shows how to create arrays, slice arrays, apply mathematical operations element-wise, reshape and flatten arrays, and iterate through arrays of different dimensions.

Uploaded by

Chanchal jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Numpy Basics Part 1

The document demonstrates how to use various NumPy functions and operations to create, manipulate, and analyze multi-dimensional arrays. It shows how to create arrays, slice arrays, apply mathematical operations element-wise, reshape and flatten arrays, and iterate through arrays of different dimensions.

Uploaded by

Chanchal jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Numpy_Codemy.

ipynb - Colab 10/05/24, 6:35 PM

import numpy as np

np1 = np.array([0, 1, 4, 5, 2, 6, 7, 9])


np1 = np.array([0, 1, 4, 5, 2, 6, 7, 9])
print(np1)
print(np1)

[0 1 4 5 2 6 7 9]

np1.shape

(8,)

np2 = np.arange(10)
np2 = np.arange(10)
print(np2)
print(np2)

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

np2.shape

(10,)

np3 = np.arange(0, 10, 2)


np3 = np.arange(0, 10, 2)
print(np3)
print(np3)

[0 2 4 6 8]

np3.shape

 (5,)

np4 = np.zeros(10)
np4 = np.zeros(10)
np4.shape
np4.shape

(10,)

print(np4)

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 1 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

np5 = np.zeros((2, 10))


print(np5)

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

np6 = np.full((2, 10), 0)


print(np6)

[[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]]

my_list = [1, 2, 3, 4, 5]
np7 = np.array(my_list)
print(np7)

[1 2 3 4 5]

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

print(np1[1:5])

[2 3 4 5]

print(np1[3:])

[4 5 6 7 8 9]

print(np1[-3:-1])

[7 8]

print(np1[1:5:2])

[2 4]

print(np1[::2])
print(np1[::3])

[1 3 5 7 9]
[1 4 7]

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 2 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

np2 = np.array([[1,2,3,4,5],[6,7,8,9,10]])

print(np2)

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

print(np2[1,2])

# Slicing a 2-d array

print(np2[0:1,1:3])

[[2 3]]

# Slice from both rows

print(np2[0:2, 1:3])

[[2 3]
[7 8]]

np1 = np.array([-3,-2,-1,0,1,2,3,4,5,6,7,8,9])
print(np1)

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

# Square root of each element

print(np.sqrt(np1))

[ nan nan nan 0. 1. 1.41421356


1.73205081 2. 2.23606798 2.44948974 2.64575131 2.82842712
3. ]
<ipython-input-34-f3f63cf39300>:3: RuntimeWarning: invalid value encountered i
print(np.sqrt(np1))

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 3 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

# Absolute Value

print(np.absolute(np1))

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

print(np.exp(np1))

[4.97870684e-02 1.35335283e-01 3.67879441e-01 1.00000000e+00


2.71828183e+00 7.38905610e+00 2.00855369e+01 5.45981500e+01
1.48413159e+02 4.03428793e+02 1.09663316e+03 2.98095799e+03
8.10308393e+03]

print(np.max(np1))

print(np.min(np1))

-3

print(np.sign(np1))

[-1 -1 -1 0 1 1 1 1 1 1 1 1 1]

print(np.sin(np1))

[-0.14112001 -0.90929743 -0.84147098 0. 0.84147098 0.90929743


0.14112001 -0.7568025 -0.95892427 -0.2794155 0.6569866 0.98935825
0.41211849]

print(np.log(np1))

[ nan nan nan -inf 0. 0.69314718


1.09861229 1.38629436 1.60943791 1.79175947 1.94591015 2.07944154
2.19722458]
<ipython-input-42-08c2794a484a>:1: RuntimeWarning: divide by zero encountered
print(np.log(np1))
<ipython-input-42-08c2794a484a>:1: RuntimeWarning: invalid value encountered i
print(np.log(np1))

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 4 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

print(np.cos(np1))

[-0.9899925 -0.41614684 0.54030231 1. 0.54030231 -0.41614684


-0.9899925 -0.65364362 0.28366219 0.96017029 0.75390225 -0.14550003
-0.91113026]

# Copy vs View

np_v1 = np.array([0, 1, 2, 3, 4, 5])

# Create a View

np_v2 = np_v1.view()

print(f'Original NP1 {np_v1}')


print(f'Original NP2 {np_v2}')

Original NP1 [0 1 2 3 4 5]
Original NP2 [0 1 2 3 4 5]

np_v1[0] = 41

print(f'Changed NP1 {np_v1}')


print(f'Original NP2 {np_v2}')

Changed NP1 [41 1 2 3 4 5]


Original NP2 [41 1 2 3 4 5]

np_v2[0] = 42

print(f'Changed NP1 {np_v1}')


print(f'Original NP2 {np_v2}')

Changed NP1 [42 1 2 3 4 5]


Original NP2 [42 1 2 3 4 5]

np_v3 = np.array([0, 1, 2, 3, 4, 5])

# Create a Copy

np_v4 = np_v3.copy()

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 5 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

print(f'Original NP1 {np_v3}')


print(f'Original NP2 {np_v4}')

Original NP1 [0 1 2 3 4 5]
Original NP2 [0 1 2 3 4 5]

np_v3[0] = 41

print(f'Changed NP1 {np_v3}')


print(f'Original NP2 {np_v4}')

Changed NP1 [41 1 2 3 4 5]


Original NP2 [0 1 2 3 4 5]

np_v4[0] = 42

print(f'Changed NP1 {np_v3}')


print(f'Original NP2 {np_v4}')

Changed NP1 [41 1 2 3 4 5]


Original NP2 [42 1 2 3 4 5]

# Create 1-D Numpy Array and Get Shape

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


print(np1)
print(np1.shape)

[ 1 2 3 4 5 6 7 8 9 10 11 12]
(12,)

# Create 2-D Array and Get Shape (Rows/Columns)

np2 = np.array([[1,2,3,4,5,6], [7,8,9,10,11,12]])

print(np2)
print(np2.shape)

[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
(2, 6)

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 6 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

# Reshape 2-D

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


np3 = np1.reshape(3,4)
print(np3)
print(np3.shape)

[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
(3, 4)

# Reshape 3-D

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


np4 = np1.reshape(2,3,2)
print(np4)
print(np4.shape)

[[[ 1 2]
[ 3 4]
[ 5 6]]

[[ 7 8]
[ 9 10]
[11 12]]]
(2, 3, 2)

# Reshape 3-D

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


np4 = np1.reshape(3,2,2)
print(np4)
print(np4.shape)

[[[ 1 2]
[ 3 4]]

[[ 5 6]
[ 7 8]]

[[ 9 10]
[11 12]]]
(3, 2, 2)

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 7 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

# Flatten to 1-D

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


np4 = np1.reshape(-1)
print(np4)
print(np4.shape)

[ 1 2 3 4 5 6 7 8 9 10 11 12]
(12,)

# 1-D Array

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])


for x in np1:
print(x)

1
2
3
4
5
6
7
8
9
10

# 2-D Array

np2 = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])


for x in np2:
print(x)

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

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 8 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

# 2-D Array

np2 = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])


for x in np2:
for y in x:
print(y)

1
2
3
4
5
6
7
8
9
10

# 2-D Array

np2 = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])


for x in np.nditer(np2):
print(x)

1
2
3
4
5
6
7
8
9
10

# 3-D Array

np3 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9],[10, 11, 12]]])


for x in np3:
print(x)

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

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 9 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

# 3-D Array

np3 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9],[10, 11, 12]]])


for x in np3:
for y in x:
for z in y:
print(z)

1
2
3
4
5
6
7
8
9
10
11
12

# 3-D Array
# numpy nditer

np3 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9],[10, 11, 12]]])

for x in np.nditer(np3):
print(x)

1
2
3
4
5
6
7
8
9
10
11
12

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 10 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

# Sorting

np1 = np.array([6,7,4,9,0,2,1])
print(np1)
print(np.sort(np1))

[6 7 4 9 0 2 1]
[0 1 2 4 6 7 9]

np2 = np.array(["John", "Tina", "Aaron", "Zed"])


print(np2)
print(np.sort(np2))

['John' 'Tina' 'Aaron' 'Zed']


['Aaron' 'John' 'Tina' 'Zed']

# Boolean T/F (1 - True) (0 - False)

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


print(np3)
print(np.sort(np3))

[ True False False True]


[False False True True]

# Sort 2-D Array

np4 = np.array([[6, 7, 1, 9], [8, 3, 5, 0]])


print(np4)
print(np.sort(np4))

[[6 7 1 9]
[8 3 5 0]]
[[1 6 7 9]
[0 3 5 8]]

# Searching through numpy arrays

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3])

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 11 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

x = np.where(np1 == 3)

print(np1)
print(x[0])
print(np1[x[0]])

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

# Return even items

y = np.where(np1 % 2 == 0)

print(np1)
print(y)

print(np1[y])

[ 1 2 3 4 5 6 7 8 9 10 3]
(array([1, 3, 5, 7, 9]),)
[ 2 4 6 8 10]

# Return odd items

z = np.where(np1 % 2 != 0)

print(np1)
print(z)

print(np1[z])

[ 1 2 3 4 5 6 7 8 9 10 3]
(array([ 0, 2, 4, 6, 8, 10]),)
[1 3 5 7 9 3]

# Filtering numpy arrays with Boolean index lists

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

x = [True, True, False, False, False, False, False, False, False, False]

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 12 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

print(np1)
print(np1[x])

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

filtered = []

for thing in np1:


if thing % 2 == 0:
print(filtered.append(True))
else:
print(filtered.append(False))

None
None
None
None
None
None
None
None
None
None

print(np1)
print(filtered)
print(np1[filtered])

[ 1 2 3 4 5 6 7 8 9 10]
[False, True, False, True, False, True, False, True, False, True]
[ 2 4 6 8 10]

# Shortcut

np1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

filtered = np1 % 2 == 0

print(np1)
print(filtered)
print(np1[filtered])

[ 1 2 3 4 5 6 7 8 9 10]
[False True False True False True False True False True]
[ 2 4 6 8 10]

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 13 of 14
Numpy_Codemy.ipynb - Colab 10/05/24, 6:35 PM

Start coding or generate with AI.

https://colab.research.google.com/drive/1YjlbHycCIPBBHUFXyLl5GOQ8bzbUSNdm Page 14 of 14

You might also like