Repository navigation
Expand file tree
/
Copy pathloss_functions.py
More file actions
100 lines (78 loc) · 2.38 KB
/
Copy pathloss_functions.py
File metadata and controls
100 lines (78 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np
def mean_square_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
"""
Calculate MSE loss
Parameters
----------
y_true: ndarray of shape (n_samples, )
True response values
y_pred: ndarray of shape (n_samples, )
Predicted response values
Returns
-------
MSE of given predictions
"""
return np.mean((y_true - y_pred)**2)
def misclassification_error(y_true: np.ndarray, y_pred: np.ndarray, normalize: bool = True) -> float:
"""
Calculate misclassification loss
Parameters
----------
y_true: ndarray of shape (n_samples, )
True response values
y_pred: ndarray of shape (n_samples, )
Predicted response values
normalize: bool, default = True
Normalize by number of samples or not
Returns
-------
Misclassification of given predictions
"""
score = np.sum(y_true != y_pred)
return score / len(y_true) if normalize else score
def accuracy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
"""
Calculate accuracy of given predictions
Parameters
----------
y_true: ndarray of shape (n_samples, )
True response values
y_pred: ndarray of shape (n_samples, )
Predicted response values
Returns
-------
Accuracy of given predictions
"""
score = np.sum(y_true == y_pred)
return score / len(y_true)
def cross_entropy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
"""
Calculate the cross entropy of given predictions
Parameters
----------
y_true: ndarray of shape (n_samples, )
True response values
y_pred: ndarray of shape (n_samples, )
Predicted response values
Returns
-------
Cross entropy of given predictions
"""
if len(y_true.shape) == 2:
correct_class_prob = (y_pred * y_true).sum(axis=1)
else:
correct_class_prob = y_pred[np.arange(len(y_true)), y_true]
return -np.log(correct_class_prob + 1e-10).mean()
def softmax(X: np.ndarray) -> np.ndarray:
"""
Compute the Softmax function for each sample in given data
Parameters:
-----------
X: ndarray of shape (n_samples, n_features)
Returns:
--------
output: ndarray of shape (n_samples, n_features)
Softmax(x) for every sample x in given data X
"""
exp_X = np.exp(X)
return exp_X / exp_X.sum(axis=1, keepdims=True)