In [1]:
#load the library and check its version, just to make sure we aren't using an older version
import numpy as np
np.__version__
Out[1]:
In [2]:
#create a list comprising numbers from 0 to 9
L = list(range(10))
In [3]:
#converting integers to string - this style of handling lists is known as list comprehension.
#List comprehension offers a versatile way to handle list manipulations tasks easily. We'll learn about them in future tutorials. Here's an example.
[str(c) for c in L]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
[type(item) for item in L]
[int, int, int, int, int, int, int, int, int, int]
Out[3]:
In [4]:
#creating arrays
np.zeros(10, dtype='int')
Out[4]:
In [5]:
#creating a 3 row x 5 column matrix
np.ones((3,5), dtype=float)
Out[5]:
In [6]:
#creating a matrix with a predefined value
np.full((3,5),1.23)
Out[6]:
In [7]:
#create an array with a set sequence
np.arange(0, 20, 2)
Out[7]:
In [8]:
#create an array of even space between the given range of values
np.linspace(0, 1, 5)
Out[8]:
In [9]:
#create a 3x3 array with mean 0 and standard deviation 1 in a given dimension
np.random.normal(0, 1, (3,3))
Out[9]:
In [10]:
#create an identity matrix
np.eye(3)
Out[10]:
In [11]:
#set a random seed
np.random.seed(0)
In [12]:
x1 = np.random.randint(10, size=6) #one dimension
x2 = np.random.randint(10, size=(3,4)) #two dimension
x3 = np.random.randint(10, size=(3,4,5)) #three dimension
print("x3 ndim:", x3.ndim)
print("x3 shape:", x3.shape)
print("x3 size: ", x3.size)
In [13]:
x1 = np.array([4, 3, 4, 4, 8, 4])
x1
Out[13]:
In [14]:
#assess value to index zero
x1[0]
Out[14]:
In [15]:
#assess fifth value
x1[4]
Out[15]:
In [16]:
#get the last value
x1[-1]
Out[16]:
In [17]:
#get the second last value
x1[-2]
Out[17]:
In [18]:
x2
Out[18]:
In [19]:
#1st row and 2nd column value
x2[2,3]
Out[19]:
In [20]:
#3rd row and last value from the 3rd column
x2[2,-1]
Out[20]:
In [21]:
#replace value at 0,0 index
x2[0,0] = 12
x2
Out[21]:
In [22]:
#array slicing
x = np.arange(10)
x
Out[22]:
In [23]:
#from start to 4th position
x[:5]
Out[23]:
In [24]:
#from 4th position to end
x[4:]
Out[24]:
In [25]:
#from 4th to 6th position
x[4:7]
Out[25]:
In [26]:
#return elements at even place
x[ : : 2]
Out[26]:
In [27]:
#return elements from first position step by two
x[1::2]
Out[27]:
In [28]:
#reverse the array
x[::-1]
Out[28]:
In [29]:
#array concatenation
x = np.array([1, 2, 3])
y = np.array([3, 2, 1])
z = [21,21,21]
np.concatenate([x, y,z])
Out[29]:
In [30]:
#can also use this function to create 2-dimensional arrays
grid = np.array([[1,2,3],[4,5,6]])
np.concatenate([grid,grid])
Out[30]:
In [31]:
#Using its axis parameter, you can define row-wise or column-wise matrix
np.concatenate([grid,grid],axis=1)
Out[31]:
In [32]:
x = np.array([3,4,5])
grid = np.array([[1,2,3],[17,18,19]])
np.vstack([x,grid])
Out[32]:
In [33]:
#Similarly, you can add an array using np.hstack
z = np.array([[9],[9]])
np.hstack([grid,z])
Out[33]:
In [34]:
x = np.arange(10)
x
Out[34]:
In [35]:
x1,x2,x3 = np.split(x,[3,6])
print(x1,x2,x3)
In [36]:
grid = np.arange(16).reshape((4,4))
grid
upper,lower = np.vsplit(grid,[2])
print (upper)
print (lower)