AI Lab9 22it3044
AI Lab9 22it3044
Q.1
# Importing necessary libraries
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
# Creating a DataFrame
df = pd.DataFrame(data)
# Splitting the data into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Splitting the data into training and test sets (60% train, 40% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,
random_state=42)
Q.1
# Importing necessary libraries
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
# Creating a DataFrame
df = pd.DataFrame(data)
# Splitting the data into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
Q.2
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
# Creating a DataFrame
df = pd.DataFrame(data)
# Splitting the data into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
print("\nFeature Importances:")
for i, feature in enumerate(features):
print(f"{feature}: {importance[i]:.4f}")
Feature Importances:
Dose: 0.0427
Age: 0.8305
Sex: 0.1268
Q.2–
# Importing necessary libraries
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
# Creating a DataFrame
df = pd.DataFrame(data)
# Splitting the data into training and test sets (60% train, 40% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,
random_state=42)
Q,3 –
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
# Creating a DataFrame
df = pd.DataFrame(data)
# Splitting the data into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
plt.show()
Accuracy: 1.0
Classification Report:
precision recall f1-score support
accuracy 1.00 2
macro avg 1.00 1.00 1.00 2
weighted avg 1.00 1.00 1.00 2
Q.4 –
# Importing necessary libraries
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
# Creating a DataFrame
df = pd.DataFrame(data)
# Splitting the data into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
Accuracy: 0.6666666666666666
Classification Report:
precision recall f1-score support
accuracy 0.67 3
macro avg 0.75 0.75 0.67 3
weighted avg 0.83 0.67 0.67 3
Sample Predictions:
Test Sample 1 -> Features: {'Dose': 60, 'Age': 48, 'Sex': 1, 'Smoking': 1}
| Actual: 0 | Predicted: 1
Test Sample 2 -> Features: {'Dose': 55, 'Age': 65, 'Sex': 0, 'Smoking': 1}
| Actual: 1 | Predicted: 1
Test Sample 3 -> Features: {'Dose': 20, 'Age': 25, 'Sex': 0, 'Smoking': 0}
| Actual: 0 | Predicted: 0
Q.5–
# Splitting the data into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
print("Accuracy:", accuracy)
print("F1 Score:", f1)
plt.figure(figsize=(8, 6))
sns.barplot(x=importances[indices], y=features[indices],
palette="viridis")
plt.title("Feature Importances in Random Forest")
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.show()
Accuracy: 0.5
F1 Score: 0.6666666666666666
Classification Report:
precision recall f1-score support
accuracy 0.50 2
macro avg 0.50 0.25 0.33 2
weighted avg 1.00 0.50 0.67 2