Practical # 8
Practical # 8
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.
Simple example
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)
Creating arrays
Output
inspecting properties
import numpy as np
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)
# 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
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.