ml-practicals-kit 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ml_practical/__init__.py +3 -0
- ml_practical/__main__.py +8 -0
- ml_practical/cli.py +113 -0
- ml_practical/desktop.py +52 -0
- ml_practical/notebook_builder.py +107 -0
- ml_practical/practicals/__init__.py +32 -0
- ml_practical/practicals/base.py +180 -0
- ml_practical/practicals/p01_naive_bayes.py +70 -0
- ml_practical/practicals/p02_simple_linear_regression.py +82 -0
- ml_practical/practicals/p03_multiple_linear_regression.py +71 -0
- ml_practical/practicals/p04_polynomial_regression.py +72 -0
- ml_practical/practicals/p05_lasso_ridge_regression.py +98 -0
- ml_practical/practicals/p06_logistic_regression.py +69 -0
- ml_practical/practicals/p07_artificial_neural_network.py +86 -0
- ml_practical/practicals/p08_knn_classifier.py +91 -0
- ml_practical/practicals/p09_decision_tree_classifier.py +84 -0
- ml_practical/practicals/p10_svm_classifier.py +80 -0
- ml_practical/practicals/p11_kmeans_clustering.py +106 -0
- ml_practical/practicals/p12_hierarchical_clustering.py +85 -0
- ml_practicals_kit-1.0.0.dist-info/METADATA +85 -0
- ml_practicals_kit-1.0.0.dist-info/RECORD +24 -0
- ml_practicals_kit-1.0.0.dist-info/WHEEL +5 -0
- ml_practicals_kit-1.0.0.dist-info/entry_points.txt +3 -0
- ml_practicals_kit-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Practical 11: K-Means Clustering."""
|
|
2
|
+
|
|
3
|
+
from ml_practical.notebook_builder import NotebookBuilder
|
|
4
|
+
from ml_practical.practicals.base import PracticalSpec, add_header
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def build_notebook(roll_number: str) -> NotebookBuilder:
|
|
8
|
+
nb = NotebookBuilder(title="Practical 11: K-Means Clustering")
|
|
9
|
+
add_header(
|
|
10
|
+
nb,
|
|
11
|
+
number=11,
|
|
12
|
+
title="K-Means Clustering",
|
|
13
|
+
roll_number=roll_number,
|
|
14
|
+
aim="Perform unsupervised K-Means clustering, determine optimal K via Elbow & Silhouette analysis, and visualize clusters.",
|
|
15
|
+
dataset_info="Iris Dataset (UCI ML Repository / sklearn.datasets)",
|
|
16
|
+
metrics_info="Inertia (WCSS), Silhouette Score, Davies-Bouldin Index",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
nb.add_markdown("## 1. Import Libraries & Load Dataset")
|
|
20
|
+
nb.add_code(
|
|
21
|
+
"""
|
|
22
|
+
import numpy as np
|
|
23
|
+
import pandas as pd
|
|
24
|
+
import matplotlib.pyplot as plt
|
|
25
|
+
from sklearn.datasets import load_iris
|
|
26
|
+
from sklearn.preprocessing import StandardScaler
|
|
27
|
+
from sklearn.decomposition import PCA
|
|
28
|
+
from sklearn.cluster import KMeans
|
|
29
|
+
from sklearn.metrics import silhouette_score, davies_bouldin_score
|
|
30
|
+
|
|
31
|
+
data = load_iris(as_frame=True)
|
|
32
|
+
X = data.data
|
|
33
|
+
X_scaled = StandardScaler().fit_transform(X)
|
|
34
|
+
print("Dataset Shape:", X.shape)
|
|
35
|
+
X.head()
|
|
36
|
+
"""
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
nb.add_markdown("## 2. Elbow Method & Silhouette Analysis")
|
|
40
|
+
nb.add_code(
|
|
41
|
+
"""
|
|
42
|
+
k_range = range(2, 9)
|
|
43
|
+
inertias = []
|
|
44
|
+
silhouettes = []
|
|
45
|
+
|
|
46
|
+
for k in k_range:
|
|
47
|
+
km = KMeans(n_clusters=k, n_init=10, random_state=42)
|
|
48
|
+
labels = km.fit_predict(X_scaled)
|
|
49
|
+
inertias.append(km.inertia_)
|
|
50
|
+
silhouettes.append(silhouette_score(X_scaled, labels))
|
|
51
|
+
|
|
52
|
+
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
|
|
53
|
+
axes[0].plot(list(k_range), inertias, marker="o")
|
|
54
|
+
axes[0].set_xlabel("Number of Clusters (K)")
|
|
55
|
+
axes[0].set_ylabel("Inertia (WCSS)")
|
|
56
|
+
axes[0].set_title("Elbow Method")
|
|
57
|
+
axes[0].grid(alpha=0.3)
|
|
58
|
+
|
|
59
|
+
axes[1].plot(list(k_range), silhouettes, marker="s", color="green")
|
|
60
|
+
axes[1].set_xlabel("Number of Clusters (K)")
|
|
61
|
+
axes[1].set_ylabel("Silhouette Score")
|
|
62
|
+
axes[1].set_title("Silhouette Score vs K")
|
|
63
|
+
axes[1].grid(alpha=0.3)
|
|
64
|
+
plt.tight_layout()
|
|
65
|
+
plt.show()
|
|
66
|
+
"""
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
nb.add_markdown("## 3. Fit K-Means (K=3) & Evaluate Clusters")
|
|
70
|
+
nb.add_code(
|
|
71
|
+
"""
|
|
72
|
+
kmeans = KMeans(n_clusters=3, n_init=10, random_state=42)
|
|
73
|
+
cluster_labels = kmeans.fit_predict(X_scaled)
|
|
74
|
+
|
|
75
|
+
sil = silhouette_score(X_scaled, cluster_labels)
|
|
76
|
+
dbi = davies_bouldin_score(X_scaled, cluster_labels)
|
|
77
|
+
print(f"Inertia (WCSS) : {kmeans.inertia_:.4f}")
|
|
78
|
+
print(f"Silhouette Score : {sil:.4f}")
|
|
79
|
+
print(f"Davies-Bouldin Index : {dbi:.4f}")
|
|
80
|
+
|
|
81
|
+
pca = PCA(n_components=2, random_state=42)
|
|
82
|
+
X_pca = pca.fit_transform(X_scaled)
|
|
83
|
+
centers_pca = pca.transform(kmeans.cluster_centers_)
|
|
84
|
+
|
|
85
|
+
plt.figure(figsize=(7, 5))
|
|
86
|
+
scatter = plt.scatter(X_pca[:, 0], X_pca[:, 1], c=cluster_labels, cmap="viridis", s=50, alpha=0.8)
|
|
87
|
+
plt.scatter(centers_pca[:, 0], centers_pca[:, 1], c="red", marker="X", s=200, label="Centroids")
|
|
88
|
+
plt.xlabel("PCA Component 1")
|
|
89
|
+
plt.ylabel("PCA Component 2")
|
|
90
|
+
plt.title("K-Means Clustering (K=3, PCA Projection)")
|
|
91
|
+
plt.legend()
|
|
92
|
+
plt.colorbar(scatter, label="Cluster")
|
|
93
|
+
plt.grid(alpha=0.3)
|
|
94
|
+
plt.show()
|
|
95
|
+
"""
|
|
96
|
+
)
|
|
97
|
+
return nb
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
SPEC = PracticalSpec(
|
|
101
|
+
number=11,
|
|
102
|
+
slug="KMeans_Clustering",
|
|
103
|
+
title="K-Means Clustering",
|
|
104
|
+
category="Clustering",
|
|
105
|
+
builder=build_notebook,
|
|
106
|
+
)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Practical 12: Hierarchical Clustering."""
|
|
2
|
+
|
|
3
|
+
from ml_practical.notebook_builder import NotebookBuilder
|
|
4
|
+
from ml_practical.practicals.base import PracticalSpec, add_header
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def build_notebook(roll_number: str) -> NotebookBuilder:
|
|
8
|
+
nb = NotebookBuilder(title="Practical 12: Hierarchical Clustering")
|
|
9
|
+
add_header(
|
|
10
|
+
nb,
|
|
11
|
+
number=12,
|
|
12
|
+
title="Hierarchical Clustering",
|
|
13
|
+
roll_number=roll_number,
|
|
14
|
+
aim="Perform Agglomerative Hierarchical Clustering, plot a Dendrogram, and evaluate cluster quality.",
|
|
15
|
+
dataset_info="Wine Dataset (UCI ML Repository / sklearn.datasets)",
|
|
16
|
+
metrics_info="Silhouette Score, Davies-Bouldin Index",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
nb.add_markdown("## 1. Import Libraries & Load Dataset")
|
|
20
|
+
nb.add_code(
|
|
21
|
+
"""
|
|
22
|
+
import numpy as np
|
|
23
|
+
import pandas as pd
|
|
24
|
+
import matplotlib.pyplot as plt
|
|
25
|
+
from scipy.cluster.hierarchy import dendrogram, linkage
|
|
26
|
+
from sklearn.datasets import load_wine
|
|
27
|
+
from sklearn.preprocessing import StandardScaler
|
|
28
|
+
from sklearn.decomposition import PCA
|
|
29
|
+
from sklearn.cluster import AgglomerativeClustering
|
|
30
|
+
from sklearn.metrics import silhouette_score, davies_bouldin_score
|
|
31
|
+
|
|
32
|
+
data = load_wine(as_frame=True)
|
|
33
|
+
X = data.data
|
|
34
|
+
X_scaled = StandardScaler().fit_transform(X)
|
|
35
|
+
print("Dataset Shape:", X.shape)
|
|
36
|
+
X.head()
|
|
37
|
+
"""
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
nb.add_markdown("## 2. Dendrogram Visualization (Ward Linkage)")
|
|
41
|
+
nb.add_code(
|
|
42
|
+
"""
|
|
43
|
+
linked = linkage(X_scaled, method="ward")
|
|
44
|
+
plt.figure(figsize=(12, 5))
|
|
45
|
+
dendrogram(linked, truncate_mode="lastp", p=30, leaf_rotation=90, leaf_font_size=8)
|
|
46
|
+
plt.title("Hierarchical Clustering Dendrogram (Ward Linkage)")
|
|
47
|
+
plt.xlabel("Sample Index or Cluster Size")
|
|
48
|
+
plt.ylabel("Euclidean Distance")
|
|
49
|
+
plt.grid(axis="y", alpha=0.3)
|
|
50
|
+
plt.show()
|
|
51
|
+
"""
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
nb.add_markdown("## 3. Fit Agglomerative Clustering & Evaluate")
|
|
55
|
+
nb.add_code(
|
|
56
|
+
"""
|
|
57
|
+
agg = AgglomerativeClustering(n_clusters=3, linkage="ward")
|
|
58
|
+
cluster_labels = agg.fit_predict(X_scaled)
|
|
59
|
+
|
|
60
|
+
sil = silhouette_score(X_scaled, cluster_labels)
|
|
61
|
+
dbi = davies_bouldin_score(X_scaled, cluster_labels)
|
|
62
|
+
print(f"Silhouette Score : {sil:.4f}")
|
|
63
|
+
print(f"Davies-Bouldin Index : {dbi:.4f}")
|
|
64
|
+
|
|
65
|
+
X_pca = PCA(n_components=2, random_state=42).fit_transform(X_scaled)
|
|
66
|
+
plt.figure(figsize=(7, 5))
|
|
67
|
+
scatter = plt.scatter(X_pca[:, 0], X_pca[:, 1], c=cluster_labels, cmap="plasma", s=50, alpha=0.8)
|
|
68
|
+
plt.xlabel("PCA Component 1")
|
|
69
|
+
plt.ylabel("PCA Component 2")
|
|
70
|
+
plt.title("Agglomerative Hierarchical Clustering (3 Clusters, PCA Projection)")
|
|
71
|
+
plt.colorbar(scatter, label="Cluster")
|
|
72
|
+
plt.grid(alpha=0.3)
|
|
73
|
+
plt.show()
|
|
74
|
+
"""
|
|
75
|
+
)
|
|
76
|
+
return nb
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
SPEC = PracticalSpec(
|
|
80
|
+
number=12,
|
|
81
|
+
slug="Hierarchical_Clustering",
|
|
82
|
+
title="Hierarchical Clustering",
|
|
83
|
+
category="Clustering",
|
|
84
|
+
builder=build_notebook,
|
|
85
|
+
)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ml_practicals_kit
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Generate university ML practical Jupyter notebooks (.ipynb) into a Desktop folder named after your university roll number.
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Provides-Extra: notebook-runtime
|
|
9
|
+
Requires-Dist: numpy; extra == "notebook-runtime"
|
|
10
|
+
Requires-Dist: pandas; extra == "notebook-runtime"
|
|
11
|
+
Requires-Dist: matplotlib; extra == "notebook-runtime"
|
|
12
|
+
Requires-Dist: scikit-learn; extra == "notebook-runtime"
|
|
13
|
+
Requires-Dist: jupyter; extra == "notebook-runtime"
|
|
14
|
+
|
|
15
|
+
# ml_practicals_kit
|
|
16
|
+
|
|
17
|
+
A lightweight, cross-platform Python CLI package that generates all 12 Machine Learning Practicals as separate **`.py` Python scripts** (or `.ipynb` Jupyter Notebooks with `--format ipynb`) into a folder on your **Desktop** named after your **University Roll Number**.
|
|
18
|
+
|
|
19
|
+
## Practicals Included
|
|
20
|
+
|
|
21
|
+
1. **Naïve Bayes Classifier** (`01_Naive_Bayes_Classifier.py`)
|
|
22
|
+
2. **Simple Linear Regression** (`02_Simple_Linear_Regression.py`)
|
|
23
|
+
3. **Multiple Linear Regression** (`03_Multiple_Linear_Regression.py`)
|
|
24
|
+
4. **Polynomial Regression** (`04_Polynomial_Regression.py`)
|
|
25
|
+
5. **Lasso and Ridge Regression** (`05_Lasso_and_Ridge_Regression.py`)
|
|
26
|
+
6. **Logistic Regression** (`06_Logistic_Regression.py`)
|
|
27
|
+
7. **Artificial Neural Network** (`07_Artificial_Neural_Network.py`)
|
|
28
|
+
8. **K-NN Classifier** (`08_KNN_Classifier.py`)
|
|
29
|
+
9. **Decision Tree Classification** (`09_Decision_Tree_Classification.py`)
|
|
30
|
+
10. **SVM Classification** (`10_SVM_Classification.py`)
|
|
31
|
+
11. **K-Means Clustering** (`11_KMeans_Clustering.py`)
|
|
32
|
+
12. **Hierarchical Clustering** (`12_Hierarchical_Clustering.py`)
|
|
33
|
+
|
|
34
|
+
## Evaluation & Metrics Covered
|
|
35
|
+
|
|
36
|
+
- **Train-Test Split** & **k-Fold Cross-Validation** (`k=5`)
|
|
37
|
+
- **Regression**: MSE and $R^2$ score, Actual vs Predicted & Residual plots
|
|
38
|
+
- **Classification**: Accuracy, TP, TN, FP, FN, Error Rate, Recall, Specificity, F1-score, AUC, Confusion Matrix & ROC Curve plots
|
|
39
|
+
- **Clustering**: Silhouette Score, Davies-Bouldin Index, Elbow / Dendrogram plots & 2D PCA cluster visualizations
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
Once published to PyPI:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install ml_practicals_kit
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Or install locally from this folder:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install .
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
To also install the libraries needed to execute the generated notebooks:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install ".[notebook-runtime]"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
Run the command anywhere in your terminal:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
ml-practical
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
It will prompt you for your university roll number, or you can pass it directly:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
ml-practical 2201234567
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Or run via module:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
python -m ml_practical 2201234567
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
To generate `.ipynb` Jupyter notebooks instead of `.py` files:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
ml-practical 2201234567 --format ipynb
|
|
85
|
+
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
ml_practical/__init__.py,sha256=hh4GLsdaY3fDW2T_fUJrt65-oxHHy8S1eCk5rbcB2e0,95
|
|
2
|
+
ml_practical/__main__.py,sha256=m4p6ZjBGD5zkb5mmrUOb_3bkaxLuerIzUio1mOJl7Ag,151
|
|
3
|
+
ml_practical/cli.py,sha256=NxaNGky-8NfGHuh2p8jmBCxIewXxBHyAZX9hxFQG_GA,3513
|
|
4
|
+
ml_practical/desktop.py,sha256=1gowycUdrfGOhIqLTsV_4Y63f_qd8xjiAbyL6s_PwQo,1639
|
|
5
|
+
ml_practical/notebook_builder.py,sha256=C5cgJA_0gKIg2b-ZnV-dKrSg1djNRTb7Dx8jy1uOGPk,3535
|
|
6
|
+
ml_practical/practicals/__init__.py,sha256=qzVPgPFs6Re4dQaNpzl0QBT396qNyvN9PTpYct7Z-Fc,1159
|
|
7
|
+
ml_practical/practicals/base.py,sha256=U7-E8gY5sYKHZtTLgudaIG8xCGdwhPN0Ag9bMHUayy0,6530
|
|
8
|
+
ml_practical/practicals/p01_naive_bayes.py,sha256=NJWyOLUySmEKUmq38N_CGGnQM3vPpgQqrBr19FowVGk,2333
|
|
9
|
+
ml_practical/practicals/p02_simple_linear_regression.py,sha256=8g12RC5tlmolZxp1MN0QLFBEqzApFWi2B2JmFAU4x8o,2737
|
|
10
|
+
ml_practical/practicals/p03_multiple_linear_regression.py,sha256=RfM3VK-gO-ZyxxkFQ2-pCKeepG18Cf5niM_uuvUPhIs,2421
|
|
11
|
+
ml_practical/practicals/p04_polynomial_regression.py,sha256=GDAKCxnvykHJrpamJF1vWaw3r3tdIvpXtqzfF4ZGH5A,2577
|
|
12
|
+
ml_practical/practicals/p05_lasso_ridge_regression.py,sha256=67qQSIrIyl32kd6W2VbAxpYB8DJyKYebKwj_7aRfxpk,3265
|
|
13
|
+
ml_practical/practicals/p06_logistic_regression.py,sha256=Y03rLuf4YroPVlRdY5rsK-6uDdT6DwG4uXbzfte2cgM,2303
|
|
14
|
+
ml_practical/practicals/p07_artificial_neural_network.py,sha256=huOe6wP_zpabQ-HGA8Azw2v3PD-LOR0a2y-qCRQ0JPs,2822
|
|
15
|
+
ml_practical/practicals/p08_knn_classifier.py,sha256=_5wDHDKTuVz21CTyRo9xV-9OLpUS2fbI12CCP88Rfc4,3074
|
|
16
|
+
ml_practical/practicals/p09_decision_tree_classifier.py,sha256=7aTX1Pe3nIJCtS9bTXF9DEZSAjRZ7nxtaufUn29NZBI,2655
|
|
17
|
+
ml_practical/practicals/p10_svm_classifier.py,sha256=2FHamnaLJx-euqhDjjhZJlyxAL3v44JOtfTA5J_x5Us,2683
|
|
18
|
+
ml_practical/practicals/p11_kmeans_clustering.py,sha256=MmWenZdnFEz5np7jxbUcmH2ennNozhxQ2susyhlRyNs,3726
|
|
19
|
+
ml_practical/practicals/p12_hierarchical_clustering.py,sha256=gvBToIAND2iRaIzHRdkO2h5x3aTKD_Z3MEe8mYY3QKM,3038
|
|
20
|
+
ml_practicals_kit-1.0.0.dist-info/METADATA,sha256=bEKLM-u5ZZ-Etqatw7vPo3rEh3-shtPYyww88dIDOXs,2798
|
|
21
|
+
ml_practicals_kit-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
22
|
+
ml_practicals_kit-1.0.0.dist-info/entry_points.txt,sha256=OWkHIwPP-nxiHSjQi-hCZUBVsclyt3bOF6Rpidw-mLI,97
|
|
23
|
+
ml_practicals_kit-1.0.0.dist-info/top_level.txt,sha256=1McV0V8-eQa45F020etkyy3OeiPSUKqtnfS0-DVrE1I,13
|
|
24
|
+
ml_practicals_kit-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ml_practical
|