numpy
numpy
objects
you can perform various mathematical operations. It can be logical, sorting, shape manipulation etc.
In [1]:
# for 1D array
my_array = [12,34,43,14,51,66]
my_array
Out[2]:
In [3]:
Out[3]:
In [4]:
Out[4]:
In [5]:
np.array(my_2D_array )
Out[5]:
array([[12, 34],
[43, 14],
[51, 66]])
Out[6]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [7]:
np.arange(0,10,2) # specify start, stop & step values
Out[7]:
array([0, 2, 4, 6, 8])
In [8]:
np.linspace(0,10,10) #returns evenly spaced numbers over a specified interval
#specify start, stop & number of values
Out[8]:
In [9]:
np.zeros(10) #generate array of zeroes
Out[9]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [10]:
Out[10]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [11]:
In [12]:
np.eye(10) #generate 2D indentity matrix or a numpy array with ones in the diagonal
Out[12]:
array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])
In [13]:
In [14]:
np.random.rand(4,5) # generate random 2D array
Out[14]:
In [15]:
np.random.randn(4) # generates a set of numbers from the Standard Normal distribution
#what is SND ?
#It's a is a normal distribution with a mean of zero and standard deviation of 1. ~ Bell
shaped graph
# It allows us to make comparisons across the infinitely many normal distributions that c
an possibly exist
Out[15]:
array([ 0.02239357, -0.36543475, -1.11612562, -0.95434795])
In [16]:
In [17]:
# some other properties
np.random.randint(1,20) #generates a random integer from 1 to 19
#it changes every time you run the cell
Out[17]:
In [18]:
np.random.randint(1,20,10) # generates 10 random integers from 1 to 19
Out[18]:
In [20]:
array_
Out[20]:
array([52, 87, 18, 67, 98, 72, 70, 66, 41, 24, 79, 35, 28, 8, 26, 80, 70,
99, 43, 32])
In [21]:
array_.shape # would give the dimension or shape of the array
Out[21]:
(20,)
In [22]:
#similarly with 2D array
In [23]:
array_.reshape(4,5) # to change the dimension
Out[23]:
array([[52, 87, 18, 67, 98],
[72, 70, 66, 41, 24],
[79, 35, 28, 8, 26],
[80, 70, 99, 43, 32]])
In [24]:
array_.reshape(4,6) # not possible as 4*6 != 10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-bc319ffd74c1> in <module>
----> 1 array_.reshape(4,6) # not possible as 4*6 != 10
In [25]:
array_.reshape(4,5).T # this would transpose the array
Out[25]:
array([[52, 72, 79, 80],
[87, 70, 35, 70],
[18, 66, 28, 99],
[67, 41, 8, 43],
[98, 24, 26, 32]])
array = np.arange(1,10)
array
Out[26]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [27]:
In [28]:
# similarly divide/add/subtract
# if you have a 0 element in the array and you divide the array with itself then it would
give 'nan' result
In [29]:
In [30]:
Out[30]:
array([ 5, 10, 15, 20, 25, 30, 35, 40, 45])
In [31]:
### Some Mathematical functions that we can perform are :
In [32]:
In [33]:
In [34]:
np.argmax(array) # for index of max element
#similarly argmin()
Out[34]:
8
In [35]:
np.log(array)# to find log of elements
Out[35]:
array([0. , 0.69314718, 1.09861229, 1.38629436, 1.60943791,
1.79175947, 1.94591015, 2.07944154, 2.19722458])
In [36]:
np.sin(array)# to find sin() of the array
# similarly exp, var , man , std
Out[36]:
In [37]:
array = np.random.randn(3,3) # consider a matrix with normalised values
# we can round off the values of this matrix using the functions
array
Out[37]:
array([[-0.32054694, -1.3981901 , -0.51937189],
[-0.70653977, -2.11046186, 0.83545895],
[ 0.91883608, 1.64084669, 0.17252355]])
In [38]:
np.round(array,decimals=3) # to round off upto 3 decimal places
Out[38]:
array = np.arange(1,10)
array
Out[39]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [40]:
Out[40]:
5
In [41]:
array[[0,2,5,8]] # to show elements from multiple indexes
Out[41]:
array([1, 3, 6, 9])
In [42]:
Out[42]:
array([1, 2, 3, 4, 5])
In [43]:
array[[0,4,7]] # to show elements from multiple indexes
Out[43]:
array([1, 5, 8])
In [44]:
Out[44]:
array([ 1, 2, 3, 8383, 8383, 8383, 8383, 8, 9])
In [45]:
# You can perform the similar operation in a 2D array
# In a 2d array the elements would be of the form array[i][j]
In [46]:
array2D = np.arange(1,21).reshape(4,5)
array2D
Out[46]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
In [47]:
# in this array
array2D[:,(2,4)] # means select all rows and cols of index 2 & 4
Out[47]:
array([[ 3, 5],
[ 8, 10],
[13, 15],
[18, 20]])
array = np.arange(1,10)
array
Out[48]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:
array <5 #returns a boolean value
Out[49]:
array([ True, True, True, True, False, False, False, False, False])
In [50]:
array[array <5]
Out[50]:
array([1, 2, 3, 4])
In [51]:
#copy a numpy array
#when you slice/make any index value changes/reshape then it affects the original array
# you can copy the array if you don't want the original array to be changed
In [52]:
#consider reshape the array
array = np.arange(1,10)
array[3]= 1000
array
Out[52]:
In [53]:
array_copy = array.copy()
array_copy
Out[53]:
In [54]:
array_to_save = np.arange(10)
array_to_save = np.arange(10)
array_to_save
Out[54]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [55]:
In [56]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [57]:
In [46]:
# we can also save the file in a zip format using the .savez() function.
# It's left for your own research & learning !
In [47]:
In [ ]:
In [ ]: