Spaces:
Sleeping
Sleeping
Commit
·
03176c2
1
Parent(s):
9e506b7
adding accuracy score metric
Browse files- example/main.py +18 -6
- example/mushrooms.csv +0 -0
- example/test.py +0 -0
- nn/nn.py +2 -8
- nn/train.py +15 -2
example/main.py
CHANGED
@@ -1,17 +1,29 @@
|
|
1 |
import requests
|
2 |
|
3 |
-
with open("
|
4 |
-
|
|
|
|
|
5 |
|
6 |
ARGS = {
|
7 |
-
"epochs":
|
8 |
"hidden_size": 8,
|
9 |
"learning_rate": 0.0001,
|
10 |
"test_size": 0.1,
|
11 |
"activation": "relu",
|
12 |
-
"features": [
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
}
|
16 |
|
17 |
if __name__ == "__main__":
|
|
|
1 |
import requests
|
2 |
|
3 |
+
with open("mushrooms.csv", "rb") as csv:
|
4 |
+
data = csv.read()
|
5 |
+
|
6 |
+
# class,cap-shape,cap-surface,cap-color,bruises,odor,gill-attachment,gill-spacing,gill-size,gill-color,stalk-shape,stalk-root,stalk-surface-above-ring,stalk-surface-below-ring,stalk-color-above-ring,stalk-color-below-ring,veil-type,veil-color,ring-number,ring-type,spore-print-color,population,habitat
|
7 |
|
8 |
ARGS = {
|
9 |
+
"epochs": 1_000,
|
10 |
"hidden_size": 8,
|
11 |
"learning_rate": 0.0001,
|
12 |
"test_size": 0.1,
|
13 |
"activation": "relu",
|
14 |
+
"features": [
|
15 |
+
"cap-shape",
|
16 |
+
"cap-surface",
|
17 |
+
"cap-color",
|
18 |
+
"bruises",
|
19 |
+
"odor",
|
20 |
+
"gill-attachment",
|
21 |
+
"gill-spacing",
|
22 |
+
"gill-size",
|
23 |
+
"gill-color",
|
24 |
+
],
|
25 |
+
"target": "class",
|
26 |
+
"data": data.decode("utf-8"),
|
27 |
}
|
28 |
|
29 |
if __name__ == "__main__":
|
example/mushrooms.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
example/test.py
DELETED
File without changes
|
nn/nn.py
CHANGED
@@ -26,7 +26,6 @@ class NN:
|
|
26 |
self.loss_hist: list[float] = None
|
27 |
self.func_prime: Callable = None
|
28 |
self.func: Callable = None
|
29 |
-
self.df: pd.DataFrame = None
|
30 |
self.X: pd.DataFrame = None
|
31 |
self.y: pd.DataFrame = None
|
32 |
self.y_dummy: pd.DataFrame = None
|
@@ -34,16 +33,11 @@ class NN:
|
|
34 |
self.output_size: int = None
|
35 |
|
36 |
def set_df(self, df: pd.DataFrame) -> None:
|
37 |
-
|
38 |
-
# issue right now here because we need a way to convert
|
39 |
-
# back and forth from dummies and non dummy vars
|
40 |
-
|
41 |
assert isinstance(df, pd.DataFrame)
|
42 |
-
self.df = df
|
43 |
-
self.y = df[self.target]
|
44 |
x = df[self.features]
|
45 |
-
|
46 |
self.X = pd.get_dummies(x, columns=self.features)
|
|
|
47 |
self.input_size = len(self.X.columns)
|
48 |
self.output_size = len(self.y_dummy.columns)
|
49 |
|
|
|
26 |
self.loss_hist: list[float] = None
|
27 |
self.func_prime: Callable = None
|
28 |
self.func: Callable = None
|
|
|
29 |
self.X: pd.DataFrame = None
|
30 |
self.y: pd.DataFrame = None
|
31 |
self.y_dummy: pd.DataFrame = None
|
|
|
33 |
self.output_size: int = None
|
34 |
|
35 |
def set_df(self, df: pd.DataFrame) -> None:
|
|
|
|
|
|
|
|
|
36 |
assert isinstance(df, pd.DataFrame)
|
|
|
|
|
37 |
x = df[self.features]
|
38 |
+
y = df[self.target]
|
39 |
self.X = pd.get_dummies(x, columns=self.features)
|
40 |
+
self.y_dummy = pd.get_dummies(y, columns=self.target)
|
41 |
self.input_size = len(self.X.columns)
|
42 |
self.output_size = len(self.y_dummy.columns)
|
43 |
|
nn/train.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from sklearn.model_selection import train_test_split
|
2 |
-
from sklearn.metrics import log_loss
|
3 |
from typing import Callable
|
4 |
from nn.nn import NN
|
5 |
import numpy as np
|
@@ -81,7 +81,8 @@ def train(nn: NN) -> dict:
|
|
81 |
)
|
82 |
|
83 |
return {
|
84 |
-
"log loss": log_loss(y_true=y_test, y_pred=y_hat)
|
|
|
85 |
}
|
86 |
|
87 |
|
@@ -107,3 +108,15 @@ def hidden_weight_prime(data, error):
|
|
107 |
|
108 |
def output_weight_prime(hidden_output, error):
|
109 |
return np.dot(hidden_output.T, error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from sklearn.model_selection import train_test_split
|
2 |
+
from sklearn.metrics import log_loss
|
3 |
from typing import Callable
|
4 |
from nn.nn import NN
|
5 |
import numpy as np
|
|
|
81 |
)
|
82 |
|
83 |
return {
|
84 |
+
"log loss": log_loss(y_true=y_test, y_pred=y_hat),
|
85 |
+
"accuracy": accuracy_score(y_true=y_test, y_pred=y_hat)
|
86 |
}
|
87 |
|
88 |
|
|
|
108 |
|
109 |
def output_weight_prime(hidden_output, error):
|
110 |
return np.dot(hidden_output.T, error)
|
111 |
+
|
112 |
+
|
113 |
+
def accuracy_score(y_true, y_pred):
|
114 |
+
# Ensure y_true and y_pred have the same shape
|
115 |
+
if y_true.shape != y_pred.shape:
|
116 |
+
raise ValueError("Input shapes do not match.")
|
117 |
+
|
118 |
+
# Calculate the accuracy
|
119 |
+
num_samples = len(y_true)
|
120 |
+
num_correct = np.sum(y_true == y_pred)
|
121 |
+
|
122 |
+
return num_correct / num_samples
|