0% found this document useful (0 votes)
7 views

Develop Programs To Understand Concept of Class and Object in Python

Uploaded by

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

Develop Programs To Understand Concept of Class and Object in Python

Uploaded by

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

SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150711

Practical 5
Aim: Develop programs to understand concept of Class and Object in python.

Theory:
- Class: A blueprint for creating objects. It defines attributes (data) and methods (behavior).
- Example:
```python
class ClassName:
- Object: An instance of a class. Objects hold the actual data and behavior defined by the
class.
- Example:
```python
obj = ClassName()
```
- Instance Variables: Unique to each object, defined inside the constructor (` init `).
- Class Variables: Shared by all objects of the class.
- Constructor (` init `): A special method called when an object is created, used to
initialize instance variables.
- Method: A function defined inside a class, used to define behavior for the objects.

ENROLLMENT NO.: 220410107118 Page|18


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

Answer :
class Person:
def init (self, name, age):
self.name = name
self.age = age

def display_info(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")

person1 = Person("Alice", 25)


person1.display_info()

print("\nMy Name: Sonu Choudhary")


print("Batch: TYCE-2 BATCH C")
print("Enrollment Number: 220410107118")

ENROLLMENT NO.: 220410107118 Page|19


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150711

Diagram:

ENROLLMENT NO.: 220410107118 Page|20


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Practical 6
Aim: Develop programs to demonstrate use of NumPy

1. Print Numpy version and configuration information.

import numpy as np
print("Numpy version :",np. version )
print("Numpy Configuration :\n",np.show_config())

Diagram:

ENROLLMENT NO.: 220410107118 Page|21


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

2. Create a numpy array with numbers ranging from 50 to 100.Also print


attributes of created object.

import numpy as np
arr = np.arange(10, 70)
print("Array:", arr)
print("Shape:", arr.shape)
print("Data Type:", arr.dtype)
print("Number of Dimensions:", arr.ndim)

Diagram:

3. Create a null vector of size 10.

import numpy as np
null_vector=np.zeros(5)
print("Null Vector:", null_vector)

Diagram:

ENROLLMENT NO.: 220410107118 Page|22


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

4. Create a vector of size 10 initialized with a seed equal to last four digits
of your enrolment number.

import numpy as np
import random
seed=2029
np.random.seed(seed)
random_vector=np.random.randint(0,100,10)
print("Random vector:",random_vector)

Diagram:

5. Create a matrix of size 5x5 initialized with random integers with a seed
equal to last four digits of your enrolment number.

import numpy as np
seed=2029
np.random.seed(seed)
random_int_vector=np.random.randint(1,10,size=(5,5))
print("Random Integer Vector:\n",random_int_vector)

Diagram:

ENROLLMENT NO.: 220410107118 Page|23


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

6. Create an identity matrix of size 5x5.

import numpy as np
identity=np.identity(5)
print("Identity Matrix:\n",identity)

Diagram:

7. Do the following
a. Create a numpy array mat1 of size5x2 with numbers ranging from 1 to
10.
b. Create an other numpy array mat2 of size2x5 with floating point
numbers between 10 to 20.
c. Calculate the matrix product of mat1 and mat2.
d. Print vector containing minimum and maximum in each row of mat1.
e. Print vector containing mean and standard deviation in each column of
mat2 Perform vstackand hstack on mat1 and mat2.
f. Perform vstackand hstack on mat1 and mat2.
g. Split mat1 horizontal into 2 and split mat2 vertical into 2 parts.

ENROLLMENT NO.: 220410107118 Page|24


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Answer:
import numpy as np
mat1 = np.arange(1, 11).reshape(5, 2)
print("Matrix 1 (mat1):\n", mat1)

mat2 = np.random.uniform(10, 20, (2, 5))


print("Matrix 2 (mat2):\n", mat2)

mat_product = np.dot(mat1, mat2)


print("Matrix Product of mat1 and mat2:\n", mat_product)

min_max_rows = np.vstack((np.min(mat1, axis=1), np.max(mat1, axis=1)))


print("Minimum and Maximum in Each Row of mat1:\n", min_max_rows)

mean_std_cols = np.vstack((np.mean(mat2, axis=0), np.std(mat2, axis=0)))


print("Mean and Standard Deviation in Each Column of mat2:\n", mean_std_cols)

hstack_result = np.hstack((mat1.T, mat2))


vstack_result = np.vstack((mat1, mat2.T))
print("Hstack result:\n", hstack_result)
print("Vstack result:\n", vstack_result)

split_mat1 = np.hsplit(mat1, 2)
split_mat2 = np.vsplit(mat2, 2)
print("Split mat1 into 2 parts horizontally:\n", split_mat1)
print("Split mat2 into 2 parts vertically:\n", split_mat2)

ENROLLMENT NO.: 220410107118 Page|25


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Diagram:

ENROLLMENT NO.: 220410107118 Page|26


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

8. Create a vector vec of size 20 initialized with double numbers ranging


from20 to 40
and do the following.
a. Reshape it to a 5x4 matrix mat. b. Print every 4th element of vec. c. Print
elements of vec less than or equal to 25.
d. In vec assign every number at even location to 1.
e. In vec assign from start position to10th position every second element to
0.
f. Reverse elements of vec. g. In mat print each row in the second column
and print each column in the second
row.
h. Apply some universal functions such as sin,sqrt,cos,exp on vec.
i. Print transpose and inverse of mat.

import numpy as np
from numpy.linalg import inv

vec = np.linspace(20, 40, 20)

mat = vec.reshape(5, 4)
print("Matrix mat:\n", mat)

print("Every 4th element of vec:", vec[3::4])

print("Elements of vec less than or equal to 25:", vec[vec <= 25])

ENROLLMENT NO.: 220410107118 Page|27


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

vec[1::2] = 1
print("vec after assigning every number at even location to 1:\n", vec)

vec[:10:2] = 0
print("vec after assigning every second element from start to 10th position to 0:\n", vec)

vec = vec[::-1]
print("Reversed vec:\n", vec)

print("Each row in the second column of mat:\n", mat[:, 1])


print("Each column in the second row of mat:\n", mat[1, :])

print("sin(vec):\n", np.sin(vec))
print("sqrt(vec):\n", np.sqrt(vec))
print("cos(vec):\n", np.cos(vec))
print("exp(vec):\n", np.exp(vec))

mat_transpose = np.transpose(mat)
print("Transpose of mat:\n", mat_transpose)

try:
mat_inverse = inv(mat)
print("Inverse of mat:\n", mat_inverse)
except np.linalg.LinAlgError:
print("Inverse of mat is not possible due to matrix dimensions or rank.")

ENROLLMENT NO.: 220410107118 Page|28


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Diagram:

9. Demonstrate shallow copy and deep copy.


import numpy as np

shallow_copy = mat.view()
deep_copy = mat.copy()

shallow_copy[0, 0] = 999
deep_copy[1, 1] = 888
print("Original mat:\n", mat)

ENROLLMENT NO.: 220410107118 Page|29


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

print("Shallow Copy:\n", shallow_copy)


print("Deep Copy:\n", deep_copy)

Diagram:

ENROLLMENT NO.: 220410107118 Page|30


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Practical 7
Aim: Python File Handling to load text & image data

1. Create File with two columns X1 and Y1.


Generate 20 rows with random data for values of X1 and Y1.
import random
data = [(random.randint(1, 100), random.randint(1, 100)) for _ in range(20)]
with open("data.txt", "w") as file:
file.write("X1\tY1\n")
for x, y in data:
file.write(f"{x}\t{y}\n")

ENROLLMENT NO.: 220410107118 Page|31


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Diagram:

ENROLLMENT NO.: 220410107118 Page|32


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

2.Demonstrate concept to fstream in gandsampling using above generated


file. Fetch every even record from file and Fetch any 5 random records
from file to demonstrate sampling.

even_records = []
with open("data.txt", "r") as file:
next(file)
for i, line in enumerate(file, start=1):
if i % 2 == 0:
x, y = line.strip().split("\t")
even_records.append((int(x), int(y)))

print("Every even record:")


print(even_records)

import random

random_records = []
with open("data.txt", "r") as file:
next(file)
lines = file.readlines()

random_records = random.sample(lines, 5)
print("Random records:")
for line in random_records:
x, y = line.strip().split("\t")
print(f"{x}\t{y}")

ENROLLMENT NO.: 220410107118 Page|33


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Diagram:

3. Download and load imaged ata from the following URL.


https://unsplash.com/photos/pypeCEaJeZY

import requests
from PIL import Image
from io import BytesIO

image_url = "https://images.unsplash.com/photo-1542744173-05336fcc7ad4?ixlib=rb-
4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNoYXJ0fGVufDB8fDB8fHww&w
=1000&q=80"

try:
response = requests.get(image_url, allow_redirects=True)
if response.status_code == 200:
image = Image.open(BytesIO(response.content))
image.show()
print("Image Displayed Successfully!!")
else:
print("Failed to download the image. Status code:", response.status_code)
except Exception as e:

ENROLLMENT NO.: 220410107118 Page|34


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

print("An error occurred:", str(e))

Diagram:

ENROLLMENT NO.: 220410107118 Page|35


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

PRACTICAL – 8

Aim: Develop programs to demonstrate use of Pandas for Conditioning Your


Data.

1. Read the xml file (test.xml) and create a dataframe from it and do the
following. Find and print duplicate records. Remove duplicates and save data in
other dataframe.

import pandas as pd

# Read the XML file into a DataFrame


xml_file_path = '/content/test.xml'
df = pd.read_xml(xml_file_path)
# Find and print duplicate records
duplicate_records = df[df.duplicated()]
print("Duplicate Records:")
print(duplicate_records)
# Remove duplicates and save data in another DataFrame
df_no_duplicates = df.drop_duplicates()
# If you want to save the DataFrame with no duplicates to a new XML file
df_no_duplicates.to_xml('/content/no_duplicates.xml', index=False)

Diagram :

ENROLLMENT NO.: 220410107118 Page|36


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Fig : test.xml

2) Read the csv file (indian_food.csv). Consider value -1 for missing or NA


values.(Replace -1 with NaN when reading a csv file.)
(a) Print the first and last 10 records of dataframe, also print column names and
summary of data.Print information about data such as data types of each
column.

import pandas as pd
import numpy as np

# Read the CSV file and replace -1 with NaN


df = pd.read_csv("indian_food.csv", na_values=-1)

# Print the first 10 records


print("First 10 records:")
print(df.head(10))

# Print the last 10 records


print("\nLast 10 records:")
print(df.tail(10))

ENROLLMENT NO.: 220410107118 Page|37


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

# Print column names


print("\nColumn names:")
print(df.columns)
# Print summary statistics
print("\nSummary of data:")
print(df.describe())

Diagram :

ENROLLMENT NO.: 220410107118 Page|38


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

ENROLLMENT NO.: 220410107118 Page|39


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

(b) Print information about data such as data types of each column.

# Print data types of each column


print("\nData types of each column:")
df.info()

Diagram:

(c) Convert columns with name course,diet,flavor_profile,state,region to


categorical data type & print data type for dataframe using info function.
Categories are defined as follows. Course ['dessert' 'main course' 'starter' 'snack']
Flavor_profile ['sweet' 'spicy' 'bitter' 'sour']
State ['West Bengal' 'Rajasthan' 'Punjab' 'Uttar Pradesh' 'Odisha' 'Maharashtra'
'Uttarakhand' 'Assam' 'Bihar' 'Andhra Pradesh' 'Karnataka' 'Telangana' 'Kerala'
'Tamil Nadu' 'Gujarat' 'Tripura' 'Manipur' 'Nagaland' 'NCT of Delhi' 'Jammu &
Kashmir' 'Chhattisgarh' 'Haryana' 'Madhya Pradesh' 'Goa'] Region ['East' 'West'
'North' nan 'North East' 'South' 'Central']

# Define the categories for the specified columns


course_categories = ['dessert', 'main course', 'starter', 'snack']
flavor_profile_categories = ['sweet', 'spicy', 'bitter', 'sour']
state_categories = ['West Bengal', 'Rajasthan', 'Punjab', 'Uttar Pradesh', 'Odisha',

ENROLLMENT NO.: 220410107118 Page|40


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

'Maharashtra', 'Uttarakhand', 'Assam', 'Bihar', 'Andhra Pradesh', 'Karnataka', 'Telangana',


'Kerala', 'Tamil Nadu', 'Gujarat', 'Tripura', 'Manipur', 'Nagaland', 'NCT of Delhi', 'Jammu &
Kashmir', 'Chhattisgarh', 'Haryana', 'Madhya Pradesh', 'Goa']

# Replace NaN values in the 'region' column with 'Unknown'


df['region'].fillna('Unknown', inplace=True)
# Convert specified columns to categorical with defined categories
df['course'] = pd.Categorical(df['course'], categories=course_categories)
df['diet'] = pd.Categorical(df['diet'])
df['flavor_profile'] = pd.Categorical(df['flavor_profile'],
categories=flavor_profile_categories)
df['state'] = pd.Categorical(df['state'], categories=state_categories)
df['region'] = pd.Categorical(df['region'])

# Print data types of each column using info()


print("\nData types of each column after conversion:")
df.info()

Diagram:

ENROLLMENT NO.: 220410107118 Page|41


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

(d)Print name of items with course as dessert.


# Select items with course as "dessert"
dessert_items = df[df['course'] == 'dessert']

# Print the names of dessert items


dessert_item_names = dessert_items['name']

print("Names of items with course as 'dessert':")


for name in dessert_item_names:
print(name)

Diagram:

ENROLLMENT NO.: 220410107118 Page|42


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

(e) Print count of items with flavor_profile with sweet type.

# Count items with flavor_profile as "sweet"


sweet_items_count = (df['flavor_profile'] == 'sweet').sum()

# Print the count


print("Count of items with flavor_profile as 'sweet':", sweet_items_count)

Diagram:

(f) Print name of items with cooking_time < prep_time.

# Select items with cooking_time < prep_time


items_with_cooking_less_than_prep = df[df[‘cook_time’] < df[‘prep_time’]]

# Print the names of items with cooking_time < prep_time


item_names = items_with_cooking_less_than_prep[‘name’]
print(“Names of items with cooking_time < prep_time:”)
for name in item_names:
print(name)

ENROLLMENT NO.: 220410107118 Page|43


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Diagram:

(g) Print summary of data grouped by diet column.

# Group the data by the ‘diet’ column and get a summary


diet_summary = df.groupby(‘diet’).describe()

# Print the summary


print(“Summary of data grouped by ‘diet’ column:”)
print(diet_summary)

ENROLLMENT NO.: 220410107118 Page|44


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Diagram:

(h) Print average cooking_time & prep_time for vegetarian diet type.

# Select items with a vegetarian diet


vegetarian_items = df[df['diet'] == 'vegetarian']
# Calculate the average cooking_time and prep_time for vegetarian items
avg_cooking_time = vegetarian_items['cook_time'].mean()
avg_prep_time = vegetarian_items['prep_time'].mean()
# Print the average cooking_time and prep_time
print("Average cooking_time for vegetarian diet type:", avg_cooking_time)
print("Average prep_time for vegetarian diet type:", avg_prep_time)

Diagram:

ENROLLMENT NO.: 220410107118 Page|45


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

(i) Insert a new column with column name as total_time which contains sum of
cooking_time & prep_time into existing dataframe.

# Insert a new column ‘total_time’ with the sum of cooking_time and prep_time
df[‘total_time’] = df[‘cook_time’] + df[‘prep_time’]
# Print the DataFrame to verify the new column
print(df)

Diagram:

ENROLLMENT NO.: 220410107118 Page|46


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

(j) Print name,cooking_time,prep_time,total_time of items with total_time


>=500.
# Filter items with total_time >= 500
items_with_total_time_over_500 = df[df['total_time'] >= 500]

# Select and print the specific columns


selected_columns = items_with_total_time_over_500[['name', 'cook_time', 'prep_time',
'total_time']]
print("Items with total_time >= 500:")
print(selected_columns)

Diagram :

(k) Print count of items with various flavour_profile per region.


#e.g.
#region flavor_profile
#Central spicy 2
#sweet 1
#East spicy 5
# sweet 20

# Group the data by 'region' and 'flavor_profile', and get the count
flavor_profile_count = df.groupby(['region',
'flavor_profile']).size().reset_index(name='count')

ENROLLMENT NO.: 220410107118 Page|47


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

# Print the count of items with various flavor profiles per region
print("Count of items with various flavor profiles per region:")
print(flavor_profile_count)

Diagram:

(l) Find & print records with missing data in the state column. Fill missing data
in the state column with -.

# Find and print records with missing data in the 'state' column
missing_state_records = df[df['state'].isnull()]
print("Records with missing data in the 'state' column:")
print(missing_state_records)

ENROLLMENT NO.: 220410107118 Page|48


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

# Fill missing data in the 'state' column with "-" and set categories
df['state'].cat.add_categories(['-'], inplace=True)
df['state'].fillna('-', inplace=True)

# Verify that missing data has been filled


missing_state_count = df['state'].isnull().sum()
print(f"\nMissing data in the 'state' column after filling: {missing_state_count}")

Diagram :

ENROLLMENT NO.: 220410107118 Page|49


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

ENROLLMENT NO.: 220410107118 Page|50


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Practical 9
Aim: Develop programs to visualize the data using matplotlib

1. Line plot
import matplotlib.pyplot as plt
x = [10, 8, 3, 2, 1]
y = [7, 6, 5, 4, 2]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')plt.show()

Diagram:

ENROLLMENT NO.: 220410107118 Page|51


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

2. Scatter Plot
import matplotlib.pyplot as plt
x = [6, 12, 13, 4, 5]
y = [3, 4, 8, 1, 2]
plt.scatter(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')
plt.show()

Diagram:

ENROLLMENT NO.: 220410107118 Page|52


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

3. Bar Chart
import matplotlib.pyplot as plt
categories = ['Category A', 'Category B', 'Category C']
values = [14, 5, 9]
plt.bar(categories, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart')
plt.show()

Diagram:

ENROLLMENT NO.: 220410107118 Page|53


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

4. Histogram

import matplotlib.pyplot as plt


import random
data = [random.gauss(0, 4) for _ in range(2500)]
plt.hist(data, bins=30, edgecolor='k')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

Diagram:

ENROLLMENT NO.: 220410107118 Page|54


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

5. Pie Chart

import matplotlib.pyplot as plt


labels = ['Section A', 'Section B', 'Section C', 'Section D', 'Section E']
sizes = [23, 34, 18, 11,14]
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('PIE CHART')
plt.show()

Diagram:

ENROLLMENT NO.: 220410107118 Page|55


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Practical 10
Aim: Demonstration of Scikit-learn and other machine learning libraries such as
keras, tensorflow etc.

1. Scikit-Learn:
from sklearn.datasets import load_iris

iris = load_iris()

X = iris.data
y = iris.target

feature_names = iris.feature_names
target_names = iris.target_names

print("Feature names:", feature_names)


print("Target names:", target_names)

print("\nType of X is:", type(X))

print("\nFirst 5 rows of X:\n", X[:5])

ENROLLMENT NO.: 220410107118 Page|56


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Diagram:

2. Keras:
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop

batch_size = 128
num_classes = 10
epochs = 5

(x_train, y_train), (x_test, y_test) = mnist.load_data()


x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

print(x_train.shape[0], 'train samples')

ENROLLMENT NO.: 220410107118 Page|57


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

print(x_test.shape[0], 'test samples')

y_train = keras.utils.to_categorical(y_train, num_classes)


y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy'])

history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1,


validation_data=(x_test, y_test))

score = model.evaluate(x_test, y_test, verbose=0)


print('Test loss:', score[0])
print('Test accuracy:', score[1])

ENROLLMENT NO.: 220410107118 Page|58


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Diagram:

ENROLLMENT NO.: 220410107118 Page|59


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Practical 11
Beyond Syllabus

Aim: Apply knowledge of Data Science to analyze and visualize any real-life
dataset using python libraries.

# Import necessary libraries


import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Creating a mock dataset for World Cup Final 2011


data = {
'Team': ['India', 'India', 'India', 'India', 'India', 'India', 'India', 'Sri Lanka', 'Sri Lanka', 'Sri
Lanka', 'Sri Lanka', 'Sri Lanka', 'Sri Lanka', 'Sri Lanka'],
'Player': ['MS Dhoni', 'Gautam Gambhir', 'Virat Kohli', 'Sachin Tendulkar', 'Yuvraj Singh',
'Zaheer Khan', 'Harbhajan Singh',
'Kumar Sangakkara', 'Mahela Jayawardene', 'Tilakaratne Dilshan', 'Thilan
Samaraweera', 'Lasith Malinga', 'Thisara Perera', 'Nuwan Kulasekara'],
'Runs': [91, 97, 35, 18, 21, 0, 0, 48, 103, 33, 21, 0, 22, 32],
'Wickets': [0, 0, 0, 0, 2, 2, 1, 0, 0, 0, 0, 2, 1, 0]
}

# Load the dataset into a pandas DataFrame


df = pd.DataFrame(data)

# Display the first few rows of the dataset


print(df.head())

ENROLLMENT NO.: 220410107118 Page|60


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

# Data Exploration: Summary statistics of the dataset


print("\nSummary statistics:\n", df.describe())

# Visualization 1: Runs scored by each player in the final


plt.figure(figsize=(10, 6))
sns.barplot(x='Player', y='Runs', hue='Team', data=df, palette='Set1')
plt.title('Runs Scored by Players in World Cup Final 2011')
plt.xlabel('Player')
plt.ylabel('Runs')
plt.xticks(rotation=45)
plt.show()

# Visualization 2: Wickets taken by each player in the final


plt.figure(figsize=(10, 6))
sns.barplot(x='Player', y='Wickets', hue='Team', data=df, palette='Set2')
plt.title('Wickets Taken by Players in World Cup Final 2011')
plt.xlabel('Player')
plt.ylabel('Wickets')
plt.xticks(rotation=45)
plt.show()

# Visualization 3: Total Runs comparison between teams


team_runs = df.groupby('Team')['Runs'].sum().reset_index()
plt.figure(figsize=(8, 5))
sns.barplot(x='Team', y='Runs', data=team_runs, palette='Blues_d')
plt.title('Total Runs Scored by Each Team in the Final')
plt.ylabel('Total Runs')
plt.show()

ENROLLMENT NO.: 220410107118 Page|61


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

# Visualization 4: Total Wickets comparison between teams


team_wickets = df.groupby('Team')['Wickets'].sum().reset_index()
plt.figure(figsize=(8, 5))
sns.barplot(x='Team', y='Wickets', data=team_wickets, palette='Reds_d')
plt.title('Total Wickets Taken by Each Team in the Final')
plt.ylabel('Total Wickets')
plt.show()

Diagram:

ENROLLMENT NO.: 220410107118 Page|62


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Fig 1 Visualization 1: Runs scored by each player in the final

ENROLLMENT NO.: 220410107118 Page|63


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Fig 2 Visualization 2: Wickets taken by each player in the final

ENROLLMENT NO.: 220410107118 Page|64


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Fig 3 Visualization 3: Total Runs comparison between teams

ENROLLMENT NO.: 220410107118 Page|65


SARDAR VALLABHBHAI PATEL INSTITUTE OF TECHNOLOGY

SUB NAME: Python for Data Science SUBJECT CODE: 3150713

Fig 4 Visualization 4: Total Wickets comparison between teams

ENROLLMENT NO.: 220410107118 Page|66

You might also like