<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 NumPy
Why NumPy ?
NumPy is an acronym for "Numeric Python" or "Numerical Python"
NumPy is the fundamental package for scientific computing with Python. It contains among other things
Step1:  numpy array (ndarray)
A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers
ndarray.ndim - the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
ndarray.shape - the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension.
ndarray.size - the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype - an object describing the type of the elements in the array.
<img src="images/fig_numpy_axes.png " alt="NumPy axes" height="300" width="300" align="left">
Step2:  Array basic operations
Step3:  Array Slicing
Step4:  Broadcasting
The term Broadcasting describes how numpy treats arrays with different shapes during arithmetic operations.
Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.
Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python.
Step5:  <img src="images/fig_broadcast_visual_1.png" alt="Broadcasting" height="500" width="500", align="left">
Exercises
Step6:  <img src="images/fig_numpy_indexing_q.png" alt="Array Slicing" height="300" width="300" align="left">
Step7:  NumPy functions used for performing computations
np.sum
np.std
np.mean
np.max
np.min
<ASSISTANT_TASK:>
Python Code:
# import NumPy library
# This library is bundled along with anaconda distribution
# np alias is the standard convention
import numpy as np
Explanation: NumPy
Why NumPy ?
NumPy is an acronym for "Numeric Python" or "Numerical Python"
NumPy is the fundamental package for scientific computing with Python. It contains among other things:
A powerful N-dimensional array object (ndarray) - efficiently implemented multi-dimensional arrays
Array oriented computing - sophisticated (broadcasting) functions
Tools for integrating C/C++ and Fortran code
Designed for scientific computation - useful linear algebra, Fourier transform, and random number capabilities
End of explanation
%%timeit
temp_list = range(100000)
temp_list1 = [ x*2 for x in temp_list]
%%timeit
temp_array = np.arange(100000)
temp_array1 = temp_array*2
# ndarray can be created for regular python list or tupple
mylist = [2,5,8,15,25]
array = np.array(mylist)
type(array)
array.shape
array[0]
array[0:3]
array.dtype
array.ndim
# dtype can be mentioned while creating an array
array2 = np.array(mylist,dtype=np.float)
array2
array2.dtype
# creating a 5 X 3 multi dimensional array
marray = np.arange(15).reshape(5,3)
marray
marray.ndim
marray.shape
marray.size
# ravel function generates a flattens 
marray.ravel()
# reshape can be used to change the shape of an array
marray.ravel().reshape(3,5)
marray.shape
Explanation: numpy array (ndarray)
A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers
ndarray.ndim - the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
ndarray.shape - the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension.
ndarray.size - the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype - an object describing the type of the elements in the array.
<img src="images/fig_numpy_axes.png " alt="NumPy axes" height="300" width="300" align="left">
End of explanation
# multiplying a scalr and ndarray
print(marray*2)
marray
# inplace change
# there are certain operations that will modify the object inplace like one below
marray += 10
marray
# Guess - what would be the result of the following
marray > 15
arr_A = np.array( [ [2,3], [4,5] ] )
arr_B = np.array( [ [1,1], [2,1] ] )
# * operates element wise
arr_A * arr_B
# dot is used for matrix multiplication
# np.dot(arr_A,arr_B) also works
arr_A.dot(arr_B)
Explanation: Array basic operations
End of explanation
marray[0]
marray[0,1]
marray
marray[:,1:3]
marray[0:3,:]
marray[1:3,1:]
Explanation: Array Slicing
End of explanation
marray
marray + 5
Explanation: Broadcasting
The term Broadcasting describes how numpy treats arrays with different shapes during arithmetic operations.
Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.
Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python.
End of explanation
# Exercise - 1
# Construct  3 by 3 ndarray with 5 as diagonal elemet and 1 as remaining elements
# [[5, 1, 1][1,5,1][1,1,5]]
# Tip : explore np.ones and np.eye functions
# the dtype should be int
# Exercise
# try following array slicing
a = np.arange(0,60).reshape(6,10)[0:6,0:6]
Explanation: <img src="images/fig_broadcast_visual_1.png" alt="Broadcasting" height="500" width="500", align="left">
Exercises
End of explanation
a
Explanation: <img src="images/fig_numpy_indexing_q.png" alt="Array Slicing" height="300" width="300" align="left">
End of explanation
# np.NaN is a datatype - Not a Number
np.NaN?
np.random.seed(0)
arr_c = np.random.random(15).reshape((5,3))
arr_c
arr_c.sum()
arr_c.min()
arr_c.max()
arr_c.mean()
arr_c.mean(axis=0)
arr_c.mean(axis=1)
Explanation: NumPy functions used for performing computations
np.sum
np.std
np.mean
np.max
np.min
End of explanation
<END_TASK>