Classification in Machine Learning: A Beginner’s Tutorial
Classification in Machine Learning: A Beginner’s Tutorial
Classification is a type of supervised learning where the goal is to predict categorical labels. Common examples include spam detection, disease diagnosis, and sentiment analysis.
1. What is Classification?
In classification, the model learns from labeled data and predicts discrete outcomes. For example, predicting whether an email is spam or not.
2. Python Setup
Install the required libraries:
pip install pandas scikit-learn
3. Example: Predicting Pass/Fail
Let’s build a model that predicts whether a student passes based on study hours.
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
# Dataset
data = {'Hours':[1,2,3,4,5], 'Passed':[0,0,1,1,1]}
df = pd.DataFrame(data)
# Features and target
X = df[['Hours']]
y = df['Passed']
# Train model
model = DecisionTreeClassifier()
model.fit(X, y)
# Predict
result = model.predict([[3.5]])
print("Prediction for 3.5 study hours:", "Pass" if result[0] == 1 else "Fail
4. Evaluating the Model
Use accuracy score to evaluate performance:
from sklearn.metrics import accuracy_score
# Predictions
y_pred = model.predict(X)
print("Accuracy:", accuracy_score(y, y_pred))
5. Conclusion
Classification is a powerful tool for predicting categories. With Python and scikit-learn, you can quickly build models to classify data and make informed decisions.


Comments
Post a Comment