← Back to Python

All Topics

Advertisement

Learn/Python/Machine Learning

Classification Models

Topic: Scikit-Learn

Advertisement

Introduction

Classification algorithms for predicting categorical outcomes.

Logistic Regression

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

print(f"Accuracy: {accuracy_score(y_test, y_pred)}")
print(classification_report(y_test, y_pred))

Decision Trees

from sklearn.tree import DecisionTreeClassifier

model = DecisionTreeClassifier(max_depth=5, min_samples_split=10)
model.fit(X_train, y_train)

Random Forest

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(n_estimators=100, max_depth=10)
model.fit(X_train, y_train)
model.feature_importances_  # Feature importance

Support Vector Machine

from sklearn.svm import SVC

model = SVC(kernel="rbf", C=1.0, gamma="scale")
model.fit(X_train, y_train)

Practice Problems

  1. Train multiple classifiers
  2. Compare decision boundaries
  3. Tune hyperparameters
  4. Handle class imbalance
  5. Visualize feature importance

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →