Spaces:
Build error
Build error
Commit ·
da3cde4
1
Parent(s): f308820
going to use this branch to re make this program in go using the gota
Browse filespackage. Code will be cleaner and we will be able to add more features
easier.
Files changed (26)
- .gitignore+18-126
- LICENSEDeleted-21
- README.mdDeleted-52
- app.pyDeleted-44
- cluster/clusterer.pyDeleted-22
- cluster/distance.pyDeleted-19
- cluster/heirarchical.pyDeleted
- cluster/kmeans.pyDeleted-78
- cluster/kmedoids.pyDeleted-15
- cluster/main.pyDeleted-22
- cluster/opts.pyDeleted-8
- cluster/plot.pyDeleted-39
- dataset/iris.pyDeleted-14
- example/kmeans.pyDeleted-31
- example/kmedoids.pyDeleted
- example/neural_network.pyDeleted-27
- go.modAdded+9
- go.sumAdded+72
- main.goAdded+5
- neural_network/activation.pyDeleted-26
- neural_network/backprop.pyDeleted-73
- neural_network/main.pyDeleted-51
- neural_network/neural_network.pyDeleted-86
- neural_network/opts.pyDeleted-18
- opts.pyDeleted-10
- requirements.txtDeleted-8
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133# Byte-compiled / optimized / DLL files__pycache__/*.py[cod]*$py.class# C extensions*.so# Distribution / packaging.Pythonbuild/develop-eggs/dist/downloads/eggs/.eggs/lib/lib64/parts/sdist/var/wheels/pip-wheel-metadata/share/python-wheels/*.egg-info/.installed.cfg*.eggMANIFEST# PyInstaller# Usually these files are written by a python script from a template# before PyInstaller builds the exe, so as to inject date/other infos into it.*.manifest*.spec# Installer logspip-log.txtpip-delete-this-directory.txt# Unit test / coverage reportshtmlcov/.tox/.nox/.coverage.coverage.*.cachenosetests.xmlcoverage.xml*.cover*.py,cover.hypothesis/.pytest_cache/# Translations*.mo*.pot# Django stuff:*.loglocal_settings.pydb.sqlite3db.sqlite3-journal# Flask stuff:instance/.webassets-cache# Scrapy stuff:.scrapy# Sphinx documentationdocs/_build/# PyBuildertarget/# Jupyter Notebook.ipynb_checkpoints# IPythonprofile_default/ipython_config.py# pyenv.python-version# pipenv# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.# However, in case of collaboration, if having platform-specific dependencies or dependencies# having no cross-platform support, pipenv may install dependencies that don't work, or not# install all needed dependencies.#Pipfile.lock# PEP 582; used by e.g. github.com/David-OConnor/pyflow__pypackages__/# Celery stuffcelerybeat-schedulecelerybeat.pid# SageMath parsed files*.sage.py# Environments.env.venvenv/venv/ENV/env.bak/venv.bak/# Spyder project settings.spyderproject.spyproject# Rope project settings.ropeproject# mkdocs documentation/site# mypy.mypy_cache/.dmypy.jsondmypy.json# Pyre type checker.pyre/.idea.vscode12345678910111213141516171819202122232425# If you prefer the allow list template instead of the deny list, see community template:# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore## Binaries for programs and plugins*.exe*.exe~*.dll*.so*.dylib# Test binary, built with `go test -c`*.test# Output of the go coverage tool, specifically when used with LiteIDE*.out# Dependency directories (remove the comment below to include it)# vendor/# Go workspace filego.work.DS_Store.vscode.idea
123456789101112131415161718192021MIT License
Copyright (c) 2023 Jensen Holm
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152# Data Mining from scratch backend
Currently living [here](https://data-mining-from-scratch-backend.onrender.com/) <br>Since the API is hosted using render's free tier, <br>every time 15 minutes goes by it gets shut down. <br>If a request is made while it is shut down, the web service <br>has to spin back up again which takes roughly 1 minute <br>
### Example Useage
```pythonimport requestsimport json
request_params = { "arguments": { "epochs": 100, "activation_func": "tanh", "hidden_size": 8, "learning_rate": 0.01 }}
headers = { "Content-Type": "application/json",}
r = requests.post( "https://data-mining-from-scratch-backend.onrender.com/neural-network", headers=headers, data=json.dumps(request_params),)
model_data = r.json()print(model_data)```
### Parameter Options
- End Points: <br>
-`"neural-network"` <br>
- `"kmeans-clustering"` <br> -`"kmedoid-clustering"` <br> -`"heirarchical-clustering"` <br>
- Algorithm Specific Arguments
- neural-network - epochs: any integer - activation_func: tanh, sigmoid, or relu - hidden_size: must be an even integer - learning_rate: any floating point number1234567891011121314151617181920212223242526272829303132333435363738394041424344from flask import Flask, request, jsonify, render_templatefrom flask_cors import CORS
from dataset.iris import irisfrom opts import options
# using the iris data set for every algorithm# just for simplicity sakeX, y = iris()
app = Flask(__name__)
CORS(app, origins="*")
@app.route("/neural-network", methods=["POST"])def neural_network(): algorithm = options["neural-network"] args = request.json["arguments"]
result = algorithm( X=X, y=y, args=args, ) return jsonify(result)
@app.route("/kmeans-clustering", methods=["POST"])def kmeans(): algorithm = options["kmeans-clustering"] args = request.json["arguments"]
result = algorithm( X=X, y=y, clusterer="kmeans-clustering", args=args, ) return jsonify(result)
if __name__ == "__main__": app.run(debug=False)12345678910111213141516171819202122from dataclasses import dataclassfrom typing import Callable
@dataclassclass Clusterer: cluster_func: Callable plt_data = None
def eval( self, pred_labels, true_labels, ) -> None: ...
def set_plot_data(self, plt_data): self.plt_data = plt_data
@classmethod def from_dict(cls, dct: dict): return cls(**dct)12345678910111213141516171819import numpy as np
# right now I am not using this function# maybe get rid of it or change it to how we # use it in our distance calculations
def euclidean( point: np.array, data: np.array,): """ Computed the euclidean distance between a point and the rest of the dataset point dims: (m,) data dims: (n, m) output dims: (n,) """ return np.sqrt(np.sum((point - data)**2), aixs=1)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778from dataclasses import dataclassimport numpy as np
from cluster.clusterer import Clusterer
@dataclassclass Kmeans(Clusterer): k: int max_iter: int centroids = None clusters = None
def build( self, X: np.array, ) -> None: # randomly initialize centroids centroids = X[np.random.choice( X.shape[0], self.k, replace=False, )]
# Calculate Euclidean distance between each data point and each centroid # then assign each point to its closest cluster clusters = self.assign_clusters(X, centroids) centroids = self.update_centroids(self.k, X, clusters)
while True: new_clusts = self.assign_clusters(X, centroids) if np.array_equal(new_clusts, clusters): break clusters = new_clusts centroids = self.update_centroids(self.k, X, clusters)
self.clusters = clusters self.centroids = centroids
@staticmethod def assign_clusters( X: np.array, centroids: np.array, ) -> np.array: distances = np.sqrt(((X - centroids[:, np.newaxis])**2).sum(axis=2)) clusts = np.argmin(distances, axis=0) return clusts
@staticmethod def update_centroids( k: int, X: np.array, clusters: np.array, ) -> np.array: centroids = np.zeros((k, X.shape[1])) for i in range(k): centroids[i] = X[clusters == i].mean(axis=0) return centroids
def to_dict( self, X: np.array, ) -> dict: cluster_data = [] for i in range(self.k): indices = np.where(self.clusters == i)[0] cluster_pts = X[indices].tolist() cluster_data.append({ "cluster_id": i, "centroid": self.centroids[i].tolist(), "points": cluster_pts, }) return { "k": self.k, "max_iter": self.max_iter, "clusters": cluster_data, "plt_data": self.plt_data, }123456789101112131415from dataclasses import dataclassimport numpy as np
from cluster.clusterer import Clusterer
@dataclassclass Kmedoids(Clusterer): k: int
def main(self, X): ...
def build(self, X: np.array): ...12345678910111213141516171819202122import numpy as np
from cluster.clusterer import Clustererfrom cluster.opts import clustering_methodsfrom cluster.plot import plot
def main( X: np.array, y: np.array, clusterer: str, args: dict,) -> dict: cluster_alg: Clusterer = clustering_methods[clusterer]
args.update({"cluster_func": cluster_alg}) alg = cluster_alg.from_dict(args)
alg.build(X) plt_data = plot(clusterer=alg, X=X) alg.set_plot_data(plt_data) return alg.to_dict(X)12345678from cluster.kmedoids import Kmedoidsfrom cluster.kmeans import Kmeans
clustering_methods = { "kmeans-clustering": Kmeans, "kmedoids-clustering": Kmedoids,}123456789101112131415161718192021222324252627282930313233343536373839import matplotlibimport matplotlib.pyplot as pltimport seaborn as snsimport io
matplotlib.use("Agg")sns.set()
def plot(clusterer, X): cluster_data = clusterer.to_dict(X)["clusters"] fig, ax = plt.subplots(figsize=(8, 6)) for cluster in cluster_data: sns.scatterplot( x=[point[0] for point in cluster["points"]], y=[point[1] for point in cluster["points"]], label=f"Cluster {cluster['cluster_id']}", ax=ax, ) ax.scatter( x=cluster["centroid"][0], y=cluster["centroid"][1], marker="x", s=100, linewidth=2, color="red", ) ax.legend() ax.set_title("K-means Clustering") ax.set_ylabel("Normalized Petal Length (cm)") ax.set_xlabel("Normalized Petal Length (cm)")
# Save the plot to a BytesIO buffer buffer = io.BytesIO() plt.savefig(buffer, format='png') buffer.seek(0)
return buffer.read()1234567891011121314from sklearn.datasets import load_irisfrom sklearn.preprocessing import OneHotEncoder, StandardScaler
def iris(): """ returns a tuple of numpy arrays containing the iris dataset split into training and testing sets after being normalized and one-hot encoded """ iris = load_iris() scaler = StandardScaler() x = scaler.fit_transform(iris.data) y = OneHotEncoder().fit_transform(iris.target.reshape(-1, 1)).toarray() return x, y12345678910111213141516171819202122232425262728293031
import jsonimport requestsimport matplotlib.pyplot as pltimport seaborn as sns
ENDPOINT: str = "http://127.0.0.1:5000/"
request_params = { "arguments": { "clusterer": "kmeans-clustering", "k": 3, "max_iter": 100, },}
headers = { "Content-Type": "application/json",}
r = requests.post( ENDPOINT, headers=headers, data=json.dumps(request_params),)
if __name__ == "__main__": print(r.json())123456789101112131415161718192021222324252627import requestsimport json
ENDPOINT: str = "http://127.0.0.1:5000/neural-network"
request_params = { "arguments": { "epochs": 100, "activation_func": "tanh", "hidden_size": 8, "learning_rate": 0.01 }}
headers = { "Content-Type": "application/json",}
r = requests.post( ENDPOINT, headers=headers, data=json.dumps(request_params),)
if __name__ == "__main__": print(r.json()["plt_data"])123456789module github.com/Jensen-holm/ml-from-scratch
go 1.19
require ( github.com/go-gota/gota v0.12.0 // indirect golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 // indirect gonum.org/v1/gonum v0.9.1 // indirect)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g=github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks=github.com/go-fonts/liberation v0.1.1/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY=github.com/go-fonts/stix v0.1.0/go.mod h1:w/c1f0ldAUlJmLBvlbkvVXLAD+tAMqobIIQpmnUIzUY=github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=github.com/go-gota/gota v0.12.0 h1:T5BDg1hTf5fZ/CO+T/N0E+DDqUhvoKBl+UVckgcAAQg=github.com/go-gota/gota v0.12.0/go.mod h1:UT+NsWpZC/FhaOyWb9Hui0jXg0Iq8e/YugZHTbyW/34=github.com/go-latex/latex v0.0.0-20210118124228-b3d85cf34e07/go.mod h1:CO1AlKB2CSIqUrmQPqA0gdRIlnLEY0gK5JGjh37zN5U=github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3/go.mod h1:NOZ3BPKG0ec/BKJQgnvsSFpcKLM5xXVWnvZS97DWHgE=golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=golang.org/x/image v0.0.0-20200430140353-33d19683fad8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=golang.org/x/image v0.0.0-20201208152932-35266b937fa6/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=golang.org/x/image v0.0.0-20210216034530-4410531fe030/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 h1:0PC75Fz/kyMGhL0e1QnypqK2kQMqKt9csD1GnMJR+Zk=golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=golang.org/x/sys v0.0.0-20210304124612-50617c2ba197/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=golang.org/x/tools v0.0.0-20190206041539-40960b6deb8e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=gonum.org/v1/gonum v0.8.2/go.mod h1:oe/vMfY3deqTw+1EZJhuvEW2iwGF1bW9wwu7XCu0+v0=gonum.org/v1/gonum v0.9.1 h1:HCWmqqNoELL0RAQeKBXWtkp04mGk8koafcB4He6+uhc=gonum.org/v1/gonum v0.9.1/go.mod h1:TZumC3NeyVQskjXqmyWt4S3bINhy7B4eYwW69EbyX+0=gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=gonum.org/v1/plot v0.9.0/go.mod h1:3Pcqqmp6RHvJI72kgb8fThyUnav364FOsdDo2aGW5lY=rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=12345package main
func main() {
}1234567891011121314151617181920212223242526import numpy as np
def relu(x): return np.maximum(x, 0)
def relu_prime(x): return np.where(x > 0, 1, 0)
def tanh(x): return np.tanh(x)
def tanh_prime(x): return 1 - np.tanh(x) ** 2
def sigmoid(x): return 1.0 / (1.0 + np.exp(-x))
def sigmoid_prime(x): s = sigmoid(x) return s / 1.0 - s12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import numpy as npfrom typing import Callable
from neural_network.neural_network import NeuralNetwork
def fp( X_train: np.array, y_train: np.array, func: Callable, w1: np.array, w2: np.array, b1: np.array, b2: np.array,): n1 = compute_node(arr=X_train, w=w1, b=b1, func=func) y_hat = compute_node(arr=n1, w=w2, b=b2, func=func) return y_hat, n1, (y_hat-y_train)
def bp( X_train: np.array, y_train: np.array, wb: dict, args: dict,) -> NeuralNetwork: args.update(wb) model = NeuralNetwork.from_dict(args) loss_history = [] for _ in range(model.epochs): # forward prop y_hat, node1, error = fp( X_train=X_train, y_train=y_train, func=model.activation_func, w1=model.w1, w2=model.w2, b1=model.b1, b2=model.b2, ) mean_squared_error = mse(y_train, y_hat) loss_history.append(mean_squared_error)
# backprop dw1 = np.dot( X_train.T, np.dot(error * model.func_prime(y_hat), model.w2.T) * model.func_prime(node1), ) dw2 = np.dot( node1.T, error * model.func_prime(y_hat), ) db2 = np.sum(error * model.func_prime(y_hat), axis=0) db1 = np.sum( np.dot(error * model.func_prime(y_hat), model.w2.T) * model.func_prime(node1), axis=0, )
# update weights & biases using gradient descent. # this is -= and not += because if the gradient descent # is positive, we want to go down. model.w1 -= (model.learning_rate * dw1) model.w2 -= (model.learning_rate * dw2) model.b1 -= (model.learning_rate * db1) model.b2 -= (model.learning_rate * db2)
model.set_loss_hist(loss_hist=loss_history) return model
def compute_node(arr, w, b, func): return func(np.dot(arr, w) + b)
def mse(y: np.array, y_hat: np.array): return np.mean((y - y_hat) ** 2)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051from sklearn.model_selection import train_test_splitimport numpy as np
from neural_network.opts import activationfrom neural_network.backprop import bp
def init(X: np.array, hidden_size: int): """ returns a dictionary containing randomly initialized weights and biases to start off the neural_network """ return { "w1": np.random.randn(X.shape[1], hidden_size), "b1": np.zeros((1, hidden_size)), "w2": np.random.randn(hidden_size, 3), # Output layer has 3 neurons "b2": np.zeros((1, 3)), # Output layer has 3 neurons }
def main( X: np.array, y: np.array, args,) -> None: wb = init(X, args["hidden_size"]) act = activation[args["activation_func"]] args["activation_func"] = act["main"] args["func_prime"] = act["prime"] X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=8675309, )
model = bp( X_train=X_train, y_train=y_train, wb=wb, args=args, )
# evaluate the model and return final results model.eval( X_test=X_test, y_test=y_test, )
model.plot() return model.to_dict()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586from dataclasses import dataclass, fieldfrom matplotlib import pyplot as pltimport matplotlibimport seaborn as snsfrom typing import Callableimport numpy as npimport base64import io
sns.set()matplotlib.use("Agg")
@dataclassclass NeuralNetwork: epochs: int learning_rate: float activation_func: Callable func_prime: Callable hidden_size: int w1: np.array w2: np.array b1: np.array b2: np.array
mse: float = 0 loss_history: list = field( default_factory=lambda: [], ) plt_data = None
def predict(self, x: np.array) -> np.array: n1 = self.compute_node(x, self.w1, self.b1, self.activation_func) return self.compute_node(n1, self.w2, self.b2, self.activation_func)
def set_loss_hist(self, loss_hist: list) -> None: self.loss_history = loss_hist
def eval(self, X_test, y_test) -> None: self.mse = np.mean((self.predict(X_test) - y_test) ** 2)
def set_plot_data(self, plot_data): self.plt_data = plot_data
def plot(self): sns.set() fig, ax = plt.subplots() sns.lineplot( x=np.arange(len(self.loss_history)), y=self.loss_history, ax=ax, ) plt.ylabel("Loss") plt.xlabel("Epoch") plt.title("Loss / Epoch")
buffer = io.BytesIO() plt.savefig(buffer, format='png') buffer.seek(0) plt_data = buffer.read() plt_data_encoded = base64.b64encode(plt_data).decode('utf-8') self.set_plot_data(plt_data_encoded) plt.close()
@staticmethod def compute_node(arr, w, b, func) -> np.array: return func(np.dot(arr, w) + b)
@classmethod def from_dict(cls, dct): return cls(**dct)
def to_dict(self) -> dict: return { # "w1": self.w1.tolist(), # "w2": self.w2.tolist(), # "b1": self.b1.tolist(), # "b2": self.b2.tolist(), "epochs": self.epochs, "learning_rate": self.learning_rate, "activation_func": self.activation_func.__name__, "func_prime": self.func_prime.__name__, "hidden_size": self.hidden_size, "mse": self.mse, "plt_data": self.plt_data, }123456789101112131415161718from neural_network.activation import *
activation = { "relu": { "main": relu, "prime": relu_prime, },
"sigmoid": { "main": sigmoid, "prime": sigmoid_prime, },
"tanh": { "main": tanh, "prime": tanh_prime, },}12345678910from neural_network.main import main as nnfrom cluster.main import main as clust
options = { "neural-network": nn, "kmeans-clustering": clust, "kmedoid-clustering": clust, "heirarchical-clustering": clust, "dbscan-clustering": clust,}12345678Flask==2.2.3Flask_Cors==3.0.10matplotlib==3.5.3numpy==1.21.6requests==2.28.2scikit_learn==1.0.2seaborn==0.12.2gunicorn==20.1.0.gitignore+18-126
LICENSEDeleted-21
README.mdDeleted-52
app.pyDeleted-44
cluster/clusterer.pyDeleted-22
cluster/distance.pyDeleted-19
cluster/heirarchical.pyDeleted
cluster/kmeans.pyDeleted-78
cluster/kmedoids.pyDeleted-15
cluster/main.pyDeleted-22
cluster/opts.pyDeleted-8
cluster/plot.pyDeleted-39
dataset/iris.pyDeleted-14
example/kmeans.pyDeleted-31
example/kmedoids.pyDeleted
example/neural_network.pyDeleted-27
go.modAdded+9
go.sumAdded+72
main.goAdded+5
neural_network/activation.pyDeleted-26
neural_network/backprop.pyDeleted-73
neural_network/main.pyDeleted-51
neural_network/neural_network.pyDeleted-86
neural_network/opts.pyDeleted-18
opts.pyDeleted-10
requirements.txtDeleted-8