0% found this document useful (0 votes)
4 views19 pages

ML Code

The document contains multiple sections on different machine learning problems, including Decision Trees, K-Means Clustering, and Linear Regression. Each section provides code examples for data preprocessing, model training, evaluation, and visualization using various datasets. The methodologies include custom implementations of linear regression and clustering techniques, along with visualizations of results.
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)
4 views19 pages

ML Code

The document contains multiple sections on different machine learning problems, including Decision Trees, K-Means Clustering, and Linear Regression. Each section provides code examples for data preprocessing, model training, evaluation, and visualization using various datasets. The methodologies include custom implementations of linear regression and clustering techniques, along with visualizations of results.
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

1. Decision Tree Problem (drug.

csv)

import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder
from [Link] import DecisionTreeClassifier, plot_tree
from [Link] import accuracy_score

df = pd.read_csv('[Link]')
[Link]()

le = LabelEncoder()
df['Sex'] = le.fit_transform(df['Sex'])
df['BP'] = le.fit_transform(df['BP'])
df['Cholesterol'] = le.fit_transform(df['Cholesterol'])
df['Drug'] = le.fit_transform(df['Drug'])

X = [Link]('Drug', axis = 1)
y = df['Drug']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,


random_state = 42)

dt_classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 42)

dt_classifier.fit(X_train, y_train)
y_pred = dt_classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)

print(f"The accuracy of the Decision Tree: {accuracy * 100 : .2f}%")

class_names = le.inverse_transform(sorted([Link]()))
[Link](figsize = (12, 8))
plot_tree(
dt_classifier,
feature_names = [Link],
class_names = class_names,
filled = True
)
[Link]("Decision Tree Visualisation")
[Link]()
2. K-Means Problem (cust_segmentation.csv)

import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import StandardScaler
from [Link] import PCA
from [Link] import KMeans
from [Link] import cdist
from mpl_toolkits.mplot3d import Axes3D

df = pd.read_csv('Cust_Segmentation.csv')
[Link]()

df_clean = [Link](columns = ['Customer Id', 'Address'], axis = 1)


df_clean = df_clean.dropna()

features = df_clean.[Link]()
X = df_clean[features]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

inertia_e = []
inertia_m = []
k_range = range(1, 11)
for k in k_range:
km_e = KMeans(n_clusters = k, random_state = 42)
km_e.fit(X_scaled)
inertia_e.append(km_e.inertia_)
km_m = KMeans(n_clusters = k, random_state = 42)
km_m.fit(X_scaled)
inertia_m.append(km_m.inertia_)

[Link](figsize = (12, 5))


[Link](1, 2, 1)
[Link](k_range, inertia_e, 'bo-')
[Link]('Number of clusters')
[Link]('Inertia (Euclidean)')
[Link]('Elbow Method (Euclidean)')

[Link](1, 2, 2)
[Link](k_range, inertia_m, 'ro-')
[Link]('Number of clusters')
[Link]('Inertia (Manhattan)')
[Link]('Elbow Method (Manhattan)')
[Link]()

k_opt = 4

km_e = KMeans(n_clusters = k_opt,random_state = 42)


cluster_e = km_e.fit_predict(X_scaled)

pca_2d = PCA(n_components = 2)
X_2d = pca_2d.fit_transform(X_scaled)
pca_3d = PCA(n_components = 3)
X_3d = pca_3d.fit_transform(X_scaled)

[Link](figsize = (8, 6))


[Link](x = X_2d[:, 0], y = X_2d[:, 1], hue = cluster_e, palette = "Set2", s =
50)
[Link]("K-Means Clustering (Euclidean) - 2D PCA")
[Link]("PCA Component 1")
[Link]("PCA Component 2")
[Link](title = "Cluster")
[Link](True)
plt.tight_layout()
[Link]()

fig = [Link](figsize = (10, 7))


ax = fig.add_subplot(111, projection = '3d')
scatter = [Link](X_3d[:, 0], X_3d[:, 1], X_3d[:, 2], c = cluster_e, cmap = 'Set2',
s = 50)
ax.set_title("K-Means Clustering (Euclidean) - 3D PCA")
ax.set_xlabel("PCA 1")
ax.set_ylabel("PCA 2")
ax.set_zlabel("PCA 3")
[Link](scatter)
plt.tight_layout()
[Link]()

# Custom K-Means with Manhattan Distance


def kmeans_manhattan(X, n_clusters = 4, max_iters = 100, random_state = 42):
[Link](random_state)
n_samples = [Link][0]
centroids = X[[Link](n_samples, n_clusters, replace = False)]

for _ in range(max_iters):
distances = cdist(X, centroids, metric = 'cityblock')
labels = [Link](distances, axis = 1)
new_centroids = [Link]([
[Link](X[labels == k], axis=0) if len(X[labels == k]) > 0 else
centroids[k]
for k in range(n_clusters)
])
if [Link](centroids, new_centroids):
break
centroids = new_centroids
return labels, centroids

cluster_m, centroids_m = kmeans_manhattan(X_scaled, n_clusters = k_opt)

[Link](figsize = (8, 6))


[Link](x = X_2d[:, 0], y = X_2d[:, 1], hue = cluster_m, palette = "Set1", s =
50)
[Link]("K-Means Clustering (Manhattan) - 2D PCA")
[Link]("PCA Component 1")
[Link]("PCA Component 2")
[Link](title = "Cluster")
[Link](True)
plt.tight_layout()
[Link]()
fig = [Link](figsize = (10, 7))
ax = fig.add_subplot(111, projection ='3d')
scatter = [Link](X_3d[:, 0], X_3d[:, 1], X_3d[:, 2], c = cluster_m, cmap = 'Set1',
s = 50)
ax.set_title("K-Means Clustering (Manhattan) - 3D PCA")
ax.set_xlabel("PCA 1")
ax.set_ylabel("PCA 2")
ax.set_zlabel("PCA 3")
[Link](scatter)
plt.tight_layout()
[Link]()
3. Linear Regression 1(Used_Car_Dataset.csv)

import pandas as pd

import numpy as np

import [Link] as plt

from [Link] import StandardScaler

from sklearn.model_selection import train_test_split

from [Link] import r2_score, mean_squared_error


# Load and clean the dataset

df = pd.read_csv("Used_Car_Dataset.csv")

[Link](inplace=True)

# ----------------------------- Custom Linear Regression ----------------------------- #

class LinearRegressionGD:

def __init__(self, learning_rate=0.01, n_iterations=10000, tolerance=1e-6):

self.learning_rate = learning_rate

self.n_iterations = n_iterations

[Link] = tolerance

def fit(self, X, y):

m, n = [Link]

X = np.c_[[Link](m), X]

y = [Link](-1, 1)

[Link] = [Link]((n + 1, 1))

self.cost_history = []

for i in range(self.n_iterations):

predictions = [Link]([Link])

errors = predictions - y

cost = (1 / (2 * m)) * [Link](errors ** 2)

self.cost_history.append(cost)

gradients = (1 / m) * [Link](errors)

prev_theta = [Link]()

[Link] -= self.learning_rate * gradients

if [Link]([Link] - prev_theta, ord=2) < [Link]:

print(f"Converged at iteration {i}")

break
self.X = X

self.y = y

def predict(self, X):

X = np.c_[[Link]([Link][0]), X]

return [Link]([Link])

# ----------------------------- Univariate Linear Regression ----------------------------- #

def univariate_linear_regression(df, feature, target):

X = df[[feature]].values

y = df[target].[Link](-1, 1)

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

model = LinearRegressionGD()

[Link](X_scaled, y)

# Plot cost history

[Link](model.cost_history)

[Link](f"Univariate Regression - Cost over Iterations ({feature})")

[Link]("Iterations")

[Link]("Cost")

[Link](True)

[Link]()

# Plot regression line

[Link](X_scaled, y, color='blue', label='Data')

[Link](X_scaled, [Link](X_scaled), color='red', label='Prediction')


[Link](f"{feature} (standardized)")

[Link](target)

[Link](f"Univariate Regression - {feature} vs {target}")

[Link]()

[Link]()

# ----------------------------- Multivariate Linear Regression ----------------------------- #

def multivariate_linear_regression(df, target_column):

X = [Link](columns=[target_column]).values

y = df[target_column].[Link](-1, 1) # Ensure column vector

X_scaled = StandardScaler().fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

model = LinearRegressionGD()

[Link](X_train, y_train)

y_pred = [Link](X_test)

r2 = r2_score(y_test, y_pred)

mse = mean_squared_error(y_test, y_pred)

print("\nHypothesis Function:")

theta = [Link]()

print(f"h(x) = {theta[0]:.2f} " + " ".join([f"+ ({theta[i]:.2f})*x{i}" for i in range(1, len(theta))]))

print(f"R² Score: {r2:.4f}")

print(f"Mean Squared Error: {mse:.2f}")

# Plot cost

[Link](model.cost_history)
[Link]("Multivariate Linear Regression - Cost Over Iterations")

[Link]("Iterations")

[Link]("Cost")

[Link](True)

[Link]()

# ----------------------------- Run ----------------------------- #

univariate_linear_regression(df, feature="Mileage_km", target="Price_INR")

multivariate_linear_regression(df, target_column="Price_INR")

4. Linear Regression 2([Link])

import pandas as pd

import numpy as np

import [Link] as plt

from [Link] import StandardScaler

from sklearn.model_selection import train_test_split

from [Link] import r2_score, mean_squared_error

# ----------------------------- Load and Clean the Dataset ----------------------------- #

df = pd.read_csv("[Link]")

# Convert binary categorical variables

binary_columns = ['mainroad', 'guestroom', 'basement', 'hotwaterheating', 'airconditioning', 'prefarea']

for col in binary_columns:

df[col] = df[col].map({'yes': 1, 'no': 0})

# One-hot encoding for furnishingstatus

df = pd.get_dummies(df, columns=['furnishingstatus'], drop_first=True)


# ----------------------------- Custom Linear Regression Class ----------------------------- #

class LinearRegressionGD:

def __init__(self, learning_rate=0.01, n_iterations=10000, tolerance=1e-6):

self.learning_rate = learning_rate

self.n_iterations = n_iterations

[Link] = tolerance

def fit(self, X, y):

m, n = [Link]

X = np.c_[[Link](m), X]

y = [Link](-1, 1)

[Link] = [Link]((n + 1, 1))

self.cost_history = []

for i in range(self.n_iterations):

predictions = [Link]([Link])

errors = predictions - y

cost = (1 / (2 * m)) * [Link](errors ** 2)

self.cost_history.append(cost)

gradients = (1 / m) * [Link](errors)

prev_theta = [Link]()

[Link] -= self.learning_rate * gradients

if [Link]([Link] - prev_theta, ord=2) < [Link]:

print(f"Converged at iteration {i}")

break
self.X = X

self.y = y

def predict(self, X):

X = np.c_[[Link]([Link][0]), X]

return [Link]([Link])

# ----------------------------- Univariate Linear Regression ----------------------------- #

def univariate_linear_regression(df, feature, target):

X = df[[feature]].values

y = df[[target]].values

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

model = LinearRegressionGD()

[Link](X_scaled, y)

# Plot cost history

[Link](model.cost_history)

[Link](f"Univariate Regression - Cost over Iterations ({feature})")

[Link]("Iterations")

[Link]("Cost")

[Link](True)

[Link]()

# Plot regression line

[Link](X_scaled, y, color='blue', label='Data')

[Link](X_scaled, [Link](X_scaled), color='red', label='Prediction')


[Link](f"{feature} (Standardized)")

[Link](target)

[Link](f"Univariate Regression - {feature} vs {target}")

[Link]()

[Link](True)

[Link]()

# ----------------------------- Multivariate Linear Regression ----------------------------- #

def multivariate_linear_regression(df, target_column):

X = [Link](columns=[target_column]).values

y = df[[target_column]].values

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

model = LinearRegressionGD()

[Link](X_train, y_train)

y_pred = [Link](X_test)

r2 = r2_score(y_test, y_pred)

mse = mean_squared_error(y_test, y_pred)

print("\nHypothesis Function:")

theta = [Link]()

print(f"h(x) = {theta[0]:.2f} " + " ".join([f"+ ({theta[i]:.2f})*x{i}" for i in range(1, len(theta))]))

print(f"R² Score: {r2:.4f}")

print(f"Mean Squared Error: {mse:.2f}")

# Plot cost history


[Link](model.cost_history)

[Link]("Multivariate Regression - Cost over Iterations")

[Link]("Iterations")

[Link]("Cost")

[Link](True)

[Link]()

# ----------------------------- Run ----------------------------- #

univariate_linear_regression(df, feature="area", target="price")

multivariate_linear_regression(df, target_column="price")

5. Linear Regression3(Fuel_Consumption_2000-[Link])

import pandas as pd

import numpy as np

import [Link] as plt

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler

from [Link] import r2_score, mean_squared_error

# ----------------------------- Load and Clean the Dataset ----------------------------- #

df = pd.read_csv("Fuel_Consumption_2000-[Link]")

[Link](inplace=True)

# ----------------------------- Custom Linear Regression Class ----------------------------- #

class LinearRegressionGD:

def __init__(self, learning_rate=0.01, n_iterations=10000, tolerance=1e-6):

self.learning_rate = learning_rate
self.n_iterations = n_iterations

[Link] = tolerance

def fit(self, X, y):

m, n = [Link]

X = np.c_[[Link](m), X]

y = [Link](-1, 1)

[Link] = [Link]((n + 1, 1))

self.cost_history = []

for i in range(self.n_iterations):

predictions = [Link]([Link])

errors = predictions - y

cost = (1 / (2 * m)) * [Link](errors ** 2)

self.cost_history.append(cost)

gradients = (1 / m) * [Link](errors)

prev_theta = [Link]()

[Link] -= self.learning_rate * gradients

if [Link]([Link] - prev_theta, ord=2) < [Link]:

print(f"Converged at iteration {i}")

break

self.X = X

self.y = y

def predict(self, X):

X = np.c_[[Link]([Link][0]), X]
return [Link]([Link])

# ----------------------------- Univariate Linear Regression ----------------------------- #

def univariate_linear_regression(df, feature, target):

X = df[[feature]].values

y = df[target].[Link](-1, 1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = [Link](X_test)

model = LinearRegressionGD()

[Link](X_train_scaled, y_train)

y_pred = [Link](X_test_scaled)

r2 = r2_score(y_test, y_pred)

mse = mean_squared_error(y_test, y_pred)

print(f"\nUnivariate Regression ({feature} -> {target})")

print(f"R² Score: {r2:.4f}")

print(f"Mean Squared Error: {mse:.2f}")

# Plot cost history

[Link](model.cost_history)

[Link](f"Univariate Regression - Cost over Iterations ({feature})")

[Link]("Iterations")

[Link]("Cost")
[Link](True)

[Link]()

# Plot predictions

[Link](X_test_scaled, y_test, color='blue', label='Actual')

[Link](X_test_scaled, y_pred, color='red', label='Prediction')

[Link](f"{feature} (Standardized)")

[Link](target)

[Link](f"Univariate Regression - {feature} vs {target}")

[Link]()

[Link](True)

[Link]()

# ----------------------------- Multivariate Linear Regression ----------------------------- #

def multivariate_linear_regression(df, feature_columns, target_column):

X = df[feature_columns].values

y = df[[target_column]].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)

X_test_scaled = [Link](X_test)

model = LinearRegressionGD()

[Link](X_train_scaled, y_train)

y_pred = [Link](X_test_scaled)

r2 = r2_score(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)

print(f"\nMultivariate Regression ({', '.join(feature_columns)} -> {target_column})")

print(f"R² Score: {r2:.4f}")

print(f"Mean Squared Error: {mse:.2f}")

# Print hypothesis

theta = [Link]()

print("\nHypothesis Function:")

print(f"h(x) = {theta[0]:.2f} " + " ".join([f"+ ({theta[i]:.2f})*x{i}" for i in range(1, len(theta))]))

# Plot cost history

[Link](model.cost_history)

[Link]("Multivariate Regression - Cost over Iterations")

[Link]("Iterations")

[Link]("Cost")

[Link](True)

[Link]()

# Plot prediction vs actual

[Link](y_test, y_pred, color='green')

[Link]([min(y_test), max(y_test)], [min(y_test), max(y_test)], 'r--')

[Link]("Actual Fuel Consumption")

[Link]("Predicted Fuel Consumption")

[Link]("Multivariate Regression - Actual vs Predicted")

[Link](True)

[Link]()

# ----------------------------- Run ----------------------------- #

univariate_linear_regression(df, feature="ENGINE SIZE", target="FUEL CONSUMPTION")


features_multi = ['ENGINE SIZE', 'CYLINDERS', 'HWY (L/100 km)', 'COMB (L/100 km)', 'COMB (mpg)', 'EMISSIONS']

multivariate_linear_regression(df, feature_columns=features_multi, target_column="FUEL CONSUMPTION")

6. Hierarchical Clustering(cars_clustering.csv)

import pandas as pd
import numpy as np
from [Link] import StandardScaler
from [Link] import KMeans
from [Link] import PCA
from [Link] import linkage, dendrogram
import [Link] as plt
import warnings
[Link](action = 'ignore')

df = pd.read_csv('cars_clustering.csv')

[Link]()

# Replace non-numeric and missing values


df_cleaned = [Link]({'\$null\$': [Link], '\$': ''}, regex = True)
df_cleaned = df_cleaned.drop(columns = ['manufact', 'model', 'lnsales', 'partition'])
# Drop non-numeric/categorical columns

# Convert all remaining columns to numeric


df_cleaned = df_cleaned.apply(pd.to_numeric, errors = 'coerce')

# Drop rows with missing values


df_cleaned = df_cleaned.dropna()

# Display the cleaned data


df_cleaned.head(), df_cleaned.shape

# Normalize the data


scaler = StandardScaler()
scaled_data = scaler.fit_transform(df_cleaned)

# Perform hierarchical clustering with different linkage methods


linkage_methods = ['single', 'complete', 'average']
linkage_matrices = {method: linkage(scaled_data, method = method) for method in
linkage_methods}
# Plot dendrograms
[Link](figsize = (18, 5))
for i, method in enumerate(linkage_methods):
[Link](1, 3, i + 1)
dendrogram(linkage_matrices[method], no_labels=True)
[Link](f'Agglomerative Clustering\n({[Link]()} Linkage)')
[Link]('Vehicles')
[Link]('Distance')
plt.tight_layout()
[Link]()

# Fixed Bisecting K-Means (Divisive Clustering)


def bisecting_kmeans(data, max_clusters=5):
clusters = [data]
cluster_indices = [[Link]([Link][0])]
labels = [Link]([Link][0], dtype=int)

for cluster_id in range(1, max_clusters):


# Find the cluster with highest SSE
sse_list = [[Link]((cluster - [Link](axis=0)) ** 2) for cluster in
clusters]
idx_to_split = [Link](sse_list)
cluster_to_split = [Link](idx_to_split)
indices_to_split = cluster_indices.pop(idx_to_split)

# Apply k-means with k=2 to the selected cluster


kmeans = KMeans(n_clusters=2, random_state=0).fit(cluster_to_split)
split_labels = kmeans.labels_

# Split the cluster and track their original indices


cluster_0 = cluster_to_split[split_labels == 0]
cluster_1 = cluster_to_split[split_labels == 1]
indices_0 = indices_to_split[split_labels == 0]
indices_1 = indices_to_split[split_labels == 1]

# Add new clusters and indices


[Link]([cluster_0, cluster_1])
cluster_indices.extend([indices_0, indices_1])

# Update labels
labels[indices_0] = idx_to_split
labels[indices_1] = cluster_id

return labels
# Apply the bisecting k-means algorithm
divisive_labels = bisecting_kmeans(scaled_data, max_clusters=5)

# Visualize the clusters using PCA (2D)


pca = PCA(n_components=2)
reduced_data = pca.fit_transform(scaled_data)

[Link](figsize=(8, 6))
scatter = [Link](reduced_data[:, 0], reduced_data[:, 1], c=divisive_labels,
cmap='tab10')
[Link]("Divisive Clustering (Simulated with Bisecting K-Means)", fontsize=14)
[Link]("PCA Component 1")
[Link]("PCA Component 2")
[Link](scatter, label='Cluster ID')
[Link](True)
plt.tight_layout()
[Link]()

You might also like