forked from trekhleb/homemade-machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
71 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
"""Add polynomial features to the features set""" | ||
|
||
import numpy as np | ||
|
||
|
||
def add_polynomials(x1, x2, polynomial_degree): | ||
def add_polynomials(dataset_1, dataset_2, polynomial_degree): | ||
"""Extends data set with polynomial features of certain degree. | ||
Returns a new feature array with more features, comprising of | ||
x1, x2, x1^2, x2^2, x1*x2, x1*x2^2, etc. | ||
:param x1: first data set. | ||
:param x2: second data set. | ||
:param dataset_1: first data set. | ||
:param dataset_2: second data set. | ||
:param polynomial_degree: the max power of new features. | ||
""" | ||
|
||
polynomials = np.empty((x1.shape[0], 0)) | ||
polynomials = np.empty((dataset_1.shape[0], 0)) | ||
|
||
for i in range(1, polynomial_degree + 1): | ||
for j in range(i + 1): | ||
polynomial_feature = (x1 ** (i - j)) * (x2 ** j) | ||
polynomial_feature = (dataset_1 ** (i - j)) * (dataset_2 ** j) | ||
polynomials = np.concatenate((polynomials, polynomial_feature), axis=1) | ||
|
||
return polynomials |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
"""Add sinusoid features to the features set""" | ||
|
||
import numpy as np | ||
|
||
|
||
def add_sinusoids(x, sinusoid_degree): | ||
def add_sinusoids(dataset, sinusoid_degree): | ||
"""Extends data set with sinusoid features. | ||
Returns a new feature array with more features, comprising of | ||
sin(x). | ||
:param x: data set. | ||
:param dataset: data set. | ||
:param sinusoid_degree: multiplier for sinusoid parameter multiplications | ||
""" | ||
|
||
sinusoids = np.empty((x.shape[0], 0)) | ||
sinusoids = np.empty((dataset.shape[0], 0)) | ||
|
||
for degree in range(1, sinusoid_degree): | ||
sinusoid_features = np.sin(degree * x) | ||
sinusoid_features = np.sin(degree * dataset) | ||
sinusoids = np.concatenate((sinusoids, sinusoid_features), axis=1) | ||
|
||
return sinusoids |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"""Normalize features""" | ||
|
||
import numpy as np | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
"""Sigmoid function""" | ||
|
||
import numpy as np | ||
|
||
|
||
def sigmoid(z): | ||
def sigmoid(matrix): | ||
"""Applies sigmoid function to NumPy matrix""" | ||
return 1 / (1 + np.exp(-z)) | ||
|
||
return 1 / (1 + np.exp(-matrix)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
"""Sigmoid gradient function""" | ||
|
||
from .sigmoid import sigmoid | ||
|
||
|
||
def sigmoid_gradient(z): | ||
def sigmoid_gradient(matrix): | ||
"""Computes the gradient of the sigmoid function evaluated at z.""" | ||
|
||
return sigmoid(z) * (1 - sigmoid(z)) | ||
return sigmoid(matrix) * (1 - sigmoid(matrix)) |
98 changes: 21 additions & 77 deletions
98
notebooks/anomaly_detection/anomaly_detection_gaussian_demo.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.