Numpy-
- – Numpy means numerical python.
- – High performance linear algebra and matrix related library.
- – It is written in Python.
- – Majorly used in 1D and multidirectional data manipulation.
We Can Install Numpy Library By Two Ways-
- 1) pip install numpy
- 2) conda install numpy
import numpy as np
list1 = [1,2,3]
np.array(list1)
Output-array([1, 2, 3])
np.array([4,5,6])
Output-array([4,5,6])
np.array([list1, list1])
Output-array([[1, 2, 3],
[1, 2, 3]])
# To create an array of element 'one' having row=3 and column=4
np.ones(shape=(3,4)) #touple will be used to define the array's row and column values
Output-array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
7*np.ones(shape=(3,4))
Output-
array([[7., 7., 7., 7.],
[7., 7., 7., 7.],
[7., 7., 7., 7.]])
# To create an array of element zero having row=3 and column=4
np.zeros(shape=(3,4)) #touple will be used to define the array's row and column values
Output-array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
# start, stop, step
np.arange(7)
Output-array([0, 1, 2, 3, 4, 5, 6])
np.arange(3,7)
Output-array([3, 4, 5, 6])
np.arange(10,1,-2)
Output-array([10, 8, 6, 4, 2])
#How to generate an array of random no.
np.random.randn(2,3)
Output-array([[-1.43932592, 0.03042645, 0.50291257],
[ 0.33814608, 1.54829719, 0.82410219]])
#How to generate an integer type random no.
np.random.randint(1,7) #(start_range, end_range, no_of_times_frequency)
Output-3
np.random.randint(1,7,3)
Output-array([4, 6, 6])
my_array = np.random.randn(3,4)
my_array
Output-array([[ 0.89226386, -0.73460617, 0.75015457, -0.66980253],
[ 1.77165398, 0.73811318, 1.31317468, -1.1242388 ],
[-0.63671896, -0.76357297, -1.34281154, 0.19971696]])
my_array.size
12
#We can change its shape but size should be same as previous one.
my_array.reshape(2,6) # This is allowed as size is same as 12.
array([[ 0.89226386, -0.73460617, 0.75015457, -0.66980253, 1.77165398,
0.73811318],
[ 1.31317468, -1.1242388 , -0.63671896, -0.76357297, -1.34281154,
0.19971696]])
my_array.reshape(3,6)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
----> 1 my_array.reshape(3,6)
ValueError: cannot reshape array of size 12 into shape (3,6)
###########################################################################
arr = np.arange(1,10)
arr
Output-array([1, 2, 3, 4, 5, 6, 7, 8, 9])
a[1] #Array will start from index 0
2
arr[star_index : end_index]
a[2:7]
array([3, 4, 5, 6, 7])
a[2:] # We didn't provide end index so it will print till end of the array
array([3, 4, 5, 6, 7, 8, 9])
#We didn't provide start index so it will print from begining of the array till the index provided by us.
a[:7]
array([1, 2, 3, 4, 5, 6, 7])
# There is no start and end index provided so it will print from 0 index to end of the array
a[:]
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
a[-1] # This is slicing so index -1 start from end
9
a[-2]
8