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

Machine Learning Classifiers in Python

The document outlines a machine learning workflow using Python, specifically focusing on loading a dataset, preprocessing it, and applying three classification algorithms: Random Forest, K-Nearest Neighbors, and Support Vector Machine. It includes code snippets for model training, evaluation, and optional hyperparameter tuning using GridSearchCV for the SVM model. There are also error messages indicating issues with dataset loading and variable definitions.

Uploaded by

tkfx2jf9zs
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)
14 views4 pages

Machine Learning Classifiers in Python

The document outlines a machine learning workflow using Python, specifically focusing on loading a dataset, preprocessing it, and applying three classification algorithms: Random Forest, K-Nearest Neighbors, and Support Vector Machine. It includes code snippets for model training, evaluation, and optional hyperparameter tuning using GridSearchCV for the SVM model. There are also error messages indicating issues with dataset loading and variable definitions.

Uploaded by

tkfx2jf9zs
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

import pandas as pd

import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV
from [Link] import RandomForestClassifier
from [Link] import KNeighborsClassifier
from [Link] import SVC
from [Link] import accuracy_score, classification_report, confusion_mat

 Load your dataset

data = pd.read_csv() # Replace with your dataset path

File "<ipython-input-2-fb2fbd479d97>", line 1


data = pd.read_csv([Link]) # Replace with your dataset
path
^
SyntaxError: invalid decimal literal

from [Link] import drive


[Link]('/content/drive')

Double-click (or enter) to edit

Assume the dataset has features in columns



'feature1', 'feature2', ..., 'featureN'

and target labels in a column named 'target'

[Link] 17/01/25, 12 19
Page 1 of 4
:
features = [Link](columns=['target'])
labels = data['target']

--------------------------------------------------------------------
-------
NameError Traceback (most recent
call last)
<ipython-input-5-5943e81b0eae> in <cell line: 1>()
----> 1 features = [Link](columns=['target'])
2 labels = data['target']

NameError: name 'data' is not defined

 Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=

 Random Forest ClassiFer

rf_model = RandomForestClassifier(random_state=42)
rf_model.fit(X_train, y_train)
rf_predictions = rf_model.predict(X_test)
print("Random Forest Results:")
print("Accuracy:", accuracy_score(y_test, rf_predictions))
print("Classification Report:\n", classification_report(y_test, rf_predictions))

 K-Nearest Neighbors (KNN) ClassiFer

knn_model = KNeighborsClassifier(n_neighbors=5) # Adjust 'n_neighbors' based on


knn_model.fit(X_train, y_train)
knn_predictions = knn_model.predict(X_test)
print("KNN Results:")
print("Accuracy:", accuracy_score(y_test, knn_predictions))
print("Classification Report:\n", classification_report(y_test, knn_predictions)

[Link] 17/01/25, 12 19
Page 2 of 4
:
 Support Vector Machine (SVM) ClassiFer

svm_model = SVC(kernel='rbf', C=1, gamma='scale', random_state=42)


svm_model.fit(X_train, y_train)
svm_predictions = svm_model.predict(X_test)
print("SVM Results:")
print("Accuracy:", accuracy_score(y_test, svm_predictions))
print("Classification Report:\n", classification_report(y_test, svm_predictions)

Optional: Hyperparameter tuning using



GridSearchCV for SVM

svm_param_grid = {
'C': [0.1, 1, 10],
'gamma': ['scale', 'auto'],
'kernel': ['linear', 'rbf']
}

svm_grid = GridSearchCV(SVC(random_state=42), svm_param_grid, cv=5, scoring='acc


svm_grid.fit(X_train, y_train)
print("Best SVM Parameters:", svm_grid.best_params_)
print("Best SVM Accuracy:", svm_grid.best_score_)

[Link] 17/01/25, 12 19
Page 3 of 4
:
[Link] 17/01/25, 12 19
Page 4 of 4
:

You might also like