Codes for Project
Codes for Project
data_encoded = data.copy()
label_encoders = {}
label_encoders[col] = LabelEncoder()
data_encoded[col] = label_encoders[col].fit_transform(data[col])
X = data_encoded.drop(columns=['y'])
y = data_encoded['y']
smote = SMOTE(random_state=42)
X_train_smote = scaler.fit_transform(X_train_smote)
X_test = scaler.transform(X_test)
# KNN Classifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train_smote, y_train_smote)
y_pred_knn = knn.predict(X_test)
print("KNN Classifier:")
print(classification_report(y_test, y_pred_knn))
# Logistic Regression
logreg.fit(X_train_smote, y_train_smote)
y_pred_logreg = logreg.predict(X_test)
print("\nLogistic Regression:")
print(classification_report(y_test, y_pred_logreg))
svm.fit(X_train_smote, y_train_smote)
y_pred_svm = svm.predict(X_test)
print(classification_report(y_test, y_pred_svm))
# Required imports
data_encoded = data.copy()
label_encoders = {}
label_encoders[col] = LabelEncoder()
data_encoded[col] = label_encoders[col].fit_transform(data[col])
X = data_encoded.drop(columns=['y'])
y = data_encoded['y']
smote = SMOTE(random_state=42)
scaler = StandardScaler()
X_train_smote = scaler.fit_transform(X_train_smote)
X_test = scaler.transform(X_test)
accuracy_scores = {}
# KNN Classifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train_smote, y_train_smote)
y_pred_knn = knn.predict(X_test)
# Logistic Regression
logreg.fit(X_train_smote, y_train_smote)
y_pred_logreg = logreg.predict(X_test)
y_pred_svm = svm.predict(X_test)
best_accuracy = accuracy_scores[best_algorithm]
print(f"{algorithm}: {score:.4f}")
Explanation:
4. Display: The scores of all algorithms and the best algorithm are
displayed.
Run this code locally to compare the performance of the classifiers and
identify the best one for your dataset.
There are many algorithms you can use to classify your dataset. Here are
additional popular classification algorithms along with Python code:
1. Decision Tree
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier(random_state=42)
dt.fit(X_train_smote, y_train_smote)
y_pred_dt = dt.predict(X_test)
2. Random Forest
rf = RandomForestClassifier(random_state=42, n_estimators=100)
rf.fit(X_train_smote, y_train_smote)
y_pred_rf = rf.predict(X_test)
# XGBoost Classifier
xgb.fit(X_train_smote, y_train_smote)
y_pred_xgb = xgb.predict(X_test)
4. Naive Bayes
nb = GaussianNB()
nb.fit(X_train_smote, y_train_smote)
y_pred_nb = nb.predict(X_test)
mlp.fit(X_train_smote, y_train_smote)
y_pred_mlp = mlp.predict(X_test)
6. AdaBoost
# AdaBoost Classifier
ada.fit(X_train_smote, y_train_smote)
y_pred_ada = ada.predict(X_test)
7. LightGBM
lgbm = LGBMClassifier(random_state=42)
lgbm.fit(X_train_smote, y_train_smote)
y_pred_lgbm = lgbm.predict(X_test)
After adding the algorithms above, update the comparison and best score
display:
print(f"{algorithm}: {score:.4f}")
best_accuracy = accuracy_scores[best_algorithm]
Steps to Use:
1. Copy and paste the desired algorithm's code into your script.