0% found this document useful (0 votes)
3 views33 pages

CSC - 310 Advanced Python Programming Continuous Assessment-2 Assignment:Ca2

The document is an advanced Python programming assignment for a BSc Data Science student named Sairam AP. It includes various coding experiments using pandas and numpy for data manipulation, handling missing values, scaling data, and creating arrays. The assignment demonstrates practical applications of data analysis techniques and group operations on DataFrames.

Uploaded by

Sairam AP
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)
3 views33 pages

CSC - 310 Advanced Python Programming Continuous Assessment-2 Assignment:Ca2

The document is an advanced Python programming assignment for a BSc Data Science student named Sairam AP. It includes various coding experiments using pandas and numpy for data manipulation, handling missing values, scaling data, and creating arrays. The assignment demonstrates practical applications of data analysis techniques and group operations on DataFrames.

Uploaded by

Sairam AP
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/ 33

CSC – 310

ADVANCED PYTHON
PROGRAMMING CONTINUOUS
ASSESSMENT-2
ASSIGNMENT:CA2

NAME : SAIRAM AP
UNIQUE ID : E62220002

DEPT: BSc DATA SCIENCE


10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook

In [1]: import pandas as pd


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

In [2]: data = {
'NAME': ['ANJU', 'BOBBY', 'RIA', 'RAM',
'LAXMAN'], 'SCORE 1': [60, 50, 78, 80, 98],
'SCORE 2': [np.nan, 77, 68, np.nan, 50],
'SCORE 3': [55, 45, 68, 90, 50],
'STATE': ['NY', 'TX', np.nan, np.nan, 'CA'],
'EDUCATION': ['HIGH SCHOOL', np.nan, 'COLLEGE', np.nan, 'COLLEGE']
}

In [3]: df = pd.DataFrame(data)

In [4]: df

Out[4]:
NAME SCORE 1 SCORE 2 SCORE 3 STATE EDUCATION

0 ANJU 60 NaN 55 NY HIGH SCHOOL

1 BOBBY 50 77.0 45 TX NaN

2 RIA 78 68.0 68 NaN COLLEGE

3 RAM 80 NaN 90 NaN NaN

4 LAXMAN 98 50.0 50 CA COLLEGE

In [5]: df_dropped =
df.dropna()
print(df_dropped)
NAME SCORE 1 SCORE 2 SCORE 3 STATE EDUCATION
4 LAXMAN 98 50.0 50 CA COLLEGE

In [6]: df_no_missing_scores = df.dropna(subset=['SCORE 1',


'SCORE 2']) print(df_no_missing_scores)

NAME SCORE 1 SCORE 2 SCORE 3 STATE EDUCATION


1 BOBBY 50 77.0 45 TX NaN
2 RIA 78 68.0 68 NaN COLLEGE
4 LAXMAN 98 50.0 50 CA COLLEGE

In [7]: df['STATE'] =
df['STATE'].fillna(method='bfill')
print(df)
NAME SCORE 1 SCORE 2 SCORE 3 STATE EDUCATION
0 ANJU 60 NaN 55 NY HIGH SCHOOL
1 BOBBY 50 77.0 45 TX NaN
2 RIA 78 68.0 68 CA COLLEGE
3 RAM 80 NaN 90 CA NaN
4 LAXMAN 98 50.0 50 CA COLLEGE

1
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [8]: df_filled = df.fillna(method='ffill',
axis=1) print(df_filled)

NAME SCORE 1 SCORE 2 SCORE 3 STATE EDUCATION


0 ANJU 60 60 55 NY HIGH SCHOOL
1 BOBBY 50 77.0 45 TX TX
2 RIA 78 68.0 68 CA COLLEGE
3 RAM 80 80 90 CA CA
4 LAXMAN 98 50.0 50 CA COLLEGE

In [9]: # lab experiment 2


import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler

In [10]: data = {
'NAME': ['ANJU', 'BOBBY', 'RAM',
'RIA', 'LAXMAN'],
'SCORE 1' [60 50 70 80, 75]
: , , , ,
'SCORE 2' [65 77 78 87, 98]
: , , , ,
'SCORE 3' [55 45 68 90, 50
: , , , ]
In [11]: df = pd.DataFrame(data)

In [12]: df_simple_scaling = df[['SCORE 1', 'SCORE 2', 'SCORE 3']].apply(lambda x: x /


x.max()) print(df_simple_scaling)

SCORE 1 SCORE 2 SCORE 3


0 0.7500 0.663265 0.611111
1 0.6250 0.785714 0.500000
2 0.8750 0.795918 0.755556
3 1.0000 0.887755 1.000000
4 0.9375 1.000000 0.555556

In [13]: scaler = MinMaxScaler()


df_min_max = , 'SCORE 2', 'SCORE
pd.DataFrame(scaler.fit_transform(df[['SCORE 1' 3']]), c
print(df_min_max)

SCORE 1 SCORE 2 SCORE 3


0 0.333333 0.000000 0.222222
1 0.000000 0.363636 0.000000
2 0.666667 0.393939 0.511111
3 1.000000 0.666667 1.000000
4 0.833333 1.000000 0.111111

In [14]: scaler = StandardScaler()


df_z_score = , 'SCORE 2', 'SCORE
pd.DataFrame(scaler.fit_transform(df[['SCORE 1' 3']]), c
print(df_z_score)

SCORE SCORE 2 SCORE 3


1
0 - - -0.409126
0.649934 1.453345
1 - - -1.029013
1.578410 0.363336
2 - 0.396728
0.278543 0.272502
3 0.545004 1.760481
1.207020
4 1.544179 -0.719070
0.742781

2
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [15]: scaler = StandardScaler()
df[['SCORE 1', 'SCORE 2', 'SCORE 3']] = scaler.fit_transform(df[['SCORE 1',
'SCORE 2', '
print(df)
NAME SCORE 1 SCORE 2 SCORE 3
0 ANJU -0.649934 -1.453345 -0.409126
1 BOBBY -1.578410 -0.363336 -1.029013
2 RIA 0.278543 -0.272502 0.396728
3 RAM 1.207020 0.545004 1.760481
4 LAXMAN 0.742781 1.544179 -0.719070

3
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [16]: # lab experiment 3

# a. Create an array of even numbers in the interval 120 to 170


even_numbers = np.arange(120,
171, 2) print("Even numbers:",
even_numbers)

# b. Create an array of 10 numbers and update the value at index 5 to 1000


array_b =
np.arange(10)
array_b[5] = 1000
print("Array with updated value at index 5:", array_b)

# c. Create a numpy array named a1 {'apple', 'mango', 'banana', 'mango'}


a1 = np.array(['apple', 'mango', 'banana', 'mango'])
b = a1 == 'mango' # Create Boolean array checking for
'mango' retrieved_mango = a1[b] # Use Boolean array to
retrieve 'mango' print("Boolean array for 'mango':", b)
print("Retrieved values from a1:", retrieved_mango)

# d. Create an array to store Fahrenheit values and convert to Centigrade


fahrenheit_values = np.array([32, 45, 64, 77, 95])
centigrade_values = np.round((fahrenheit_values - 32) *
(5/9), 3) print("Fahrenheit to Centigrade:",
centigrade_values)

# e. Array 'mark' with shape (5, 6) for student marks


mark = np.random.randint(50, 100,
size=(5, 6)) print("\nMark array:\n",
mark)

# i. mark[:,:]
print("\nAll marks:\n", mark[:, :])

# ii. mark[1:,2]
print("\nmark[1:, 2]:\n", mark[1:, 2])

# iii. mark[1:3:1]
print("\nmark[1:3:1]:\n", mark[1:3:1])

# iv. mark[1:3:1,2]
print("\nmark[1:3:1, 2]:\n", mark[1:3:1, 2])

4
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
Even numbers: [120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150
152 154
156 158 160 162 164 166 168 170]
Array with updated value at index 5: [ 0 1 2 3 4 1000 6 7
8
9]
Boolean array for 'mango': [False True False
True] Retrieved values from a1: ['mango'
'mango']
Fahrenheit to Centigrade: [ 0. 7.222 17.778 25. 35. ]

Mark array:
[[91 69 88 73 55 55]
[76 61 86 69 75 76]
[85 56 56 68 81 92]
[54 95 79 98 97 77]
[78 89 90 67 81 91]]

All marks:
[[91 69 88 73 55 55]
[76 61 86 69 75 76]
[85 56 56 68 81 92]
[54 95 79 98 97 77]
[78 89 90 67 81 91]]

mark[1:, 2]:
[86 56 79 90]

mark[1:3:1]:
[[76 61 86 69 75 76]
[85 56 56 68 81 92]]

mark[1:3:1, 2]:
[86 56]

mark[-1:, 3]:
[67]

5
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook

6
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In
[17]: # lab experiment 4

# Create a DataFrame using the data from the table


data = {
'Courses': ['Spark', 'PySpark', 'Hadoop', 'Python', 'Hadoop', 'PySpark',
'Python', ' 'Fee': [22000, 25000, 23000, 24000, 67000, 13000, 12000, 11000],
'Duration': [30, 50, 55, 40, 60, 50, 10, 20],
'Discount': [1000, 2300, 1000, 1200, 4000, 1300, 1000, 500]
}

# Create DataFrame
df = pd.DataFrame(data)

# i) Group the dataframe based on Courses


grouped = df.groupby('Courses')

# ii) Display the instances present in each group


print("Instances in each group:")
for name, group in
grouped: print(f"\
nGroup: {name}")
print(group)

# iii) Display the max for each group


print("\nMax for each
group:")
print(grouped.max())

# iv) Display the min for each group


print("\nMin for each
group:")
print(grouped.min())

# v) Display the sum of each group


print("\nSum for each
group:")
print(grouped.sum())

# vi) Display the mean of each group


print("\nMean for each
group:")
print(grouped.mean())

# vii) Display the first instance in each group


print("\nFirst instance in each
group:") print(grouped.first())

# viii) Display the last instance in each group


print("\nLast instance in each
group:") print(grouped.last())

# ix) Display the number of unique groups created


print("\nNumber of unique groups
created:") print(grouped.ngroups)

# x) Display the group information


print("\nGroup information:")
grouped.describe().apply(lambda x: x)

# xi) Calculate the sum for the group Spark for the column Fee
spark_sum_fee = grouped['Fee'].sum()['Spark']
print(f"\nSum for the group Spark (Fee column): {spark_sum_fee}")

# xii) Calculate the mean for the group PySpark for the column Duration, Discount
pyspark_mean_duration = grouped['Duration'].mean()
['PySpark'] pyspark_mean_discount =
grouped['Discount'].mean()['PySpark']
print(f"\nMean for the group PySpark (Duration column):
7
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
{pyspark_mean_duration}") print(f"Mean for the group PySpark (Discount
column): {pyspark_mean_discount}")

# xiii) For each group, display the count and min value
print("\nCount and min value for each
group:") grouped_result =
grouped.agg(['count', 'min'])
print(grouped_result)

8
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook

9
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
Instances in group:
each

Group: Hadoop Duration Discount


Courses Fee
2 Hadoop 23000 55 1000
4 Hadoop 67000 60 4000

Group: PySpark
Courses Fee Duratio Discount
n
1 PySpark 50 2300
25000
5 PySpark 50 1300
13000

Group: Python
Course Fee Duration Discount
s
3 24000 40 1200
Python
6 12000 10 1000
Python

Group: Spark
Courses Fee Duration Discount
0 Spark 22000 30 1000
7 Spark 11000 20 500

Max for each group:


Fee Duration Discount
Courses
Hadoop 67000 60 4000
PySpark 25000 50 2300
Python 24000 40 1200
Spark 22000 30 1000

Min for each group:


Fee Duration Discount
Courses
Hadoop 23000 55 1000
PySpark 13000 50 1300
Python 12000 10 1000
Spark 11000 20 500

Sum for each group:


Fee Duration Discount
Courses
Hadoop 90000 115 5000
PySpark 38000 100 3600
Python 36000 50 2200
Spark 33000 50 1500

Mean for each group:


Fe Duratio Discount
Courses e n
Hadoop 45000. 57. 2500.0
0 5
PySpark 19000. 50. 1800.0
0 0
Python 18000. 25. 1100.0
0 0
Spark 16500. 25. 750.0
0 0

First instance in each group:


Fee Duration Discount
Courses
Hadoop 23000 55 1000
PySpark 25000 50 2300

10
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Python 24000 40 Notebook
1200
Spark 22000 30 1000

Last instance in each group:


Fee Duration Discount
Courses
Hadoop 67000 60 4000
PySpark 13000 50 1300
Python 12000 10 1000

11
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
Spark 11000 20 500

Number of unique groups created:


4

Group information:

Sum for the group Spark (Fee column): 33000

Mean for the group PySpark (Duration column): 50.0


Mean for the group PySpark (Discount column): 1800.0

Count and min value for each group:


Fee Duration Discount
count min count min count min
Courses
Hadoop 2 23000 2 55 2 1000
PySpark 2 13000 2 50 2 1300
Python 2 12000 2 10 2 1000
Spark 2 11000 2 20 2 500

In [18]: # lab experiment 5

data = {
'Favorite Subject': ['Python', 'C', 'C++',
'Java'], 'Number of Students': [24, 6, 8, 4]
}

# Create DataFrame
df = pd.DataFrame(data)

localhost:8888/notebooks/ 12/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [19]: # a) Scatter Plot
plt.figure(figsize=(8, 6))
plt.scatter(df['Favorite Subject'], df['Number of Students'],
color='blue', s=100) plt.title('Scatter Plot of Favorite Subjects vs
Number of Students')
plt.xlabel('Favorite
Subject') plt.ylabel('Number
of Students') plt.show()

localhost:8888/notebooks/ 13/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [20]: # b) Bar Chart
plt.figure(figsize=(8, 6))
plt.bar(df['Favorite Subject'], df['Number of Students'],
color='green') plt.title('Bar Chart of Favorite Subjects vs
Number of Students')
plt.xlabel('Favorite
Subject') plt.ylabel('Number
of Students') plt.show()

localhost:8888/notebooks/ 14/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [21]: # c) Histogram
plt.figure(figsize=(8, 6))
plt.hist(df['Number of Students'], bins=4, color='purple',
edgecolor='black') plt.title('Histogram of Number of Students')
plt.xlabel('Number of
Students')
plt.ylabel('Frequency')
plt.show()

localhost:8888/notebooks/ 15/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [22]: # d) Box Plot
plt.figure(figsize=(8, 6))
sns.boxplot(data=df['Number of Students'],
color='cyan') plt.title('Box Plot of Number of
Students')
plt.xlabel('Number of
Students') plt.show()

localhost:8888/notebooks/ 16/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [23]: # e) Pie Chart
plt.figure(figsize=(8, 6))
plt.pie(df['Number of Students'], labels=df['Favorite Subject'], autopct='%1.1f%
%', star plt.title('Pie Chart of Favorite Subjects')
plt.show()

In [24]: # lab experiment 6


data_week1 = [0.0, 12.2, 2.1, 0.0, 20.5, 5.5, 1.0]
data_week2 = [1.1, 11.2, 3.1, 4.1, 21.4, 5.1, 7.1]
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
df_rainfall = pd.DataFrame({
'Day': days,
'Week 1': data_week1,
'Week 2': data_week2
})

localhost:8888/notebooks/ 17/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [25]: plt.figure(figsize=(10, 6))
plt.plot(days, data_week1, marker='o',
label='Week 1') plt.plot(days, data_week2,
marker='o', label='Week 2') plt.title('Weekly
Rainfall Comparison')
plt.xlabel('Day')
plt.ylabel('Rainfall
(mm)') plt.legend()
plt.show()

localhost:8888/notebooks/ 18/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [26]: import seaborn as sns
plt.figure(figsize=(10, 6))
sns.lineplot(x='Day', y='value', hue='variable', data=pd.melt(df_rainfall,
['Day'])) plt.title('Weekly Rainfall Comparison')
plt.xlabel('Day')
plt.ylabel('Rainfall
(mm)') plt.show()

localhost:8888/notebooks/ 19/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [27]: import plotly.express as px
fig = px.line(df_rainfall, x='Day', y=['Week 1', 'Week 2'], title='Weekly
Rainfall Compa fig.show()

Weekly Rainfall Comparison

20

15
value

10

localhost:8888/notebooks/ 20/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [28]: plt.figure(figsize=(8, 6))
plt.boxplot([data_week1, data_week2], labels=['Week 1',
'Week 2']) plt.title('Rainfall Distribution')
plt.ylabel('Rainfall
(mm)') plt.show()

localhost:8888/notebooks/ 21/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [29]: plt.figure(figsize=(8, 6))
sns.boxplot(data=pd.melt(df_rainfall, ['Day']), x='variable',
y='value') plt.title('Rainfall Distribution')
plt.xlabel('Week')
plt.ylabel('Rainfall
(mm)') plt.show()

localhost:8888/notebooks/ 22/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [30]: import plotly.graph_objects as
go fig = go.Figure()
fig.add_trace(go.Box(y=data_week1, name='Week 1'))
fig.add_trace(go.Box(y=data_week2, name='Week 2'))
fig.update_layout(title='Rainfall Distribution', yaxis_title='Rainfall
(mm)') fig.show()

Rainfall Distribution

20

15
Rainfall (mm)

10

In [31]: # lab experiment 7


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

In [32]: data = pd.read_csv('auto_insurance.csv')

localhost:8888/notebooks/ 23/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In data
[33]:
Out[33]
: months_as_customer age policy_number policy_bind_date policy_state policy_csl policy_deductable

0 328 48 521585 2014-10-17 OH 250/500 1000

1 228 42 342868 2006-06-27 IN 250/500 2000

2 134 29 687698 2000-09-06 OH 100/300 2000

3 256 41 227811 1990-05-25 IL 250/500 2000

4 228 44 367455 2014-06-06 IL 500/1000 1000

... ... ... ... ... ... ... ...

995 3 38 941851 1991-07-16 OH 500/1000 1000

996 285 41 186934 2014-01-05 IL 100/300 1000

997 130 34 918516 2003-02-17 OH 250/500 500

998 458 62 533940 2011-11-18 IL 500/1000 2000

999 456 60 556080 1996-11-11 OH 250/500 1000

1000 rows × 40 columns

In [34]: data.head(5)

Out[34]:
months_as_customer age policy_number policy_bind_date policy_state policy_csl policy_deductable p

0 328 48 521585 2014-10-17 OH 250/50 1000


0
1 228 42 342868 2006-06-27 IN 250/50 2000
0
2 134 29 687698 2000-09-06 OH 100/30 2000
0
3 256 41 227811 1990-05-25 IL 250/50 2000
0
4 228 44 367455 2014-06-06 IL 500/100 1000
0

5 rows × 40 columns

In [35]: X = data[['months_as_customer', 'age', 'policy_deductable',


'policy_annual_premium', 'um y = data['total_claim_amount']

In [36]: X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,


random_state=42

In [37]: model =
LinearRegression()
model.fit(X_train,
Out[37]: ▾
LinearRegression
LinearRegression

In [38]: y_pred = model.predict(X_test)

localhost:8888/notebooks/ 24/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [39]: mae = mean_absolute_error(y_test,
y_pred) mse =
mean_squared_error(y_test, y_pred)
rmse = mean_squared_error(y_test, y_pred,
squared=False) variance_score = model.score(X_test,
y_test)

In [40]: print(f'MAE: {mae}')


print(f'MSE: {mse}')
print(f'RMSE: {rmse}')
print(f'Variance Score:
{variance_score}') print(f'R^2 Score:
{r2}')
MAE: 7.703215487708803e-
10 MSE:
1.0706986257954323e-18
RMSE: 1.0347456816993402e-09
Variance Score: 1.0
R^2 Score: 1.0

In [41]: import pandas as pd


from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, accuracy_score

In [42]: df = pd.read_csv('Insurance claims


data.csv') df.head(5)

Out[42]:
policy_id subscription_length vehicle_age customer_age region_code region_density segment model

0 POL04536 9.3 1.2 41 C8 8794 C2 M4


0
1 POL01674 8.2 1.8 35 C2 27003 C1 M9
5
2 POL00719 9.5 0.2 44 C8 8794 C2 M4
4
3 POL01814 5.2 0.4 44 C10 73430 A M1
6
4 POL04901 10.1 1.0 56 C13 5410 B2 M5
1

5 rows × 41 columns

In [43]: X = df[['customer_age', 'vehicle_age',


'region_density', 'subscription_length',
'fuel_type', 'ncap_rating',
'is_brake_assist', 'is_central_locking',
'is_power_steering']] y = df['claim_status']

In [44]: X = pd.get_dummies(X, drop_first=True)

In [45]: X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,


random_state=42

In [46]: model =
LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
Out[46]:
▾ LogisticRegressi
on
LogisticRegression(max_iter=100
0)

localhost:8888/notebooks/ 25/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [47]: y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred,
zero_division=1)) print(f'Accuracy:
{accuracy_score(y_test, y_pred):.2f}')
precision recall f1-score support

0 0.93 1.00 0.97 16434


1 1.00 0.00 0.00 1144

accuracy 0.93 17578


macro avg 0.97 0.50 0.48 17578
weighted avg 0.94 0.93 0.90 17578

Accuracy: 0.93

In [48]: # lab experiment 8


from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cluster import KMeans
from sklearn.metrics import classification_report

In [49]: iris = load_iris()


X = iris.data
y = iris.target

In [50]: X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,


random_state=42

In [51]: svm_model = SVC()


svm_model.fit(X_train, y_train)
svm_pred =
svm_model.predict(X_test)
print("SVM Classification
Report")
SVM Classification Report
precision recall f1-score support

0 1.00 1.00 1.00 19


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 13

accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted 1.00 1.00 1.00 45
avg

localhost:8888/notebooks/ 26/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [52]: knn_model =
KNeighborsClassifier()
knn_model.fit(X_train, y_train)
knn_pred =
knn_model.predict(X_test)
print("KNN Classification
KNN Classification Report
precision recall f1-score support

0 1.00 1.00 1.00 19


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 13

accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted 1.00 1.00 1.00 45
avg

In [53]: kmeans_model = KMeans(n_clusters=3, n_init='auto',


random_state=42) kmeans_model.fit(X_train)
y_pred_kmeans = kmeans_model.predict(X_test)
y_pred_kmeans_adjusted = np.choose(y_pred_kmeans, [0, 1,
2]).astype(np.int64) print(classification_report(y_test,
y_pred_kmeans_adjusted))
print(f"K-Means Accuracy: {accuracy_score(y_test, y_pred_kmeans_adjusted):.2f}")
precision recall f1-score support

0 0.00 0.00 0.00 19


1 0.00 0.00 0.00 13
2 0.19 0.23 0.21 13

accuracy 0.07 45
macro avg 0.06 0.08 0.07 45
weighted avg 0.05 0.07 0.06 45

K-Means Accuracy: 0.07

C:\Users\divya\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436:
UserWarnin g:

KMeans is known to have a memory leak on Windows with MKL, when there are less
chunks t han available threads. You can avoid it by setting the environment
variable OMP_NUM_THR EADS=1.

localhost:8888/notebooks/ 27/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [54]: !pip install tensorflow

localhost:8888/notebooks/ 28/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
Requirement already satisfied: tensorflow in c:\users\divya\anaconda3\lib\site-
packages (2.17.0)
Requirement already satisfied: tensorflow-intel==2.17.0 in c:\users\divya\
anaconda3\lib
\site-packages (from tensorflow) (2.17.0)
Requirement already satisfied: absl-py>=1.0.0 in c:\users\divya\anaconda3\lib\
site-pack ages (from tensorflow-intel==2.17.0->tensorflow) (2.1.0)
Requirement already satisfied: astunparse>=1.6.0 in c:\users\divya\anaconda3\
lib\site-p ackages (from tensorflow-intel==2.17.0->tensorflow) (1.6.3)
Requirement already satisfied: flatbuffers>=24.3.25 in c:\users\divya\
anaconda3\lib\sit e-packages (from tensorflow-intel==2.17.0->tensorflow)
(24.3.25)
Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in c:\users\
divya\an aconda3\lib\site-packages (from tensorflow-intel==2.17.0->tensorflow)
(0.6.0)
Requirement already satisfied: google-pasta>=0.1.1 in c:\users\divya\anaconda3\
lib\site
-packages (from tensorflow-intel==2.17.0->tensorflow) (0.2.0)
Requirement already satisfied: h5py>=3.10.0 in c:\users\divya\anaconda3\lib\
site-packag es (from tensorflow-intel==2.17.0->tensorflow) (3.12.1)
Requirement already satisfied: libclang>=13.0.0 in c:\users\divya\anaconda3\
lib\site-pa ckages (from tensorflow-intel==2.17.0->tensorflow) (18.1.1)
Requirement already satisfied: ml-dtypes<0.5.0,>=0.3.1 in c:\users\divya\
anaconda3\lib
\site-packages (from tensorflow-intel==2.17.0->tensorflow) (0.4.1)
Requirement already satisfied: opt-einsum>=2.3.2 in c:\users\divya\anaconda3\
lib\site-p ackages (from tensorflow-intel==2.17.0->tensorflow) (3.4.0)
Requirement already satisfied: packaging in c:\users\divya\anaconda3\lib\site-
packages (from tensorflow-intel==2.17.0->tensorflow) (23.1)
Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!
=4.21.4,!= 4.21.5,<5.0.0dev,>=3.20.3 in c:\users\divya\anaconda3\lib\site-
packages (from tensorflo w-intel==2.17.0->tensorflow) (4.25.5)
Requirement already satisfied: requests<3,>=2.21.0 in c:\users\divya\anaconda3\
lib\site
-packages (from tensorflow-intel==2.17.0->tensorflow) (2.31.0)
Requirement already satisfied: setuptools in c:\users\divya\anaconda3\lib\site-
packages (from tensorflow-intel==2.17.0->tensorflow) (68.0.0)
Requirement already satisfied: six>=1.12.0 in c:\users\divya\anaconda3\lib\
site-package s (from tensorflow-intel==2.17.0->tensorflow) (1.16.0)
Requirement already satisfied: termcolor>=1.1.0 in c:\users\divya\anaconda3\
lib\site-pa ckages (from tensorflow-intel==2.17.0->tensorflow) (2.5.0)
Requirement already satisfied: typing-extensions>=3.6.6 in c:\users\divya\
anaconda3\lib
\site-packages (from tensorflow-intel==2.17.0->tensorflow) (4.7.1)
Requirement already satisfied: wrapt>=1.11.0 in c:\users\divya\anaconda3\lib\
site-packa ges (from tensorflow-intel==2.17.0->tensorflow) (1.14.1)
Requirement already satisfied: grpcio<2.0,>=1.24.3 in c:\users\divya\anaconda3\
lib\site
-packages (from tensorflow-intel==2.17.0->tensorflow) (1.66.2)
Requirement already satisfied: tensorboard<2.18,>=2.17 in c:\users\divya\
anaconda3\lib
\site-packages (from tensorflow-intel==2.17.0->tensorflow) (2.17.1)
Requirement already satisfied: keras>=3.2.0 in c:\users\divya\anaconda3\lib\
site-packag es (from tensorflow-intel==2.17.0->tensorflow) (3.6.0)
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in c:\
users\divya\a naconda3\lib\site-packages (from tensorflow-intel==2.17.0-
>tensorflow) (0.31.0)
Requirement already satisfied: numpy<2.0.0,>=1.23.5 in c:\users\divya\
anaconda3\lib\sit e-packages (from tensorflow-intel==2.17.0->tensorflow)
(1.24.3)
Requirement already satisfied: wheel<1.0,>=0.23.0 in c:\users\divya\anaconda3\
lib\site- packages (from astunparse>=1.6.0->tensorflow-intel==2.17.0-
>tensorflow) (0.38.4)
Requirement already satisfied: rich in c:\users\divya\anaconda3\lib\site-
packages (from keras>=3.2.0->tensorflow-intel==2.17.0->tensorflow) (13.9.2)
Requirement already satisfied: namex in c:\users\divya\anaconda3\lib\site-
packages (fro m keras>=3.2.0->tensorflow-intel==2.17.0->tensorflow) (0.0.8)
Requirement already satisfied: optree in c:\users\divya\anaconda3\lib\site-
packages (fr om keras>=3.2.0->tensorflow-intel==2.17.0->tensorflow) (0.13.0)
localhost:8888/notebooks/ 29/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Requirement already satisfied:Notebook
charset-normalizer<4,>=2 in c:\users\divya\
anaconda3\lib
\site-packages (from requests<3,>=2.21.0->tensorflow-intel==2.17.0->tensorflow)
(2.0.4) Requirement already satisfied: idna<4,>=2.5 in c:\users\divya\
anaconda3\lib\site-packag es (from requests<3,>=2.21.0->tensorflow-
intel==2.17.0->tensorflow) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\divya\anaconda3\
lib\site- packages (from requests<3,>=2.21.0->tensorflow-intel==2.17.0-
>tensorflow) (1.26.16)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\divya\anaconda3\
lib\site- packages (from requests<3,>=2.21.0->tensorflow-intel==2.17.0-
>tensorflow) (2024.6.2)
Requirement already satisfied: markdown>=2.6.8 in c:\users\divya\anaconda3\lib\
site-pac kages (from tensorboard<2.18,>=2.17->tensorflow-intel==2.17.0-
>tensorflow) (3.4.1)
Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in c:\users\
divya

localhost:8888/notebooks/ 30/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
\anaconda3\lib\site-packages (from tensorboard<2.18,>=2.17->tensorflow-
intel==2.17.0->t ensorflow) (0.7.2)
Requirement already satisfied: werkzeug>=1.0.1 in c:\users\divya\anaconda3\lib\
site-pac kages (from tensorboard<2.18,>=2.17->tensorflow-intel==2.17.0-
>tensorflow) (2.2.3)
Requirement already satisfied: MarkupSafe>=2.1.1 in c:\users\divya\anaconda3\
lib\site-p ackages (from werkzeug>=1.0.1->tensorboard<2.18,>=2.17->tensorflow-
intel==2.17.0->tenso rflow) (2.1.1)
Requirement already satisfied: markdown-it-py>=2.2.0 in c:\users\divya\
anaconda3\lib\si te-packages (from rich->keras>=3.2.0->tensorflow-
intel==2.17.0->tensorflow) (2.2.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in c:\users\divya\
anaconda3\lib
\site-packages (from rich->keras>=3.2.0->tensorflow-intel==2.17.0->tensorflow)
(2.15.1) Requirement already satisfied: mdurl~=0.1 in c:\users\divya\anaconda3\
lib\site-packages (from markdown-it-py>=2.2.0->rich->keras>=3.2.0->tensorflow-
intel==2.17.0->tensorflow) (0.1.0)

In [55]: import tensorflow as tf

# Create scalar tensor variables a and b


a = tf.constant(5) # Example scalar value for a
b = tf.constant(10) # Example scalar value for b

# Perform addition operation


result = tf.add(a, b)

# Display the result


print("Addition Result:", result.numpy())

Addition Result: 15

In [56]: import tensorflow as tf

# Create a variable for x and use the provided values


x = tf.constant([0, 5, 15, 25])

# Compute y = x * 10 + 500
y = x * 10 + 500

# Display the result


print("Computed y values:", y.numpy())

Computed y values: [500 550 650 750]

In [57]: # lab experiment 10


import tensorflow as tf

# Create a constant tensor for x


x = tf.constant([[12, 2, 0, -2], [14, 4, 1, 0]])

# Compute y = x * 10 + 1
y = x * 10 + 1

# Display the result


print("Computed y values:\n", y.numpy())

Computed y
values: [[121
21 1 -
19]
[141 41 11 1]]

localhost:8888/notebooks/ 31/2
ca2_advanced_python.ipynb
10/14/24, 7:10 ca2_advanced_python - Jupyter
PM Notebook
In [58]: import tensorflow as tf

# Input scalar values at runtime


tens1_value = float(input("Enter a scalar value for
tens1: ")) tens2_value = float(input("Enter a scalar
value for tens2: "))

# Create tensor variables for tens1 and tens2


tens1 =
tf.constant(tens1_value)
tens2 =
tf.constant(tens2_value)

# Perform multiplication
result = tf.multiply(tens1, tens2)

# Display the result


Enter a scalar value for tens1: 12
Enter a scalar value for tens2: 2
Multiplication Result: 24.0

In [ ]:

localhost:8888/notebooks/ 32/2
ca2_advanced_python.ipynb

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy