Unit 4 Python
Unit 4 Python
NumPy is a Python package that stands for ‘Numerical Python’. It is the core library for
scientific computing, which contains a powerful n-dimensional array object.
Python NumPy arrays provide tools for integrating C, C++, etc. It is also useful in linear
algebra, random number capability etc. NumPy array can also be used as an efficient multi-
dimensional container for generic data. Now, let me tell you what exactly is a Python NumPy
array.
Python NumPy Array: Numpy array is a powerful N-dimensional array object which is in the
form of rows and columns. We can initialize NumPy arrays from nested Python lists and access
it elements. In order to perform these NumPy operations, the next question which will come in
your mind is:
To install Python NumPy, go to your command prompt and type “pip install numpy”. Once the
installation is completed, go to your IDE (For example: PyCharm) and simply import it by
typing: “import numpy as np”
Moving ahead in python numpy tutorial, let us understand what exactly is a multi-dimensional
numPy array.
Here, I have different elements that are stored in their respective memory locations. It is said
to be two dimensional because it has rows as well as columns. In the above image, we have 3
columns and 4 rows available.
import numpy as
np
a=np.array([1,2,3])
print(a)
Output – [1 2 3]
Multi-dimensional Array:
a=np.array([(1,2,3),(4,5,6)])
print(a)
O/P – [[ 1 2 3]
[4 5 6]]
Many of you must be wondering that why do we use python NumPy if we already have Python
list? So, let us understand with some examples in this python NumPy tutorial.
We use python NumPy array instead of a list because of the below three reasons:
1. Less Memory
2. Fast
3. Convenient
The very first reason to choose python NumPy array is that it occupies less memory as
compared to list. Then, it is pretty fast in terms of execution and at the same time, it is very
convenient to work with NumPy. So these are the major advantages that Python NumPy array
has over list. Don’t worry, I am going to prove the above points one by one practically in
PyCharm. Consider the below example:
import numpy as np
import time
import sys
S= range(1000)
print(sys.getsizeof(5)*len(S))
D= np.arange(1000)
print(D.size*D.itemsize)
O/P – 14000
4000
The above output shows that the memory allocated by list (denoted by S) is 14000 whereas the
memory allocated by the NumPy array is just 4000. From this, you can conclude that there is
a major difference between the two and this makes Python NumPy array as the preferred choice
over list.
Next, let’s talk how python NumPy array is faster and more convenient when compared to list.
import time
import sys
SIZE = 1000000
L1= range(SIZE)
L2= range(SIZE)
A1= np.arange(SIZE)
A2=np.arange(SIZE)
start= time.time()
result=[(x,y) for x,y in zip(L1,L2)]
print((time.time()-start)*1000)
start=time.time()
result= A1+A2
print((time.time()-start)*1000)
O/P – 380.9998035430908
49.99995231628418
In the above code, we have defined two lists and two numpy arrays. Then, we have compared
the time taken in order to find the sum of lists and sum of numpy arrays both. If you see the
output of the above program, there is a significant change in the two values. List took 380ms
whereas the numpy array took almost 49ms. Hence, numpy array is faster than list. Now, if you
noticed we had run a ‘for’ loop for a list which returns the concatenation of both the lists
whereas for numpy arrays, we have just added the two array by simply printing A1+A2. That’s
why working with numpy is much easier and convenient when compared to the lists.
Therefore, the above examples proves the point as to why you should go for python numpy
array rather than a list!
Moving forward in python numpy tutorial, let’s focus on some of its operations.
You may go through this recording of Python NumPy tutorial where our instructor has
explained the topics in a detailed manner with examples that will help you to
Ndim
•
You can find the dimension of the array, whether it is a two-dimensional array or a
single dimensional array. So, let us see this practically how we can find the dimensions.
In the below code, with the help of ‘ndim’ function, I can find whether the array is of
single dimension or multi dimension.
import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
Output – 2
itemsize:
•
You can calculate the byte size of each element. In the below code, I have defined a
single dimensional array and with the help of ‘itemsize’ function, we can find the size
of each element.
import numpy as np
a = np.array([(1,2,3)])
print(a.itemsize)
Output – 4
dtype:
•
You can find the data type of the elements that are stored in an array. So, if you want
to know the data type of a particular element, you can use ‘dtype’ function which will
print the datatype along with the size. In the below code, I have defined an array
where I have used the same function.
import numpy as np
a = np.array([(1,2,3)])
print(a.dtype)
Output – int32
As you can see, the data type of the array is integer 32 bits. Similarly, you can find the size and
shape of the array using ‘size’ and ‘shape’ function respectively.
import numpy as np
a = np.array([(1,2,3,4,5,6)])
print(a.size)
print(a.shape)
Output – 6 (1,6)
Next, let us move forward and see what are the other operations that you can perform with
python numpy module. We can also perform reshape as well as slicing operation using python
numpy operation. But, what exactly is reshape and slicing? So let me explain this one by one
in this python numpy tutorial.
• reshape:
Reshape is when you change the number of rows and columns which gives a new view
to an object. Now, let us take an example to reshape the below array:
As you can see in the above image, we have 3 columns and 2 rows which has converted
into 2 columns and 3 rows. Let me show you practically how it’s done.
import numpy as np
a = np.array([(8,9,10),(11,12,13)])
print(a)
a=a.reshape(3,2)
print(a)
Output – [[ 8 9 10] [11 12 13]]
• linspace
This is another operation in python numpy which returns evenly spaced numbers over
a specified interval. Consider the below example:
import numpy as np
a=np.linspace(1,3,10)
print(a)
• Output – [ 1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111
2.33333333 2.55555556 2.77777778 3. ]
• As you can see in the result, it has printed 10 values between 1 to 3.
• max/ min
Next, we have some more operations in numpy such as to find the minimum, maximum
as well the sum of the numpy array. Let’s go ahead in python numpy tutorial and
execute it practically.
1import numpy as np
2
3a= np.array([1,2,3])
4print(a.min())
5print(a.max())
6print(a.sum())
• Output – 1 3 6
• You must be finding these pretty basic, but with the help of this knowledge you can
perform a lot bigger tasks as well. Now, lets understand the concept of axis in python
numpy.
•
•
•
As you can see in the figure, we have a numpy array 2*3. Here the rows are called as
axis 1 and the columns are called as axis 0. Now you must be wondering what is the
use of these axis?
• Suppose you want to calculate the sum of all the columns, then you can make use of
axis. Let me show you practically, how you can implement axis in your PyCharm:
1a= np.array([(1,2,3),(3,4,5)])
2print(a.sum(axis=0))
• Output – [4 6 8]
• Therefore, the sum of all the columns are added where 1+3=4, 2+4=6 and 3+5=8.
Similarly, if you replace the axis by 1, then it will print [6 12] where all the rows get
added.
• Square Root & Standard Deviation
There are various mathematical functions that can be performed using python numpy.
You can find the square root, standard deviation of the array. So, let’s implement
these operations:
1import numpy as np
2a=np.array([(1,2,3),(3,4,5,)])
3print(np.sqrt(a))
4print(np.std(a))
Output – [[ 1. 1.41421356 1.73205081]
[ 1.73205081 2. 2.23606798]]
1.29099444874
As you can see the output above, the square root of all the elements are printed. Also,
the standard deviation is printed for the above array i.e how much each element varies
from the mean value of the python numpy array.
• Addition Operation
1import numpy as np
2x= np.array([(1,2,3),(3,4,5)])
3y= np.array([(1,2,3),(3,4,5)])
4print(x+y)
• Output – [[ 2 4 6] [ 6 8 10]]
• This is extremely simple! Right? Similarly, we can perform other operations such as
subtraction, multiplication and division. Consider the below example:
1import numpy as np
2x= np.array([(1,2,3),(3,4,5)])
3y= np.array([(1,2,3),(3,4,5)])
4print(x-y)
5print(x*y)
6print(x/y)
• Output – [[0 0 0] [0 0 0]]
[[ 1 4 9] [ 9 16 25]]
[[ 1. 1. 1.] [ 1. 1. 1.]]
• Vertical & Horizontal Stacking
Next, if you want to concatenate two arrays and not just add them, you
• ravel
There is one more operation where you can convert one numpy array into a single
column i.e ravel. Let me show how it is implemented practically:
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
print(x.ravel())
• Output – [ 1 2 3 3 4 5]
• Let’s move forward in python numpy tutorial, and look at some of its special
functions.
2012. Over time many versions of pandas have been released. The latest version of the
pandas is 1.3.4
Advantages
• Fast and efficient for manipulating and analyzing data.
• Data from different file objects can be loaded.
• Easy handling of missing data (represented as NaN) in floating point as well as
non-floating point data
• Size mutability: columns can be inserted and deleted from DataFrame and higher
dimensional objects
• Data set merging and joining.
• Flexible reshaping and pivoting of data sets
• Provides time-series functionality.
• Powerful group by functionality for performing split-apply-combine operations on
data sets.
Getting Started
After the pandas have been installed into the system, you need to import the library. This
module is generally imported as:
import pandas as pd
Here, pd is referred to as an alias to the Pandas. However, it is not necessary to import the
library using the alias, it just helps in writing less amount code every time a method or
property is called.
Pandas generally provide two data structures for manipulating data, They are:
• Series
• DataFrame
Series:
Pandas Series is a one-dimensional labelled array capable of holding data of any type
(integer, string, float, python objects, etc.). The axis labels are collectively called indexes.
Pandas Series is nothing but a column in an excel sheet. Labels need not be unique but must
be a hashable type. The object supports both integer and label-based indexing and provides a
host of methods for performing operations involving the index.
A pandas Series can be created using the following constructor −
pandas.Series( data, index, dtype, copy)
In the real world, a Pandas Series will be created by loading the datasets from existing
storage, storage can be SQL Database, CSV file, an Excel file. Pandas Series can be created
from the lists, dictionary, and from a scalar value etc.
Example:
import pandas as pd
import numpy as np
# Creating empty series
ser = pd.Series()
print(ser)
# simple array
data = np.array(['g', 'e', 'e', 'k', 's'])
ser = pd.Series(data)
print(ser)
Output:
Series([], dtype: float64)
0 g
1 e
2 e
3 k
4 s
dtype: object
Example 2
DataFrame
Creating a DataFrame:
In the real world, a Pandas DataFrame will be created by loading the datasets from existing
storage, storage can be SQL Database, CSV file, an Excel file. Pandas DataFrame can be
created from the lists, dictionary, and from a list of dictionaries, etc.
A pandas DataFrame can be created using various inputs like −
• Lists
• dict
• Series
• Numpy ndarrays
• Another DataFrame
Example:
import pandas as p
# Calling DataFrame constructor
df = pd.DataFrame()
print(df)
# list of strings
lst = ['python', 'programming', 'is', 'easy',
'to', 'undertanding', 'students']
Output:
Empty DataFrame
Columns: []
Index: []
0
0 python
1 language
2 is
3 easy
4 to
5 understanding
6 students
Example 2
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print df
Its output is as follows –
Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
panel
A panel is a 3D container of data. The term Panel data is derived from econometrics and is
partially responsible for the name pandas − pan(el)-da(ta)-s.
The names for the 3 axes are intended to give some semantic meaning to describing operations
involving panel data. They are −
• items − axis 0, each item corresponds to a DataFrame contained inside.
Importing matplotlib :
y = [10, 5, 8, 4, 2]
# Function to plot
plt.plot(x,y)
# function to show the plot
plt.show()
Output :
Bar plot :
Output :
Histogram :
Output :
Scatter Plot :
Output :
Python Plotly
Python Plotly Library is an open-source library that can be used for data visualization
and understanding data simply and easily. Plotly supports various types of plots like line
charts, scatter plots, histograms, cox plots, etc. So you all must be wondering why Plotly over
other visualization tools or libraries? Here’s the answer –
• Plotly has hover tool capabilities that allow us to detect any outliers or anomalies
in a large number of data points.
• It is visually attractive that can be accepted by a wide range of audiences.
• It allows us for the endless customization of our graphs that makes our plot more
meaningful and understandable for others.
Installation
Plotly does not come built-in with Python. To install it type the below command in
the terminal.
pip install plotly
Package Structure of Plotly
There are three main modules in Plotly. They are:
• plotly.plotly
• plotly.graph.objects
• plotly.tools
plotly.plotly acts as the interface between the local machine and Plotly. It contains functions
that require a response from Plotly’s server.
plotly.graph_objects module contains the objects (Figure, layout, data, and the definition of
the plots like scatter plot, line chart) that are responsible for creating the plots. The Figure
can be represented either as dict or instances of plotly.graph_objects.Figure and these are
serialized as JSON before it gets passed to plotly.js. Consider the below example for better
understanding.
Example:
• Python3
import plotly.express as px
Output:
Figures are represented as trees where the root node has three top layer attributes
– data, layout, and frames and the named nodes called ‘attributes’. Consider the above
example, layout.legend is a nested dictionary where the legend is the key inside the
dictionary whose value is also a dictionary.
plotly.tools module contains various tools in the forms of the functions that can enhance
the Plotly experience.
Line Chart
Line plot in Plotly is much accessible and illustrious annexation to plotly which manage a
variety of types of data and assemble easy-to-style statistic. With px.line each data position
is represented as a vertex (which location is given by the x and y columns) of a polyline
mark in 2D space.
Example:
import plotly.express as px
Output:
Bar Chart
A bar chart is a pictorial representation of data that presents categorical data with
rectangular bars with heights or lengths proportional to the values that they represent. In other
words, it is the pictorial representation of dataset. These data sets contain the numerical
values of variables that represent the length or height.
Example:
import plotly.express as px
Output:
Python Frameworks
Python is one of the most lucrative programming languages that is used as
the main coding language by more than 80% of the developers. It is one of
the fastest-growing programming languages that is embedded with extensive
libraries and frameworks to fuel up different processes. Popular companies like
Oracle, Amazon, Instagram, Google, and many more are using Python in different
ways. Python is the home of most trending technologies like Artificial Intelligence
and Machine Learning processes.
Some of the amazing features of Python are – easy to learn and code, facilitates
object-oriented programming, platform independence, enhanced performance,
and security. It is used in different industries like Finance, Healthcare, Education,
Banking, Electronics, and many more.
It is embedded with a lot of frameworks for web development, data science,
artificial intelligence, and a lot more
1. Django
It is a high-level Python framework that facilitates the concise design and rapid
development. As per the latest survey, Django is the 4th most wanted framework
for web development. It offers some distinguishing features like ORM, URL
routing, database schema migration which makes Django really fast. Some of the
popular sites that use Django are Instagram, Mozilla, Pinterest, National
Geographic, and many more. So this is one of the in-demand frameworks of
Python that you must learn in 2022.
It offers amazing features like:
• Ready-made components to use for rapid development
• It is embedded with enhanced security features
• Highly scalable and extremely versatile
Flask:
Flask is a web application framework written in Python. Flask is based on the
Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.
Installation:
We will require two package to setup your environment. virtualenv for a user to
create multiple Python environments side-by-side. Thereby, it can avoid
compatibility issues between the different versions of the libraries and the next
will be Flask itself.
• virtualenv
pip install virtualenv
• Flask
pip install Flask
After completing the installation of the package, let’s get our hands on the code.
Save it in a file and then run the script we will be getting an output like this.
Then go to the url given there you will seeing your first webpage displaying hello
world there on your local server.
Digging further into the context, the route() decorator in Flask is used to bind
URL to a function. Now to extend this functionality our small web app is also
equipped with another method add_url_rule() which is a function of an
application object is also available to bind a URL with a function as in the above
example, route() is used.
Example:
def gfg():
return ‘geeksforgeeks’
app.add_url_rule(‘/’, ‘g2g’, gfg)
Output:
geeksforgeeks
You can also add variables in your webapp, well you might be thinking how it’ll
help you, it’ll help you to build a URL dynamically. So lets figure it out with an
example.
app = Flask(__name__)
@app.route('/hello/<name>')
def hello_name(name):
if __name__ == '__main__':
app.run()
We can a
.