ML5 Implementation
ML5 Implementation
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
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)
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)
RAJAD SHAKYA