3 Python
3 Python
Dr.M.Dhurgadevi
Associate Professor
Sri Krishna College of Technology
Coimbatore
Libraries
NumPy is the most foundational package for numerical
computing in Python.
Indeed, many other libraries, such as pandas and scikit-
learn, use NumPy’s array objects as the lingua franca for
data exchange.
One of the reasons as to why NumPy is so important for
numerical computations is because it is designed for
efficiency with large arrays of data. The reasons for this
include:
- It stores data internally in a continuous block of
memory, independent of other in-built Python objects.
- It performs complex computations on entire arrays
without the need for for loops.
Creating a NumPy array
To understand these advantages, lets create an array.
One of the most common, of the many, ways to create a NumPy
array is to create one from a list by passing it to the np.array()
function.
I O
n ut
: :
Differences between lists and ndarrays
The key difference between an array and
a list is that arrays are designed to handle
vectorised operations while a python lists
are not.
That means, if you apply a function, it is
performed on every item in the array,
rather than on the whole array object.
Pandas
Pandas, like NumPy, is one of the most
popular Python libraries for data analysis.
It is a high-level abstraction over low-
level NumPy, which is written in pure C.
Pandas provides high-performance, easy-
to-use data structures and data analysis
tools.
There are two main structures used by
pandas; data frames and series.
Indices in a pandas series
A pandas series is similar to a list, but differs in the fact that a series
associates a label with each element. This makes it look like a dictionary.
If an index is not explicitly provided by the user, pandas creates a
RangeIndex ranging from 0 to N-1.
Each series object also has a data type.
I O
n u
: t:
As you may suspect by this point, a series has ways to
extract all of the values in the series, as well as
individual elements by index.
I O
n u
: t:
I
n
:
O
ut
:
You can also create a data frame from a list.
I O
n ut
: :
Python Code
import pandas as pd
# reading the database
data = pd.read_csv("tips.csv")
# printing the top 10 rows
display(data.head(10))
Output
Python code – Draw Scatter plot
import pandas as pd
import matplotlib.pyplot as plt
# Scatter plot with day against tip
plt.scatter(data['day'], data['tip'], c=data['size'],
s=data['total_bill'])
# Adding Title to the Plot
plt.title("Scatter Plot")
# Setting the X and Y labels
plt.xlabel('Day')
plt.ylabel('Tip')
plt.colorbar()
Plt.show()
Output
Matrix Multiplication
import numpy as np
A = np.array([[1,2,3],[4,5,6]]) # (2 x 3)mat
matrix B =np.array([[7,8],[9,10],[11,12]])
# create (3 x 2)
matrix A.shape[1] == B.shape[0]
# ensures two matrices are compatible
C = np.zeros((2,2)) # (2 x 2) matrix
for i in range(2): for k in range(2):
for j in range(3):
C[i,k]= C[i,k] + A[i,j]*B[j,k]
print(C)
Output
[[ 58, 64] [139, 154]]