FML LabFile 7exps
FML LabFile 7exps
Practical File
Submitted in partial fulfillment for the evaluation of
“Fundamentals of Machine
Learning-Lab”
Submitted By:
Students Name: Kunal Saini , Aditya Jain , Aditi Jain , Ananya Tyagi , Aditya Pachisia.
Enrolment Number: 07317711621, 07617711621, 08217711621, 08817711621, 09117711621
Branch & Section: AIML(B)
Submitted To:
1
FML_LAB FILE
Index
S.No Details Experiment Date Grade/Evaluation Sign
No.
2
FML_LAB FILE
3
FML_LAB FILE
# Perform predictions
y_pred = model.predict(X)
4
FML_LAB FILE
# Create a linear regression model
model = LinearRegression()
# Perform predictions
y_pred = model.predict(X)
# Perform predictions
y_pred = model.predict(X)
5
FML_LAB FILE
mse = mean_squared_error(y, y_pred)
print("Mean Squared Error:", mse)
Output:
Mean Squared Error: 0.019054032881784515
6
FML_LAB FILE
Mean Squared Error: 0.05962293563300296
7
FML_LAB FILE
8
FML_LAB FILE
Code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.model_selection import train_test_split
# Select the ' lengthOfKernel ' and ' lengthofkernelgroove ' columns for logistic
regression
X = data[[' lengthOfKernel ']] # Predictor variable
y = data[' lengthofkernelgroove '] # Target variable
9
FML_LAB FILE
print(confusion_mat)
10
FML_LAB FILE
print("Confusion Matrix:")
print(confusion_mat)
Output:
Accuracy: 0.5024390243902439
Confusion Matrix:
[[39 63]
[39 64]]
11
FML_LAB FILE
Accuracy: 0.624390243902439
Confusion Matrix:
[[66 36]
[41 62]]
12
FML_LAB FILE
13
FML_LAB FILE
Code :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
14
FML_LAB FILE
# Histogram of asymmetryCoefficient (maximum heart rate achieved)
plt.hist(data[data['lengthofkernelgroove'] == 0]['asymmetryCoefficient'],
bins=30, alpha=0.5, label='Lengthofkernelgroove 0')
plt.hist(data[data['lengthofkernelgroove'] == 1]['asymmetryCoefficient'],
bins=30, alpha=0.5, label='Lengthofkernelgroove 1')
plt.xlabel('asymmetryCoefficient')
plt.ylabel('Frequency')
plt.title('KNN')
plt.legend()
plt.show()
Output:
15
FML_LAB FILE
16
FML_LAB FILE
KNN
asymmetryCoefficient
17
Experiment 4: Study and Implement classification
using SVM.
Abstract:
17
Code:
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
# Evaluate the performance of the model using the mean squared error and R-
squared metric
print("Mean squared error:", mean_squared_error(y_test, y_pred))
print("R-squared:", r2_score(y_test, y_pred))
18
# Evaluate the performance of the model using the mean squared error and R-
squared metric
print("Mean squared error:", mean_squared_error(y_test, y_pred))
print("R-squared:", r2_score(y_test, y_pred))
19
FML_LAB FILE
# Plot the predicted values and the actual values on the test data
plt.figure(figsize=(10, 5))
plt.scatter(X_test, y_test, color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=3)
plt.xlabel('area')
plt.ylabel('widthOfKernel')
plt.title('Support Vector Regression')
plt.show()
# Plot the predicted values and the actual values on the test data
plt.figure(figsize=(10, 5))
plt.scatter(X_test, y_test, color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=3)
plt.xlabel('lengthOfKernel ')
20
plt.ylabel('asymmetryCoefficient')
plt.title('Support Vector Regression')
plt.show()
Output:
compactness
21
22
23
FML_LAB FILE
23
FML_LAB FILE
Code:
import pandas as
pd import numpy
as np
import matplotlib.pyplot as
plt import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
# Feature Importance
feature_importance = rf.feature_importances_ feature_names = X.columns
24
FML_LAB FILE
#Bar plot of feature importance
plt.figure(figsize=(10, 6))
# Heatmap of confusion
matrix plt.figure(figsize=(8,
6))
sns.heatmap(confusion_mat, annot=True, cmap='Blues', fmt='d')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Random Forest: Confusion
Matrix') plt.show()
25
Output:
25
26
FML_LAB FILE
27
FML_LAB FILE
Code:
import pandas as pd
import seaborn as
sns
import matplotlib.pyplot as plt
from sklearn.model_selection import
train_test_split from sklearn.naive_bayes import
GaussianNB
from sklearn.metrics import confusion_matrix, classification_report
# Split the dataset into features (X) and lengthofkernelgroove variable (y)
X = data.drop("lengthofkernelgroove", axis=1)
y = data["lengthofkernelgroove"]
28
FML_LAB FILE
29
FML_LAB FILE
Output:
29
FML_LAB FILE
Experiment 7:
Abstract:
30
FML_LAB FILE
Code:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import confusion_matrix
# Split the dataset into features (X) and lengthofkernelgroove variable (y)
X = data.drop("lengthofkernelgroove", axis=1)
y = data["lengthofkernelgroove"]
31
FML_LAB FILE
# Plot the confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, cmap="Blues", fmt="d", cbar=False)
plt.title("Confusion Matrix")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
# Generate the classification report
report = classification_report(y_test, y_pred)
print("Classification Report:")
print(report)
Output:
32
FML_LAB FILE
33