Elvarix 1.0.0__tar.gz
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.
- elvarix-1.0.0/Elvarix.egg-info/PKG-INFO +5 -0
- elvarix-1.0.0/Elvarix.egg-info/SOURCES.txt +6 -0
- elvarix-1.0.0/Elvarix.egg-info/dependency_links.txt +1 -0
- elvarix-1.0.0/Elvarix.egg-info/top_level.txt +1 -0
- elvarix-1.0.0/PKG-INFO +5 -0
- elvarix-1.0.0/elvarix/__init__.py +211 -0
- elvarix-1.0.0/pyproject.toml +12 -0
- elvarix-1.0.0/setup.cfg +4 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
elvarix
|
elvarix-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"""Elvarix: ML Lab practicals. Usage: from elvarix import show; show(1)"""
|
|
2
|
+
|
|
3
|
+
__all__ = ["show", "PRACTICALS"]
|
|
4
|
+
|
|
5
|
+
PRACTICALS = {
|
|
6
|
+
1: ("Data preprocessing (heart dataset)", r'''import pandas as pd
|
|
7
|
+
from sklearn.preprocessing import LabelEncoder
|
|
8
|
+
from sklearn.model_selection import train_test_split
|
|
9
|
+
|
|
10
|
+
df = pd.read_csv("Heart.csv")
|
|
11
|
+
|
|
12
|
+
# a. Data cleaning
|
|
13
|
+
df = df.drop(columns=["Unnamed: 0"]).drop_duplicates()
|
|
14
|
+
|
|
15
|
+
# b. Handling missing data
|
|
16
|
+
print(df.isnull().sum())
|
|
17
|
+
df = df.fillna(df.mode().iloc[0])
|
|
18
|
+
|
|
19
|
+
# c. Data transformation
|
|
20
|
+
for c in ["ChestPain", "Thal", "AHD"]:
|
|
21
|
+
df[c] = LabelEncoder().fit_transform(df[c])
|
|
22
|
+
|
|
23
|
+
# d. Train 75% / test 25%
|
|
24
|
+
x = df.drop(columns="AHD")
|
|
25
|
+
y = df["AHD"]
|
|
26
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
|
|
27
|
+
print(x_train.shape, x_test.shape)
|
|
28
|
+
'''),
|
|
29
|
+
2: ("Confusion matrix (COVID)", r'''tp, fp, fn = 45, 55, 5
|
|
30
|
+
tn = 500 - tp - fp - fn
|
|
31
|
+
print("Confusion matrix [[TP, FN], [FP, TN]]:", [[tp, fn], [fp, tn]])
|
|
32
|
+
|
|
33
|
+
p = tp / (tp + fp)
|
|
34
|
+
r = tp / (tp + fn)
|
|
35
|
+
print("Accuracy:", (tp + tn) / 500)
|
|
36
|
+
print("Precision:", p)
|
|
37
|
+
print("Recall:", r)
|
|
38
|
+
print("F1 score:", 2 * p * r / (p + r))
|
|
39
|
+
'''),
|
|
40
|
+
3: ("Decision tree (GRE admission)", r'''import pandas as pd
|
|
41
|
+
import matplotlib.pyplot as plt
|
|
42
|
+
from sklearn.model_selection import train_test_split
|
|
43
|
+
from sklearn.tree import DecisionTreeClassifier, plot_tree
|
|
44
|
+
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
|
|
45
|
+
|
|
46
|
+
df = pd.read_csv("Admission_Predict.csv")
|
|
47
|
+
|
|
48
|
+
# a. Pre-processing: chance (0 to 1) -> admitted 0/1
|
|
49
|
+
df["Chance of Admit "] = (df["Chance of Admit "] >= 0.5).astype(int)
|
|
50
|
+
|
|
51
|
+
# b. Train-test split
|
|
52
|
+
x = df.drop(columns=["Serial No.", "Chance of Admit "])
|
|
53
|
+
y = df["Chance of Admit "]
|
|
54
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
|
|
55
|
+
|
|
56
|
+
# c. Decision tree + evaluate
|
|
57
|
+
model = DecisionTreeClassifier().fit(x_train, y_train)
|
|
58
|
+
y_pred = model.predict(x_test)
|
|
59
|
+
print("Accuracy:", accuracy_score(y_test, y_pred))
|
|
60
|
+
print(confusion_matrix(y_test, y_pred))
|
|
61
|
+
|
|
62
|
+
# Graphs: confusion matrix + tree (top 2 levels)
|
|
63
|
+
ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
|
|
64
|
+
plt.show()
|
|
65
|
+
plt.figure(figsize=(14, 7))
|
|
66
|
+
plot_tree(model, max_depth=2, feature_names=list(x.columns), filled=True)
|
|
67
|
+
plt.show()
|
|
68
|
+
'''),
|
|
69
|
+
4: ("Logistic regression (personal loan)", r'''import pandas as pd
|
|
70
|
+
import matplotlib.pyplot as plt
|
|
71
|
+
from sklearn.model_selection import train_test_split
|
|
72
|
+
from sklearn.linear_model import LogisticRegression
|
|
73
|
+
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
|
|
74
|
+
|
|
75
|
+
df = pd.read_csv("Bank_Personal_Loan_Modelling.csv")
|
|
76
|
+
df = df.replace("/", ".", regex=True).astype(float)
|
|
77
|
+
|
|
78
|
+
x = df.drop(columns=["ID", "ZIP Code", "Personal Loan"])
|
|
79
|
+
y = df["Personal Loan"]
|
|
80
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
|
|
81
|
+
|
|
82
|
+
model = LogisticRegression(max_iter=5000).fit(x_train, y_train)
|
|
83
|
+
y_pred = model.predict(x_test)
|
|
84
|
+
print("Accuracy:", accuracy_score(y_test, y_pred))
|
|
85
|
+
print(confusion_matrix(y_test, y_pred))
|
|
86
|
+
|
|
87
|
+
# Graph: confusion matrix
|
|
88
|
+
ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
|
|
89
|
+
plt.show()
|
|
90
|
+
'''),
|
|
91
|
+
5: ("Linear regression (India temperatures)", r'''import pandas as pd
|
|
92
|
+
import matplotlib.pyplot as plt
|
|
93
|
+
from sklearn.linear_model import LinearRegression
|
|
94
|
+
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
|
|
95
|
+
|
|
96
|
+
df = pd.read_csv("temperatures.csv")
|
|
97
|
+
x = df[["YEAR"]]
|
|
98
|
+
|
|
99
|
+
# a + b. Predict each month, print MSE, MAE, R2
|
|
100
|
+
for m in df.columns[1:13]:
|
|
101
|
+
y = df[m]
|
|
102
|
+
y_pred = LinearRegression().fit(x, y).predict(x)
|
|
103
|
+
print(m, mean_squared_error(y, y_pred), mean_absolute_error(y, y_pred),
|
|
104
|
+
r2_score(y, y_pred))
|
|
105
|
+
|
|
106
|
+
# c. Visualize (last month in loop = DEC)
|
|
107
|
+
plt.scatter(x, y)
|
|
108
|
+
plt.plot(x, y_pred, color="red")
|
|
109
|
+
plt.xlabel("Year"); plt.ylabel("Temperature (°C)")
|
|
110
|
+
plt.show()
|
|
111
|
+
'''),
|
|
112
|
+
6: ("SMS spam filter (Naive Bayes)", r'''import pandas as pd
|
|
113
|
+
from sklearn.preprocessing import LabelEncoder
|
|
114
|
+
from sklearn.feature_extraction.text import CountVectorizer
|
|
115
|
+
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
|
|
116
|
+
from sklearn.naive_bayes import MultinomialNB
|
|
117
|
+
from sklearn.linear_model import LogisticRegression
|
|
118
|
+
from sklearn.metrics import accuracy_score
|
|
119
|
+
|
|
120
|
+
df = pd.read_csv("SMSSpamCollection", sep="\t", names=["label", "msg"], quoting=3)
|
|
121
|
+
|
|
122
|
+
# a. Pre-processing: ham/spam -> 0/1, text -> word counts
|
|
123
|
+
y = LabelEncoder().fit_transform(df["label"])
|
|
124
|
+
x = CountVectorizer().fit_transform(df["msg"])
|
|
125
|
+
|
|
126
|
+
# b. Train-test split
|
|
127
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
|
|
128
|
+
|
|
129
|
+
# c + d. Two models: accuracy and cross-validation
|
|
130
|
+
models = {"NB": MultinomialNB(), "LR": LogisticRegression(max_iter=1000)}
|
|
131
|
+
for name, model in models.items():
|
|
132
|
+
model.fit(x_train, y_train)
|
|
133
|
+
print(name, "Accuracy:", accuracy_score(y_test, model.predict(x_test)))
|
|
134
|
+
print(name, "CV:", cross_val_score(model, x, y, cv=5).mean())
|
|
135
|
+
|
|
136
|
+
# e. Hyperparameter tuning
|
|
137
|
+
params = {"NB": {"alpha": [0.1, 0.5, 1]}, "LR": {"C": [0.1, 1, 10]}}
|
|
138
|
+
for name, model in models.items():
|
|
139
|
+
g = GridSearchCV(model, params[name], cv=5).fit(x_train, y_train)
|
|
140
|
+
print(name, "Best:", g.best_params_)
|
|
141
|
+
print(name, "Tuned:", accuracy_score(y_test, g.predict(x_test)))
|
|
142
|
+
'''),
|
|
143
|
+
7: ("Clustering (mall customers)", r'''import pandas as pd
|
|
144
|
+
import matplotlib.pyplot as plt
|
|
145
|
+
from sklearn.preprocessing import LabelEncoder
|
|
146
|
+
from sklearn.model_selection import train_test_split
|
|
147
|
+
from sklearn.cluster import KMeans, AgglomerativeClustering
|
|
148
|
+
|
|
149
|
+
df = pd.read_csv("Mall_Customers.csv")
|
|
150
|
+
|
|
151
|
+
# a. Pre-processing
|
|
152
|
+
df["Genre"] = LabelEncoder().fit_transform(df["Genre"])
|
|
153
|
+
|
|
154
|
+
# b. Train-test split
|
|
155
|
+
x = df[["Annual Income (k$)", "Spending Score (1-100)"]]
|
|
156
|
+
x_train, x_test = train_test_split(x, test_size=0.25)
|
|
157
|
+
|
|
158
|
+
# Algorithm 1: K-Means
|
|
159
|
+
km = KMeans(n_clusters=5, n_init=10).fit(x_train)
|
|
160
|
+
print(x_train.groupby(km.labels_).mean())
|
|
161
|
+
print("Test clusters:", km.predict(x_test))
|
|
162
|
+
plt.scatter(x_train.iloc[:, 0], x_train.iloc[:, 1], c=km.labels_)
|
|
163
|
+
plt.xlabel("Annual Income"); plt.ylabel("Spending Score"); plt.title("K-Means")
|
|
164
|
+
plt.show()
|
|
165
|
+
|
|
166
|
+
# Algorithm 2: Agglomerative
|
|
167
|
+
ag = AgglomerativeClustering(n_clusters=5).fit(x_train)
|
|
168
|
+
print(x_train.groupby(ag.labels_).mean())
|
|
169
|
+
plt.scatter(x_train.iloc[:, 0], x_train.iloc[:, 1], c=ag.labels_)
|
|
170
|
+
plt.xlabel("Annual Income"); plt.ylabel("Spending Score"); plt.title("Agglomerative")
|
|
171
|
+
plt.show()
|
|
172
|
+
'''),
|
|
173
|
+
8: ("Association rules (Apriori)", r'''# Run once first: !pip install apyori
|
|
174
|
+
import pandas as pd
|
|
175
|
+
from apyori import apriori
|
|
176
|
+
|
|
177
|
+
# a. Data preprocessing
|
|
178
|
+
df = pd.read_csv("Market_Basket_Optimisation.csv", header=None)
|
|
179
|
+
|
|
180
|
+
# b. List of transactions (skip empty cells)
|
|
181
|
+
transactions = [[str(i) for i in row if str(i) != "nan"] for row in df.values]
|
|
182
|
+
|
|
183
|
+
# c. Train Apriori
|
|
184
|
+
rules = list(apriori(transactions, min_support=0.003,
|
|
185
|
+
min_confidence=0.2, min_lift=3))
|
|
186
|
+
|
|
187
|
+
# d. Visualize rules
|
|
188
|
+
for r in rules:
|
|
189
|
+
s = r.ordered_statistics[0]
|
|
190
|
+
print(list(s.items_base), "->", list(s.items_add))
|
|
191
|
+
print(" confidence:", round(s.confidence, 2), "lift:", round(s.lift, 2))
|
|
192
|
+
|
|
193
|
+
# e. Rules change with hyperparameters
|
|
194
|
+
for c in [0.2, 0.3, 0.4]:
|
|
195
|
+
r = list(apriori(transactions, min_support=0.003,
|
|
196
|
+
min_confidence=c, min_lift=3))
|
|
197
|
+
print("min_confidence", c, "-> rules:", len(r))
|
|
198
|
+
'''),
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def show(n=None):
|
|
203
|
+
"""Print code of practical n (1-8). No argument: list all practicals."""
|
|
204
|
+
if n is None:
|
|
205
|
+
for k, (title, _) in PRACTICALS.items():
|
|
206
|
+
print(f"{k}. {title}")
|
|
207
|
+
return
|
|
208
|
+
if n not in PRACTICALS:
|
|
209
|
+
raise ValueError("Practical number must be 1-8")
|
|
210
|
+
title, code = PRACTICALS[n]
|
|
211
|
+
print(f"# P{n}. {title}\n\n{code}")
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "Elvarix"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "ML Lab practicals 1-8: show(n) prints the code"
|
|
9
|
+
requires-python = ">=3.7"
|
|
10
|
+
|
|
11
|
+
[tool.setuptools]
|
|
12
|
+
packages = ["elvarix"]
|
elvarix-1.0.0/setup.cfg
ADDED