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

Numpy

NumPy is a Python library used for working with multi-dimensional arrays and matrices. It allows fast operations on arrays and matrices including mathematical, logical, shape manipulation, and other operations. NumPy arrays have advantages over regular Python lists including faster operations and the ability to perform element-wise operations. NumPy provides functions to create arrays filled with zeros, ones, a constant value, random values, and evenly spaced values. Arrays can also be transposed and manipulated.

Uploaded by

lokesh k
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

NumPy is a Python library used for working with multi-dimensional arrays and matrices. It allows fast operations on arrays and matrices including mathematical, logical, shape manipulation, and other operations. NumPy arrays have advantages over regular Python lists including faster operations and the ability to perform element-wise operations. NumPy provides functions to create arrays filled with zeros, ones, a constant value, random values, and evenly spaced values. Arrays can also be transposed and manipulated.

Uploaded by

lokesh k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

b.

shape
NumPy - Numerical Python
(2, 4)

Advantages of Numpy Arrays:


c = np.array([(1,2,3,4),(5,6,7,8)],dtype=float)
1. Allows several Mathematical Operations print(c)
2. Faster operations
[[1. 2. 3. 4.]
[5. 6. 7. 8.]]

import numpy as np
Initial Placeholders in numpy arrays
List vs Numpy - Time Taken
# create a numpy array of Zeros
x = np.zeros((4,5))
from time import process_time print(x)

[[0. 0. 0. 0. 0.]
Time taken by a list [0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
python_list = [i for i in range(10000)]

start_time = process_time() # create a numpy array of ones


y = np.ones((3,3))
python_list = [i+5 for i in python_list] print(y)

end_time = process_time() [[1. 1. 1.]


[1. 1. 1.]
print(end_time - start_time) [1. 1. 1.]]

0.0017722080000002194
# array of a particular value
z = np.full((5,4),5)
np_array = np.array([i for i in range(10000)]) print(z)

start_time = process_time() [[5 5 5 5]


[5 5 5 5]
[5 5 5 5]
np_array += 5
[5 5 5 5]
[5 5 5 5]]
end_time = process_time()

print(end_time - start_time) # create an identity matrix


a = np.eye(5)
0.0006466729999994314 print(a)

[[1. 0. 0. 0. 0.]
Numpy Arrays [0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
# list [0. 0. 0. 0. 1.]]
list1 = [1,2,3,4,5]
print(list1)
type(list1) # create a numpy array with random values
b = np.random.random((3,4))
[1, 2, 3, 4, 5] print(b)
list
[[0.06180442 0.35700298 0.90695408 0.03091635]
[0.58655837 0.66048197 0.29741526 0.89522409]
np_array = np.array([1,2,3,4,5]) [0.75529751 0.8197227 0.89382064 0.85936446]]
print(np_array)
type(np_array)
# random integer values array within a specific range
[1 2 3 4 5] c = np.random.randint(10,100,(3,5))
numpy.ndarray print(c)

[[24 82 43 91 73]
# creating a 1 dim array [98 11 52 38 38]
a = np.array([1,2,3,4]) [13 68 44 21 11]]
print(a)

[1 2 3 4] # array of evenly spaced values --> specifying the number of values required
d = np.linspace(10,30,5)
print(d)
a.shape
[10. 15. 20. 25. 30.]
(4,)

# array of evenly spaced values --> specifying the step


b = np.array([(1,2,3,4),(5,6,7,8)]) e = np.arange(10,30,5)
print(b) print(e)

[[1 2 3 4] [10 15 20 25]


[5 6 7 8]]
# convert a list to a numpy array [[0.46666667 0.2 0. ]
list2 = [10,20,20,20,50] [0.44444444 0.57142857 0.42857143]
[0.41666667 0.53846154 0. ]]
np_array = np.asarray(list2)
print(np_array)
a = np.random.randint(0,10,(3,3))
type(np_array)
b = np.random.randint(10,20,(3,3))

account_circle [10 20 20 20 50]


numpy.ndarray print(a)
print(b)

Analysing a numpy array [[4 7 7]


[3 9 9]
[4 9 7]]
c = np.random.randint(10,90,(5,5)) [[11 12 10]
print(c) [19 14 16]
[10 19 15]]
[[66 80 57 82 62]
[61 81 62 75 65]
[24 43 42 35 11] print(np.add(a,b))
[64 70 50 80 45] print(np.subtract(a,b))
[70 23 53 80 23]] print(np.multiply(a,b))
print(np.divide(a,b))
# array dimension
[[15 19 17]
print(c.shape)
[22 23 25]
[14 28 22]]
(5, 5) [[ -7 -5 -3]
[-16 -5 -7]
[ -6 -10 -8]]
# number of dimensions
[[ 44 84 70]
print(c.ndim) [ 57 126 144]
[ 40 171 105]]
2 [[0.36363636 0.58333333 0.7 ]
[0.15789474 0.64285714 0.5625 ]
[0.4 0.47368421 0.46666667]]
# number of elements in an array
print(c.size)
Array Manipulation
25

array = np.random.randint(0,10,(2,3))
# checking the data type of the values in the array
print(array)
print(c.dtype)
print(array.shape)
int64
[[1 2 2]
[8 0 7]]
(2, 3)
Mathematical operations on a np array

# transpose
list1 = [1,2,3,4,5] trans = np.transpose(array)
list2 = [6,7,8,9,10] print(trans)
print(trans.shape)
print(list1 + list2) # concatenate or joins two list
[[1 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [2 0]
[2 7]]
(3, 2)
a = np.random.randint(0,10,(3,3))
b = np.random.randint(10,20,(3,3))
array = np.random.randint(0,10,(2,3))
print(array)
print(a)
print(array.shape)
print(b)
[[8 1 0]
[[7 2 0] [2 5 9]]
[8 8 6] (2, 3)
[5 7 0]]
[[15 10 13]
[18 14 14] trans2 = array.T
[12 13 10]] print(trans2)
print(trans2.shape)
print(a+b)
[[8 2]
print(a-b)
[1 5]
print(a*b)
[0 9]]
print(a/b) (3, 2)

[[22 12 13]
[26 22 20] # reshaping a array
[17 20 10]] a = np.random.randint(0,10,(2,3))
[[ -8 -8 -13] print(a)
[-10 -6 -8]
print(a.shape)
[ -7 -6 -10]]
[[105 20 0]
[[4 3 7]
[144 112 84]
[4 6 6]]
[ 60 91 0]]
(2, 3)

b = a.reshape(3,2)
print(b)
print(b.shape)

[[4 3]
[7 4]
[6 6]]
(3, 2)

You might also like