-
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
1 parent
0ff4389
commit 87460ac
Showing
4 changed files
with
330 additions
and
13,868 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np, pandas as pd" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class GDAMulticlass(object):\n", | ||
" \n", | ||
" def __init__(self, features, target):\n", | ||
" self.features=features\n", | ||
" self.target=target\n", | ||
" \n", | ||
" def train(self):\n", | ||
" combined=pd.concat([self.features, self.target], axis=1)\n", | ||
" self.mu_s=[combined[combined['target']==j].drop('target', axis=1).mean(axis=0)\\\n", | ||
" for j in range(len(self.target.unique()))]\n", | ||
" self.phi_s=(1.0/len(self.target))*np.array([self.target[self.target==j].count() \\\n", | ||
" for j in range(len(self.target.unique()))])\n", | ||
" sigma=np.matrix(np.zeros([self.features.shape[1], self.features.shape[1]]))\n", | ||
" for i in range(self.target.shape[0]):\n", | ||
" sigma += np.dot(np.matrix(self.features.iloc[i, :]-\\\n", | ||
" self.mu_s[self.target.iloc[i]]).T, \\\n", | ||
" np.matrix(self.features.iloc[i, :]-self.mu_s[self.target.iloc[i]]))\n", | ||
" \n", | ||
" self.sigma=(1.0/self.target.shape[0])*sigma\n", | ||
" \n", | ||
" def P_y(self, y, phi_s):\n", | ||
" return phi_s[y]\n", | ||
" \n", | ||
" def P_x_given_y(self, sigma, x, mu):\n", | ||
" comp1 = 1.0/(np.sqrt((2*np.pi)**self.features.shape[1]) * np.sqrt(\\\n", | ||
" np.linalg.det(sigma)))\n", | ||
" comp2 = float(np.exp(np.dot(-0.5*np.dot(x-mu, np.linalg.inv(sigma)), x-mu)))\n", | ||
" return comp1*comp2\n", | ||
" \n", | ||
" def predict(self, X):\n", | ||
" predictions=[]\n", | ||
" for i in range(X.shape[0]):\n", | ||
" Prob=[self.P_x_given_y(self.sigma, X.iloc[i, :], self.mu_s[j])*self.P_y(j,\\\n", | ||
" self.phi_s) for j in range(len(self.target.unique()))]\n", | ||
" \n", | ||
" predictions.append(np.argmax(Prob))\n", | ||
" \n", | ||
" return np.array(predictions)\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np, pandas as pd" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class GDAMulticlass(object):\n", | ||
" \n", | ||
" def __init__(self, features, target):\n", | ||
" self.features=features\n", | ||
" self.target=target\n", | ||
" \n", | ||
" def train(self):\n", | ||
" combined=pd.concat([self.features, self.target], axis=1)\n", | ||
" self.mu_s=[combined[combined['target']==j].drop('target', axis=1).mean(axis=0)\\\n", | ||
" for j in range(len(self.target.unique()))]\n", | ||
" self.phi_s=(1.0/len(self.target))*np.array([self.target[self.target==j].count() \\\n", | ||
" for j in range(len(self.target.unique()))])\n", | ||
" sigma=np.matrix(np.zeros([self.features.shape[1], self.features.shape[1]]))\n", | ||
" for i in range(self.target.shape[0]):\n", | ||
" sigma += np.dot(np.matrix(self.features.iloc[i, :]-\\\n", | ||
" self.mu_s[self.target.iloc[i]]).T, \\\n", | ||
" np.matrix(self.features.iloc[i, :]-self.mu_s[self.target.iloc[i]]))\n", | ||
" \n", | ||
" self.sigma=(1.0/self.target.shape[0])*sigma\n", | ||
" \n", | ||
" def P_y(self, y, phi_s):\n", | ||
" return phi_s[y]\n", | ||
" \n", | ||
" def P_x_given_y(self, sigma, x, mu):\n", | ||
" comp1 = 1.0/(np.sqrt((2*np.pi)**self.features.shape[1]) * np.sqrt(\\\n", | ||
" np.linalg.det(sigma)))\n", | ||
" comp2 = float(np.exp(np.dot(-0.5*np.dot(x-mu, np.linalg.inv(sigma)), x-mu)))\n", | ||
" return comp1*comp2\n", | ||
" \n", | ||
" def predict(self, X):\n", | ||
" predictions=[]\n", | ||
" for i in range(X.shape[0]):\n", | ||
" Prob=[self.P_x_given_y(self.sigma, X.iloc[i, :], self.mu_s[j])*self.P_y(j,\\\n", | ||
" self.phi_s) for j in range(len(self.target.unique()))]\n", | ||
" \n", | ||
" predictions.append(np.argmax(Prob))\n", | ||
" \n", | ||
" return np.array(predictions)\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.