import numpy as np
lst1 = [1,2,3,4]

arr = np.array(lst1)
type(arr)
numpy.ndarray
arr
array([1, 2, 3, 4])
arr.shape
(4,)
list1=[1,2,3,4]
list2=[6,7,8,9]
list3=[3,4,5,6]

arr = np.array([list1,list2,list3])
arr
array([[1, 2, 3, 4],
       [6, 7, 8, 9],
       [3, 4, 5, 6]])
arr.shape
(3, 4)
arr.reshape(4,3)
array([[1, 2, 3],
       [4, 6, 7],
       [8, 9, 3],
       [4, 5, 6]])
arr.reshape(1,12)
array([[1, 2, 3, 4, 6, 7, 8, 9, 3, 4, 5, 6]])
arr.shape
(3, 4)

Indexing

arr
array([[1, 2, 3, 4],
       [6, 7, 8, 9],
       [3, 4, 5, 6]])
arr[0][1]
2
arr[1:,3:]
array([[9],
       [6]])
arr[1:,2:]
array([[8, 9],
       [5, 6]])
arr[:,2:]
array([[3, 4],
       [8, 9],
       [5, 6]])
arr[0:2,0:2] # always remember left:right is left exact and right is one value greater than actual one 
array([[1, 2],
       [6, 7]])

Inbuilt functions

arr = np.arange(0,10)
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr = np.arange(0,10,step=2)
arr
array([0, 2, 4, 6, 8])
# shift+tab for elaborate the function


np.linspace(1,10,50)
array([ 1.        ,  1.18367347,  1.36734694,  1.55102041,  1.73469388,
        1.91836735,  2.10204082,  2.28571429,  2.46938776,  2.65306122,
        2.83673469,  3.02040816,  3.20408163,  3.3877551 ,  3.57142857,
        3.75510204,  3.93877551,  4.12244898,  4.30612245,  4.48979592,
        4.67346939,  4.85714286,  5.04081633,  5.2244898 ,  5.40816327,
        5.59183673,  5.7755102 ,  5.95918367,  6.14285714,  6.32653061,
        6.51020408,  6.69387755,  6.87755102,  7.06122449,  7.24489796,
        7.42857143,  7.6122449 ,  7.79591837,  7.97959184,  8.16326531,
        8.34693878,  8.53061224,  8.71428571,  8.89795918,  9.08163265,
        9.26530612,  9.44897959,  9.63265306,  9.81632653, 10.        ])
print(arr)

arr[3:] =100 # replace all indexes starting from 3rd to all by 100

print(arr)
[0 2 4 6 8]
[  0   2   4 100 100]
arr1=arr
arr1[3:]=500
arr1
array([  0,   2,   4, 500, 500])
arr # array is actually a reference type hence change is reflected to actual array also
array([  0,   2,   4, 500, 500])
arr1 = arr.copy()
arr1
array([  0,   2,   4, 500, 500])
arr1[3:] = 800
print(arr1)
print(arr)
[  0   2   4 800 800]
[  0   2   4 500 500]

Some useful conditions for Exploratorty data analysis

arr = np.array([1,2,3,4,5])
val = 2
arr 
array([1, 2, 3, 4, 5])
print(arr<2)
print(arr*2)
print(arr%2)
[ True False False False False]
[ 2  4  6  8 10]
[1 0 1 0 1]
arr[arr<2]
array([1])
np.ones((2,5),dtype=int)
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])
np.ones(4)
array([1., 1., 1., 1.])
np.random.rand(3,3)
array([[0.97016302, 0.13230666, 0.31222633],
       [0.85189366, 0.07856671, 0.57296934],
       [0.71915461, 0.48997742, 0.24332137]])
arr_ex = np.random.randn(4,4) # selects from random distribution

arr_ex
array([[-0.60890655, -0.67170484, -0.28552398,  1.14748824],
       [-1.27784825, -0.60587355, -0.87103948, -0.75084882],
       [ 0.1356478 ,  0.67908955, -0.18930585, -1.23064491],
       [ 0.0557476 ,  0.96733176, -0.0119645 ,  0.94036578]])
arr_ex.reshape(16,1)
array([[-0.60890655],
       [-0.67170484],
       [-0.28552398],
       [ 1.14748824],
       [-1.27784825],
       [-0.60587355],
       [-0.87103948],
       [-0.75084882],
       [ 0.1356478 ],
       [ 0.67908955],
       [-0.18930585],
       [-1.23064491],
       [ 0.0557476 ],
       [ 0.96733176],
       [-0.0119645 ],
       [ 0.94036578]])
import seaborn as sns
import pandas as pd
sns.distplot(pd.DataFrame(arr_ex.reshape(16,1)))
<matplotlib.axes._subplots.AxesSubplot at 0x1b903b859c8>
np.random.randint(0,100,8).reshape(4,2)
array([[17, 74],
       [67, 13],
       [91, 60],
       [92, 75]])
np.random.random_sample((1,5))
array([[0.02247093, 0.32708592, 0.95730227, 0.40039247, 0.43461314]])