aniket jagtap
Aug 28, 2018 · 9 min read

Python is one of the coolest and simplest language to learn.Python has many different libraries , Here I’m going to introduce some of the well Libraries of python such as Numpy , Pandas and Matpotlib . Also we will be seeing little about how object oreintation work in Python.

1- What is NumPy?

NumPy is the basis of Pandas and many other packages. What makes NumPy such an incredible package is its data type (ndarray). ndarray stands for n-dimensional array, which basically looks like a Python list. However, it is a lot faster than a regular Python list. A Python list can contain different kinds of data types such as integers, strings, Boolean, True, False and even lists. On the other hand, NumPy arrays can hold only one type of data, and therefore doesn’t have to check the type of data type for every single element of the array when it is doing the computations. This feature makes NumPy a great tool for data science research and projects.

2- How to create NumPy arrays

There are many ways to create arrays in NumPy. Let’s take a look at a few of them here.

>>> x = np.array([2,3,1,0])
>>> x = np.array([2, 3, 1, 0])
>>> x = np.array([[1,2.0],[0,0],(1+1j,3.)]) # note mix of tuple and lists,
and types
>>> x = np.array([[ 1.+0.j, 2.+0.j], [ 0.+0.j, 0.+0.j], [ 1.+1.j, 3.+0.j]])
>>> array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Create 3x4 array values between 0 and 1
np.random.random((3,4))
>>> array([[ 0.85957774, 0.90323213, 0.08000421, 0.45366519],
[ 0.15077925, 0.57901453, 0.72878536, 0.88573099],
[ 0.51431053, 0.46266243, 0.54166614, 0.72836133]])

Parameters for using Numpy array are :

object : array_like

An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.

dtype : data-type, optional

The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.

copy : bool, optional

If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements

subok : bool, optional

If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).

ndmin : int, optional

Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement.

3- Indexing and Fancy Indexing

Indexing allows us to access the elements in the list. Indexing is the simplest way to do that. However, there are other ways, too such as fancy indexing, slicing and masking.

Different types of sequences in python are strings, Unicode strings, lists, tuples, buffers, and xrange objects
- Slicing & indexing operations are salient features of sequence.
- Indexing operation allows to access a particular item in the sequence directly ( similar to the array/list indexing) and the slicing operation allows to retrieve a part of the sequence.
- The slicing operation is used by specifying the name of the sequence followed by an optional pair of numbers separated by a colon within square brackets say S[startno.:stopno].
- The startno in the slicing operation indicates the position from where the slice starts and the stopno indicates where the slice will stop at.
- If the startno is ommited, Python will start at the beginning of the sequence. If the stopno is ommited, Python will stop at the end of the sequence..

>>> cosmeticList =[‘lipsstick’,’facepowder’,eyeliner’,’blusher’,kajal’] 
>>> print “Slicing operation :”,cosmeticList[2:]
Slicing operation :[‘eyeliner’,’blusher’,kajal’]
>>>print “Indexing operation :”,cosmeticList[0]
“Indexing operation :lipsstick

4- Universal Functions (Ufuncs)

Universal functions are useful when it comes to doing statistical and mathematical operations in NumPy arrays. NumPy Ufuncs are significantly faster than Python because the same operation in Python might require loops.

In NumPy, universal functions are instances of the numpy.ufunc class. Many of the built-in functions are implemented in compiled C code. The basic ufuncs operate on scalars, but there is also a generalized kind for which the basic elements are sub-arrays (vectors, matrices, etc.), and broadcasting is done over other dimensions. One can also produce custom ufunc instances using the frompyfunc factory function.

The umath module is a computer-generated C-module that creates many ufuncs. It provides a great many examples of how to create a universal function. Creating your own ufunc that will make use of the ufunc machinery is not difficult either. Suppose you have a function that you want to operate element-by-element over its inputs. By creating a new ufunc you will obtain a function that handles

np.arange() :

np.arange(0,10,2)
====================================================================array([0, 2, 4, 6, 8])

What arange([start],stop,[step]) does is that it arranges numbers from starting to stop, in steps of step. Here is what it means for np.arange(0,10,2):

return an np list starting from 0 all the way upto 10 but don’t include 10 and increment numbers by 2 each time.

Before I proceed further, I’ll have to warn you that this “array” is interchangeably called “matrix” or also “vector”. So don’t get panicked when I say for example “Matrix shape is 2 X 3”. All it means is that array looks something like this:

array([ 2,  7, 12,], 
[17, 22, 27])

Now, Let’s talk about the shape of a default np array.

Shape is an attribute for np array. When a default array, say for example A is called with shape, here is how it looks.

A = [1, 2, 3, 4, 5, 6, 7, 8, 9] 
A.shape
===================
(9,)

This is a rank 1 matrix(array), where it just has 9 elements in a row.
Ideally it should be a 1 X 9 matrix right?

I agree with you, so that’s where reshape() comes into play. It is a method that changes the dimensions of your original matrix into your desired dimension.

Let’s look at reshape in action. You can pass a tuple of whatever dimension you want as long as the reshaped matrix and original matrix have the same number of elements.

A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
A.reshape(1,9)
====================================================================
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])

Notice that reshape returns a multi-dim matrix. Two square brackets in the beginning indicate that. [[1, 2, 3, 4, 5, 6, 7, 8, 9]] is a potentially multi-dim matrix as opposed to [1, 2, 3, 4, 5, 6, 7, 8, 9].

Let’s guess again here: what does is np.eye() do?

Hint: eye() stands for Identity.

np.eye(5)
====================================================================
array([[ 1., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0.],
[ 0., 0., 1., 0., 0.],
[ 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 1.]])

np.eye() returns an identity matrix with the specified dimensions.

What if we have to multiply 2 matrices?

No problem, we have np.dot().

np.dot() performs matrix multiplication, provided both the matrices are “multiply-able”. It just means that the number of columns of the first matrix must match the number of rows in second matrix.

ex: A = (2,3) & B=(3,2). Here number of cols in A= 3. Number of rows in B = 3. Since they match, multiplication is possible.

Let’s illustrate multiplication via np code:

# generate an identity matrix of (3 x 3)
I = np.eye(3)
I
====================================================================
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
# generate another (3 x 3) matrix to be multiplied.
D = np.arange(1,10).reshape(3,3)
D
====================================================================
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

1. Sum along the rows.

# sum along the rows
np.sum(M,axis=1)
====================================================================
array([ 6., 15., 24.])

6 is the sum of 1st row (1, 2, 3).

15 is the sum of 2nd row (4, 5, 6).

24 is the sum of 3rd row (7, 8, 9).

2. Sum along the columns.

# sum along the cols
np.sum(M,axis=0)
====================================================================
array([ 12., 15., 18.])

12 is the sum of 1st col (1, 4, 7).

15 is the sum of 2nd col (2, 5, 8).

18 is the sum of 3rd col (3, 6, 9).

Here is the video link to learn numpy for absolute beginners.

http://www.youtube.com/watch?v=LPcgo-SFXWU

Now we start with a amazing library which is used for analysing data which is called Pandas.

Pandas is hands down one of the best libraries of python. It supports reading and writing excel spreadsheets, CVS’s and a whole lot of manipulation. It is more like a mandatory library you need to know if you’re dealing with datasets from excel files and CSV files. i.e for Machine learning and data science.

This is part one of Pandas tutorial. I’m not going to cover everything possible with pandas, however, I want to give you a taste of what it is and how you can get started with it. This tutorial is going to be super short just introducing you to Series object of pandas.

As other libraries, you’d import pandas and reference it as pd.

import pandas as pd

We’re officially indicating to python that pandas must be hence fourth referred to as pd.

If you like trance music, I’m positive you’ve heard of songs mentioned in this list.

# lets create a list of songs.
songs = ['In the name of love','Scream','Till the sky falls down','In and out of Love']
# lets also create a list of corresponding artists. FYI: 'MG' stands # for Martin Garrix, 'TI' for Tiesto, 'DB' for Dash Berlin, 'AV'for # Armin Van Buuren.
artists = ['MG','TI','DB','AV']
# likewise lets create a dictionary that contains artists and songs.
song_arts = {'MG':'In the name of love','TI':'Scream','DB':'Till the sky falls down','AV':'In and out of Love'}

How do I create a table like structure using these lists? pd.Series()

pd.Series() is a method that creates a series object from data passed. The data must be defined as a parameter.

# create a Series object whose data is coming from songs list.
ser_num = pd.Series(data=songs)
ser_num
====================================================================
0 In the name of love
1 Scream
2 Till the sky falls down
3 In and out of Love
dtype: object

So, what is a “Series” object in Pandas?

It is a data structure defined by Pandas. Basically it looks like a table having rows and columns.

0        In the name of love
1 Scream
2 Till the sky falls down
3 In and out of Love

Notice that these numbers on the first column were added automatically by pandas. They serve as index.

The first column here are the indices of the series and the second column are values of the series.

Say supposing you want to access ‘In and out of Love’. How would you do that?

# get the element that corresponds to index 3.
ser_num[3]
====================================================================
'In and out of Love'

What if you want the artists name to be the index of the song?

# make artists the index this time.
ser_art = pd.Series(data=songs,index=artists)
ser_art
====================================================================
MG In the name of love
TI Scream
DB Till the sky falls down
AV In and out of Love
dtype: object

This time instead of numbers, name of artists are made as the index. But how?Notice, this time we passed artists as index parameter additionally to pd.Series().

How to access via custom index defined? i.e How to get songs by their artist name?

Just pass the name of the artist and you get their song.

ser_art['MG']
====================================================================
'In the name of love'
ser_art['AV']
====================================================================
'In and out of Love'
ser_art['DB']
====================================================================
'Till the sky falls down'

It is kind of like accessing elements via dictionary. There you pass the ‘key’, here in series you pass ‘index’ to retrieve elements.

Not to mention even numbers still work as index.

ser_art[0]
====================================================================
'In the name of love'
ser_art[2]
====================================================================
'Till the sky falls down'

Great! Seems interesting. But how to create a series object from dictionary?

It’s as simple as passing the dictionary element to pd.Series(), like so:

ser_dict= pd.Series(song_arts)
ser_dict
====================================================================
AV In and out of Love
DB Till the sky falls down
MG In the name of love
TI Scream
dtype: object

pandas elegantly created series object by taking keys as series’s indices and values as series’s values.

Accessing still works fine like before.

ser_art['TI']
====================================================================
'Scream'
ser_art['DB']
====================================================================
'Till the sky falls down'

What to do if I want to get all the indices and values separately from a Series object?

Series object has index and values attribute that can pump out only indices and values of a particular series.

# get the indices only
ser_art.index
====================================================================
Index(['MG', 'TI', 'DB', 'AV'], dtype='object')
# get only values of the series
ser_art.values
====================================================================
array(['In the name of love', 'Scream', 'Till the sky falls down',
'In and out of Love'], dtype=object)

GreyAtom

GreyAtom is committed to building an educational ecosystem for learners to upskill & help them make a career in data science.

aniket jagtap

Written by

GreyAtom

GreyAtom

GreyAtom is committed to building an educational ecosystem for learners to upskill & help them make a career in data science.

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