0% found this document useful (0 votes)
11 views32 pages

ML5 Implementation

The document outlines various implementations of machine learning algorithms including K-Nearest Neighbors, K-Means Clustering, Naive Bayes, Decision Trees, and Linear Regression, using Python and libraries like Scikit-learn. It includes code snippets for each algorithm, demonstrating how to train models, make predictions, and visualize results. Additionally, it covers performance evaluation techniques such as confusion matrices and accuracy scores.

Uploaded by

ramzanrawal777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views32 pages

ML5 Implementation

The document outlines various implementations of machine learning algorithms including K-Nearest Neighbors, K-Means Clustering, Naive Bayes, Decision Trees, and Linear Regression, using Python and libraries like Scikit-learn. It includes code snippets for each algorithm, demonstrating how to train models, make predictions, and visualize results. Additionally, it covers performance evaluation techniques such as confusion matrices and accuracy scores.

Uploaded by

ramzanrawal777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

LO3 Develop a machine learning

application using an appropriate


programming language or
machine learning tool for solving
a real-world problem
RAJAD SHAKYA
KNN Implementation
import numpy as np
X = np.array([[1, 2], [2, 3], [3, 1], [6, 5], [7, 7], [8, 6]])
y = np.array([0, 0, 0, 1, 1, 1])
from sklearn.neighbors import KNeighborsClassifier
k=3
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X, y)
KNN Implementation
from sklearn.metrics import accuracy_score
test_point = np.array([[5, 5]])

prediction = knn.predict(test_point)
print(f'predicted class for{test_point} is
{prediction[0]}')
KNN Implementation
plt.figure(figsize=(10, 6))
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='red', label='Class 0')
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='blue', label='Class 1')
plt.scatter(test_point[0, 0], test_point[0, 1], color='green', label='Test
Point', marker='X', s=200)
neighbors = knn.kneighbors(test_point, return_distance=False)
for neighbor in neighbors[0]:
plt.scatter(X[neighbor, 0], X[neighbor, 1], facecolors='none',
edgecolors='black', s=100, linewidths=2)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('k-Nearest Neighbors (k = 3)')
plt.show()
K Means Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
X = np.array([
[2, 3],
[3, 4],
[5, 8],
[8, 8],
[9, 10],
[10, 12]
])
k=2
kmeans = KMeans(n_clusters=k)
kmeans.fit(X)
K Means Implementation
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
test_point = np.array([[7, 9]])
test_label = kmeans.predict(test_point)
print(test_label)
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.scatter(centroids[:, 0], centroids[:, 1], marker='X')
plt.scatter(test_point[:, 0], test_point[:, 1], c=test_label,
marker='D')
K Means Implementation
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('K-Means Clustering')
plt.legend()
plt.show()
Elbow Method Implementation
X=…
k_values = range(1, 5)
wcss = []
for k in k_values:
kmeans = KMeans(n_clusters=k, random_state=0)
kmeans.fit(X)
wcss.append(kmeans.inertia_)
plt.plot(k_values, wcss, 'bx-')
plt.show()
Naives Bayes
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import export_text
from sklearn import tree
import matplotlib.pyplot as plt
data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast', 'Sunny', 'Sunny',
'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cool', 'Mild', 'Mild',
'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal',
'Normal', 'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Weak',
'Weak', 'Strong', 'Strong', 'Weak', 'Strong'],
'PlayTennis': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'No']
}
Naives Bayes
df = pd.DataFrame(data)
le = LabelEncoder()
df['Outlook'] = le.fit_transform(df['Outlook'])
df['Temperature'] = le.fit_transform(df['Temperature'])
df['Humidity'] = le.fit_transform(df['Humidity'])
df['Wind'] = le.fit_transform(df['Wind'])
df['PlayTennis'] = le.fit_transform(df['PlayTennis'])
nb_model = GaussianNB()
nb_model.fit(df.drop('PlayTennis', axis=1), df['PlayTennis'])
y_pred_nb = nb_model.predict(df.drop('PlayTennis', axis=1))
accuracy_nb = accuracy_score(df['PlayTennis'], y_pred_nb)
print(f"Naive Bayes Accuracy: {accuracy_nb * 100:.2f}%")
Decision Trees
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import export_text
from sklearn import tree
import matplotlib.pyplot as plt
data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast', 'Sunny', 'Sunny',
'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cool', 'Mild', 'Mild',
'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal',
'Normal', 'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Weak',
'Weak', 'Strong', 'Strong', 'Weak', 'Strong'],
'PlayTennis': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
'No']
}
Decision Trees
df = pd.DataFrame(data)

le = LabelEncoder()
df['Outlook'] = le.fit_transform(df['Outlook'])
df['Temperature'] = le.fit_transform(df['Temperature'])
df['Humidity'] = le.fit_transform(df['Humidity'])
df['Wind'] = le.fit_transform(df['Wind'])
df['PlayTennis'] = le.fit_transform(df['PlayTennis'])
Decision Trees
clf = DecisionTreeClassifier(criterion='entropy')
clf.fit(X, y)
tree_rules = export_text(clf,
feature_names=list(X.columns))
print("\nDecision Tree Rules:")
print(tree_rules)
plt.figure(figsize=(20,10))
tree.plot_tree(clf, feature_names=list(X.columns),
class_names=['No', 'Yes'], filled=True)
plt.show()
Confusion Matrix
from sklearn.metrics import confusion_matrix, precision_score,
recall_score, f1_score, classification_report
cm = confusion_matrix(y_test, y_pred)
# Plot the confusion matrix using seaborn
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['Predicted 0', 'Predicted 1'], yticklabels=['Actual 0', 'Actual
1'])
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
IRIS dataset project
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(data=iris.data,
columns=iris.feature_names)
df['target'] = iris.target
Df
print(df.describe())
IRIS dataset project
import seaborn as sns
import matplotlib.pyplot as plt
sns.pairplot(df, hue='target', markers=["o", "s", "D"])
plt.show()
plt.figure(figsize=(10, 8))
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
IRIS dataset project
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df_scaled = pd.DataFrame(scaler.fit_transform(df.iloc[:,
:-1]), columns=iris.feature_names)
df_scaled['target'] = df['target']
print(df_scaled.head())
IRIS dataset project
from sklearn.model_selection import train_test_split
# Split the data into training and testing sets
X = df_scaled.iloc[:, :-1]
y = df_scaled['target']
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=42)
IRIS dataset project
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
# Initialize and train the Decision Tree classifier
dt_clf = DecisionTreeClassifier(random_state=42)
dt_clf.fit(X_train, y_train)
# Visualize the Decision Tree
plt.figure(figsize=(12, 8))
tree.plot_tree(dt_clf, feature_names=iris.feature_names,
class_names=iris.target_names, filled=True)
plt.title('Decision Tree')
plt.show()
IRIS dataset project
from sklearn.naive_bayes import GaussianNB

# Initialize and train the Naive Bayes classifier


nb_clf = GaussianNB()
nb_clf.fit(X_train, y_train)
IRIS dataset project
from sklearn.metrics import confusion_matrix,
classification_report, accuracy_score
dt_y_pred = dt_clf.predict(X_test)
nb_y_pred = nb_clf.predict(X_test)
confusion_matrix(y_test, dt_y_pred)
classification_report(y_test, dt_y_pred,
target_names=iris.target_names)
confusion_matrix(y_test, nb_y_pred)
classification_report(y_test, nb_y_pred,
target_names=iris.target_names)
LR
import numpy as np
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
np.random.seed(42)
n_samples = 100
X = np.linspace(0, 10, n_samples)
y = 3 * X + 2 + np.random.randn(n_samples)
plt.scatter(X, y)
plt.xlabel(‘X’)
plt.ylabel(‘y’)
plt.show()
LR
X = X.reshape(-1, 1)
y = y.reshape(-1, 1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
from sklearn.linear_model import SGDRegressor
sgd = SGDRegressor(alpha=0.01, max_iter=1000,
tol=1e-3)
sgd.fit(X_train, y_train.ravel())
LR
y_pred = sgd.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(f’Test set MSE: {mse:.2f}’)

plt.scatter(X_test, y_test)
plt.plot(X_test, y_pred, color=’red’)
plt.xlabel(‘X’)
plt.ylabel(‘y’)
plt.show()
LR
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 pandas.core.common import random_state
from sklearn.linear_model import LinearRegression
LR
df_sal =
pd.read_csv('https://raw.githubusercontent.com/krishn
aik06/simple-Linear-Regression/master/Salary_Data.cs
v')
df_sal.head()

X = df.iloc[:, :-1].values
y = df.iloc[:, -1].values
LR
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size = 0.2, random_state = 0)
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
plt.scatter(X_test, y_test)
plt.plot(X_test, y_pred)
plt.show()
LR
print(f'Coefficient: {regressor.coef_}')
print(f'Intercept: {regressor.intercept_}')
from sklearn.metrics import r2_score,
mean_squared_error
r2 = r2_score(y_test, y_pred_test)
mse = mean_squared_error(y_test, y_pred_test)
print(y_test, y_pred_test)
print("R²:", r2)
print("MSE:", mse)
Random Forest
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score,
classification_report
titanic_data = pd.read_csv(url)
X = titanic_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch',
'Fare']]
y = titanic_data['Survived']
Random Forest
X.loc[:, 'Sex'] = X['Sex'].map({'female': 0, 'male': 1})
X.loc[:, 'Age'].fillna(X['Age'].median(), inplace=True)

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


test_size=0.2, random_state=42)

rf_classifier =
RandomForestClassifier(n_estimators=100,
random_state=42)
Random Forest
rf_classifier.fit(X_train, y_train)
y_pred = rf_classifier.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


classification_rep = classification_report(y_test, y_pred)
Thank You

RAJAD SHAKYA

You might also like