Follow

Follow

Â·Aug 28, 2021Â·

# Numpy Basics for data science

NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices.

### Import Numpy

``````import numpy as np
``````
``````list=[1,2,3,4,5]
print(list)
``````

Output:

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

### Create Array

``````arr=np.array(list)
print(arr)
``````

Output:

``````[1 2 3 4 5]
``````

Type of Array:

``````type(arr)
print(arr.shape)
``````

Output:

``````(5,)
``````
``````l1=[1,2,3,4,5]
l2=[2,3,4,5,6]
l3=[4,5,6,7,8]

arr=np.array([l1,l2,l3])
``````

Array Type and Shape

``````print(arr,"\n",type(arr),"\n",arr.shape)
``````

Output:

``````[[1 2 3 4 5]
[2 3 4 5 6]
[4 5 6 7 8]]
<class 'numpy.ndarray'>
(3, 5)
``````

### Reshape

``````arr.reshape(15,1)
``````

Output:

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

### Indexing

``````l1=[1,2,3,4,5]
l2=[2,3,4,5,6]
l3=[4,5,6,7,8]

arr=np.array([l1,l2,l3])
``````

### Slicing

``````arr[:,:] #all row and column
arr[0:2,0:2]
``````

Output:

``````array([[1, 2],
[2, 3]])
``````

In:

``````#pick 4,5,6,7 from second and third row
arr[1:,2:4]
``````

Output:

``````array([[4, 5],
[6, 7]])
``````

In:

``````#pick 3,4,5 from seconnd row
arr[1,1:4]
``````

Output:

``````array([3, 4, 5])
``````

### Generate Array

``````arr=np.arange(0,10,2) #genrte numbers basically third parametr is step basically
print(arr)
``````

Output:

``````[0 2 4 6 8]
``````

Multi dimen array : In:

``````arr=np.linspace(1,10,100) # third paramter how many values u want
print(arr)
``````

Output:

``````[ 1.          1.09090909  1.18181818  1.27272727  1.36363636  1.45454545
1.54545455  1.63636364  1.72727273  1.81818182  1.90909091  2.
2.09090909  2.18181818  2.27272727  2.36363636  2.45454545  2.54545455
2.63636364  2.72727273  2.81818182  2.90909091  3.          3.09090909
3.18181818  3.27272727  3.36363636  3.45454545  3.54545455  3.63636364
3.72727273  3.81818182  3.90909091  4.          4.09090909  4.18181818
4.27272727  4.36363636  4.45454545  4.54545455  4.63636364  4.72727273
4.81818182  4.90909091  5.          5.09090909  5.18181818  5.27272727
5.36363636  5.45454545  5.54545455  5.63636364  5.72727273  5.81818182
5.90909091  6.          6.09090909  6.18181818  6.27272727  6.36363636
6.45454545  6.54545455  6.63636364  6.72727273  6.81818182  6.90909091
7.          7.09090909  7.18181818  7.27272727  7.36363636  7.45454545
7.54545455  7.63636364  7.72727273  7.81818182  7.90909091  8.
8.09090909  8.18181818  8.27272727  8.36363636  8.45454545  8.54545455
8.63636364  8.72727273  8.81818182  8.90909091  9.          9.09090909
9.18181818  9.27272727  9.36363636  9.45454545  9.54545455  9.63636364
9.72727273  9.81818182  9.90909091 10.        ]
``````

``````#copy and brodcasting
arr=np.array([1,2,3,4,5])
arr1=arr.copy()#create copy of it rather than sharing same memroy location
arr1[2:]=100
print("arr=",arr)
print("arr1=",arr1)
``````

Output:

``````arr= [1 2 3 4 5]
arr1= [  1   2 100 100 100]
``````

In:

``````#some conditions use in practice
print(arr)

print(arr<2)
print(arr*2)
print(arr[arr<5]) # here u passing condition inside so it returns value
``````

Output:

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

### Arrays of one

``````#array of 1's

print(np.ones(4,dtype=int))
print(np.ones((4,2),dtype=int))
``````

Output:

``````[1 1 1 1]
[[1 1]
[1 1]
[1 1]
[1 1]]
``````

### Random values

``````#random values

np.random.randint(0,100,8).reshape(2,4)
``````

Output:

``````array([[63, 23, 77,  9],
[94, 11, 30, 77]])
``````