Skip to content

Latest commit

ย 

History

73 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“Š Machine Learning Basics with Python (Jupyter + Scikit-Learn)

Based on notes and examples from the following tutorial: ๐Ÿ‘‰


๐Ÿš€ Machine Learning Workflow

๐Ÿ” Standard ML Pipeline

image

ex:

image

๐Ÿ“ฆ Required Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import tree

โš™๏ธ Environment Setup

Install Anaconda + Jupyter

After installing Anaconda, run:

jupyter notebook

โžก๏ธ This will open Jupyter in your browser.


๐Ÿ“‚ Importing Dataset

๐Ÿ” Source for datasets


๐Ÿ“ฅ Load CSV File

import pandas as pd

df = pd.read_csv('vgsales.csv')
df.shape  # (rows, columns)

๐Ÿ“Š Basic Exploration

df.describe()
df.values

โŒจ๏ธ Jupyter Shortcuts

Shortcut Description
H Show all shortcuts
Shift + Tab Show function documentation
Ctrl + / Comment/uncomment

๐ŸŽฏ Example Project: Music Recommendation

๐Ÿ“Œ Goal

Predict music genre based on:

  • Age
  • Gender

๐Ÿ“ฅ Load Data

import pandas as pd

music_data = pd.read_csv('music.csv')
music_data.head()

๐Ÿงน Data Cleaning

Typical steps:

  • Remove duplicates
  • Handle missing values
  • Fix inconsistencies
  • Drop irrelevant data

๐Ÿง  Prepare Data

Define Features (X) and Target (y)

X = music_data.drop(columns=['genre'])
y = music_data['genre']

๐Ÿค– Create & Train Model

from sklearn.tree import DecisionTreeClassifier

model = DecisionTreeClassifier()
model.fit(X, y)

๐Ÿ”ฎ Make Predictions

predictions = model.predict([[21, 1], [22, 0]])
print(predictions)

Expected Output:

['HipHop', 'Dance']

๐Ÿ“ Model Evaluation

Split Data

from sklearn.model_selection import train_test_split

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

Measure Accuracy

from sklearn.metrics import accuracy_score

model.fit(X_train, y_train)
predictions = model.predict(X_test)

score = accuracy_score(y_test, predictions)
print(score)

๐Ÿ’พ Persisting Models (Save & Load)

Save Model

import joblib

joblib.dump(model, 'music-recommender.joblib')

Load Model

model = joblib.load('music-recommender.joblib')
predictions = model.predict([[21, 1]])

๐ŸŒณ Visualizing Decision Trees

Export Tree

from sklearn import tree

tree.export_graphviz(
    model,
    out_file='music-recommender.dot',
    feature_names=['age', 'gender'],
    class_names=sorted(y.unique()),
    label='all',
    rounded=True,
    filled=True
)

๐Ÿ“Œ Visualization Steps

  1. Install Graphviz
  2. Install VSCode extension: Dot / Graphviz
  3. Open .dot file
  4. Click Preview

๐Ÿ“Š Final Output

The model creates a decision tree showing:

  • Splits based on age & gender
  • Gini impurity
  • Class prediction (HipHop, Jazz, Classical, etc.)

๐Ÿง  Key Concepts Summary

Concept Explanation
Features (X) Input variables
Target (y) Output variable
Train/Test Split Prevent overfitting
Accuracy Model performance metric
Model Persistence Save trained models
Decision Tree Interpretable ML model

๐ŸŽฏ Key Takeaways

  • ML is a step-by-step pipeline
  • Data preparation is critical
  • Simple models like Decision Trees are powerful
  • Always evaluate using test data
  • Save models to reuse in production

๐Ÿ“Ž Resources


๐Ÿ’ก Pro Tip (for interviews)

When explaining ML projects:

๐Ÿ‘‰ Always structure like this:

Problem โ†’ Data โ†’ Model โ†’ Evaluation โ†’ Business Impact

Here is a clean, GitHub-ready explanation you can paste directly under your code:


๐ŸŒณ tree.export_graphviz() โ€“ Explanation of Parameters

This function exports a trained Decision Tree model into a .dot file, which can be visualized using Graphviz.

tree.export_graphviz(
    model,
    out_file='music-recommender.dot',
    feature_names=['age', 'gender'],
    class_names=sorted(y.unique()),
    label='all',
    rounded=True,
    filled=True
)

๐Ÿ“Œ Parameters Breakdown

Parameter Description
model The trained Decision Tree model that you want to visualize.
out_file='music-recommender.dot' Output file where the tree structure is saved. This .dot file can be rendered using Graphviz.
feature_names=['age', 'gender'] Names of input features used in the model. These appear in the tree nodes to make splits understandable.
class_names=sorted(y.unique()) Names of target classes (labels). y.unique() gets all unique values, and sorted() ensures consistent ordering.
label='all' Displays detailed information in each node (e.g., Gini index, samples, value).
rounded=True Rounds the corners of the boxes in the tree for better visual appearance.
filled=True Colors the nodes based on class prediction (helps quickly interpret the model).

๐ŸŽฏ What This Produces

  • A .dot file describing the tree structure

  • Each node shows:

    • Split condition (e.g., age <= 30)
    • Gini impurity
    • Number of samples
    • Class distribution
    • Predicted class

๐Ÿ“Š Example Node Output

age <= 30.5
gini = 0.5
samples = 10
value = [5, 3, 2]
class = HipHop

๐Ÿš€ How to Visualize

  1. Install Graphviz
  2. Open .dot file in VSCode
  3. Use Graphviz preview extension

About

I'm learning machine learning and deep learning again, but I've decided to put all my knowledge here.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages