mgpylib 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.
- mgpylib-0.1.0/PKG-INFO +10 -0
- mgpylib-0.1.0/README.md +0 -0
- mgpylib-0.1.0/pyproject.toml +21 -0
- mgpylib-0.1.0/setup.cfg +4 -0
- mgpylib-0.1.0/src/mgpylib/__init__.py +0 -0
- mgpylib-0.1.0/src/mgpylib/main.py +64 -0
- mgpylib-0.1.0/src/mgpylib.egg-info/PKG-INFO +10 -0
- mgpylib-0.1.0/src/mgpylib.egg-info/SOURCES.txt +8 -0
- mgpylib-0.1.0/src/mgpylib.egg-info/dependency_links.txt +1 -0
- mgpylib-0.1.0/src/mgpylib.egg-info/top_level.txt +1 -0
mgpylib-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mgpylib
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: python library C-1
|
|
5
|
+
Author-email: mgmg19 <mgmuraliganesh@email.com>
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.7
|
|
10
|
+
Description-Content-Type: text/markdown
|
mgpylib-0.1.0/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mgpylib"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="mgmg19", email="mgmuraliganesh@email.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "python library C-1"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.7"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[tool.setuptools.packages.find]
|
|
21
|
+
where = ["src"]
|
mgpylib-0.1.0/setup.cfg
ADDED
|
File without changes
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#import numpy as np
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
from sklearn.datasets import load_iris
|
|
4
|
+
from sklearn.model_selection import train_test_split
|
|
5
|
+
from sklearn.preprocessing import StandardScaler
|
|
6
|
+
from sklearn.metrics import accuracy_score, f1_score
|
|
7
|
+
from collections import Counter
|
|
8
|
+
|
|
9
|
+
def euclidean_distance(x1, x2):
|
|
10
|
+
return np.sqrt(np.sum((x1 - x2) ** 2))
|
|
11
|
+
|
|
12
|
+
class KNN:
|
|
13
|
+
def __init__(self, k=3, weighted=False):
|
|
14
|
+
self.k = k
|
|
15
|
+
self.weighted = weighted
|
|
16
|
+
|
|
17
|
+
def fit(self, X_train, y_train):
|
|
18
|
+
self.X_train = X_train
|
|
19
|
+
self.y_train = y_train
|
|
20
|
+
|
|
21
|
+
def predict(self, X_test):
|
|
22
|
+
predictions = [self._predict(x) for x in X_test]
|
|
23
|
+
return np.array(predictions)
|
|
24
|
+
|
|
25
|
+
def _predict(self, x):
|
|
26
|
+
distances = [euclidean_distance(x, x_train) for x_train in self.X_train]
|
|
27
|
+
k_indices = np.argsort(distances)[:self.k]
|
|
28
|
+
k_nearest_labels = [self.y_train[i] for i in k_indices]
|
|
29
|
+
|
|
30
|
+
if self.weighted:
|
|
31
|
+
weights = [1 / (distances[i] ** 2 + 1e-5) for i in k_indices]
|
|
32
|
+
class_votes = {}
|
|
33
|
+
for label, weight in zip(k_nearest_labels, weights):
|
|
34
|
+
class_votes[label] = class_votes.get(label, 0) + weight
|
|
35
|
+
return max(class_votes, key=class_votes.get)
|
|
36
|
+
else:
|
|
37
|
+
most_common = Counter(k_nearest_labels).most_common(1)
|
|
38
|
+
return most_common[0][0]
|
|
39
|
+
|
|
40
|
+
iris = load_iris()
|
|
41
|
+
X, y = iris.data, iris.target
|
|
42
|
+
|
|
43
|
+
scaler = StandardScaler()
|
|
44
|
+
X = scaler.fit_transform(X)
|
|
45
|
+
|
|
46
|
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
47
|
+
|
|
48
|
+
k_values = [1, 3, 5]
|
|
49
|
+
|
|
50
|
+
for k in k_values:
|
|
51
|
+
knn = KNN(k=k, weighted=False)
|
|
52
|
+
knn.fit(X_train, y_train)
|
|
53
|
+
y_pred = knn.predict(X_test)
|
|
54
|
+
acc = accuracy_score(y_test, y_pred)
|
|
55
|
+
f1 = f1_score(y_test, y_pred, average='weighted')
|
|
56
|
+
print(f'k={k}, Regular k-NN -> Accuracy: {acc:.4f}, F1-score: {f1:.4f}')
|
|
57
|
+
|
|
58
|
+
for k in k_values:
|
|
59
|
+
knn_weighted = KNN(k=k, weighted=True)
|
|
60
|
+
knn_weighted.fit(X_train, y_train)
|
|
61
|
+
y_pred_weighted = knn_weighted.predict(X_test)
|
|
62
|
+
acc_weighted = accuracy_score(y_test, y_pred_weighted)
|
|
63
|
+
f1_weighted = f1_score(y_test, y_pred_weighted, average='weighted')
|
|
64
|
+
print(f'k={k}, Weighted k-NN -> Accuracy: {acc_weighted:.4f}, F1-score: {f1_weighted:.4f}')
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mgpylib
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: python library C-1
|
|
5
|
+
Author-email: mgmg19 <mgmuraliganesh@email.com>
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.7
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mgpylib
|