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]:
'1.12.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]:
[int, int, int, int, int, int, int, int, int, int]

In [4]:
#creating arrays
np.zeros(10, dtype='int')


Out[4]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [5]:
#creating a 3 row x 5 column matrix
np.ones((3,5), dtype=float)


Out[5]:
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

In [6]:
#creating a matrix with a predefined value
np.full((3,5),1.23)


Out[6]:
array([[ 1.23,  1.23,  1.23,  1.23,  1.23],
       [ 1.23,  1.23,  1.23,  1.23,  1.23],
       [ 1.23,  1.23,  1.23,  1.23,  1.23]])

In [7]:
#create an array with a set sequence
np.arange(0, 20, 2)


Out[7]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

In [8]:
#create an array of even space between the given range of values
np.linspace(0, 1, 5)


Out[8]:
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])

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]:
array([[-0.69914079,  0.43516419,  1.54925985],
       [ 1.5832228 , -1.1688609 , -0.05525857],
       [-1.93700122, -0.04674854,  0.54766042]])

In [10]:
#create an identity matrix
np.eye(3)


Out[10]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

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)


x3 ndim: 3
x3 shape: (3, 4, 5)
x3 size:  60

In [13]:
x1 = np.array([4, 3, 4, 4, 8, 4])
x1


Out[13]:
array([4, 3, 4, 4, 8, 4])

In [14]:
#assess value to index zero
x1[0]


Out[14]:
4

In [15]:
#assess fifth value
x1[4]


Out[15]:
8

In [16]:
#get the last value
x1[-1]


Out[16]:
4

In [17]:
#get the second last value
x1[-2]


Out[17]:
8

In [18]:
x2


Out[18]:
array([[3, 5, 2, 4],
       [7, 6, 8, 8],
       [1, 6, 7, 7]])

In [19]:
#1st row and 2nd column value
x2[2,3]


Out[19]:
7

In [20]:
#3rd row and last value from the 3rd column
x2[2,-1]


Out[20]:
7

In [21]:
#replace value at 0,0 index
x2[0,0] = 12
x2


Out[21]:
array([[12,  5,  2,  4],
       [ 7,  6,  8,  8],
       [ 1,  6,  7,  7]])

In [22]:
#array slicing
x = np.arange(10)
x


Out[22]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [23]:
#from start to 4th position
x[:5]


Out[23]:
array([0, 1, 2, 3, 4])

In [24]:
#from 4th position to end
x[4:]


Out[24]:
array([4, 5, 6, 7, 8, 9])

In [25]:
#from 4th to 6th position
x[4:7]


Out[25]:
array([4, 5, 6])

In [26]:
#return elements at even place
x[ : : 2]


Out[26]:
array([0, 2, 4, 6, 8])

In [27]:
#return elements from first position step by two
x[1::2]


Out[27]:
array([1, 3, 5, 7, 9])

In [28]:
#reverse the array
x[::-1]


Out[28]:
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

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]:
array([ 1,  2,  3,  3,  2,  1, 21, 21, 21])

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]:
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])

In [31]:
#Using its axis parameter, you can define row-wise or column-wise matrix
np.concatenate([grid,grid],axis=1)


Out[31]:
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])

In [32]:
x = np.array([3,4,5])
grid = np.array([[1,2,3],[17,18,19]])
np.vstack([x,grid])


Out[32]:
array([[ 3,  4,  5],
       [ 1,  2,  3],
       [17, 18, 19]])

In [33]:
#Similarly, you can add an array using np.hstack
z = np.array([[9],[9]])
np.hstack([grid,z])


Out[33]:
array([[ 1,  2,  3,  9],
       [17, 18, 19,  9]])

In [34]:
x = np.arange(10)
x


Out[34]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [35]:
x1,x2,x3 = np.split(x,[3,6])
print(x1,x2,x3)


[0 1 2] [3 4 5] [6 7 8 9]

In [36]:
grid = np.arange(16).reshape((4,4))
grid
upper,lower = np.vsplit(grid,[2])
print (upper)
print (lower)


[[0 1 2 3]
 [4 5 6 7]]
[[ 8  9 10 11]
 [12 13 14 15]]