3/20/25, 6:27 PM ML 7 Decision tree.
ipynb - Colab
E0323007 - POOJA JAYAGANAPTHI
import numpy as np
import matplotlib.pyplot as plt
def function(x):
return x**2
def gradient(x):
return 2*x
def gradient_descent(starting_point, learning_rate, num_iterations):
x = starting_point
history = [x]
for i in range(num_iteration):
grad = gradient(x)
x=x-learning_rate * grad
history.append(x)
return x, history
starting_point = 10
num_iteration = 100
learning_rates = [0.1,0.01,0.001]
plt.figure(figsize=(10,6))
for lr in learning_rates:
_ ,history = gradient_descent(starting_point, lr, num_iterations)
plt.plot(history,label=f'Learning Rate: {lr}')
plt.xlabel('Iteration')
plt.ylabel('x value')
plt.title('Gradient Descent with convergence')
plt.legend()
plt.grid(True)
plt.show()
# Import necessary libraries
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# Load a dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
https://colab.research.google.com/drive/1cZHo4IkS8yeMXmS8fREmbGrHRQQz_zua?authuser=1#printMode=true 1/4
3/20/25, 6:27 PM ML 7 Decision tree.ipynb - Colab
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Standardize the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Create an SVM classifier
svm = SVC(kernel='linear', C=1.0, random_state=42)
# Train the classifier
svm.fit(X_train, y_train)
# Make predictions
y_pred = svm.predict(X_test)
# Calculate the accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
Accuracy: 0.98
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
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4.7, 1.6],
[4.9, 2.4, 3.3, 1. ],
https://colab.research.google.com/drive/1cZHo4IkS8yeMXmS8fREmbGrHRQQz_zua?authuser=1#printMode=true 2/4
3/20/25, 6:27 PM ML 7 Decision tree.ipynb - Colab
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text, plot_tree
import matplotlib.pyplot as plt
# Sample dataframe
data = {
'Age': [25, 30, 35, 40, 45, 50, 55, 60, 65, 70],
'Income': ['Low', 'Medium', 'High', 'Low', 'Medium', 'High', 'Low', 'Medium', 'High', 'Low'],
'Student': ['No', 'No', 'Yes', 'Yes', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes'],
'Credit_Rating': ['Fair', 'Excellent', 'Fair', 'Excellent', 'Fair', 'Fair', 'Excellent', 'Fair', 'Excellent', 'Fair'],
'Buys_Computer': ['No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No']
}
df = pd.DataFrame(data)
# Convert categorical variables to numerical values
df = pd.get_dummies(df, columns=['Income', 'Student', 'Credit_Rating'], drop_first=True)
# Splitting data into features and target
X = df.drop(columns=['Buys_Computer'])
y = df['Buys_Computer'].map({'No': 0, 'Yes': 1}) # Convert target to numeric
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create Decision Tree classifier with Gini index
clf = DecisionTreeClassifier(criterion='gini', random_state=42)
clf.fit(X_train, y_train)
# Display the decision tree rules
print(export_text(clf, feature_names=list(X.columns)))
|--- Age <= 57.50
| |--- Student_Yes <= 0.50
| | |--- Age <= 47.50
| | | |--- class: 0
| | |--- Age > 47.50
| | | |--- class: 1
| |--- Student_Yes > 0.50
| | |--- class: 1
|--- Age > 57.50
| |--- class: 0
# Visualize the Decision Tree
plt.figure(figsize=(12, 8))
plot_tree(clf, feature_names=X.columns, class_names=['No', 'Yes'], filled=True, rounded=True)
plt.show()
https://colab.research.google.com/drive/1cZHo4IkS8yeMXmS8fREmbGrHRQQz_zua?authuser=1#printMode=true 3/4
3/20/25, 6:27 PM ML 7 Decision tree.ipynb - Colab
new data = pd DataFrame({'Age': [29] 'Income Medium': [1] 'Income High': [0] 'Student Yes': [1] 'Credit Rating Excellent': [0]})
https://colab.research.google.com/drive/1cZHo4IkS8yeMXmS8fREmbGrHRQQz_zua?authuser=1#printMode=true 4/4