The previous post took you through the basics of machine learning using python with scikit-learn and tensorflow, this post will give you a brief introduction to Numpy.
NumPy is a python package which stands for "Numeric Python". It enables you to do mathematical computations easily and effectively hence making coding simpler.
Installing via pip:
After having python3 and pip installed, installing packages can be done using the following command on windows cmd.
--user makes pip install packages in your home directory instead, which doesn't require any special privileges.Import Numpy:
>>> import numpy as np
Now we use np as a reference to numpy in the code.
Numpy arrays:
Create a numpy array of type np.ndarray:
>>> A = np.array([[1,2,3],[4,5,6],[7,8,9]])
Create a numpy array containing ones/ zeros of a desired shape:
>>> A = np.ones(shape, dtype= None)
>>> B= np.zeros(shape, dtype= None)
Random:
>>> A = np.random.randint(low, high= None, size= None, dtype= 'I')
Returns random integers in the range [low, high)
If high is None: Returns random integers in the range [0, low)
size: Int or tuple of ints, optional
dtype: desired type of the result
Generate a 2 x 4 array of ints between 0 and 4, inclusive:
Read csv files:
It is a general practice to import csv files containing dataset in python code.
Pandas or Numpy can be used to import these csv files.
>>> data= np.genfromtxt(fname, delimiter)
fname: Provide the path of the csv file
delimiter: generally "," or " " depending on the csv file.
returns data from text file as ndarray.
Save as csv file:
>>> np.savetxt(location, fname, delimiter)
Similarly, csv file can be read using Pandas which returns the data as dataframe.

Comments
Post a Comment