prajvaltools 0.1.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.
- prajvaltools-0.1.0/PKG-INFO +6 -0
- prajvaltools-0.1.0/README.md +31 -0
- prajvaltools-0.1.0/mynotes.txt +665 -0
- prajvaltools-0.1.0/prajvaltools.egg-info/PKG-INFO +6 -0
- prajvaltools-0.1.0/prajvaltools.egg-info/SOURCES.txt +8 -0
- prajvaltools-0.1.0/prajvaltools.egg-info/dependency_links.txt +1 -0
- prajvaltools-0.1.0/prajvaltools.egg-info/top_level.txt +1 -0
- prajvaltools-0.1.0/prajvaltools.py +20 -0
- prajvaltools-0.1.0/pyproject.toml +16 -0
- prajvaltools-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
# PrajvalTools
|
|
3
|
+
|
|
4
|
+
My personal Python utility library.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install prajvaltools
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
import prajvaltools
|
|
16
|
+
|
|
17
|
+
print(prajvaltools.add(10, 20))
|
|
18
|
+
print(prajvaltools.mean([10, 20, 30]))
|
|
19
|
+
print(prajvaltools.factorial(5))
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
- Addition
|
|
25
|
+
- Subtraction
|
|
26
|
+
- Multiplication
|
|
27
|
+
- Division
|
|
28
|
+
- Mean
|
|
29
|
+
- Factorial
|
|
30
|
+
- Square
|
|
31
|
+
- Cube
|
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
LR
|
|
2
|
+
from sklearn.linear_model import LinearRegression
|
|
3
|
+
from sklearn.metrics import mean_squared_error, r2_score
|
|
4
|
+
|
|
5
|
+
model = LinearRegression()
|
|
6
|
+
|
|
7
|
+
model.fit(X_train, y_train)
|
|
8
|
+
|
|
9
|
+
y_pred = model.predict(X_test)
|
|
10
|
+
|
|
11
|
+
print("Coefficient:", model.coef_)
|
|
12
|
+
print("Intercept:", model.intercept_)
|
|
13
|
+
print("MSE:", mean_squared_error(y_test, y_pred))
|
|
14
|
+
print("R2:", r2_score(y_test, y_pred))
|
|
15
|
+
|
|
16
|
+
GD
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
X = np.array([1, 2, 3, 4, 5])
|
|
21
|
+
y = np.array([2, 4, 6, 8, 10])
|
|
22
|
+
|
|
23
|
+
m = 0
|
|
24
|
+
c = 0
|
|
25
|
+
alpha = 0.01
|
|
26
|
+
n = len(X)
|
|
27
|
+
|
|
28
|
+
for i in range(1000):
|
|
29
|
+
y_pred = m * X + c
|
|
30
|
+
|
|
31
|
+
dm = (-2/n) * np.sum(X * (y - y_pred))
|
|
32
|
+
dc = (-2/n) * np.sum(y - y_pred)
|
|
33
|
+
|
|
34
|
+
m = m - alpha * dm
|
|
35
|
+
c = c - alpha * dc
|
|
36
|
+
|
|
37
|
+
print("Slope:", m)
|
|
38
|
+
print("Intercept:", c)
|
|
39
|
+
|
|
40
|
+
DT
|
|
41
|
+
|
|
42
|
+
import numpy as np
|
|
43
|
+
from sklearn.datasets import load_iris
|
|
44
|
+
from sklearn.model_selection import train_test_split
|
|
45
|
+
|
|
46
|
+
iris = load_iris()
|
|
47
|
+
X = iris.data # 4 measurements per flower
|
|
48
|
+
y = iris.target # 3 species
|
|
49
|
+
|
|
50
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
51
|
+
X, y, test_size=0.33, random_state=42)
|
|
52
|
+
|
|
53
|
+
print(X_train.shape, X_test.shape)
|
|
54
|
+
|
|
55
|
+
from sklearn.tree import DecisionTreeClassifier
|
|
56
|
+
from sklearn import tree
|
|
57
|
+
import matplotlib.pyplot as plt
|
|
58
|
+
|
|
59
|
+
model = DecisionTreeClassifier(random_state=42)
|
|
60
|
+
model.fit(X_train, y_train)
|
|
61
|
+
|
|
62
|
+
print(model.get_depth()) # 6
|
|
63
|
+
print(model.get_n_leaves()) # 10
|
|
64
|
+
|
|
65
|
+
plt.figure(figsize=(15, 10))
|
|
66
|
+
tree.plot_tree(model, filled=True,
|
|
67
|
+
feature_names=iris.feature_names,
|
|
68
|
+
class_names=iris.target_names)
|
|
69
|
+
plt.show()
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
PCA
|
|
73
|
+
|
|
74
|
+
import numpy as np
|
|
75
|
+
import matplotlib.pyplot as plt
|
|
76
|
+
from sklearn.datasets import load_iris
|
|
77
|
+
from sklearn.preprocessing import StandardScaler
|
|
78
|
+
from sklearn.decomposition import PCA
|
|
79
|
+
|
|
80
|
+
iris = load_iris()
|
|
81
|
+
X, y = iris.data, iris.target # y: only for colour
|
|
82
|
+
print(X.shape) # (150, 4)
|
|
83
|
+
|
|
84
|
+
Z = StandardScaler().fit_transform(X)
|
|
85
|
+
print(Z.mean(axis=0).round(3)) # [0. 0. 0. 0.]
|
|
86
|
+
print(Z.std(axis=0).round(3)) # [1. 1. 1. 1.]
|
|
87
|
+
pca = PCA(n_components=2)
|
|
88
|
+
scores = pca.fit_transform(Z) # 150 × 2
|
|
89
|
+
|
|
90
|
+
print(pca.explained_variance_ratio_)
|
|
91
|
+
# [0.7296 0.2285]
|
|
92
|
+
print(pca.components_.shape) # (2, 4)
|
|
93
|
+
|
|
94
|
+
ev = 100 * pca.explained_variance_ratio_
|
|
95
|
+
for k in range(3):
|
|
96
|
+
plt.scatter(scores[y == k, 0], scores[y == k, 1],
|
|
97
|
+
label=iris.target_names[k])
|
|
98
|
+
plt.xlabel(f'PC1 ({ev[0]:.1f}%)')
|
|
99
|
+
plt.ylabel(f'PC2 ({ev[1]:.1f}%)')
|
|
100
|
+
plt.legend(); plt.show()
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
LDA
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
import numpy as np
|
|
107
|
+
import matplotlib.pyplot as plt
|
|
108
|
+
from sklearn.datasets import load_iris
|
|
109
|
+
from sklearn.model_selection import train_test_split
|
|
110
|
+
from sklearn.preprocessing import StandardScaler
|
|
111
|
+
from sklearn.pipeline import make_pipeline
|
|
112
|
+
from sklearn.discriminant_analysis import (
|
|
113
|
+
LinearDiscriminantAnalysis)
|
|
114
|
+
|
|
115
|
+
X, y = load_iris(return_X_y=True)
|
|
116
|
+
Xtr, Xte, ytr, yte = train_test_split(
|
|
117
|
+
X, y, test_size=0.30, stratify=y, random_state=42)
|
|
118
|
+
print(Xtr.shape, Xte.shape) # (105, 4) (45, 4)
|
|
119
|
+
|
|
120
|
+
model = make_pipeline(StandardScaler(),
|
|
121
|
+
LinearDiscriminantAnalysis())
|
|
122
|
+
model.fit(Xtr, ytr) # labels ARE used here
|
|
123
|
+
from sklearn.metrics import (accuracy_score,
|
|
124
|
+
confusion_matrix)
|
|
125
|
+
pred = model.predict(Xte)
|
|
126
|
+
print(accuracy_score(yte, pred)) # 0.9778
|
|
127
|
+
print(confusion_matrix(yte, pred))
|
|
128
|
+
# [[15 0 0]
|
|
129
|
+
# [ 0 15 0]
|
|
130
|
+
# [ 0 1 14]]
|
|
131
|
+
|
|
132
|
+
print(model.score(Xtr, ytr)) # 0.9714 (train)
|
|
133
|
+
Zte = model.transform(Xte) # 45 × 2
|
|
134
|
+
lda = model[-1] # the LDA step
|
|
135
|
+
print(lda.explained_variance_ratio_) # [0.988 0.012]
|
|
136
|
+
|
|
137
|
+
names = ['setosa', 'versicolor', 'virginica']
|
|
138
|
+
for k, name in enumerate(names):
|
|
139
|
+
plt.scatter(Zte[yte == k, 0], Zte[yte == k, 1],
|
|
140
|
+
label=name)
|
|
141
|
+
plt.xlabel('LD1'); plt.ylabel('LD2')
|
|
142
|
+
plt.legend(); plt.show()
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
KNN
|
|
146
|
+
|
|
147
|
+
import numpy as np
|
|
148
|
+
import matplotlib.pyplot as plt
|
|
149
|
+
from sklearn.datasets import load_iris
|
|
150
|
+
from sklearn.model_selection import train_test_split
|
|
151
|
+
from sklearn.preprocessing import StandardScaler
|
|
152
|
+
from sklearn.pipeline import make_pipeline
|
|
153
|
+
from sklearn.neighbors import KNeighborsClassifier
|
|
154
|
+
|
|
155
|
+
X, y = load_iris(return_X_y=True)
|
|
156
|
+
Xtr, Xte, ytr, yte = train_test_split(
|
|
157
|
+
X, y, test_size=0.30, stratify=y, random_state=42)
|
|
158
|
+
|
|
159
|
+
model = make_pipeline(StandardScaler(),
|
|
160
|
+
KNeighborsClassifier(n_neighbors=5))
|
|
161
|
+
model.fit(Xtr, ytr) # only stores scaled data
|
|
162
|
+
from sklearn.metrics import (accuracy_score,
|
|
163
|
+
confusion_matrix)
|
|
164
|
+
pred = model.predict(Xte)
|
|
165
|
+
print(accuracy_score(yte, pred)) # 0.9111
|
|
166
|
+
print(confusion_matrix(yte, pred))
|
|
167
|
+
# [[15 0 0]
|
|
168
|
+
# [ 0 15 0]
|
|
169
|
+
# [ 0 4 11]]
|
|
170
|
+
from sklearn.model_selection import (
|
|
171
|
+
cross_val_score, StratifiedKFold)
|
|
172
|
+
cv = StratifiedKFold(5, shuffle=True, random_state=0)
|
|
173
|
+
scores = []
|
|
174
|
+
for k in range(1, 31):
|
|
175
|
+
m = make_pipeline(StandardScaler(),
|
|
176
|
+
KNeighborsClassifier(k))
|
|
177
|
+
s = cross_val_score(m, Xtr, ytr, cv=cv)
|
|
178
|
+
scores.append(s.mean())
|
|
179
|
+
best_k = int(np.argmax(scores)) + 1
|
|
180
|
+
print(best_k, max(scores)) # 5 0.990
|
|
181
|
+
plt.plot(range(1, 31), scores, 'o-'); plt.show()
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
SVM
|
|
185
|
+
|
|
186
|
+
import numpy as np
|
|
187
|
+
import matplotlib.pyplot as plt
|
|
188
|
+
from sklearn.datasets import load_iris
|
|
189
|
+
from sklearn.model_selection import train_test_split
|
|
190
|
+
from sklearn.preprocessing import StandardScaler
|
|
191
|
+
from sklearn.pipeline import make_pipeline
|
|
192
|
+
from sklearn.svm import SVC
|
|
193
|
+
|
|
194
|
+
X, y = load_iris(return_X_y=True)
|
|
195
|
+
Xtr, Xte, ytr, yte = train_test_split(
|
|
196
|
+
X, y, test_size=0.30, stratify=y, random_state=42)
|
|
197
|
+
|
|
198
|
+
model = make_pipeline(StandardScaler(),
|
|
199
|
+
SVC(kernel='linear', C=1))
|
|
200
|
+
model.fit(Xtr, ytr)
|
|
201
|
+
print(model[-1].n_support_) # [ 2 10 9]
|
|
202
|
+
from sklearn.metrics import (accuracy_score,
|
|
203
|
+
confusion_matrix)
|
|
204
|
+
pred = model.predict(Xte)
|
|
205
|
+
print(accuracy_score(yte, pred)) # 0.9111
|
|
206
|
+
print(confusion_matrix(yte, pred))
|
|
207
|
+
# [[15 0 0]
|
|
208
|
+
# [ 0 14 1]
|
|
209
|
+
# [ 0 3 12]]
|
|
210
|
+
print(model.score(Xtr, ytr)) # 0.9714 (train)
|
|
211
|
+
|
|
212
|
+
rbf = make_pipeline(StandardScaler(),
|
|
213
|
+
SVC(kernel='rbf'))
|
|
214
|
+
print(rbf.fit(Xtr, ytr).score(Xte, yte)) # 0.9333
|
|
215
|
+
from sklearn.model_selection import (
|
|
216
|
+
GridSearchCV, StratifiedKFold)
|
|
217
|
+
cv = StratifiedKFold(5, shuffle=True, random_state=0)
|
|
218
|
+
grid = {'svc__C': [0.1, 1, 10, 100],
|
|
219
|
+
'svc__gamma': [0.01, 0.1, 1, 10]}
|
|
220
|
+
pipe = make_pipeline(StandardScaler(),
|
|
221
|
+
SVC(kernel='rbf'))
|
|
222
|
+
gs = GridSearchCV(pipe, grid, cv=cv).fit(Xtr, ytr)
|
|
223
|
+
|
|
224
|
+
print(gs.best_params_) # C = 1, gamma = 0.1
|
|
225
|
+
print(gs.best_score_) # 0.981
|
|
226
|
+
print(gs.score(Xte, yte)) # 0.9111
|
|
227
|
+
P = np.array([[1, 1], [0, 0], [1, 0],
|
|
228
|
+
[3, 3], [4, 3], [3, 5]])
|
|
229
|
+
t = np.array([-1, -1, -1, 1, 1, 1])
|
|
230
|
+
|
|
231
|
+
svm = SVC(kernel='linear', C=1e6).fit(P, t) # hard margin
|
|
232
|
+
w, b = svm.coef_[0], svm.intercept_[0]
|
|
233
|
+
print(w, b) # [0.5 0.5] -2.0
|
|
234
|
+
print(svm.support_vectors_) # [[1. 1.] [3. 3.]]
|
|
235
|
+
print(2 / np.linalg.norm(w)) # 2.83 (margin)
|
|
236
|
+
|
|
237
|
+
q = np.array([[2, 3], [1, 2]])
|
|
238
|
+
print(q @ w + b) # [ 0.5 -0.5]
|
|
239
|
+
print(svm.decision_function(q)) # [ 0.5 -0.5]
|
|
240
|
+
from sklearn.datasets import load_wine
|
|
241
|
+
Xw, yw = load_wine(return_X_y=True)
|
|
242
|
+
a, b, c, d = train_test_split(Xw, yw, test_size=0.30,
|
|
243
|
+
stratify=yw, random_state=42)
|
|
244
|
+
raw = SVC().fit(a, c) # RBF, no scaling
|
|
245
|
+
std = make_pipeline(StandardScaler(), SVC()).fit(a, c)
|
|
246
|
+
print(raw.score(b, d), std.score(b, d)) # 0.667 0.981
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
WITH DATA SET
|
|
250
|
+
LR
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
import pandas as pd
|
|
254
|
+
import numpy as np
|
|
255
|
+
import matplotlib.pyplot as plt
|
|
256
|
+
|
|
257
|
+
from sklearn.model_selection import train_test_split
|
|
258
|
+
from sklearn.linear_model import LinearRegression
|
|
259
|
+
from sklearn.metrics import mean_squared_error, r2_score
|
|
260
|
+
|
|
261
|
+
# Load dataset
|
|
262
|
+
df = pd.read_csv("data.csv")
|
|
263
|
+
|
|
264
|
+
# Select features and target
|
|
265
|
+
X = df.drop("target", axis=1)
|
|
266
|
+
y = df["target"]
|
|
267
|
+
|
|
268
|
+
# Select numerical features
|
|
269
|
+
X = X.select_dtypes(include=np.number)
|
|
270
|
+
|
|
271
|
+
# Split dataset
|
|
272
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
273
|
+
X, y, test_size=0.2, random_state=42
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
# Train model
|
|
277
|
+
model = LinearRegression()
|
|
278
|
+
model.fit(X_train, y_train)
|
|
279
|
+
|
|
280
|
+
# Prediction
|
|
281
|
+
y_pred = model.predict(X_test)
|
|
282
|
+
|
|
283
|
+
# Evaluation
|
|
284
|
+
mse = mean_squared_error(y_test, y_pred)
|
|
285
|
+
r2 = r2_score(y_test, y_pred)
|
|
286
|
+
|
|
287
|
+
print("Coefficients:", model.coef_)
|
|
288
|
+
print("Intercept:", model.intercept_)
|
|
289
|
+
print("MSE:", mse)
|
|
290
|
+
print("R2 Score:", r2)
|
|
291
|
+
|
|
292
|
+
# Plot
|
|
293
|
+
plt.scatter(y_test, y_pred)
|
|
294
|
+
plt.xlabel("Actual Values")
|
|
295
|
+
plt.ylabel("Predicted Values")
|
|
296
|
+
plt.title("Linear Regression")
|
|
297
|
+
plt.show()
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
GD
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
import pandas as pd
|
|
304
|
+
import numpy as np
|
|
305
|
+
import matplotlib.pyplot as plt
|
|
306
|
+
|
|
307
|
+
from sklearn.preprocessing import StandardScaler
|
|
308
|
+
|
|
309
|
+
# Load dataset
|
|
310
|
+
df = pd.read_csv("data.csv")
|
|
311
|
+
|
|
312
|
+
X = df.drop("target", axis=1).select_dtypes(include=np.number).values
|
|
313
|
+
y = df["target"].values.reshape(-1, 1)
|
|
314
|
+
|
|
315
|
+
# Feature scaling
|
|
316
|
+
scaler = StandardScaler()
|
|
317
|
+
X = scaler.fit_transform(X)
|
|
318
|
+
|
|
319
|
+
# Add intercept
|
|
320
|
+
X = np.c_[np.ones(X.shape[0]), X]
|
|
321
|
+
|
|
322
|
+
# Initialize parameters
|
|
323
|
+
theta = np.zeros((X.shape[1], 1))
|
|
324
|
+
|
|
325
|
+
alpha = 0.01
|
|
326
|
+
iterations = 1000
|
|
327
|
+
m = len(y)
|
|
328
|
+
|
|
329
|
+
# Gradient Descent
|
|
330
|
+
for i in range(iterations):
|
|
331
|
+
|
|
332
|
+
y_pred = X @ theta
|
|
333
|
+
|
|
334
|
+
error = y_pred - y
|
|
335
|
+
|
|
336
|
+
gradient = (1/m) * (X.T @ error)
|
|
337
|
+
|
|
338
|
+
theta = theta - alpha * gradient
|
|
339
|
+
|
|
340
|
+
# Final predictions
|
|
341
|
+
y_pred = X @ theta
|
|
342
|
+
|
|
343
|
+
# Evaluation
|
|
344
|
+
mse = np.mean((y - y_pred)**2)
|
|
345
|
+
|
|
346
|
+
print("Theta:", theta)
|
|
347
|
+
print("MSE:", mse)
|
|
348
|
+
|
|
349
|
+
# Plot cost
|
|
350
|
+
plt.scatter(y, y_pred)
|
|
351
|
+
plt.xlabel("Actual Values")
|
|
352
|
+
plt.ylabel("Predicted Values")
|
|
353
|
+
plt.title("Gradient Descent Regression")
|
|
354
|
+
plt.show()
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
DT
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
import pandas as pd
|
|
361
|
+
import matplotlib.pyplot as plt
|
|
362
|
+
|
|
363
|
+
from sklearn.model_selection import train_test_split
|
|
364
|
+
from sklearn.tree import DecisionTreeClassifier, plot_tree
|
|
365
|
+
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
|
366
|
+
|
|
367
|
+
# Load dataset
|
|
368
|
+
df = pd.read_csv("data.csv")
|
|
369
|
+
|
|
370
|
+
# Features and target
|
|
371
|
+
X = df.drop("target", axis=1)
|
|
372
|
+
y = df["target"]
|
|
373
|
+
|
|
374
|
+
# Convert categorical input features to numerical
|
|
375
|
+
X = pd.get_dummies(X, drop_first=False)
|
|
376
|
+
|
|
377
|
+
# Split data
|
|
378
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
379
|
+
X, y, test_size=0.2, random_state=42
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
# Create model
|
|
383
|
+
model = DecisionTreeClassifier(
|
|
384
|
+
criterion="gini",
|
|
385
|
+
max_depth=4,
|
|
386
|
+
random_state=42
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
# Train
|
|
390
|
+
model.fit(X_train, y_train)
|
|
391
|
+
|
|
392
|
+
# Predict
|
|
393
|
+
y_pred = model.predict(X_test)
|
|
394
|
+
|
|
395
|
+
# Evaluation
|
|
396
|
+
print("Accuracy:", accuracy_score(y_test, y_pred))
|
|
397
|
+
print("Confusion Matrix:")
|
|
398
|
+
print(confusion_matrix(y_test, y_pred))
|
|
399
|
+
|
|
400
|
+
print("Classification Report:")
|
|
401
|
+
print(classification_report(y_test, y_pred))
|
|
402
|
+
|
|
403
|
+
# Feature Importance
|
|
404
|
+
print("Feature Importance:")
|
|
405
|
+
print(model.feature_importances_)
|
|
406
|
+
|
|
407
|
+
# Plot tree
|
|
408
|
+
plt.figure(figsize=(15, 8))
|
|
409
|
+
|
|
410
|
+
plot_tree(
|
|
411
|
+
model,
|
|
412
|
+
feature_names=X.columns,
|
|
413
|
+
class_names=[str(c) for c in model.classes_],
|
|
414
|
+
filled=True
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
plt.title("Decision Tree")
|
|
418
|
+
plt.show()
|
|
419
|
+
|
|
420
|
+
PCA
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
import pandas as pd
|
|
424
|
+
import numpy as np
|
|
425
|
+
import matplotlib.pyplot as plt
|
|
426
|
+
|
|
427
|
+
from sklearn.preprocessing import StandardScaler
|
|
428
|
+
from sklearn.decomposition import PCA
|
|
429
|
+
|
|
430
|
+
df = pd.read_csv("data.csv")
|
|
431
|
+
|
|
432
|
+
# Change target to your target column
|
|
433
|
+
X = df.drop("target", axis=1)
|
|
434
|
+
|
|
435
|
+
# Select numerical input features
|
|
436
|
+
X = X.select_dtypes(include=np.number)
|
|
437
|
+
|
|
438
|
+
# Scaling
|
|
439
|
+
scaler = StandardScaler()
|
|
440
|
+
X_scaled = scaler.fit_transform(X)
|
|
441
|
+
|
|
442
|
+
# PCA
|
|
443
|
+
pca = PCA(n_components=2)
|
|
444
|
+
X_pca = pca.fit_transform(X_scaled)
|
|
445
|
+
|
|
446
|
+
# Results
|
|
447
|
+
print("Transformed Data:")
|
|
448
|
+
print(X_pca)
|
|
449
|
+
|
|
450
|
+
print("Explained Variance Ratio:")
|
|
451
|
+
print(pca.explained_variance_ratio_)
|
|
452
|
+
|
|
453
|
+
print("Total Explained Variance:")
|
|
454
|
+
print(np.sum(pca.explained_variance_ratio_))
|
|
455
|
+
|
|
456
|
+
# Plot
|
|
457
|
+
plt.scatter(X_pca[:, 0], X_pca[:, 1])
|
|
458
|
+
|
|
459
|
+
plt.xlabel("PC1")
|
|
460
|
+
plt.ylabel("PC2")
|
|
461
|
+
plt.title("PCA Visualization")
|
|
462
|
+
plt.grid()
|
|
463
|
+
plt.show()
|
|
464
|
+
|
|
465
|
+
LDA
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
import pandas as pd
|
|
469
|
+
import numpy as np
|
|
470
|
+
import matplotlib.pyplot as plt
|
|
471
|
+
|
|
472
|
+
from sklearn.preprocessing import StandardScaler
|
|
473
|
+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
|
|
474
|
+
|
|
475
|
+
df = pd.read_csv("data.csv")
|
|
476
|
+
|
|
477
|
+
X = df.drop("target", axis=1)
|
|
478
|
+
y = df["target"]
|
|
479
|
+
|
|
480
|
+
X = pd.get_dummies(X, drop_first=False)
|
|
481
|
+
X = X.select_dtypes(include=np.number)
|
|
482
|
+
|
|
483
|
+
scaler = StandardScaler()
|
|
484
|
+
X_scaled = scaler.fit_transform(X)
|
|
485
|
+
|
|
486
|
+
# Automatically choose valid number of components
|
|
487
|
+
n_components = min(2, X.shape[1], len(np.unique(y)) - 1)
|
|
488
|
+
|
|
489
|
+
lda = LinearDiscriminantAnalysis(n_components=n_components)
|
|
490
|
+
|
|
491
|
+
X_lda = lda.fit_transform(X_scaled, y)
|
|
492
|
+
|
|
493
|
+
print("Number of components:", n_components)
|
|
494
|
+
print("LDA transformed data:")
|
|
495
|
+
print(X_lda)
|
|
496
|
+
|
|
497
|
+
# Plot
|
|
498
|
+
if n_components == 2:
|
|
499
|
+
|
|
500
|
+
for cls in np.unique(y):
|
|
501
|
+
plt.scatter(
|
|
502
|
+
X_lda[y == cls, 0],
|
|
503
|
+
X_lda[y == cls, 1],
|
|
504
|
+
label=str(cls)
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
plt.xlabel("LD1")
|
|
508
|
+
plt.ylabel("LD2")
|
|
509
|
+
plt.legend()
|
|
510
|
+
plt.title("LDA Visualization")
|
|
511
|
+
plt.grid()
|
|
512
|
+
plt.show()
|
|
513
|
+
|
|
514
|
+
else:
|
|
515
|
+
|
|
516
|
+
for cls in np.unique(y):
|
|
517
|
+
plt.scatter(
|
|
518
|
+
X_lda[y == cls, 0],
|
|
519
|
+
np.zeros(np.sum(y == cls)),
|
|
520
|
+
label=str(cls)
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
plt.xlabel("LD1")
|
|
524
|
+
plt.title("One-Dimensional LDA")
|
|
525
|
+
plt.legend()
|
|
526
|
+
plt.grid()
|
|
527
|
+
plt.show()
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
KNN
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
import pandas as pd
|
|
534
|
+
import numpy as np
|
|
535
|
+
import matplotlib.pyplot as plt
|
|
536
|
+
|
|
537
|
+
from sklearn.model_selection import train_test_split, cross_val_score
|
|
538
|
+
from sklearn.preprocessing import StandardScaler
|
|
539
|
+
from sklearn.neighbors import KNeighborsClassifier
|
|
540
|
+
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
|
541
|
+
|
|
542
|
+
# Load dataset
|
|
543
|
+
df = pd.read_csv("data.csv")
|
|
544
|
+
|
|
545
|
+
# Features and target
|
|
546
|
+
X = df.drop("target", axis=1)
|
|
547
|
+
y = df["target"]
|
|
548
|
+
|
|
549
|
+
# Convert categorical features
|
|
550
|
+
X = pd.get_dummies(X, drop_first=False)
|
|
551
|
+
|
|
552
|
+
# Train-test split
|
|
553
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
554
|
+
X, y, test_size=0.2, random_state=42
|
|
555
|
+
)
|
|
556
|
+
|
|
557
|
+
# Feature scaling
|
|
558
|
+
scaler = StandardScaler()
|
|
559
|
+
|
|
560
|
+
X_train = scaler.fit_transform(X_train)
|
|
561
|
+
X_test = scaler.transform(X_test)
|
|
562
|
+
|
|
563
|
+
# KNN model
|
|
564
|
+
model = KNeighborsClassifier(n_neighbors=5)
|
|
565
|
+
|
|
566
|
+
# Training
|
|
567
|
+
model.fit(X_train, y_train)
|
|
568
|
+
|
|
569
|
+
# Prediction
|
|
570
|
+
y_pred = model.predict(X_test)
|
|
571
|
+
|
|
572
|
+
# Evaluation
|
|
573
|
+
print("Accuracy:", accuracy_score(y_test, y_pred))
|
|
574
|
+
|
|
575
|
+
print("Confusion Matrix:")
|
|
576
|
+
print(confusion_matrix(y_test, y_pred))
|
|
577
|
+
|
|
578
|
+
print("Classification Report:")
|
|
579
|
+
print(classification_report(y_test, y_pred))
|
|
580
|
+
|
|
581
|
+
# Try different K values
|
|
582
|
+
k_values = range(1, 11)
|
|
583
|
+
scores = []
|
|
584
|
+
|
|
585
|
+
for k in k_values:
|
|
586
|
+
|
|
587
|
+
knn = KNeighborsClassifier(n_neighbors=k)
|
|
588
|
+
|
|
589
|
+
score = cross_val_score(
|
|
590
|
+
knn, X_train, y_train, cv=5
|
|
591
|
+
).mean()
|
|
592
|
+
|
|
593
|
+
scores.append(score)
|
|
594
|
+
|
|
595
|
+
# Plot K vs accuracy
|
|
596
|
+
plt.plot(k_values, scores, marker="o")
|
|
597
|
+
|
|
598
|
+
plt.xlabel("K Value")
|
|
599
|
+
plt.ylabel("Cross Validation Accuracy")
|
|
600
|
+
plt.title("Finding Optimal K")
|
|
601
|
+
|
|
602
|
+
plt.grid()
|
|
603
|
+
plt.show()
|
|
604
|
+
|
|
605
|
+
# Best K
|
|
606
|
+
best_k = list(k_values)[np.argmax(scores)]
|
|
607
|
+
|
|
608
|
+
print("Best K:", best_k)
|
|
609
|
+
print("Best Accuracy:", max(scores))
|
|
610
|
+
|
|
611
|
+
SVM
|
|
612
|
+
|
|
613
|
+
import pandas as pd
|
|
614
|
+
import numpy as np
|
|
615
|
+
import matplotlib.pyplot as plt
|
|
616
|
+
|
|
617
|
+
from sklearn.model_selection import train_test_split
|
|
618
|
+
from sklearn.preprocessing import StandardScaler
|
|
619
|
+
|
|
620
|
+
from sklearn.svm import SVC
|
|
621
|
+
|
|
622
|
+
from sklearn.metrics import (
|
|
623
|
+
accuracy_score,
|
|
624
|
+
confusion_matrix,
|
|
625
|
+
classification_report
|
|
626
|
+
)
|
|
627
|
+
|
|
628
|
+
# Load dataset
|
|
629
|
+
df = pd.read_csv("data.csv")
|
|
630
|
+
|
|
631
|
+
# Features and target
|
|
632
|
+
X = df.drop("target", axis=1)
|
|
633
|
+
y = df["target"]
|
|
634
|
+
|
|
635
|
+
# Convert categorical input features
|
|
636
|
+
X = pd.get_dummies(X, drop_first=False)
|
|
637
|
+
|
|
638
|
+
# Split data
|
|
639
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
640
|
+
X, y, test_size=0.2, random_state=42
|
|
641
|
+
)
|
|
642
|
+
|
|
643
|
+
# Scaling
|
|
644
|
+
scaler = StandardScaler()
|
|
645
|
+
|
|
646
|
+
X_train = scaler.fit_transform(X_train)
|
|
647
|
+
X_test = scaler.transform(X_test)
|
|
648
|
+
|
|
649
|
+
# SVM model
|
|
650
|
+
model = SVC(kernel="rbf", C=1.0, gamma="scale")
|
|
651
|
+
|
|
652
|
+
# Training
|
|
653
|
+
model.fit(X_train, y_train)
|
|
654
|
+
|
|
655
|
+
# Prediction
|
|
656
|
+
y_pred = model.predict(X_test)
|
|
657
|
+
|
|
658
|
+
# Evaluation
|
|
659
|
+
print("Accuracy:", accuracy_score(y_test, y_pred))
|
|
660
|
+
|
|
661
|
+
print("Confusion Matrix:")
|
|
662
|
+
print(confusion_matrix(y_test, y_pred))
|
|
663
|
+
|
|
664
|
+
print("Classification Report:")
|
|
665
|
+
print(classification_report(y_test, y_pred))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prajvaltools
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
def open_notes():
|
|
4
|
+
# First, look inside the installed library directory
|
|
5
|
+
file_path = Path(__file__).parent / "mynotes.txt"
|
|
6
|
+
|
|
7
|
+
# If not found, look inside the original Downloads folder
|
|
8
|
+
if not file_path.exists():
|
|
9
|
+
file_path = Path.home() / "Downloads" / "PrajvalLibrary" / "mynotes.txt"
|
|
10
|
+
|
|
11
|
+
if not file_path.exists():
|
|
12
|
+
print("mynotes.txt was not found!")
|
|
13
|
+
print("Checked locations:")
|
|
14
|
+
print(Path(__file__).parent / "mynotes.txt"
|
|
15
|
+
)
|
|
16
|
+
print(Path.home() / "Downloads" / "PrajvalLibrary" / "mynotes.txt")
|
|
17
|
+
return
|
|
18
|
+
|
|
19
|
+
content = file_path.read_text(encoding="utf-8")
|
|
20
|
+
print(content)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "prajvaltools"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "My personal Python library"
|
|
9
|
+
authors = [{ name = "Prajval Balte" }]
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
|
|
12
|
+
[tool.setuptools]
|
|
13
|
+
py-modules = ["prajvaltools"]
|
|
14
|
+
|
|
15
|
+
[tool.setuptools.data-files]
|
|
16
|
+
"." = ["mynotes.txt"]
|