predlr 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.
predlr-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,57 @@
1
+ Metadata-Version: 2.4
2
+ Name: predlr
3
+ Version: 1.0.0
4
+ Summary: logistic regression library
5
+ Author: Charles
6
+ License-Expression: MIT
7
+ Keywords: machine learning
8
+ Requires-Python: >=3.9
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: pandas
11
+ Requires-Dist: numpy
12
+ Requires-Dist: scikit-learn
13
+ Requires-Dist: matplotlib
14
+ Requires-Dist: seaborn
15
+ Requires-Dist: xgboost
16
+ Requires-Dist: openpyxl
17
+ Requires-Dist: xlrd
18
+
19
+ # predlr
20
+
21
+ ```python
22
+ !pip install predlr
23
+ from predlr import lrn, nxt
24
+ lrn("svm")
25
+ ```
26
+
27
+ `lrn("name")` puts stage 1 of a program in the next cell. Press **Shift + Enter** on each cell and the next stage appears, until the program ends.
28
+
29
+ - Fill in `pd.read_csv(r"")` with the full path to your file (use `pd.read_excel` for Excel).
30
+ - Fill in `target = ""` with the column to predict.
31
+ - If a cell gives an error, fix it and run it again; the next stage then appears.
32
+ - `nxt()` adds the next stage by hand. `lrn("svm", 5)` starts from stage 5.
33
+ - `list_programs()` shows all programs.
34
+
35
+ For `lrshap`, run `!pip install shap` once.
36
+
37
+ | Name | Method |
38
+ |---|---|
39
+ | `gnb` | Gaussian Naive Bayes |
40
+ | `mnb` | Multinomial Naive Bayes (text) |
41
+ | `senti` | Sentiment analysis: Logistic Regression vs Naive Bayes |
42
+ | `pipe` | Pipeline + GridSearchCV (Scaler, PCA, KNN) |
43
+ | `rf` | Random Forest |
44
+ | `xgb` | XGBoost |
45
+ | `rfxgb` | Random Forest vs XGBoost with cross-validation |
46
+ | `logreg` | Logistic Regression with log loss |
47
+ | `dtsvm` | Decision Tree vs SVM with ROC-AUC |
48
+ | `dte` | Decision Tree (entropy) |
49
+ | `dt` | Decision Tree (gini) |
50
+ | `svm` | SVM with decision boundary |
51
+ | `multilr` | Multi-class Logistic Regression (OvA vs OvO) |
52
+ | `prep` | Data preprocessing (two files) |
53
+ | `slr` | Simple Linear Regression |
54
+ | `mlr` | Multiple Linear Regression |
55
+ | `knn` | K-Nearest Neighbours with best-k curve |
56
+ | `kmeans` | K-Means clustering |
57
+ | `lrshap` | Logistic Regression with GridSearchCV and SHAP |
predlr-1.0.0/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # predlr
2
+
3
+ ```python
4
+ !pip install predlr
5
+ from predlr import lrn, nxt
6
+ lrn("svm")
7
+ ```
8
+
9
+ `lrn("name")` puts stage 1 of a program in the next cell. Press **Shift + Enter** on each cell and the next stage appears, until the program ends.
10
+
11
+ - Fill in `pd.read_csv(r"")` with the full path to your file (use `pd.read_excel` for Excel).
12
+ - Fill in `target = ""` with the column to predict.
13
+ - If a cell gives an error, fix it and run it again; the next stage then appears.
14
+ - `nxt()` adds the next stage by hand. `lrn("svm", 5)` starts from stage 5.
15
+ - `list_programs()` shows all programs.
16
+
17
+ For `lrshap`, run `!pip install shap` once.
18
+
19
+ | Name | Method |
20
+ |---|---|
21
+ | `gnb` | Gaussian Naive Bayes |
22
+ | `mnb` | Multinomial Naive Bayes (text) |
23
+ | `senti` | Sentiment analysis: Logistic Regression vs Naive Bayes |
24
+ | `pipe` | Pipeline + GridSearchCV (Scaler, PCA, KNN) |
25
+ | `rf` | Random Forest |
26
+ | `xgb` | XGBoost |
27
+ | `rfxgb` | Random Forest vs XGBoost with cross-validation |
28
+ | `logreg` | Logistic Regression with log loss |
29
+ | `dtsvm` | Decision Tree vs SVM with ROC-AUC |
30
+ | `dte` | Decision Tree (entropy) |
31
+ | `dt` | Decision Tree (gini) |
32
+ | `svm` | SVM with decision boundary |
33
+ | `multilr` | Multi-class Logistic Regression (OvA vs OvO) |
34
+ | `prep` | Data preprocessing (two files) |
35
+ | `slr` | Simple Linear Regression |
36
+ | `mlr` | Multiple Linear Regression |
37
+ | `knn` | K-Nearest Neighbours with best-k curve |
38
+ | `kmeans` | K-Means clustering |
39
+ | `lrshap` | Logistic Regression with GridSearchCV and SHAP |
@@ -0,0 +1,57 @@
1
+ Metadata-Version: 2.4
2
+ Name: predlr
3
+ Version: 1.0.0
4
+ Summary: logistic regression library
5
+ Author: Charles
6
+ License-Expression: MIT
7
+ Keywords: machine learning
8
+ Requires-Python: >=3.9
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: pandas
11
+ Requires-Dist: numpy
12
+ Requires-Dist: scikit-learn
13
+ Requires-Dist: matplotlib
14
+ Requires-Dist: seaborn
15
+ Requires-Dist: xgboost
16
+ Requires-Dist: openpyxl
17
+ Requires-Dist: xlrd
18
+
19
+ # predlr
20
+
21
+ ```python
22
+ !pip install predlr
23
+ from predlr import lrn, nxt
24
+ lrn("svm")
25
+ ```
26
+
27
+ `lrn("name")` puts stage 1 of a program in the next cell. Press **Shift + Enter** on each cell and the next stage appears, until the program ends.
28
+
29
+ - Fill in `pd.read_csv(r"")` with the full path to your file (use `pd.read_excel` for Excel).
30
+ - Fill in `target = ""` with the column to predict.
31
+ - If a cell gives an error, fix it and run it again; the next stage then appears.
32
+ - `nxt()` adds the next stage by hand. `lrn("svm", 5)` starts from stage 5.
33
+ - `list_programs()` shows all programs.
34
+
35
+ For `lrshap`, run `!pip install shap` once.
36
+
37
+ | Name | Method |
38
+ |---|---|
39
+ | `gnb` | Gaussian Naive Bayes |
40
+ | `mnb` | Multinomial Naive Bayes (text) |
41
+ | `senti` | Sentiment analysis: Logistic Regression vs Naive Bayes |
42
+ | `pipe` | Pipeline + GridSearchCV (Scaler, PCA, KNN) |
43
+ | `rf` | Random Forest |
44
+ | `xgb` | XGBoost |
45
+ | `rfxgb` | Random Forest vs XGBoost with cross-validation |
46
+ | `logreg` | Logistic Regression with log loss |
47
+ | `dtsvm` | Decision Tree vs SVM with ROC-AUC |
48
+ | `dte` | Decision Tree (entropy) |
49
+ | `dt` | Decision Tree (gini) |
50
+ | `svm` | SVM with decision boundary |
51
+ | `multilr` | Multi-class Logistic Regression (OvA vs OvO) |
52
+ | `prep` | Data preprocessing (two files) |
53
+ | `slr` | Simple Linear Regression |
54
+ | `mlr` | Multiple Linear Regression |
55
+ | `knn` | K-Nearest Neighbours with best-k curve |
56
+ | `kmeans` | K-Means clustering |
57
+ | `lrshap` | Logistic Regression with GridSearchCV and SHAP |
@@ -0,0 +1,8 @@
1
+ README.md
2
+ predlr.py
3
+ pyproject.toml
4
+ predlr.egg-info/PKG-INFO
5
+ predlr.egg-info/SOURCES.txt
6
+ predlr.egg-info/dependency_links.txt
7
+ predlr.egg-info/requires.txt
8
+ predlr.egg-info/top_level.txt
@@ -0,0 +1,8 @@
1
+ pandas
2
+ numpy
3
+ scikit-learn
4
+ matplotlib
5
+ seaborn
6
+ xgboost
7
+ openpyxl
8
+ xlrd
@@ -0,0 +1 @@
1
+ predlr
predlr-1.0.0/predlr.py ADDED
@@ -0,0 +1,1134 @@
1
+ import difflib
2
+ import sys
3
+
4
+ ORDER = [
5
+ 'gnb',
6
+ 'mnb',
7
+ 'senti',
8
+ 'pipe',
9
+ 'rf',
10
+ 'xgb',
11
+ 'rfxgb',
12
+ 'logreg',
13
+ 'dtsvm',
14
+ 'dte',
15
+ 'dt',
16
+ 'svm',
17
+ 'multilr',
18
+ 'prep',
19
+ 'slr',
20
+ 'mlr',
21
+ 'knn',
22
+ 'kmeans',
23
+ 'lrshap',
24
+ ]
25
+
26
+ TITLES = {
27
+ 'gnb': 'Gaussian Naive Bayes',
28
+ 'mnb': 'Multinomial Naive Bayes (text classification)',
29
+ 'senti': 'Sentiment analysis: Logistic Regression vs Naive Bayes',
30
+ 'pipe': 'Pipeline + GridSearchCV (Scaler, PCA, KNN)',
31
+ 'rf': 'Random Forest',
32
+ 'xgb': 'XGBoost',
33
+ 'rfxgb': 'Random Forest vs XGBoost with cross-validation',
34
+ 'logreg': 'Logistic Regression with log loss',
35
+ 'dtsvm': 'Decision Tree vs SVM with ROC-AUC',
36
+ 'dte': 'Decision Tree (entropy)',
37
+ 'dt': 'Decision Tree (gini)',
38
+ 'svm': 'SVM with decision boundary',
39
+ 'multilr': 'Multi-class Logistic Regression (OvA vs OvO)',
40
+ 'prep': 'Data preprocessing (two files: clean, merge, scale, encode)',
41
+ 'slr': 'Simple Linear Regression',
42
+ 'mlr': 'Multiple Linear Regression',
43
+ 'knn': 'K-Nearest Neighbours with best-k curve',
44
+ 'kmeans': 'K-Means clustering',
45
+ 'lrshap': 'Logistic Regression with GridSearchCV and SHAP',
46
+ }
47
+
48
+ STAGES = {}
49
+
50
+ STAGES['gnb'] = [
51
+ '''import pandas as pd
52
+ from sklearn.model_selection import train_test_split
53
+ from sklearn.preprocessing import LabelEncoder
54
+ from sklearn.naive_bayes import GaussianNB
55
+ from sklearn.metrics import accuracy_score, classification_report, confusion_matrix''',
56
+ '''df = pd.read_csv(r"")
57
+ print(df.isnull().sum())
58
+ df.head()''',
59
+ '''df = df.drop_duplicates()
60
+ for col in df.columns:
61
+ if pd.api.types.is_numeric_dtype(df[col]):
62
+ df[col] = df[col].fillna(df[col].median())
63
+ else:
64
+ df[col] = df[col].fillna(df[col].mode()[0])
65
+ df.isnull().sum()''',
66
+ '''target = ""
67
+ x = df.drop(target, axis=1)
68
+ y = df[target]''',
69
+ '''le = LabelEncoder()
70
+ for col in x.select_dtypes(exclude="number").columns:
71
+ x[col] = le.fit_transform(x[col].astype(str))
72
+ x.head()''',
73
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)''',
74
+ '''model = GaussianNB()
75
+ model.fit(x_train, y_train)''',
76
+ '''y_pred = model.predict(x_test)''',
77
+ '''print(f"Accuracy: {accuracy_score(y_test, y_pred)*100:.2f}%")
78
+ print("\\nClassification report:\\n", classification_report(y_test, y_pred))''',
79
+ '''cm = confusion_matrix(y_test, y_pred)
80
+ cm''',
81
+ ]
82
+
83
+ STAGES['mnb'] = [
84
+ '''import pandas as pd
85
+ from sklearn.model_selection import train_test_split
86
+ from sklearn.feature_extraction.text import TfidfVectorizer
87
+ from sklearn.naive_bayes import MultinomialNB
88
+ from sklearn.metrics import accuracy_score, classification_report''',
89
+ '''df = pd.read_csv(r"")
90
+ print(df.isnull().sum())
91
+ df.head()''',
92
+ '''text = ""
93
+ target = ""
94
+ df = df.dropna(subset=[text, target]).drop_duplicates()
95
+ x = df[text].astype(str)
96
+ y = df[target]''',
97
+ '''xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42)''',
98
+ '''vectorizer = TfidfVectorizer(stop_words="english", max_df=0.5)
99
+ x_train_tfidf = vectorizer.fit_transform(xtrain)
100
+ x_test_tfidf = vectorizer.transform(xtest)''',
101
+ '''nb_classifier = MultinomialNB()
102
+ nb_classifier.fit(x_train_tfidf, ytrain)''',
103
+ '''y_pred = nb_classifier.predict(x_test_tfidf)''',
104
+ '''accuracy = accuracy_score(ytest, y_pred)
105
+ class_report = classification_report(ytest, y_pred)
106
+ print(f"Accuracy: {accuracy*100:.2f}%\\n")
107
+ print(f"Classification Report:\\n{class_report}")''',
108
+ ]
109
+
110
+ STAGES['senti'] = [
111
+ '''import pandas as pd
112
+ from sklearn.model_selection import train_test_split
113
+ from sklearn.feature_extraction.text import TfidfVectorizer
114
+ from sklearn.linear_model import LogisticRegression
115
+ from sklearn.naive_bayes import MultinomialNB
116
+ from sklearn.metrics import accuracy_score''',
117
+ '''path = r""
118
+ text = ""
119
+ target = ""
120
+ data = {
121
+ "text": [],
122
+ "sentiment": []
123
+ }
124
+ if path:
125
+ df = pd.read_csv(path)
126
+ df = df.dropna(subset=[text, target])
127
+ x = df[text].astype(str)
128
+ y = df[target]
129
+ else:
130
+ df = pd.DataFrame(data)
131
+ x = df["text"]
132
+ y = df["sentiment"]
133
+ df''',
134
+ '''xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42)''',
135
+ '''vectorize = TfidfVectorizer(stop_words="english", lowercase=True)
136
+ x_train_vec = vectorize.fit_transform(xtrain)
137
+ x_test_vec = vectorize.transform(xtest)''',
138
+ '''lr_model = LogisticRegression()
139
+ lr_model.fit(x_train_vec, ytrain)
140
+ nb_model = MultinomialNB()
141
+ nb_model.fit(x_train_vec, ytrain)''',
142
+ '''lr_preds = lr_model.predict(x_test_vec)
143
+ nb_preds = nb_model.predict(x_test_vec)''',
144
+ '''print("---Model Accuracy Comparison---")
145
+ print(f"Logistic regression accuracy: {accuracy_score(ytest, lr_preds)*100:.2f}%")
146
+ print(f"Multinomial Naive bayes accuracy: {accuracy_score(ytest, nb_preds)*100:.2f}%")''',
147
+ '''def predict_sentiment(new_texts):
148
+ new_texts_vec = vectorize.transform(new_texts)
149
+ lr_predictions = lr_model.predict(new_texts_vec)
150
+ nb_predictions = nb_model.predict(new_texts_vec)
151
+ print("--- Testing Custom Input ---")
152
+ for t, lr_pred, nb_pred in zip(new_texts, lr_predictions, nb_predictions):
153
+ print(f"Text: '{t}'")
154
+ print(f" -> LR Prediction: {lr_pred}")
155
+ print(f" -> NB Prediction: {nb_pred}\\n")''',
156
+ '''custom_samples = []
157
+ predict_sentiment(custom_samples)''',
158
+ ]
159
+
160
+ STAGES['pipe'] = [
161
+ '''import pandas as pd
162
+ from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedKFold
163
+ from sklearn.preprocessing import StandardScaler, LabelEncoder
164
+ from sklearn.decomposition import PCA
165
+ from sklearn.neighbors import KNeighborsClassifier
166
+ from sklearn.pipeline import Pipeline
167
+ from sklearn.metrics import confusion_matrix
168
+ import seaborn as sns
169
+ import matplotlib.pyplot as plt''',
170
+ '''df = pd.read_csv(r"")
171
+ print(df.isnull().sum())
172
+ df.head()''',
173
+ '''df = df.drop_duplicates()
174
+ for col in df.columns:
175
+ if pd.api.types.is_numeric_dtype(df[col]):
176
+ df[col] = df[col].fillna(df[col].median())
177
+ else:
178
+ df[col] = df[col].fillna(df[col].mode()[0])
179
+ df.isnull().sum()''',
180
+ '''target = ""
181
+ x = df.drop(target, axis=1)
182
+ y = df[target]''',
183
+ '''le = LabelEncoder()
184
+ for col in x.select_dtypes(exclude="number").columns:
185
+ x[col] = le.fit_transform(x[col].astype(str))
186
+ x.head()''',
187
+ '''pipeline = Pipeline([
188
+ ("scaler", StandardScaler()),
189
+ ("pca", PCA(n_components=2)),
190
+ ("knn", KNeighborsClassifier(n_neighbors=5))
191
+ ])''',
192
+ '''xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42, stratify=y)''',
193
+ '''pipeline.fit(xtrain, ytrain)
194
+ test_score = pipeline.score(xtest, ytest)
195
+ print(f"pipeline accuracy\\n-----------------\\ntest score: {test_score:.3f}")''',
196
+ '''ypred = pipeline.predict(xtest)''',
197
+ '''labels = pipeline.classes_
198
+ conf_matrix = confusion_matrix(ytest, ypred, labels=labels)
199
+ plt.figure()
200
+ sns.heatmap(conf_matrix, annot=True, cmap="Blues", fmt="d", xticklabels=labels, yticklabels=labels)
201
+ plt.title("classification pipeline confusion matrix")
202
+ plt.xlabel("Predicted")
203
+ plt.ylabel("Actual")
204
+ plt.show()''',
205
+ '''pipeline = Pipeline([
206
+ ("scaler", StandardScaler()),
207
+ ("pca", PCA()),
208
+ ("knn", KNeighborsClassifier())
209
+ ])
210
+ param_grid = {
211
+ "pca__n_components": [2, 3],
212
+ "knn__n_neighbors": [3, 5, 7]
213
+ }
214
+ cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
215
+ best_model = GridSearchCV(estimator=pipeline, param_grid=param_grid, cv=cv, scoring="accuracy", verbose=2)''',
216
+ '''best_model.fit(xtrain, ytrain)''',
217
+ '''test_score = best_model.score(xtest, ytest)
218
+ print(f"test score: {test_score:.3f}")
219
+ best_model.best_params_''',
220
+ '''y_pred = best_model.predict(xtest)''',
221
+ ]
222
+
223
+ STAGES['rf'] = [
224
+ '''import pandas as pd
225
+ from sklearn.model_selection import train_test_split
226
+ from sklearn.preprocessing import LabelEncoder
227
+ from sklearn.ensemble import RandomForestClassifier
228
+ from sklearn.metrics import accuracy_score, classification_report''',
229
+ '''df = pd.read_csv(r"")
230
+ print(df.isnull().sum())
231
+ df.head()''',
232
+ '''df = df.drop_duplicates()
233
+ for col in df.columns:
234
+ if pd.api.types.is_numeric_dtype(df[col]):
235
+ df[col] = df[col].fillna(df[col].median())
236
+ else:
237
+ df[col] = df[col].fillna(df[col].mode()[0])
238
+ df.isnull().sum()''',
239
+ '''target = ""
240
+ x = df.drop(target, axis=1)
241
+ y = df[target]''',
242
+ '''le = LabelEncoder()
243
+ for col in x.select_dtypes(exclude="number").columns:
244
+ x[col] = le.fit_transform(x[col].astype(str))
245
+ x.head()''',
246
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.35, random_state=42)''',
247
+ '''rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
248
+ rf_classifier.fit(x_train, y_train)''',
249
+ '''y_pred = rf_classifier.predict(x_test)''',
250
+ '''accuracy = accuracy_score(y_test, y_pred)
251
+ classification_rep = classification_report(y_test, y_pred)
252
+ print(f"accuracy: {accuracy*100:.2f}%")
253
+ print("classification report:")
254
+ print(classification_rep)''',
255
+ ]
256
+
257
+ STAGES['xgb'] = [
258
+ '''import pandas as pd
259
+ import matplotlib.pyplot as plt
260
+ import seaborn as sns
261
+ from sklearn.model_selection import train_test_split
262
+ from sklearn.preprocessing import LabelEncoder
263
+ from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
264
+ from xgboost import XGBClassifier, plot_importance''',
265
+ '''df = pd.read_csv(r"")
266
+ print(df.isnull().sum())
267
+ df.head()''',
268
+ '''df = df.drop_duplicates()
269
+ for col in df.columns:
270
+ if pd.api.types.is_numeric_dtype(df[col]):
271
+ df[col] = df[col].fillna(df[col].median())
272
+ else:
273
+ df[col] = df[col].fillna(df[col].mode()[0])
274
+ df.isnull().sum()''',
275
+ '''target = ""
276
+ x = df.drop(target, axis=1)
277
+ y = df[target]''',
278
+ '''le = LabelEncoder()
279
+ for col in x.select_dtypes(exclude="number").columns:
280
+ x[col] = le.fit_transform(x[col].astype(str))
281
+ y = le.fit_transform(y.astype(str))
282
+ classes = le.classes_
283
+ x.head()''',
284
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)''',
285
+ '''model = XGBClassifier(random_state=42)
286
+ model.fit(x_train, y_train)''',
287
+ '''y_pred = model.predict(x_test)''',
288
+ '''labels = list(range(len(classes)))
289
+ print(f"XGBoost accuracy: {accuracy_score(y_test, y_pred)*100:.2f}%\\n")
290
+ print("Classification report:\\n", classification_report(y_test, y_pred, labels=labels, target_names=[str(c) for c in classes], zero_division=0))''',
291
+ '''cm = confusion_matrix(y_test, y_pred, labels=labels)
292
+ plt.figure(figsize=(6, 4.5))
293
+ sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=classes, yticklabels=classes, cbar=False, annot_kws={"size": 14, "weight": "bold"})
294
+ plt.title("xgboost classification confusion matrix")
295
+ plt.xlabel("predicted")
296
+ plt.ylabel("actual")
297
+ plt.tight_layout()
298
+ plt.show()''',
299
+ '''plt.figure(figsize=(8, 4))
300
+ plot_importance(model, importance_type="weight", grid=False, color="skyblue")
301
+ plt.title("XGBoost feature importance", fontsize=14, pad=15)
302
+ plt.tight_layout()
303
+ plt.show()''',
304
+ ]
305
+
306
+ STAGES['rfxgb'] = [
307
+ '''import numpy as np
308
+ import pandas as pd
309
+ import matplotlib.pyplot as plt
310
+ from sklearn.model_selection import train_test_split, cross_val_score, KFold
311
+ from sklearn.preprocessing import LabelEncoder
312
+ from sklearn.ensemble import RandomForestClassifier
313
+ from sklearn.metrics import classification_report, confusion_matrix
314
+ from xgboost import XGBClassifier''',
315
+ '''df = pd.read_csv(r"")
316
+ print(df.isnull().sum())
317
+ df.head()''',
318
+ '''df = df.drop_duplicates()
319
+ for col in df.columns:
320
+ if pd.api.types.is_numeric_dtype(df[col]):
321
+ df[col] = df[col].fillna(df[col].median())
322
+ else:
323
+ df[col] = df[col].fillna(df[col].mode()[0])
324
+ df.isnull().sum()''',
325
+ '''target = ""
326
+ x = df.drop(target, axis=1)
327
+ y = df[target]''',
328
+ '''le = LabelEncoder()
329
+ for col in x.select_dtypes(exclude="number").columns:
330
+ x[col] = le.fit_transform(x[col].astype(str))
331
+ y = le.fit_transform(y.astype(str))
332
+ classes = le.classes_
333
+ x.head()''',
334
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42, stratify=y)''',
335
+ '''models = {
336
+ "Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
337
+ "XGBoost": XGBClassifier(n_estimators=100, learning_rate=0.1, random_state=42)
338
+ }
339
+ kf = KFold(n_splits=5, shuffle=True, random_state=42)''',
340
+ '''for name, model in models.items():
341
+ print("\\n" + "=" * 50)
342
+ print(f"MODEL: {name}")
343
+ print("=" * 50)
344
+
345
+ model.fit(x_train, y_train)
346
+ y_pred = model.predict(x_test)
347
+
348
+ print("\\n[Classification Report]")
349
+ print(classification_report(y_test, y_pred, labels=list(range(len(classes))), target_names=[str(c) for c in classes], zero_division=0))
350
+
351
+ print("[Confusion Matrix]")
352
+ cm = confusion_matrix(y_test, y_pred)
353
+ print(cm)
354
+
355
+ cv_scores = cross_val_score(model, x_train, y_train, cv=kf, scoring="accuracy")
356
+ print("\\n[5-Fold Cross-Validation Accuracy]")
357
+ print(f"Scores per fold: {cv_scores}")
358
+ print(f"Mean Accuracy: {cv_scores.mean():.4f} (+/- {cv_scores.std()*2:.4f})")
359
+
360
+ print("\\n[Top 5 important features]")
361
+ importances = model.feature_importances_
362
+ indices = np.argsort(importances)[::-1]
363
+ for i in range(min(5, len(x.columns))):
364
+ print(f"{i+1}. {x.columns[indices[i]]}: {importances[indices[i]]:.4f}")''',
365
+ ]
366
+
367
+ STAGES['logreg'] = [
368
+ '''import pandas as pd
369
+ import numpy as np
370
+ from sklearn.model_selection import train_test_split
371
+ from sklearn.linear_model import LogisticRegression
372
+ from sklearn.preprocessing import StandardScaler, LabelEncoder
373
+ from sklearn.metrics import log_loss
374
+ import matplotlib.pyplot as plt
375
+ import warnings
376
+ warnings.filterwarnings("ignore")''',
377
+ '''df = pd.read_csv(r"")
378
+ print(df.isnull().sum())
379
+ df.head()''',
380
+ '''df = df.drop_duplicates()
381
+ for col in df.columns:
382
+ if pd.api.types.is_numeric_dtype(df[col]):
383
+ df[col] = df[col].fillna(df[col].median())
384
+ else:
385
+ df[col] = df[col].fillna(df[col].mode()[0])
386
+ df.isnull().sum()''',
387
+ '''target = ""
388
+ x = df.drop(target, axis=1)
389
+ y = df[target]''',
390
+ '''le = LabelEncoder()
391
+ for col in x.select_dtypes(exclude="number").columns:
392
+ x[col] = le.fit_transform(x[col].astype(str))
393
+ x.head()''',
394
+ '''x_norm = StandardScaler().fit(x).transform(x)
395
+ x_norm[0:5]''',
396
+ '''x_train, x_test, y_train, y_test = train_test_split(x_norm, y, test_size=0.2, random_state=4)''',
397
+ '''LR = LogisticRegression().fit(x_train, y_train)''',
398
+ '''yhat = LR.predict(x_test)
399
+ yhat_prob = LR.predict_proba(x_test)
400
+ print(yhat[:10])
401
+ print(yhat_prob[:10])''',
402
+ '''coefficients = pd.Series(LR.coef_[0], index=x.columns)
403
+ coefficients.sort_values().plot(kind="barh")
404
+ plt.title("feature coefficients in logistic regression model")
405
+ plt.xlabel("coefficient value")
406
+ plt.show()''',
407
+ '''log_loss(y_test, yhat_prob, labels=LR.classes_)''',
408
+ ]
409
+
410
+ STAGES['dtsvm'] = [
411
+ '''import pandas as pd
412
+ import numpy as np
413
+ import matplotlib.pyplot as plt
414
+ from sklearn.model_selection import train_test_split
415
+ from sklearn.preprocessing import normalize, StandardScaler, LabelEncoder
416
+ from sklearn.utils.class_weight import compute_sample_weight
417
+ from sklearn.tree import DecisionTreeClassifier
418
+ from sklearn.metrics import roc_auc_score
419
+ from sklearn.svm import LinearSVC
420
+ import warnings
421
+ warnings.filterwarnings("ignore")''',
422
+ '''df = pd.read_csv(r"")
423
+ print(df.isnull().sum())
424
+ df.head()''',
425
+ '''df = df.drop_duplicates()
426
+ for col in df.columns:
427
+ if pd.api.types.is_numeric_dtype(df[col]):
428
+ df[col] = df[col].fillna(df[col].median())
429
+ else:
430
+ df[col] = df[col].fillna(df[col].mode()[0])
431
+ df.isnull().sum()''',
432
+ '''target = ""
433
+ x = df.drop(target, axis=1)
434
+ y = df[target]''',
435
+ '''counts = y.value_counts()
436
+ fig, ax = plt.subplots()
437
+ ax.pie(counts.values, labels=counts.index, autopct="%1.1f%%")
438
+ ax.set_title("Target Variable Value Counts")
439
+ plt.show()''',
440
+ '''le = LabelEncoder()
441
+ for col in x.select_dtypes(exclude="number").columns:
442
+ x[col] = le.fit_transform(x[col].astype(str))
443
+ y = le.fit_transform(y.astype(str))
444
+ classes = le.classes_
445
+ x.head()''',
446
+ '''correlation_values = x.corrwith(pd.Series(y, index=x.index))
447
+ correlation_values.plot(kind="barh", figsize=(10, 6))
448
+ plt.show()''',
449
+ '''x = StandardScaler().fit_transform(x)
450
+ x = normalize(x, norm="l1")''',
451
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
452
+ w_train = compute_sample_weight("balanced", y_train)''',
453
+ '''dt = DecisionTreeClassifier(max_depth=4, random_state=35)
454
+ dt.fit(x_train, y_train, sample_weight=w_train)''',
455
+ '''svm = LinearSVC(class_weight="balanced", random_state=31, loss="hinge", fit_intercept=False)
456
+ svm.fit(x_train, y_train)''',
457
+ '''y_pred_dt = dt.predict_proba(x_test)[:, 1]
458
+ roc_auc_dt = roc_auc_score(y_test, y_pred_dt)
459
+ print("decision tree roc-auc score : {0:.3f}".format(roc_auc_dt))''',
460
+ '''y_pred_svm = svm.decision_function(x_test)
461
+ roc_auc_svm = roc_auc_score(y_test, y_pred_svm)
462
+ print("svm roc-auc score : {0:.3f}".format(roc_auc_svm))''',
463
+ ]
464
+
465
+ STAGES['dte'] = [
466
+ '''import pandas as pd
467
+ import matplotlib.pyplot as plt
468
+ from sklearn.model_selection import train_test_split
469
+ from sklearn.preprocessing import LabelEncoder
470
+ from sklearn.tree import DecisionTreeClassifier, plot_tree
471
+ from sklearn import metrics
472
+ import warnings
473
+ warnings.filterwarnings("ignore")''',
474
+ '''df = pd.read_csv(r"")
475
+ print(df.isnull().sum())
476
+ df.head()''',
477
+ '''df = df.drop_duplicates()
478
+ for col in df.columns:
479
+ if pd.api.types.is_numeric_dtype(df[col]):
480
+ df[col] = df[col].fillna(df[col].median())
481
+ else:
482
+ df[col] = df[col].fillna(df[col].mode()[0])
483
+ df.isnull().sum()''',
484
+ '''target = ""
485
+ x = df.drop(target, axis=1)
486
+ y = df[target]''',
487
+ '''le = LabelEncoder()
488
+ for col in x.select_dtypes(exclude="number").columns:
489
+ x[col] = le.fit_transform(x[col].astype(str))
490
+ x.head()''',
491
+ '''x.corrwith(y.astype("category").cat.codes)''',
492
+ '''category_counts = y.value_counts()
493
+ print(category_counts)
494
+ plt.bar(category_counts.index.astype(str), category_counts.values, color="blue")
495
+ plt.xlabel(target)
496
+ plt.ylabel("count")
497
+ plt.title("category distribution")
498
+ plt.xticks(rotation=45)
499
+ plt.show()''',
500
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)''',
501
+ '''dt_model = DecisionTreeClassifier(criterion="entropy", max_depth=4)
502
+ dt_model.fit(x_train, y_train)''',
503
+ '''y_pred = dt_model.predict(x_test)''',
504
+ '''print("Decision Tree Accuracy:", metrics.accuracy_score(y_test, y_pred))''',
505
+ '''plot_tree(dt_model)
506
+ plt.show()''',
507
+ ]
508
+
509
+ STAGES['dt'] = [
510
+ '''import pandas as pd
511
+ import matplotlib.pyplot as plt
512
+ import seaborn as sns
513
+ from sklearn.model_selection import train_test_split
514
+ from sklearn.preprocessing import LabelEncoder
515
+ from sklearn.tree import DecisionTreeClassifier, plot_tree
516
+ from sklearn.metrics import accuracy_score, confusion_matrix, classification_report''',
517
+ '''df = pd.read_csv(r"")
518
+ print(df.isnull().sum())
519
+ df.head()''',
520
+ '''df = df.drop_duplicates()
521
+ for col in df.columns:
522
+ if pd.api.types.is_numeric_dtype(df[col]):
523
+ df[col] = df[col].fillna(df[col].median())
524
+ else:
525
+ df[col] = df[col].fillna(df[col].mode()[0])
526
+ df.isnull().sum()''',
527
+ '''target = ""
528
+ x = df.drop(target, axis=1)
529
+ y = df[target]''',
530
+ '''le = LabelEncoder()
531
+ for col in x.select_dtypes(exclude="number").columns:
532
+ x[col] = le.fit_transform(x[col].astype(str))
533
+ x.head()''',
534
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42, stratify=y)''',
535
+ '''dt = DecisionTreeClassifier(criterion="gini", random_state=42)
536
+ dt.fit(x_train, y_train)''',
537
+ '''y_pred = dt.predict(x_test)
538
+ y_pred''',
539
+ '''accuracy = accuracy_score(y_test, y_pred)
540
+ print("Accuracy:", accuracy)
541
+ print(classification_report(y_test, y_pred))''',
542
+ '''labels = dt.classes_
543
+ cm = confusion_matrix(y_test, y_pred, labels=labels)
544
+ sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=labels, yticklabels=labels)
545
+ plt.xlabel("Predicted")
546
+ plt.ylabel("Actual")
547
+ plt.title("Confusion Matrix")
548
+ plt.show()''',
549
+ '''plt.figure(figsize=(15, 10))
550
+ plot_tree(dt, feature_names=list(x.columns), class_names=[str(c) for c in dt.classes_], filled=True)
551
+ plt.title("Decision Tree")
552
+ plt.show()''',
553
+ ]
554
+
555
+ STAGES['svm'] = [
556
+ '''import pandas as pd
557
+ import numpy as np
558
+ import matplotlib.pyplot as plt
559
+ from sklearn.model_selection import train_test_split
560
+ from sklearn.preprocessing import StandardScaler, LabelEncoder
561
+ from sklearn.svm import SVC
562
+ from sklearn.metrics import accuracy_score''',
563
+ '''df = pd.read_csv(r"")
564
+ print(df.isnull().sum())
565
+ df.head()''',
566
+ '''df = df.drop_duplicates()
567
+ for col in df.columns:
568
+ if pd.api.types.is_numeric_dtype(df[col]):
569
+ df[col] = df[col].fillna(df[col].median())
570
+ else:
571
+ df[col] = df[col].fillna(df[col].mode()[0])
572
+ df.isnull().sum()''',
573
+ '''target = ""
574
+ x = df.drop(target, axis=1)
575
+ y = df[target]''',
576
+ '''le = LabelEncoder()
577
+ for col in x.select_dtypes(exclude="number").columns:
578
+ x[col] = le.fit_transform(x[col].astype(str))
579
+ x.head()''',
580
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42, stratify=y)
581
+ print("Training Features:", x_train.shape)
582
+ print("Testing Features:", x_test.shape)
583
+ print("Training Labels:", y_train.shape)
584
+ print("Testing Labels:", y_test.shape)''',
585
+ '''scaler = StandardScaler()
586
+ x_train_scaled = scaler.fit_transform(x_train)
587
+ x_test_scaled = scaler.transform(x_test)''',
588
+ '''model = SVC(kernel="rbf", random_state=42)
589
+ model.fit(x_train_scaled, y_train)''',
590
+ '''y_pred = model.predict(x_test_scaled)
591
+ accuracy = accuracy_score(y_test, y_pred)
592
+ print("Accuracy:", accuracy)''',
593
+ '''x_visual = x.iloc[:, :2].values
594
+ scaler = StandardScaler()
595
+ x_visual = scaler.fit_transform(x_visual)
596
+ y_codes = y.astype("category").cat.codes
597
+ visual_model = SVC(kernel="rbf", gamma="scale")
598
+ visual_model.fit(x_visual, y_codes)
599
+ x_min, x_max = x_visual[:, 0].min() - 1, x_visual[:, 0].max() + 1
600
+ y_min, y_max = x_visual[:, 1].min() - 1, x_visual[:, 1].max() + 1
601
+ xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))
602
+ Z = visual_model.predict(np.c_[xx.ravel(), yy.ravel()])
603
+ Z = Z.reshape(xx.shape)
604
+ plt.figure(figsize=(8, 6))
605
+ plt.contourf(xx, yy, Z, alpha=0.3, cmap=plt.cm.coolwarm)
606
+ plt.scatter(x_visual[:, 0], x_visual[:, 1], c=y_codes, cmap=plt.cm.coolwarm, edgecolors="k")
607
+ plt.xlabel(f"{x.columns[0]} (Scaled)")
608
+ plt.ylabel(f"{x.columns[1]} (Scaled)")
609
+ plt.title("SVM Decision Boundary (RBF Kernel)")
610
+ plt.show()''',
611
+ ]
612
+
613
+ STAGES['multilr'] = [
614
+ '''import pandas as pd
615
+ import numpy as np
616
+ import matplotlib.pyplot as plt
617
+ import seaborn as sns
618
+ from sklearn.model_selection import train_test_split
619
+ from sklearn.preprocessing import OneHotEncoder, StandardScaler
620
+ from sklearn.linear_model import LogisticRegression
621
+ from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
622
+ from sklearn.metrics import accuracy_score
623
+ import warnings
624
+ warnings.filterwarnings("ignore")''',
625
+ '''df = pd.read_csv(r"")
626
+ print(df.isnull().sum())
627
+ df.head()''',
628
+ '''df = df.drop_duplicates()
629
+ for col in df.columns:
630
+ if pd.api.types.is_numeric_dtype(df[col]):
631
+ df[col] = df[col].fillna(df[col].median())
632
+ else:
633
+ df[col] = df[col].fillna(df[col].mode()[0])
634
+ df.isnull().sum()''',
635
+ '''target = ""
636
+ x = df.drop(target, axis=1)
637
+ y = df[target]''',
638
+ '''sns.countplot(y=y.astype(str))
639
+ plt.title(f"Distribution of {target}")
640
+ plt.show()''',
641
+ '''continuous_columns = x.select_dtypes(include="number").columns.tolist()
642
+ scaler = StandardScaler()
643
+ scaled_features = scaler.fit_transform(x[continuous_columns])
644
+ scaled_df = pd.DataFrame(scaled_features, columns=continuous_columns, index=x.index)
645
+ scaled_data = pd.concat([x.drop(columns=continuous_columns), scaled_df], axis=1)''',
646
+ '''categorical_columns = scaled_data.select_dtypes(exclude="number").columns.tolist()
647
+ encoder = OneHotEncoder(sparse_output=False, drop="first")
648
+ encoded_features = encoder.fit_transform(scaled_data[categorical_columns].astype(str))
649
+ encoded_df = pd.DataFrame(encoded_features, columns=encoder.get_feature_names_out(categorical_columns), index=scaled_data.index)
650
+ x = pd.concat([scaled_data.drop(columns=categorical_columns), encoded_df], axis=1)
651
+ x.head()''',
652
+ '''y = y.astype("category").cat.codes
653
+ x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42, stratify=y)''',
654
+ '''model_ova = OneVsRestClassifier(LogisticRegression(max_iter=1000))
655
+ model_ova.fit(x_train, y_train)''',
656
+ '''y_pred_ova = model_ova.predict(x_test)
657
+ print("One vs All (OVA) Strategy")
658
+ print(f"Accuracy: {np.round(100 * accuracy_score(y_test, y_pred_ova), 2)}%")''',
659
+ '''model_ovo = OneVsOneClassifier(LogisticRegression(max_iter=1000))
660
+ model_ovo.fit(x_train, y_train)''',
661
+ '''y_pred_ovo = model_ovo.predict(x_test)
662
+ print("one vs one ovo strategy")
663
+ print(f"accuracy:{np.round(100 * accuracy_score(y_test, y_pred_ovo), 2)}%")''',
664
+ ]
665
+
666
+ STAGES['prep'] = [
667
+ '''import pandas as pd
668
+ from sklearn.preprocessing import MinMaxScaler, StandardScaler, LabelEncoder''',
669
+ '''df = pd.read_csv(r"")
670
+ print(df.isnull().sum())
671
+ df.head()''',
672
+ '''df = df.drop_duplicates()
673
+ num_cols = df.select_dtypes(include="number").columns
674
+ df[num_cols] = df[num_cols].fillna(df[num_cols].median())
675
+ cat_cols = df.select_dtypes(exclude="number").columns
676
+ for col in cat_cols:
677
+ df[col] = df[col].fillna(df[col].mode()[0])
678
+ df[col] = df[col].astype(str).str.strip().str.title()
679
+ print("Missing Values")
680
+ print(df.isnull().sum())
681
+ df.head()''',
682
+ '''df2 = pd.read_csv(r"")
683
+ print(df2.isnull().sum())
684
+ df2.head()''',
685
+ '''df2 = df2.drop_duplicates()
686
+ num_cols2 = df2.select_dtypes(include="number").columns
687
+ df2[num_cols2] = df2[num_cols2].fillna(df2[num_cols2].median())
688
+ print("Second file missing values")
689
+ print(df2.isnull().sum())''',
690
+ '''key = ""
691
+ df_final = pd.merge(df, df2, on=key, how="left")
692
+ num_cols = df_final.select_dtypes(include="number").columns.drop(key, errors="ignore")
693
+ df_final[num_cols] = df_final[num_cols].fillna(0)
694
+ df_final.head()''',
695
+ '''for col in num_cols:
696
+ df_final[col + "_MinMax"] = MinMaxScaler().fit_transform(df_final[[col]]).ravel()
697
+ df_final[col + "_ZScore"] = StandardScaler().fit_transform(df_final[[col]]).ravel()
698
+ df_final.head()''',
699
+ '''bin_col = ""
700
+ bins = [0, 25, 40, 60, 100]
701
+ labels = ["Young", "Adult", "Middle", "Senior"]
702
+ df_final[bin_col + "_Group"] = pd.cut(df_final[bin_col], bins=bins, labels=labels)
703
+ df_final.head()''',
704
+ '''le = LabelEncoder()
705
+ for col in df_final.select_dtypes(exclude="number").columns:
706
+ df_final[col + "_Encoded"] = le.fit_transform(df_final[col].astype(str))
707
+ df_final.head()''',
708
+ '''print(df_final.info())
709
+ print("\\nMissing Values")
710
+ print(df_final.isnull().sum())
711
+ print("\\nFirst Five Rows")
712
+ print(df_final.head())''',
713
+ '''level_col = ""
714
+
715
+
716
+ def loyalty_level(value):
717
+ if value >= 20:
718
+ return "Platinum"
719
+ elif value >= 10:
720
+ return "Gold"
721
+ elif value >= 5:
722
+ return "Silver"
723
+ else:
724
+ return "Bronze"
725
+
726
+
727
+ df_final["Loyalty_Level"] = df_final[level_col].apply(loyalty_level)
728
+ discount_map = {"Bronze": 5, "Silver": 10, "Gold": 15, "Platinum": 20}
729
+ df_final["Discount_Percentage"] = df_final["Loyalty_Level"].map(discount_map)
730
+ df_final[[key, level_col, "Loyalty_Level", "Discount_Percentage"]].head()''',
731
+ ]
732
+
733
+ STAGES['slr'] = [
734
+ '''import pandas as pd
735
+ import numpy as np
736
+ import matplotlib.pyplot as plt
737
+ from sklearn.model_selection import train_test_split
738
+ from sklearn import linear_model
739
+ from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score''',
740
+ '''df = pd.read_csv(r"")
741
+ print(df.isnull().sum())
742
+ df.head()''',
743
+ '''df = df.drop_duplicates()
744
+ for col in df.columns:
745
+ if pd.api.types.is_numeric_dtype(df[col]):
746
+ df[col] = df[col].fillna(df[col].median())
747
+ else:
748
+ df[col] = df[col].fillna(df[col].mode()[0])
749
+ df.isnull().sum()''',
750
+ '''df.describe()''',
751
+ '''target = ""
752
+ x = df[[""]]
753
+ y = df[target]''',
754
+ '''pd.concat([x, y], axis=1).hist()
755
+ plt.show()''',
756
+ '''plt.scatter(x.iloc[:, 0], y, color="blue")
757
+ plt.xlabel(x.columns[0])
758
+ plt.ylabel(target)
759
+ plt.show()''',
760
+ '''x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)''',
761
+ '''regressor = linear_model.LinearRegression()
762
+ regressor.fit(x_train, y_train)
763
+ print("coefficient:", regressor.coef_[0])
764
+ print("Intercept:", regressor.intercept_)''',
765
+ '''y_pred = regressor.predict(x_test)
766
+ print("mean absolute error:%.2f" % mean_absolute_error(y_test, y_pred))
767
+ print("mean squared error:%.2f" % mean_squared_error(y_test, y_pred))
768
+ print("root mean squared error:%.2f" % np.sqrt(mean_squared_error(y_test, y_pred)))
769
+ print("r2-score:%.2f" % r2_score(y_test, y_pred))''',
770
+ '''plt.scatter(x_train.iloc[:, 0], y_train, color="red")
771
+ plt.plot(x_train.iloc[:, 0], regressor.predict(x_train), "-b")
772
+ plt.xlabel(x.columns[0])
773
+ plt.ylabel(target)
774
+ plt.show()''',
775
+ '''plt.scatter(x_test.iloc[:, 0], y_test, color="red", label="Actual Test Data")
776
+ plt.plot(x_test.iloc[:, 0], y_pred, color="blue", label="Regression Line")
777
+ plt.title("Regression Model on Test Data")
778
+ plt.xlabel(x.columns[0])
779
+ plt.ylabel(target)
780
+ plt.legend()
781
+ plt.show()''',
782
+ ]
783
+
784
+ STAGES['mlr'] = [
785
+ '''import pandas as pd
786
+ import numpy as np
787
+ import matplotlib.pyplot as plt
788
+ from sklearn import preprocessing, linear_model
789
+ from sklearn.model_selection import train_test_split
790
+ from sklearn.preprocessing import LabelEncoder
791
+ from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score''',
792
+ '''df = pd.read_csv(r"")
793
+ print(df.isnull().sum())
794
+ df.head()''',
795
+ '''df = df.drop_duplicates()
796
+ for col in df.columns:
797
+ if pd.api.types.is_numeric_dtype(df[col]):
798
+ df[col] = df[col].fillna(df[col].median())
799
+ else:
800
+ df[col] = df[col].fillna(df[col].mode()[0])
801
+ df.isnull().sum()''',
802
+ '''df.describe()''',
803
+ '''target = ""
804
+ x = df.drop(target, axis=1)
805
+ y = df[target]''',
806
+ '''le = LabelEncoder()
807
+ for col in x.select_dtypes(exclude="number").columns:
808
+ x[col] = le.fit_transform(x[col].astype(str))
809
+ x.head()''',
810
+ '''pd.concat([x, y], axis=1).corr()''',
811
+ '''axes = pd.plotting.scatter_matrix(pd.concat([x, y], axis=1), alpha=0.2)
812
+ for ax in axes.flatten():
813
+ ax.xaxis.label.set_rotation(90)
814
+ ax.yaxis.label.set_rotation(0)
815
+ ax.yaxis.label.set_ha("right")
816
+ plt.tight_layout()
817
+ plt.gcf().subplots_adjust(wspace=0, hspace=0)
818
+ plt.show()''',
819
+ '''std_scaler = preprocessing.StandardScaler()
820
+ x_std = std_scaler.fit_transform(x)
821
+ pd.DataFrame(x_std, columns=x.columns).describe().round(2)''',
822
+ '''x_train, x_test, y_train, y_test = train_test_split(x_std, y, test_size=0.2, random_state=42)''',
823
+ '''regressor = linear_model.LinearRegression()
824
+ regressor.fit(x_train, y_train)
825
+ coef_ = regressor.coef_
826
+ intercept_ = regressor.intercept_
827
+ print("coefficient:", coef_)
828
+ print("intercept:", intercept_)''',
829
+ '''y_pred = regressor.predict(x_test)
830
+ print("mean absolute error:%.2f" % mean_absolute_error(y_test, y_pred))
831
+ print("mean squared error:%.2f" % mean_squared_error(y_test, y_pred))
832
+ print("root mean squared error:%.2f" % np.sqrt(mean_squared_error(y_test, y_pred)))
833
+ print("r2-score:%.2f" % r2_score(y_test, y_pred))''',
834
+ '''mean_ = std_scaler.mean_
835
+ std_devs = np.sqrt(std_scaler.var_)
836
+ coef_original = coef_ / std_devs
837
+ intercept_original = intercept_ - np.sum((mean_ * coef_) / std_devs)
838
+ print("coefficient:", coef_original)
839
+ print("intercept:", intercept_original)
840
+ pd.Series(coef_original, index=x.columns)''',
841
+ ]
842
+
843
+ STAGES['knn'] = [
844
+ '''import pandas as pd
845
+ import numpy as np
846
+ import seaborn as sns
847
+ from sklearn.model_selection import train_test_split
848
+ from sklearn.preprocessing import StandardScaler, LabelEncoder
849
+ from sklearn.neighbors import KNeighborsClassifier
850
+ from sklearn.metrics import accuracy_score
851
+ import matplotlib.pyplot as plt
852
+ import warnings
853
+ warnings.filterwarnings("ignore")''',
854
+ '''df = pd.read_csv(r"")
855
+ print(df.isnull().sum())
856
+ df.head()''',
857
+ '''df = df.drop_duplicates()
858
+ for col in df.columns:
859
+ if pd.api.types.is_numeric_dtype(df[col]):
860
+ df[col] = df[col].fillna(df[col].median())
861
+ else:
862
+ df[col] = df[col].fillna(df[col].mode()[0])
863
+ df.isnull().sum()''',
864
+ '''target = ""
865
+ x = df.drop(target, axis=1)
866
+ y = df[target]''',
867
+ '''y.value_counts()''',
868
+ '''le = LabelEncoder()
869
+ for col in x.select_dtypes(exclude="number").columns:
870
+ x[col] = le.fit_transform(x[col].astype(str))
871
+ x.head()''',
872
+ '''correlation_matrix = pd.concat([x, y.astype("category").cat.codes.rename(target)], axis=1).corr()
873
+ plt.figure(figsize=(10, 8))
874
+ sns.heatmap(correlation_matrix, annot=True, cmap="coolwarm", fmt=".2f", linewidths=0.5)
875
+ plt.show()''',
876
+ '''correlation_values = abs(x.corrwith(y.astype("category").cat.codes)).sort_values(ascending=False)
877
+ correlation_values''',
878
+ '''x_norm = StandardScaler().fit_transform(x)''',
879
+ '''x_train, x_test, y_train, y_test = train_test_split(x_norm, y, test_size=0.2, random_state=42)''',
880
+ '''k = 4
881
+ knn_classifier = KNeighborsClassifier(n_neighbors=k)
882
+ knn_model = knn_classifier.fit(x_train, y_train)''',
883
+ '''yhat = knn_model.predict(x_test)''',
884
+ '''print("test set accuracy:", accuracy_score(y_test, yhat))''',
885
+ '''ks = 10
886
+ acc = np.zeros((ks))
887
+ std_acc = np.zeros((ks))
888
+ for n in range(1, ks + 1):
889
+ knn_model_n = KNeighborsClassifier(n_neighbors=n).fit(x_train, y_train)
890
+ y_hat = knn_model_n.predict(x_test)
891
+ acc[n - 1] = accuracy_score(y_test, y_hat)
892
+ std_acc[n - 1] = np.std(y_hat == y_test) / np.sqrt(y_hat.shape[0])''',
893
+ '''plt.plot(range(1, ks + 1), acc, "g")
894
+ plt.fill_between(range(1, ks + 1), acc - 1 * std_acc, acc + 1 * std_acc, alpha=0.10)
895
+ plt.legend(["accuracy value", "standard deviation"])
896
+ plt.ylabel("model accuracy")
897
+ plt.xlabel("number of neighbors (k)")
898
+ plt.tight_layout()
899
+ plt.show()''',
900
+ '''print("the best accuracy was with", acc.max(), "with k=", acc.argmax() + 1)''',
901
+ ]
902
+
903
+ STAGES['kmeans'] = [
904
+ '''import pandas as pd
905
+ import numpy as np
906
+ import matplotlib.pyplot as plt
907
+ import seaborn as sns
908
+ from sklearn.preprocessing import StandardScaler
909
+ from sklearn.cluster import KMeans
910
+ from sklearn.metrics import silhouette_score, davies_bouldin_score, calinski_harabasz_score''',
911
+ '''df = pd.read_csv(r"")
912
+ print(df.isnull().sum())
913
+ df.head()''',
914
+ '''df = df.drop_duplicates()
915
+ for col in df.columns:
916
+ if pd.api.types.is_numeric_dtype(df[col]):
917
+ df[col] = df[col].fillna(df[col].median())
918
+ else:
919
+ df[col] = df[col].fillna(df[col].mode()[0])
920
+ df.isnull().sum()''',
921
+ '''print(df.describe())
922
+ plt.figure(figsize=(10, 8))
923
+ sns.heatmap(df.corr(numeric_only=True), annot=True, cmap="coolwarm", fmt=".2f")
924
+ plt.show()''',
925
+ '''x = df.drop([], axis=1)
926
+ x = pd.get_dummies(x, drop_first=True)
927
+ x.head()''',
928
+ '''scaler = StandardScaler()
929
+ x_scaled = scaler.fit_transform(x)''',
930
+ '''inertia = []
931
+ for k in range(1, 11):
932
+ km = KMeans(n_clusters=k, n_init=10, random_state=42)
933
+ km.fit(x_scaled)
934
+ inertia.append(km.inertia_)
935
+ plt.plot(range(1, 11), inertia, marker="o")
936
+ plt.xlabel("number of clusters (k)")
937
+ plt.ylabel("inertia (WCSS)")
938
+ plt.title("Elbow Method")
939
+ plt.show()''',
940
+ '''sample = 5000 if len(x_scaled) > 5000 else None
941
+ scores = {}
942
+ for k in range(2, 11):
943
+ labels_k = KMeans(n_clusters=k, n_init=10, random_state=42).fit_predict(x_scaled)
944
+ scores[k] = silhouette_score(x_scaled, labels_k, sample_size=sample, random_state=42)
945
+ print(f"k={k}: silhouette score = {scores[k]:.3f}")
946
+ print("best k by silhouette:", max(scores, key=scores.get))''',
947
+ '''k = 3
948
+ kmeans = KMeans(n_clusters=k, n_init=10, random_state=42)
949
+ labels = kmeans.fit_predict(x_scaled)
950
+ print("silhouette score:", round(silhouette_score(x_scaled, labels, sample_size=sample, random_state=42), 3))
951
+ print("davies-bouldin index:", round(davies_bouldin_score(x_scaled, labels), 3))
952
+ print("calinski-harabasz score:", round(calinski_harabasz_score(x_scaled, labels), 3))''',
953
+ '''df["Cluster"] = labels
954
+ print(df["Cluster"].value_counts())
955
+ df.groupby("Cluster").mean(numeric_only=True)''',
956
+ '''centers = kmeans.cluster_centers_
957
+ plt.figure(figsize=(8, 6))
958
+ plt.scatter(x_scaled[:, 0], x_scaled[:, 1], c=labels, cmap="viridis", alpha=0.6)
959
+ plt.scatter(centers[:, 0], centers[:, 1], c="red", marker="X", s=200, label="centers")
960
+ plt.xlabel(f"{x.columns[0]} (Scaled)")
961
+ plt.ylabel(f"{x.columns[1]} (Scaled)")
962
+ plt.title("K-Means Clusters")
963
+ plt.legend()
964
+ plt.show()''',
965
+ ]
966
+
967
+ STAGES['lrshap'] = [
968
+ '''import numpy as np
969
+ import pandas as pd
970
+ import matplotlib.pyplot as plt
971
+ from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedKFold
972
+ from sklearn.pipeline import Pipeline
973
+ from sklearn.preprocessing import StandardScaler, LabelEncoder
974
+ from sklearn.linear_model import LogisticRegression
975
+ from sklearn.metrics import classification_report, roc_auc_score, ConfusionMatrixDisplay''',
976
+ '''df = pd.read_csv(r"")
977
+ print(df.isnull().sum())
978
+ df.head()''',
979
+ '''df = df.drop_duplicates()
980
+ for col in df.columns:
981
+ if pd.api.types.is_numeric_dtype(df[col]):
982
+ df[col] = df[col].fillna(df[col].median())
983
+ else:
984
+ df[col] = df[col].fillna(df[col].mode()[0])
985
+ df.isnull().sum()''',
986
+ '''target = ""
987
+ x = df.drop(target, axis=1)
988
+ y = df[target]''',
989
+ '''le = LabelEncoder()
990
+ for col in x.select_dtypes(exclude="number").columns:
991
+ x[col] = le.fit_transform(x[col].astype(str))
992
+ y = le.fit_transform(y.astype(str))
993
+ classes = le.classes_
994
+ x.head()''',
995
+ '''print(df.shape, df.isnull().sum().sum(), pd.Series(y).value_counts().to_dict())
996
+ x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42, stratify=y)''',
997
+ '''binary = len(classes) == 2
998
+ pipe = Pipeline([("scaler", StandardScaler()),
999
+ ("logreg", LogisticRegression(max_iter=5000))])
1000
+ param_grid = {"logreg__C": [0.001, 0.01, 0.1, 1, 10, 100],
1001
+ "logreg__penalty": ["l1", "l2"],
1002
+ "logreg__solver": ["liblinear" if binary else "saga"]}
1003
+ grid = GridSearchCV(pipe, param_grid, scoring="roc_auc" if binary else "roc_auc_ovr",
1004
+ cv=StratifiedKFold(5, shuffle=True, random_state=42))
1005
+ grid.fit(x_train, y_train)
1006
+ model = grid.best_estimator_
1007
+ print(grid.best_params_, round(grid.best_score_, 4))''',
1008
+ '''labels = list(range(len(classes)))
1009
+ print(classification_report(y_test, model.predict(x_test), labels=labels, target_names=[str(c) for c in classes], digits=3, zero_division=0))
1010
+ proba = model.predict_proba(x_test)
1011
+ print("ROC-AUC:", roc_auc_score(y_test, proba[:, 1]) if binary else roc_auc_score(y_test, proba, multi_class="ovr"))
1012
+ ConfusionMatrixDisplay.from_estimator(model, x_test, y_test, display_labels=classes)
1013
+ plt.show()''',
1014
+ '''cls = 1
1015
+ coef = model.named_steps["logreg"].coef_[0 if binary else cls]
1016
+ imp = pd.DataFrame({"Feature": x.columns, "Coefficient": coef, "Odds Ratio": np.exp(coef)})
1017
+ imp.reindex(imp.Coefficient.abs().sort_values(ascending=False).index).head(6)''',
1018
+ '''import shap
1019
+ xtr_s = pd.DataFrame(model.named_steps["scaler"].transform(x_train), columns=x.columns)
1020
+ xte_s = pd.DataFrame(model.named_steps["scaler"].transform(x_test), columns=x.columns)
1021
+ explainer = shap.LinearExplainer(model.named_steps["logreg"], xtr_s)
1022
+ shap_values = explainer(xte_s)
1023
+ sv = shap_values if binary else shap_values[:, :, cls]''',
1024
+ '''shap.summary_plot(sv, xte_s)''',
1025
+ '''i = 0
1026
+ shap.plots.waterfall(sv[i])''',
1027
+ ]
1028
+
1029
+ _state = {"name": None, "step": None, "skip": False}
1030
+ _hooked = [False]
1031
+
1032
+
1033
+ def _ipython():
1034
+ if "google.colab" in sys.modules:
1035
+ return None
1036
+ try:
1037
+ from IPython import get_ipython
1038
+ return get_ipython()
1039
+ except ImportError:
1040
+ return None
1041
+
1042
+
1043
+ def _resolve(name):
1044
+ if isinstance(name, int) and not isinstance(name, bool):
1045
+ if 1 <= name <= len(ORDER):
1046
+ return ORDER[name - 1]
1047
+ print(f"Choose a number from 1 to {len(ORDER)} or a name.")
1048
+ list_programs()
1049
+ return None
1050
+ key = str(name).strip().lower()
1051
+ if key in STAGES:
1052
+ return key
1053
+ close = difflib.get_close_matches(key, ORDER, n=3, cutoff=0.5)
1054
+ if close:
1055
+ print(f"'{name}' not found. Did you mean: {', '.join(close)}?")
1056
+ else:
1057
+ print(f"'{name}' not found.")
1058
+ list_programs()
1059
+ return None
1060
+
1061
+
1062
+ def _insert(name, index):
1063
+ ip = _ipython()
1064
+ _state["name"], _state["step"], _state["skip"] = name, index, True
1065
+ ip.set_next_input(STAGES[name][index], replace=False)
1066
+
1067
+
1068
+ def _after_cell(result):
1069
+ name, step = _state["name"], _state["step"]
1070
+ if name is None:
1071
+ return
1072
+ if _state["skip"]:
1073
+ _state["skip"] = False
1074
+ return
1075
+ if not getattr(result, "success", True):
1076
+ return
1077
+ nxt_index = step + 1
1078
+ if nxt_index >= len(STAGES[name]):
1079
+ _state["name"], _state["step"] = None, None
1080
+ return
1081
+ _state["step"] = nxt_index
1082
+ _ipython().set_next_input(STAGES[name][nxt_index], replace=False)
1083
+
1084
+
1085
+ def lrn(name, step=1):
1086
+ key = _resolve(name)
1087
+ if key is None:
1088
+ return
1089
+ total = len(STAGES[key])
1090
+ if not 1 <= step <= total:
1091
+ print(f"'{key}' has stages 1 to {total}.")
1092
+ return
1093
+ ip = _ipython()
1094
+ if ip is None:
1095
+ for i, s in enumerate(STAGES[key][step - 1:], start=step):
1096
+ print(f"----- stage {i} -----")
1097
+ print(s)
1098
+ print()
1099
+ return
1100
+ if not _hooked[0]:
1101
+ for cb in list(ip.events.callbacks.get("post_run_cell", [])):
1102
+ if getattr(cb, "__name__", "") == "_after_cell" and getattr(cb, "__module__", "") == __name__:
1103
+ ip.events.unregister("post_run_cell", cb)
1104
+ ip.events.register("post_run_cell", _after_cell)
1105
+ _hooked[0] = True
1106
+ _insert(key, step - 1)
1107
+
1108
+
1109
+ def nxt():
1110
+ name, step = _state["name"], _state["step"]
1111
+ if name is None:
1112
+ print("Nothing to continue. Start with lrn(\"name\").")
1113
+ return
1114
+ if step + 1 >= len(STAGES[name]):
1115
+ print("That was the last stage.")
1116
+ _state["name"], _state["step"] = None, None
1117
+ return
1118
+ _insert(name, step + 1)
1119
+
1120
+
1121
+ def stop():
1122
+ _state["name"], _state["step"], _state["skip"] = None, None, False
1123
+
1124
+
1125
+ def code(name):
1126
+ key = _resolve(name)
1127
+ if key is None:
1128
+ return ""
1129
+ return "\n\n".join(STAGES[key]) + "\n"
1130
+
1131
+
1132
+ def list_programs():
1133
+ for i, key in enumerate(ORDER, start=1):
1134
+ print(f"{i:>2}. {key:<8} {TITLES[key]} ({len(STAGES[key])} stages)")
@@ -0,0 +1,17 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "predlr"
7
+ version = "1.0.0"
8
+ description = "logistic regression library"
9
+ readme = "README.md"
10
+ authors = [{ name = "Charles" }]
11
+ license = "MIT"
12
+ keywords = ["machine learning"]
13
+ requires-python = ">=3.9"
14
+ dependencies = ["pandas", "numpy", "scikit-learn", "matplotlib", "seaborn", "xgboost", "openpyxl", "xlrd"]
15
+
16
+ [tool.setuptools]
17
+ py-modules = ["predlr"]
predlr-1.0.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+