From a3700f6f8f12492dcba7432db7861415c55042e4 Mon Sep 17 00:00:00 2001 From: Zeynep Hakguder <zhakguder@cse.unl.edu> Date: Thu, 24 May 2018 09:41:53 -0500 Subject: [PATCH] added notebook tips --- model.ipynb | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 model.ipynb diff --git a/model.ipynb b/model.ipynb new file mode 100644 index 0000000..7ba1ea9 --- /dev/null +++ b/model.ipynb @@ -0,0 +1,129 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# JUPYTER NOTEBOOK TIPS\n", + "\n", + "Each rectangular box is called a cell. \n", + "* ctrl+ENTER evaluates the current cell; if it contains Python code, it runs the code, if it contains Markdown, it returns rendered text.\n", + "* alt+ENTER evaluates the current cell and adds a new cell below it.\n", + "* If you click to the left of a cell, you'll notice the frame changes color to blue. You can erase a cell by hitting 'dd' (that's two \"d\"s in a row) when the frame is blue." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Supervised Learning Model Skeleton\n", + "\n", + "We'll use this skeleton for implementing different supervised learning algorithms. Please complete \"preprocess\" and \"partition\" methods below." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You might need to preprocess your dataset depending on which dataset you are using. This step is for reading the dataset and for extracting features and labels. The \"preprocess\" function should return an $n \\times d$ features array, and an $n \\times 1$ labels array, where $n$ is the number of examples and $d$ is the number of features in the dataset. In cases where there is a big difference between the scales of feautures, we want to normalize the features to have values in the same range [0,1]. If that is the case with your dataset, output normalized features to get better prediction results." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "def preprocess(file_path):\n", + " '''\n", + " file_path: where to read the dataset from\n", + " returns nxd features, nx1 labels\n", + " '''\n", + " # You might find np.genfromtxt useful for reading in the file. Be careful with the file delimiter, \n", + " # e.g. for comma-separated files use delimiter=',' argument.\n", + " \n", + " raise NotImplementedError\n", + "\n", + " \n", + " return features, labels" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, you'll need to split your dataset into training and validation and test sets. The \"split\" function should take as input the size of the whole dataset and randomly sample a proportion $p$ of the dataset as test partition and a proportion of $v$ as validation partition. The remaining will be used as training data. For example, to keep 30% of the examples as test and %10 as validation, set $p=0.3$ and $v=0.1$. You should choose these values according to the size of the data available to you. The \"split\" function should return indices of the training, validation and test sets. These will be used to index into the whole training set." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "def partition(size, p, v):\n", + " '''\n", + " size: number of examples in the whole dataset\n", + " p: proportion kept for test\n", + " v: proportion kept for validation\n", + " '''\n", + " \n", + " # np.random.choice might come in handy. Do not sample with replacement!\n", + " # Be sure to not use the same indices in test and validation sets!\n", + " \n", + " raise NotImplementedError\n", + " \n", + " # return two 1d arrays: one keeping validation set indices, the other keeping test set indices \n", + " return val_indices, test_indices" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class Model:\n", + " # set the preprocessing function, partition_function\n", + " # use kwargs to pass arguments to preprocessor_f and partition_f\n", + " # kwargs is a dictionary and should contain p, v and file_path\n", + " # e.g. {'p': 0.3, 'v': 0.1, 'file_path': some_path}\n", + " \n", + " def __init__(self, preprocessor_f, partition_f, **kwargs):\n", + " \n", + " self.features, self.labels = preprocessor_f(kwargs['file_path'])\n", + " self.size = len(self.labels) # number of examples in dataset\n", + " self.feat_dim = self.features.shape[1] # number of features\n", + " self.val_indices, self.test_indices = partition_f(self.size, kwargs['p'], kwargs['v'])\n", + " self.train_indices = np.delete(np.arange(self.size), np.append(self.test_indices, self.val_indices), 0)\n", + " \n", + " def fit(self):\n", + " raise NotImplementedError\n", + " \n", + " def predict(self, testpoint):\n", + " raise NotImplementedError" + ] + } + ], + "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.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- GitLab