How to Build Your First Machine Learning Model from Scratch
๐ง Project: Build a Machine Learning Model to Predict Iris Flower Species
We’ll use the classic Iris dataset to train a model that predicts the type of iris flower based on features like petal length and width.
✅ Step-by-Step Guide
Step 1: Install Required Libraries
If you don’t already have them installed:
pip install scikit-learn pandas matplotlib
Step 2: Import Libraries
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
Step 3: Load the Dataset
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['species'] = iris.target
Step 4: Explore the Data (Optional but Important)
print(df.head()) # Preview first few rows
print(df['species'].value_counts()) # Check class distribution
Step 5: Prepare the Data
Split the dataset into features (X) and labels (y):
X = df[iris.feature_names] # Features
y = df['species'] # Labels
Now split into training and testing data:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Step 6: Choose a Model and Train It
We’ll use a Random Forest Classifier — it’s simple and works well for beginners.
model = RandomForestClassifier()
model.fit(X_train, y_train)
Step 7: Make Predictions
y_pred = model.predict(X_test)
Step 8: Evaluate the Model
accuracy = accuracy_score(y_test, y_pred)
print("Model Accuracy:", accuracy)
You should see something like Model Accuracy: 0.96 — that means 96% accuracy on the test data!
Step 9: Try a Custom Prediction
sample = [[5.1, 3.5, 1.4, 0.2]] # Input feature values
prediction = model.predict(sample)
print("Predicted Species:", iris.target_names[prediction[0]])
๐ Summary
You just built your first machine learning model from scratch using:
A real-world dataset
A training/test split
A classification algorithm
Accuracy evaluation
A prediction example
๐ What's Next?
Try other algorithms like KNeighborsClassifier or SVC.
Visualize the data using matplotlib or seaborn.
Try using your own dataset (e.g., CSV file).
Upload this project to GitHub or Colab.
Learn AI ML Course in Hyderabad
Read More
Top AI Projects to Try as a Beginner
Hands-On Learning and Projects
AI for the Environment: How Machine Learning is Addressing Climate Change
How to Apply Machine Learning in Agriculture
Visit Our Quality Thought Training Institute in Hyderabad
Comments
Post a Comment