In [1]:
#numpy
In [3]:
import numpy as np
In [4]:
#making array from list
a = np.array([2,3,4])
In [5]:
a
Out[5]:
array([2, 3, 4])
In [6]:
#using arange function in np same as range function
a = np.arange(1,13,2)
In [7]:
a
Out[7]:
array([ 1,  3,  5,  7,  9, 11])
In [9]:
#linspace takes 3 values start,end, number of items
a = np.linspace(1,12,6)
In [10]:
a
Out[10]:
array([ 1. ,  3.2,  5.4,  7.6,  9.8, 12. ])
In [11]:
#changing the dimension of array
a = a.reshape(3,2)
In [12]:
a
Out[12]:
array([[ 1. ,  3.2],
       [ 5.4,  7.6],
       [ 9.8, 12. ]])
In [13]:
#number of elements in array
a.size
Out[13]:
6
In [14]:
#get shape of array 
a.shape
#(3,2) represents 3 X 2 matrix
Out[14]:
(3, 2)
In [15]:
#datatype
a.dtype
Out[15]:
dtype('float64')
In [16]:
#memory each item takes in array
a.itemsize
Out[16]:
8
In [17]:
#creating multi dimension array
b = np.array([(1.5,2,3), (4,5,6)])
In [19]:
b
Out[19]:
array([[1.5, 2. , 3. ],
       [4. , 5. , 6. ]])
In [20]:
a
Out[20]:
array([[ 1. ,  3.2],
       [ 5.4,  7.6],
       [ 9.8, 12. ]])
In [21]:
#comparing each element of array
a < 7
Out[21]:
array([[ True,  True],
       [ True, False],
       [False, False]])
In [22]:
#multiplying each element by 3
a * 3
#in list we have to write a for loop and multiply each element by 3.
Out[22]:
array([[ 3. ,  9.6],
       [16.2, 22.8],
       [29.4, 36. ]])
In [23]:
#creating empty array of zeroes
np.zeros((3,4))
Out[23]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
In [24]:
#creating arrays of 1
np.ones((2,3))
Out[24]:
array([[1., 1., 1.],
       [1., 1., 1.]])
In [35]:
#passing datatype
np.ones(10, dtype=np.int16)
Out[35]:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int16)
In [36]:
#this will set decimal point to 2 places and won't show scientific notation and will be activate till it is not changed again
np.set_printoptions(precision=2, suppress=True)
In [37]:
#creating array of random values ranging 0 to 1
a=np.random.random((2,3))
In [38]:
a
Out[38]:
array([[0.65, 0.21, 0.57],
       [0.03, 0.26, 0.58]])
In [79]:
#random integer from 0 to 10  creating 5 values
a=np.random.randint(0,10,5)
In [80]:
a
Out[80]:
array([3, 0, 8, 5, 6])
In [81]:
#getting sum of arrays
a.sum()
Out[81]:
22
In [82]:
#getting max
a.max()
Out[82]:
8
In [83]:
#getting min
a.min()
Out[83]:
0
In [84]:
#getting mean
a.mean()
Out[84]:
4.4
In [85]:
#getting variance
a.var()
Out[85]:
7.44
In [86]:
#getting standand deviations
a.std()
Out[86]:
2.727636339397171
In [87]:
a = np.random.randint(1,10,6)
In [88]:
a
Out[88]:
array([7, 5, 7, 7, 9, 6])
In [89]:
a = a.reshape(3,2)
In [90]:
a
Out[90]:
array([[7, 5],
       [7, 7],
       [9, 6]])
In [91]:
#sum of horizontal axis
a.sum(axis=1) #7+5, #7+7, #9+6
Out[91]:
array([12, 14, 15])
In [92]:
#sum of vertical axis
a.sum(axis=0) #7+7+9 and 5+7+6
Out[92]:
array([23, 18])
In [93]:
a = np.arange(10)
In [94]:
a
Out[94]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [95]:
#works inplace like sort, shuffles the array
np.random.shuffle(a)
In [96]:
a
Out[96]:
array([4, 0, 2, 6, 3, 8, 1, 5, 7, 9])
In [99]:
#get a random element from array
np.random.choice(a)
Out[99]:
6
In [101]:
np.random.choice(a)
Out[101]:
8
In [102]:
#sorting array elements
np.sort(a)
Out[102]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [ ]:
#loading file in numpy
data = np.loadtxt("data.txt", dtype=np.uint8, delimiter=",", skiprows=1)
#skiprows skip the first line which is header in this case