Data Science — Classification
Logistic regression
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=1000, n_features=10, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LogisticRegression()
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
print(f"Accuracy: {accuracy:.4f}")
Decision trees
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=5)
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
Random forest
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
SVM
from sklearn.svm import SVC
model = SVC(kernel='rbf')
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
Evaluation metrics
from sklearn.metrics import classification_report, confusion_matrix
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
Mini Practice
- Train different classifiers
- Compare performance
- Tune hyperparameters
- Analyze confusion matrix
Up Next
Continue with Clustering - Grouping data.
Related Topics
Frequently Asked Questions about Classification
What is Classification in Data Science?
Classification is a fundamental concept in Data Science. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Classification?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Classification.
Why is Classification important in Data Science?
Classification is essential for Data Science development. Understanding this concept will help you write better code and solve real-world problems more effectively.