Practicacl - 1 Aim: Basic Python Programs. (Numpy, Pandas, Matplotlib)
Practicacl - 1 Aim: Basic Python Programs. (Numpy, Pandas, Matplotlib)
PRACTICACL - 1
Aim: Basic Python programs. [ NumPy, Pandas, Matplotlib].
Numpy:
NumPy (Numerical Python) is a perfect tool for scientific computing and performing
basic and advanced array operations.
The library offers many handy features performing operations on n-arrays and
matrices in Python. It helps to process arrays that store values of the same data type
and makes performing math operations on arrays (and their vectorization) easier.
In fact, the vectorization of mathematical operations on the NumPy array type
increases performance and accelerates the execution time.
Example:
import numpy as np
array = np.array([1,2,3,4,5,6,7])
print(array)
[1 2 3 4 5 6 7]
array_1 = np.arange(1,100)
print(array_1)
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
97 98 99]
array_2 = np.mean(array)
print(array_2)
4.0
array_3 = np.random.rand(3,2)
print(array_3)
[[0.35188334 0.98612433]
[0.52235066 0.72803476]
[0.13853962 0.81346437]]
array([[1, 3, 2, 3],
[2, 4, 0, 4]])
IU2141230089 DSC(CE0630) 6CSE-B1
Pandas:
Pandas is a library created to help developers work with "labeled" and "relational"
data intuitively. It's based on two main data structures: "Series" (one-dimensional,
like a list of items) and "Data Frames" (two-dimensional, like a table with multiple
columns).
Pandas allows converting data structures to Data Frame objects, handling missing
data, and adding/deleting columns from Data Frame, imputing missing files, and
plotting data with histogram or plot box. It’s a must-have for data wrangling,
manipulation, and visualization.
Example:
import pandas as pd
series = pd.Series([1,2,3,4])
series
0 1
1 2
2 3
3 4
dtype: int64
dataframe =
pd.DataFrame({"Name":["Vinayak","Chirag","Kavan","Harsh","Jenil","Darshil","A
yush"],"Age":[20,20,20,20,20,20,20]})
dataframe
Name Age
0 Vinayak 20
1 Chirag 20
2 Kavan 20
3 Harsh 20
4 Jenil 20
5 Darshil 20
6 Ayush 20
Matplotlib:
This is a standard data science library that helps to generate data visualizations
such as two-dimensional diagrams and graphs (histograms, scatterplots, non-
Cartesian coordinates graphs). Matplotlib is one of those plotting libraries that are
really useful in data science projects — it provides an object-oriented API for
embedding plots into applications.
It's thanks to this library that Python can compete with scientific tools like Mat Lab
or Mathematica. However, developers need to write more code than usual while
IU2141230089 DSC(CE0630) 6CSE-B1
using this library for generating advanced visualizations. Note that popular plotting
libraries work seamlessly with Matplotlib.
Example:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()
IU2141230089 DSC(CE0630) 6CSE-B1
img = np.asarray(Image.open('stinkbug.png'))
print(repr(img))
...,
imgplot = plt.imshow(img)
lum_img = img[:, :, 0]
plt.imshow(lum_img)
IU2141230089 DSC(CE0630) 6CSE-B1
<matplotlib.image.AxesImage at 0x7991e8426440>