0% found this document useful (0 votes)
29 views16 pages

Practical # 8

Practical nunber 8 for DSA

Uploaded by

Alishba Aleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views16 pages

Practical # 8

Practical nunber 8 for DSA

Uploaded by

Alishba Aleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Department of Software Engineering

Mehran University of Engineering and Technology, Jamshoro

Course: SWE – Data Analytics and Business Intelligence


Instructor Ms Sana Faiz Practical/Lab No. 08
Date CLOs 03
Signature Assessment Score

Topic To understand basics of python


Objectives To become familiar with Python libraries for Data Sciences (NumPy
and SciPy)

Lab Discussion: Theoretical concepts and Procedural steps

Python Libraries for Data Science


 Many popular Python toolboxes/libraries:
o NumPy
o SciPy
o Pandas
o SciKit-Learn

Visualization libraries
 Visualize computed data using following libraries.
 matplotlib
 Seaborn

NumPy
 NumPy is the fundamental package for scientific computing with Python. It contains among
other things:
 a powerful N-dimensional array object
 Sophisticated (broadcasting) functions
 Tools for integrating C/C++ and Fortran code
 Useful linear algebra, Fourier transform, and random number capabilities
 Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional
container of generic data. Arbitrary data-types can be defined. This allows NumPy to
seamlessly and speedily integrate with a wide variety of databases.
 NumPy is licensed under the BSD license, enabling reuse with few restrictions.

NumPy – A Replacement for MatLab


 NumPy is often used along with packages like SciPy (Scientific Python)
and Mat−plotlib (plotting library). This combination is widely used as a replacement for
MatLab, a popular platform for technical computing

Simple example

from google.colab import files

uploaded = files.upload()

import numpy as np

# Load the CSV file, assuming the first row is the header and we want to
skip it
arr = np.genfromtxt("weather1.csv", delimiter=",", skip_header=1)

# Print the first three rows of the array


print(arr[:3])

Creating arrays
Output

inspecting properties
import numpy as np

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

print("Returns number of elements in arr \n", arr.size)


print("Returns dimensions of arr (rows, columns) \n", arr.shape)
print("Returns type of elements in arr \n", arr.dtype)
print("Convert arr elements to type dtype \n", arr.astype(float))
print("Convert arr to a Python list \n", arr.tolist())
print("View documentation for np.eye \n", np.info(np.eye))

Output

Copying/sorting/reshaping
Output

Adding/removing Elements
output

Combining/splitting

output
Indexing/slicing/subsetting

Output

Scalar Math
output

Vector Math

output
Statistics
Output

SCIPY
 SciPy, pronounced as Sigh Pi, is a scientific python open source, distributed under the BSD
licensed library to perform Mathematical, Scientific and Engineering Computations.
 The SciPy library depends on NumPy, which provides convenient and fast N-dimensional
array manipulation. The SciPy library is built to work with NumPy arrays and provides many
user-friendly and efficient numerical practices such as routines for numerical integration and
optimization. Together, they run on all popular operating systems, are quick to install and are
free of charge. NumPy and SciPy are easy to use, but powerful enough to depend on by some
of the world's leading scientists and engineers.
Why scipy
 SciPy. Numpy provides a high-performance multidimensional array and basic tools to
compute with and manipulate these arrays. SciPy builds on this, and provides a large number
of functions that operate on numpy arrays and are useful for different types of scientific and
engineering applications.
What is the difference between NumPy and SciPy?
 In an ideal world, NumPy would contain nothing but the array data type and the most basic
operations: indexing, sorting, reshaping, basic elementwise functions, et cetera. All numerical
code would reside in SciPy. However, one of NumPy’s important goals is compatibility, so
NumPy tries to retain all features supported by either of its predecessors.
 Thus NumPy contains some linear algebra functions, even though these more properly
belong in SciPy. In any case, SciPy contains more fully-featured versions of the linear
algebra modules, as well as many other numerical algorithms. If you are doing scientific
computing with python, you should probably install both NumPy and SciPy. Most new
features belong in SciPy rather than NumPy.

Scipy function
 Scipy functions

import numpy as np
from scipy import linalg

# Creating Matrices
A = np.array([[1, 2, 3], [5, 3, 8], [5, 2, 9]])
F = np.eye(3)

# Basic Matrix Operations

# Inverse
print("Inverse \n", linalg.inv(A))

# Transposition
print("Transpose matrix \n", A.T)
print("Conjugate transposition \n", A.conj().T)

# Trace
print("Trace \n", np.trace(A))

# Determinant
print("Determinant \n", linalg.det(A))
SCIPY function

Output
Linear Equations

 The scipy.linalg.solve feature solves the linear equation a * x + b * y = Z, for the unknown x,
y values.
 As an example, assume that it is desired to solve the following simultaneous equations.
x + 3y + 5z = 10
2x + 5y + z = 8
2x + 3y + 8z = 3

CODE
Output

MATPLOTLIB
 Python 2D plotting library which produces publication quality figures in a variety of
hardcopy formats
 A set of functionalities similar to those of MATLAB
 Line plots, scatter plots, bar charts, histograms, pie charts etc.
 Relatively low-level; some effort needed to create advanced visualization

Histogram
import numpy as np
import matplotlib.pyplot as plt

# Create the 2D array


arr = np.array([[1, 2, 3], [4, 5, 6], [6, 5, 4]])

# Flatten the array before constructing the histogram


arr_flattened = arr.flatten()

# Construct the histogram with the flattened array


plt.hist(arr_flattened)
# Add a title to the plot
plt.title('Histogram')

# Show the plot


plt.show()

Pie chart
import matplotlib.pyplot as plt

# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0) # explode 1st slice

# Plot
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)

plt.axis('equal')
plt.show()
Xz

Class Tasks
Submission Date: --

 Perform all the included operations on the dataset of your own choice and plot histogram and
pie chart of each task.

You might also like