PRACTICAL AI
Q1. Create a DataFrame from dict of Numpy Array
import numpy as np
import pandas as pd
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
print(df)
OUTPUT-
A B C
0 1 2 3
1 4 5 6
2 7 8 9
Q2. Create a DataFrame from a List of Dictionaries.
import pandas as pd
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
df = pd.DataFrame(dict)
print(df)
OUTPUT-
name degree score
0 aparna MBA 90
1 pankaj BCA 40
2 sudhir M.Tech 80
3 Geeku MBA 98
Q3. Create a DataFrame and fetch first three rows fron the given DataFrame.
import pandas as pd
dict = {'Name' : ['Sumit ', 'Sukritin','Akriti ', 'Sanskriti','Abhishek '],
'Age':[22, 20, 45, 21, 22],
'Marks':[90, 84, 33, 87, 82]}
df = pd.DataFrame(dict)
print(df)
# Getting first 3 rows from the DataFrame
r = df.head(3)
print("First three rows are: \n" )
print(r)
OUTPUT-
Name Age Marks
0 Sumit 22 90
1 Sukritin 20 84
2 Akriti 45 33
3 Sanskriti 21 87
4 Abhishek 22 82
First three rows are:
Name Age Marks
0 Sumit 22 90
1 Sukritin 20 84
2 Akriti 45 33
Q4. Create a DataFrame and fetch last three rows fron the given DataFrame.
import pandas as pd
dict = {'Name' : ['Sumit ', 'Sukritin','Akriti ', 'Sanskriti','Abhishek '],
'Age':[22, 20, 45, 21, 22],
'Marks':[90, 84, 33, 87, 82]}
df = pd.DataFrame(dict)
print(df)
# Getting first 3 rows from the DataFrame
r = df.tail(3)
print("First three rows are: \n" )
print(r)
OUTPUT-
Name Age Marks
0 Sumit 22 90
1 Sukritin 20 84
2 Akriti 45 33
3 Sanskriti 21 87
4 Abhishek 22 82
Last three rows are:
Name Age Marks
2 Akriti 45 33
3 Sanskriti 21 87
4 Abhishek 22 82
Q5. Create a DataFrame and display the missing values from it.
import pandas as pd
import numpy as np
# Sample DataFrame with missing values
d = {'First ': [100, 90, np.nan, 95],
'Second ': [30, 45, 56, np.nan],
'Third ': [np.nan, 40, 80, 98]}
df = pd.DataFrame(d)
# Checking for missing values using isnull()
mv = df.isnull()
print(mv)
OUTPUT-
First Second Third
0 False False True
1 False False False
2 True False False
3 False True False
Q6. Create a DataFrame and display the number of missing values from it.
import pandas as pd
import numpy as np
# Sample DataFrame with missing values
d = {'First ': [100, 90, np.nan, 95],
'Second ': [30, 45, 56, np.nan],
'Third ': [np.nan, 40, 80, 98]}
df = pd.DataFrame(d)
# Checking for missing values using isnull()
mv = df.isnull()
print(mv)
# Counting missing values in a column using sum( )
c= df.isnull().sum()
print("no.of null values in each column is :\n",c)
#Counting missing values in a dataframe using sum( )
a= df.isnull().sum().sum()
print("no.of null values in dataframe is:\n",a)
OUTPUT-
First Second Third
0 False False True
1 False False False
2 True False False
3 False True False
no.of null values in each column is :
First 1
Second 1
Third 1
dtype: int64
no.of null values in dataframe is:
3
Q7. Write a program to read the data from .csv file-.
It takes the first row from the csv file as column names for the dataframes.
import pandas as pd
df=pd.read_csv(“C:\\Users\\hp\\Desktop\\sample.csv”)
print(df)
output-
Dataframe Excel- Sample.csv
Q8. Write a program to insert missing values in a csv file through a dataframe
import pandas as pd
import numpy as np
details = { 'Name' : ['Ankit', 'Aishwarya', np.NaN, 'Shivangi'],
'Age' : [23, 21, 22, 21],
'University' : ['BHU', np.NaN, 'DU', 'BHU'] }
df=pd.DataFrame(details)
df.to_csv("C:\\Users\\hp\\Desktop\\sample2.csv",sep='|')
the “ | | “ sign shows NaN values and “ | “ also shows NaN value shown as red in output.
output-
Excel Dataframe
Q9. Write a program to fill the missing values in a csv file .
import pandas as pd
import numpy as np
details = { 'Name' : ['Ankit', 'Aishwarya', np.NaN, 'Shivangi'],
'Age' : [23, 21, 22, 21],
'University' : ['BHU', np.NaN, 'DU', 'BHU'] }
df=pd.DataFrame(details)
# filling missing value with NULL in a csv file
df.to_csv("C:\\Users\\hp\\Desktop\\sample2.csv",sep='|', na_rep="NULL")
output
dataframe Excel
Name Age University
0 Ankit 23 BHU
1 Aishwarya 21 NaN
2 NaN 22 DU
3 Shivangi 21 BHU
Q10. Create a graph to show the dropout rate after implementation of mid-day meal program.
#DATA STORY TELLING
import pandas as pd
import matplotlib.pyplot as plt
year=[2010,2011,2012,2013,2014]
droppercent=[4,3,1,2,1]
dropout=[17,12,20,10,5]
plt.plot(year,droppercent)
plt.plot(year,dropout)
plt.title(" dropout rate after implementation of mid-day meal")
plt.xlabel("dropout percentage")
plt.ylabel("academic year")
plt.show()
Q11.Calculate MSE and RMSE values for the data given below using MS Excel.
Type Formula into any cell, and then click ctrl+shift+enter to execute the formula otherwise
it gives error
RMSE = √[ Σ(Pi – Oi)2 / n ]
where:
Σ means “sum”
Pi is the predicted value for the ith observation in the dataset
Oi is the observed value for the ith observation in the dataset
n is the sample size
=SQRT(SUMSQ(B3:B12-A3:A12) / COUNTA(A3:A12)) or
=SQRT(SUMSQ(C3:C12)/10) or
=SQRT(SUMSQ(c3:c12) / COUNTA(c3:c12))
a) sqrt( ) – to find square root of a number
b) sumsq( )- calculate the sum of the squared differences between the predicted and actual values
c)counta( )- counts the number of cells in a range that are not empty
Q 12 Calculate MSE and RMSE values for the data given below using MS Excel.
Steps in Excel:
1. Enter the Data
In Column A, input the actual values.
In Column B, input the predicted values.
Fill in the data under the respective columns.
2. Calculate Squared Errors
Label Column C as Squared Error.
In Cell C2, enter the formula: =(𝐴2−𝐵2)2
Drag this formula down to fill all rows (C2:C11).
This calculates the squared error for each row.
3. Calculate the Mean Squared Error (MSE)
In an empty cell (e.g., D1), label it MSE.
In D2, use the formula: =SUM(d3:d12)/10
This computes the average of all squared errors.
4.Calculate the Root Mean Squared Error (RMSE)
In an empty cell (e.g., E1), label it RMSE.
In E2, use the formula: =SQRT(SUMSQ(C3:C12) / COUNTA(C3:C12))
This takes the square root of the MSE to compute the RMSE.
For the given data:
MSE = 62.5
RMSE = 7.90569415
Q13) Given a confusion matrix, calculate Precision, Recall, F1 score and Accuracy . A 2x2
Confusion matrix is shown below for the image recognition having a Dog image or Not Dog
image:
True Positive (TP): It is the total counts having both predicted and actual values are Dog.
True Negative (TN): It is the total counts having both predicted and actual values are Not Dog.
False Positive (FP): It is the total counts having prediction is Dog while actually Not Dog.
False Negative (FN): It is the total counts having prediction is Not Dog while actually, it is Dog.
Actual Dog Counts = 6
Actual Not Dog Counts = 4
True Positive Counts = 5
False Positive Counts = 1
True Negative Counts = 3
False Negative Counts = 1
A)PRECISION--
Precision=TP/TP+FP = 5/5+1=5/6=0.83=83%
Precision indicates how many of the predicted positives are correct.
B)RECALL-
Recall= TP/TP+FN = 5/5+1=5/6=0.83=83%
Recall shows how many of the actual positives are correctly predicted.
C) F1 Score-
F1 score=2*((precision * recall)/(precision+recall))
F1 score=2*((0.83 * 0.83) / (0.83+0.83))
F1 score= 2*(0.6889/1.66) =0.83=83%
F1 score balances precision and recall
d) Accuracy –
accuracy=TP+TN / TP+TN+FP+FN
accuracy= 5+3 / 5+3+1+1 = 8/10= 0.8= 80%
accuracy indicates the overall correctness of predictions
Summary of Metrics:
Precision: 83%
Recall: 83%
F1 Score: 83%
Accuracy: 80%
14. Given a confusion matrix, calculate Precision, Recall, F1 score and Accuracy.
15. Python Code to Evaluate a Model (in practical notebook ) do in idle
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df=pd.read_csv("C:\\Users\\hp\Desktop\\Salary.csv",sep=",")
print(df)
print(df.head())
print(df.shape)
print(df.isnull().sum())
#Data Preparation
X=np.array(df['Exp']).reshape(-1,1)
Y=np.array(df['sal']).reshape(-1,1)
print(X.shape,Y.shape)
plt.plot(X,Y,color='black')
plt.scatter(df['Exp'],Y,color='red')
plt.show( )
Exp sal
0 1.1 39343
1 1.3 46205
2 1.5 37731
3 2.0 43525
4 2.2 39891
5 2.9 56642
6 3.0 60150
7 3.2 54445
8 3.2 64445
9 3.7 57189
Exp sal
0 1.1 39343
1 1.3 46205
2 1.5 37731
3 2.0 43525
4 2.2 39891
(10, 2)
Exp 0
sal 0
dtype: int64
(10, 1) (10, 1)
16. Python Code to Evaluate a Model
import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
df=pd.DataFrame({'Exp':[1.1,1.3,1.5,2.0,2.2,2.9,3.0,3.2,3.2,3.7],
'sal':[39343,46205,37731,43525,39891,56642,60150,54445,64445,57189]})
print(df)
# Data Preparation
X=np.array(df['Exp']).reshape(-1,1)
Y=np.array(df['sal']).reshape(-1,1)
print(X.shape,Y.shape)
# Configuring the train-test split
X_train,x_test,Y_train,y_test=train_test_split(X,Y,test_size=0.2,shuffle=True,random_state=10)
# Fitting the model
model=LinearRegression()
model.fit(X_train,Y_train)
# Evaluation the score of training data
print(model.score(X_train,Y_train))
# Evaluation the score of testing data
print(model.score(x_test,y_test))
# Evaluating Mean Squared error
y_pred=model.predict(x_test)
print("mean squared error",mean_squared_error(y_test,y_pred))
output-
Exp sal
0 1.1 39343
1 1.3 46205
2 1.5 37731
3 2.0 43525
4 2.2 39891
5 2.9 56642
6 3.0 60150
7 3.2 54445
8 3.2 64445
9 3.7 57189
(10, 1) (10, 1)
0.6855940862107255
0.7022285224650959
mean squared error 53125245.23491811
17. Creating a website containing an Machine learning model
1. Go to the website https://teachablemachine.withgoogle.com/
2. Click on Get Started.
3. Teachable machine offers 3 options as you can see.
4. Choose the „Image‟ project.
5. Select „Standard Image Model‟.
6. You have the option to choose between two methods: using your webcam to capture
images or uploading existing images. For the webcam option, you will need to position
the image in front of the camera and hold down the record button to capture the image.
Alternatively, with the upload option, you have the choice to upload images either from
your local computer or directly from Google Drive.
7. Let us name „Class 1‟ as Kittens and upload pictures already saved on the computer.
8. Now, let us add another class of images “Puppies” saved on the computer.
9. Click on Train Model.
10. Once the model is trained, you can test the working by showing an image infront of the
web camera. Else, you can also upload the image from your local computer / Google
drive.
11. Now click on „Export Model‟. A screen will open up as shown. Now, click on Upload my
model.
12. Once your model is uploaded, Teachable Machine will create a URL which we will use
in the Javascript code. Copy the Javascript code by clicking on Copy.
13. Open Notepad and paste the JavaScript code and save this file as web.html.
14. Let us now deploy this model in a website.
15. Once you create a free account on Weebly, go to Edit website and create an appealing
website using the tools given.
16. Click on Embed Code and drag and place it on the webpage.
17. Click on „Edit HTML Code‟ and copy the Javascript code from the html file (web.html)
and paste it here as shown.
18. Click „Publish‟ (keep on Weebly subdomain for free version).
19. Copy the URL and paste it into a new browser window to check the working of your
model.
20. Click on start and you can show pictures of kitten and puppy to check the predictions of
your model.