Learn data science and machine learning by building real-world projects on Jovian
In [ ]:
! pip install jovian --upgrade
In [5]:
import jovian

Section 1

  • Creating numpy arrays from Python Lists
  • Creating numpy arrays from scratch
  • Creating an array with sequence of numbers
  • Creating an array of Random numbers
In [6]:
import numpy as np
In [3]:
#Creating Arrays from Python Lists

# integer array:
np1 = np.array([1, 4, 2, 5, 3])
#float type:
np2 = np.array([1, 2, 3, 4], dtype='float32')

print("Integer numpy array",np1)
print("Float type numpy array",np2)
Integer numpy array [1 4 2 5 3] Float type numpy array [1. 2. 3. 4.]
Note:
  • In Python, a list may contain different datatypes
In [4]:
ls = [3, 4, 2, 3]
ls
Out[4]:
[3, 4, 2, 3]
  • In Numpy array, all elements should be same type. If not, Numpy will upcast if possible.
In [5]:
np.array([3.14, 4, 2, 3])
Out[5]:
array([3.14, 4.  , 2.  , 3.  ])
In [6]:
# Creating Arrays from scratch
print("Array with 10 zeros:")
print(np.zeros(10, dtype=int))
print("A 3X5 Array filled with 1's:")
print(np.ones((3, 5), dtype=float))
Array with 10 zeros: [0 0 0 0 0 0 0 0 0 0] A 3X5 Array filled with 1's: [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]]
In [10]:
# Creating an array with sequence of numbers
print("An one dimentional array of 10 elements starting from 0:")
print(np.arange(10))
print("An one dimentional array from 1 to 10 stepping by 2:")
print(np.arange(1,10,2))
An one dimentional array of 10 elements starting from 0: [0 1 2 3 4 5 6 7 8 9] An one dimentional array from 1 to 10 stepping by 2: [1 3 5 7 9]

Creating Random numbers

In [12]:
# Creating an array of Random numbers
print("A 3X3 Array of uniformedly distributed random numbers:")
print(np.random.random((3,3)))
print("A 3x3 Array of normally distributed random numbers with mean 0 and standarad deviation 1:")
print(np.random.normal(0,1,(3,3)))
print("A 3x3 Array of random integers in range [0,10):")
print(np.random.randint(0,10,(3,3)))
print("A 3X3 Identity matrix:")
print(np.eye(3))
A 3X3 Array of uniformedly distributed random numbers: [[0.25474012 0.09443159 0.33265327] [0.42095532 0.50203519 0.92751003] [0.54469968 0.77783238 0.36597871]] A 3x3 Array of normally distributed random numbers with mean 0 and standarad deviation 1: [[ 0.74975539 -0.5958377 0.54455375] [ 0.86762911 1.10867477 -0.56806397] [ 0.86346929 -1.15748111 -1.20980048]] A 3x3 Array of random integers in range [0,10): [[1 3 3] [9 2 0] [3 1 1]] A 3X3 Identity matrix: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]

Section 2

  • NumPy array manipulation to access data and subarrays.
  • Split, reshape, and join of NumPy arrays

Note:

  • numpy.random generates different random numbers for each execution. I order to generate same random numbers we will seed.
In [13]:
np.random.seed(0)  # seed for reproducibility

x1 = np.random.randint(10, size=6)  # One-dimensional array
x2 = np.random.randint(10, size=(3, 4))  # Two-dimensional array
x3 = np.random.randint(10, size=(3, 4, 5))  # Three-dimensional array
In [14]:
# Determinig size, shape and size of NumPy array
print("x3 ndim (dimensions): ", x3.ndim)
print("x3 shape:", x3.shape)
print("x3 size: ", x3.size)
x3 ndim (dimensions): 3 x3 shape: (3, 4, 5) x3 size: 60

Indexing

In [15]:
# Indexing of NumPy array
x1
Out[15]:
array([5, 0, 3, 3, 7, 9])
In [16]:
print("Element in 0th position in x1:",x1[0])
print("Element in last position of array in x1:",x1[-1])
print("Element in second last position of array in x1:",x1[-2])
Element in 0th position in x1: 5 Element in last position of array in x1: 9 Element in second last position of array in x1: 7
In [17]:
# Indexing of n dimensional NumPy array
x2
Out[17]:
array([[3, 5, 2, 4],
       [7, 6, 8, 8],
       [1, 6, 7, 7]])
In [18]:
print("Element in 0th row, 0th column position in x2:",x2[0][0])
print("Element in 2nd row, 0th column position in x2:",x2[2][0])
Element in 0th row, 0th column position in x2: 3 Element in 2nd row, 0th column position in x2: 1
In [19]:
# Element Assignment
x2[0][0] = 34
x2
Out[19]:
array([[34,  5,  2,  4],
       [ 7,  6,  8,  8],
       [ 1,  6,  7,  7]])

Subarrays

In [20]:
x = np.arange(10)
x
Out[20]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [21]:
print('x elements from index 0 to 5th element: ',x[:5])
print('x elements from index 5 to all elements: ',x[5:])
print('x elements from index 3 to 5th element: ',x[3:5])
print('x elements from index 0 to end with 2 steps: ',x[::2])
x elements from index 0 to 5th element: [0 1 2 3 4] x elements from index 5 to all elements: [5 6 7 8 9] x elements from index 3 to 5th element: [3 4] x elements from index 0 to end with 2 steps: [0 2 4 6 8]
In [22]:
x2
Out[22]:
array([[34,  5,  2,  4],
       [ 7,  6,  8,  8],
       [ 1,  6,  7,  7]])
In [24]:
print('x2 elements from index 0 to 2nd row, 0 to 3rd column elements: \n',x2[:2,:3])
print('x2 elements from index 0 to 3rd row, all column with step length 2:\n',x2[:3, ::2])
print('All elements of x2 in reversed order:\n',x2[::-1, ::-1])
x2 elements from index 0 to 2nd row, 0 to 3rd column elements: [[34 5 2] [ 7 6 8]] x2 elements from index 0 to 3rd row, all column with step length 2: [[34 2] [ 7 8] [ 1 7]] All elements of x2 in reversed order: [[ 7 7 6 1] [ 8 8 6 7] [ 4 2 5 34]]
In [25]:
x2_sub = x2[:2, :2]
print("subarray of x2 with 2 rows and 2 column\n",x2_sub)
subarray of x2 with 2 rows and 2 column [[34 5] [ 7 6]]
In [26]:
x2_sub_copy = x2[:2, :2].copy()
print("Copy of x2 subarray:\n",x2_sub_copy)
Copy of x2 subarray: [[34 5] [ 7 6]]

Reshaping of Arrays

In [28]:
grid = np.arange(1, 10)
print("A linear array:\n",grid)
grid = np.arange(1, 10).reshape((3, 3))
print("An array of 3X3 dimensions:\n",grid)
A linear array: [1 2 3 4 5 6 7 8 9] An array of 3X3 dimensions: [[1 2 3] [4 5 6] [7 8 9]]
In [29]:
x = np.array([1, 2, 3])
# row vector via reshape
print("Using reshape:\n",x.reshape((3, 1)))
# Using newaxis keyword
print("Using newaxis keyword:\n",x[:, np.newaxis])
Using reshape: [[1] [2] [3]] Using newaxis keyword: [[1] [2] [3]]

Array Concatenation

In [9]:
# concatenate linear arrays
p = np.array([11, 22, 33])
q = np.array([0, 0, 0])
r = [100, 200, 300]
print("concatenate p and q",np.concatenate([p, q]))

print("concatenate p, q and r",np.concatenate([p, q, r]))
concatenate p and q [11 22 33 0 0 0] concatenate p, q and r [ 11 22 33 0 0 0 100 200 300]
In [31]:
# Concatenate multidimentional array
ar = np.arange(1,10).reshape((3,3))
print("ar:\n",ar)
# Concatenate along first-axis (or) row-wise 
print("Row-wise Concatenation:\n",np.concatenate([ar,ar]))
# Concatenate along second-axis (or) column-wise
print("Concatenate along column-wise:\n",np.concatenate([grid, grid], axis=1))
ar: [[1 2 3] [4 5 6] [7 8 9]] Row-wise Concatenation: [[1 2 3] [4 5 6] [7 8 9] [1 2 3] [4 5 6] [7 8 9]] Concatenate along column-wise: [[1 2 3 1 2 3] [4 5 6 4 5 6] [7 8 9 7 8 9]]
In [ ]:
jovian.commit(environment='none')
[jovian] Attempting to save notebook..