Created
January 16, 2018 06:05
-
-
Save beader/0cf972dbaca30f4565a07f27d6af8165 to your computer and use it in GitHub Desktop.
NTU Machine Learning 2017 Fall - Assignment 2
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"X_TRAIN_FILE = './X_train'\n", | |
"Y_TRAIN_FILE = './Y_train'\n", | |
"X_TEST_FILE = './X_test'\n", | |
"Y_TEST_FILE = './correct_answer.csv'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x_train = pd.read_csv(X_TRAIN_FILE, header=0).values\n", | |
"y_train = pd.read_csv(Y_TRAIN_FILE, header=0).label.values\n", | |
"x_test = pd.read_csv(X_TEST_FILE, header=0).values\n", | |
"y_test = pd.read_csv(Y_TEST_FILE, header=0)['label'].values" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(32561, 106)\n", | |
"(32561,)\n", | |
"(16281, 106)\n" | |
] | |
} | |
], | |
"source": [ | |
"print(x_train.shape)\n", | |
"print(y_train.shape)\n", | |
"print(x_test.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def accuracy_score(y_true, y_pred):\n", | |
" return np.mean(y_true == y_pred)\n", | |
"\n", | |
"def train_valid_split(X, y, valid_size=0.2):\n", | |
" assert len(X) == len(y)\n", | |
" valid_size = int(len(X) * valid_size) if valid_size < 1 else int(valid_size)\n", | |
" random_indexer = np.arange(len(X))\n", | |
" np.random.shuffle(random_indexer)\n", | |
" return (X[random_indexer][valid_size:], y[random_indexer][valid_size:],\n", | |
" X[random_indexer][:valid_size], y[random_indexer][:valid_size])\n", | |
"\n", | |
"def normalize(*Xs):\n", | |
" X_all = np.concatenate(Xs)\n", | |
" mu = X_all.mean(axis=0, keepdims=True)\n", | |
" sigma = X_all.std(axis=0, keepdims=True)\n", | |
" Xs = tuple((X - mu) / sigma for X in Xs)\n", | |
" return Xs if len(Xs) > 1 else Xs[0]\n", | |
"\n", | |
"class GenerativeModel:\n", | |
" \n", | |
" def __init__(self):\n", | |
" pass\n", | |
" \n", | |
" def _sigmoid(self, z):\n", | |
" res = 1 / (1.0 + np.exp(-z))\n", | |
" return np.clip(res, 1e-8, 1-(1e-8))\n", | |
" \n", | |
" def fit(self, X, y, report_every_n_epoch=1):\n", | |
" print('training generative model')\n", | |
" class_0 = (y == 0)\n", | |
" class_1 = (y == 1)\n", | |
" self.N_0_ = class_0.sum()\n", | |
" self.N_1_ = class_1.sum()\n", | |
" mu_0 = X[class_0].mean(axis=0)\n", | |
" mu_1 = X[class_1].mean(axis=0)\n", | |
" sigma_0 = np.cov(X[class_0].T)\n", | |
" sigma_1 = np.cov(X[class_1].T)\n", | |
" self.mu_0_ = mu_0\n", | |
" self.mu_1_ = mu_1\n", | |
" self.sigma_ = self.N_0_ / len(y) * sigma_0 + self.N_1_ / len(y) * sigma_1\n", | |
" print('finish!')\n", | |
"\n", | |
" def predict(self, X):\n", | |
" sigma_inv = np.linalg.inv(self.sigma_)\n", | |
" w = np.dot(self.mu_1_ - self.mu_0_, sigma_inv)\n", | |
" b = (-0.5 * np.dot(np.dot(self.mu_1_, sigma_inv), self.mu_1_) + \n", | |
" 0.5 * np.dot(np.dot(self.mu_0_, sigma_inv), self.mu_1_) +\n", | |
" np.log(self.N_1_ / self.N_0_))\n", | |
" z = np.dot(X, w) + b\n", | |
" y = self._sigmoid(z)\n", | |
" y_ = np.around(y)\n", | |
" return y_" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x_train_norm, x_test_norm = normalize(x_train, x_test)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"training generative model\n", | |
"finish!\n" | |
] | |
} | |
], | |
"source": [ | |
"gm = GenerativeModel()\n", | |
"gm.fit(x_train_norm, y_train)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.84085744118911609" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"accuracy_score(gm.predict(x_test_norm), y_test)" | |
] | |
} | |
], | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment