Getting Started with Machine Learning Using Python and scikit-learn

← Back to Home

Part 4: Introduction to Machine Learning with scikit-learn



What Is Machine Learning?

Machine Learning (ML) is a subset of AI where systems learn from data rather than being explicitly programmed. The goal is to make predictions or decisions without human intervention.



Types of Machine Learning

Type Description Example
Supervised Learning Learn from labeled data Spam detection, housing price prediction
Unsupervised Learning Discover patterns in unlabeled data Customer segmentation
Reinforcement Learning Learn from actions and rewards Game playing, robotics


Why Use scikit-learn?

scikit-learn is a powerful and beginner-friendly library for:

  • Classification
  • Regression
  • Clustering
  • Preprocessing
  • Model Evaluation


Installing scikit-learn

Install with pip:

pip install scikit-learn


First Machine Learning Project: Iris Classification

Step 1: Load Dataset

from sklearn.datasets import load_iris
import pandas as pd

iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target
print(df.head())


Step 2: Train/Test Split

from sklearn.model_selection import train_test_split

X = df.drop('target', axis=1)
y = df['target']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


Step 3: Train a Classifier

Let’s use a Decision Tree:

from sklearn.tree import DecisionTreeClassifier

model = DecisionTreeClassifier()
model.fit(X_train, y_train)


Step 4: Evaluate the Model

from sklearn.metrics import accuracy_score

y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy * 100:.2f}%")


Bonus: Make a Prediction

sample = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(sample)
print("Predicted class:", iris.target_names[prediction[0]])


Practice Challenge

Try using a Logistic Regression model instead of a Decision Tree:

from sklearn.linear_model import LogisticRegression

log_model = LogisticRegression(max_iter=200)
log_model.fit(X_train, y_train)
print("Accuracy:", log_model.score(X_test, y_test))


🎓 What You’ve Learned:

  • What machine learning is and its main types
  • How to load and prepare data
  • Training and evaluating a simple ML model using scikit-learn


🧭 What’s Next?

In Part 5, we’ll move into Deep Learning and explore how to build Neural Networks using TensorFlow and Keras.