NumPy Implementation of Machine Learning course by Hackveda

sameer saxena
Sep 1, 2018 · 5 min read

Using NumPy

NumPy is a library used in Python Programming Language adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Pre- Requisites

Basic coding knowledge about Python.

NumPY

NumPy targets the CPython reference implementation of Python, which is a non-optimizing bytecode interpreter. Mathematical algorithms written for this version of Python often run much slower than compiled equivalents. NumPy addresses the slowness problem partly by providing multidimensional arrays and functions and operators that operate efficiently on arrays, requiring rewriting some code, mostly inner loops using NumPy.

So first we will learn about
1. Identity
2. Astype
3. Arange
4. Linspace
5. Indices
6. Data types
7. Reshape Function
8. Converting List into an Array
9. Slicing

To start with numpy first include the header

importing numpy

Identity

Identity is used in Python to create the identity matrix. Identity matrix is a square matrix in which all the elements of the principal diagonal are ones and all other elements are zeros. The effect of multiplying a given matrix by an identity matrix is to leave the given matrix unchanged.
so the code is as shown below.

using identity

in this the number inside the identity function gives the size of identity matrix. the data type of this matrix is float.

showcasing the data type of identity

Astype

Astype is used to change the data type in python programming language.
so for above example we will convert all the values into int data type.

example for astype

so the data type of identity is now changed into integer data type.

Arange

Arange is used to return an array with evenly spaced elements as per the interval.
arange([start,] stop[, step,][, dtype])
Parameters:
start : [optional] start of interval range. By default start = 0
stop : end of interval range
step : [optional] step size of interval. By default step size = 1,
For any output out, this is the distance between two adjacent values, out[i+1] — out[i].
dtype : type of output array

examples how to use arange

Linspace

Returns number spaces evenly w.r.t interval. Similiar to arange but instead of step it uses sample number.
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)
Parameters:
-> start : [optional] start of interval range. By default start = 0
-> stop : end of interval range
-> restep : If True, return (samples, step). By deflut restep = False
-> num : [int, optional] No. of samples to generate
-> dtype : type of output array

example of linspace

Indices

Return an array representing the indices of a grid. Compute an array where the subarrays contain index values 0,1,… varying only along the corresponding axis.
numpy.indices(dimensions, dtype = None)
Parameters:
dimensions : sequence of ints. The shape of grid.
dtype : type of output array.

example how to use the indices

Data Types

A data type object describes interpretation of fixed block of memory corresponding to an array, depending on the following aspects −
1. Type of data (integer, float or Python object)
2. Size of data
3. Byte order (little-endian or big-endian)
4. In case of structured type, the names of fields, data type of each field and part of the memory block taken by each field.
5. If data type is a subarray, its shape and data type
numpy.dtype(object, align, copy)
Parameters:
Object − To be converted to data type object
Align − If true, adds padding to the field to make it similar to C-struct
Copy − Makes a new copy of dtype object. If false, the result is reference to builtin data type object

checking the data type

Reshape Function

shapes an array without changing data of array.
numpy.reshape(array, shape, order = ‘C’)
Parameters:
array : [array_like]Input array
shape : [int or tuples of int] e.g. if we are aranging an array with 10 elements then shaping
it like numpy.reshape(4, 8) is wrong; we can
order : [C-contiguous, F-contiguous, A-contiguous; optional]
C-contiguous order in memory(last index varies the fastest)
C order means that operating row-rise on the array will be slightly quicker
FORTRAN-contiguous order in memory (first index varies the fastest).
F order means that column-wise operations will be faster.
‘A’ means to read / write the elements in Fortran-like index order if,
array is Fortran contiguous in memory, C-like order otherwise

example how to use reshape
reshape on 2 D array and transpose

Converting List into an Array

This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.

converting a list into an array

Slicing

Basic slicing is an extension of Python’s basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function. This slice object is passed to the array to extract a part of array. Result can also be obtained by giving the slicing parameters separated by a colon : (start:stop:step)

ways to use slicing

Resources

For more Info : http://www.hackveda.in/campus.php?campusno=online_ml_course_demo_2018

Sameer Saxena

Python implementation of Machine Learning by Hackveda

sameer saxena

Written by

Sameer Saxena

Python implementation of Machine Learning by Hackveda

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade