0% found this document useful (0 votes)
3 views4 pages

exp9-10

The document outlines the implementation of two classification models: K-Nearest Neighbors (KNN) and Support Vector Machine (SVM). It includes code snippets for loading datasets, splitting data, training models, making predictions, and evaluating accuracy. Both models utilize Python libraries such as pandas and scikit-learn for data manipulation and machine learning tasks.
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)
3 views4 pages

exp9-10

The document outlines the implementation of two classification models: K-Nearest Neighbors (KNN) and Support Vector Machine (SVM). It includes code snippets for loading datasets, splitting data, training models, making predictions, and evaluating accuracy. Both models utilize Python libraries such as pandas and scikit-learn for data manipulation and machine learning tasks.
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/ 4

9) KNN (K-NEAREST NEIGHBOUR) CLASSIFICATION MODEL :

from sklearn.neighbors import KNeighborsClassifier


from sklearn.metrics import confusion_matrix, accuracy_score, classification_report
from sklearn.model_selection import train_test_split
import pandas as pd

# Load the dataset


dataset = pd.read_csv("/iris.csv")

# Split the data


X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, test_size=0.25)

# Build KNN model


classifier = KNeighborsClassifier(n_neighbors=8, p=3, metric='euclidean')
classifier.fit(X_train, y_train)

# Predict the test results


y_pred = classifier.predict(X_test)

# Evaluation
cm = confusion_matrix(y_test, y_pred)
print("Confusion matrix is as follows\n", cm)
print("Accuracy Metrics")
print(classification_report(y_test, y_pred))
print("Correct prediction:", accuracy_score(y_test, y_pred))
print("Wrong prediction:", 1 - accuracy_score(y_test, y_pred))
OUTPUT:
10) SUPPORT VECTOR MACHINE ALGORITHM

import matplotlib.pyplot as plt


import pandas as pd
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 the Dataset


dataset = pd.read_csv('/Social_Network_Ads.csv')

# Split Dataset into X and y


X = dataset.iloc[:, [0, 1]].values
y = dataset.iloc[:, 2].values

# Split the X and y Dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)

# Perform Feature Scaling


sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Fit SVM to the Training set


classifier = SVC(kernel='rbf', random_state=0)
classifier.fit(X_train, y_train)

# Predict the Test Set Results


y_pred = classifier.predict(X_test)
print(y_pred)

# Predict accuracy
print("Accuracy:", accuracy_score(y_test, y_pred))
OUTPUT:

You might also like