|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 103, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from skmultilearn.ext import Keras\n", |
| 10 | + "from keras.models import Sequential\n", |
| 11 | + "from keras.layers import Dense\n", |
| 12 | + "import numpy\n", |
| 13 | + "import sklearn.metrics as metrics\n", |
| 14 | + "from skmultilearn.dataset import load_dataset" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "X_train, y_train, feature_names, label_names = load_dataset('emotions', 'train')\n", |
| 24 | + "X_test, y_test, _, _ = load_dataset('emotions', 'test')" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "# Single-class Keras classifier" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "We train a two-layer neural network using Keras and tensortflow as backend (feel free to use others), the network is fairly simple 12 x 8 RELU that finish with a sigmoid activator optimized via binary cross entropy. This is a case from the [Keras example page](https://keras.io/scikit-learn-api/). Note that the model creation function must create a model that accepts an input dimension and outpus a relevant output dimension. The Keras wrapper from scikit-multilearn will pass relevant dimensions upon fitting. " |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": 105, |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "def create_model_single_class(input_dim, output_dim):\n", |
| 48 | + "\t# create model\n", |
| 49 | + "\tmodel = Sequential()\n", |
| 50 | + "\tmodel.add(Dense(12, input_dim=input_dim, activation='relu'))\n", |
| 51 | + "\tmodel.add(Dense(8, activation='relu'))\n", |
| 52 | + "\tmodel.add(Dense(output_dim, activation='sigmoid'))\n", |
| 53 | + "\t# Compile model\n", |
| 54 | + "\tmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n", |
| 55 | + "\treturn model" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "markdown", |
| 60 | + "metadata": {}, |
| 61 | + "source": [ |
| 62 | + "Let's use it with a problem transformation method which converts multi-label classification problems to single-label single-class problems, ex. Binary Relevance which trains a classifier per label." |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 123, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "from skmultilearn.problem_transform import BinaryRelevance" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": 126, |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "clf = BinaryRelevance(classifier=Keras(create_model_single_class), require_dense=[True,True])" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 127, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [ |
| 88 | + { |
| 89 | + "data": { |
| 90 | + "text/plain": [ |
| 91 | + "BinaryRelevance(classifier=<__main__.Keras object at 0x7f4d0b040978>,\n", |
| 92 | + " require_dense=[True, True])" |
| 93 | + ] |
| 94 | + }, |
| 95 | + "execution_count": 127, |
| 96 | + "metadata": {}, |
| 97 | + "output_type": "execute_result" |
| 98 | + } |
| 99 | + ], |
| 100 | + "source": [ |
| 101 | + "clf.fit(X_train,y_train)" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": 130, |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [], |
| 109 | + "source": [ |
| 110 | + "y_pred = clf.predict(X_test)" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": 133, |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [ |
| 118 | + { |
| 119 | + "data": { |
| 120 | + "text/plain": [ |
| 121 | + "0.25495049504950495" |
| 122 | + ] |
| 123 | + }, |
| 124 | + "execution_count": 133, |
| 125 | + "metadata": {}, |
| 126 | + "output_type": "execute_result" |
| 127 | + } |
| 128 | + ], |
| 129 | + "source": [ |
| 130 | + "metrics.hamming_loss(y_test, y_pred)" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "metadata": {}, |
| 136 | + "source": [ |
| 137 | + "# Single-class Keras classifier" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "markdown", |
| 142 | + "metadata": {}, |
| 143 | + "source": [ |
| 144 | + "We now train a multi-class neural network using Keras and tensortflow as backend (feel free to use others) optimized via categorical cross entropy. This is a case from the [Keras multi-class tutorial](https://machinelearningmastery.com/multi-class-classification-tutorial-keras-deep-learning-library/). Note again that the model creation function must create a model that accepts an input dimension and outpus a relevant output dimension. The Keras wrapper from scikit-multilearn will pass relevant dimensions upon fitting. We must also tel the Keras wrapper that this is a multi-class case. We use the Label Powerset multi-label to multi-class transformation approach, but this can also be used with all the advanced label space division methods available in scikit-multilearn." |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": 136, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [], |
| 152 | + "source": [ |
| 153 | + "from skmultilearn.problem_transform import LabelPowerset" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": 137, |
| 159 | + "metadata": {}, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "def create_model_multiclass(input_dim, output_dim):\n", |
| 163 | + "\t# create model\n", |
| 164 | + "\tmodel = Sequential()\n", |
| 165 | + "\tmodel.add(Dense(8, input_dim=input_dim, activation='relu'))\n", |
| 166 | + "\tmodel.add(Dense(output_dim, activation='softmax'))\n", |
| 167 | + "\t# Compile model\n", |
| 168 | + "\tmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n", |
| 169 | + "\treturn model" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": 138, |
| 175 | + "metadata": {}, |
| 176 | + "outputs": [], |
| 177 | + "source": [ |
| 178 | + "clf = LabelPowerset(classifier=Keras(create_model_multiclass, multi_class=True), require_dense=[True,True])" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": 139, |
| 184 | + "metadata": {}, |
| 185 | + "outputs": [ |
| 186 | + { |
| 187 | + "data": { |
| 188 | + "text/plain": [ |
| 189 | + "LabelPowerset(classifier=<__main__.Keras object at 0x7f4d09229be0>,\n", |
| 190 | + " require_dense=[True, True])" |
| 191 | + ] |
| 192 | + }, |
| 193 | + "execution_count": 139, |
| 194 | + "metadata": {}, |
| 195 | + "output_type": "execute_result" |
| 196 | + } |
| 197 | + ], |
| 198 | + "source": [ |
| 199 | + "clf.fit(X_train,y_train)" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": 140, |
| 205 | + "metadata": {}, |
| 206 | + "outputs": [], |
| 207 | + "source": [ |
| 208 | + "y_pred = clf.predict(X_test)" |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "code", |
| 213 | + "execution_count": 142, |
| 214 | + "metadata": {}, |
| 215 | + "outputs": [ |
| 216 | + { |
| 217 | + "data": { |
| 218 | + "text/plain": [ |
| 219 | + "0.22277227722772278" |
| 220 | + ] |
| 221 | + }, |
| 222 | + "execution_count": 142, |
| 223 | + "metadata": {}, |
| 224 | + "output_type": "execute_result" |
| 225 | + } |
| 226 | + ], |
| 227 | + "source": [ |
| 228 | + "metrics.accuracy_score(y_test, y_pred)" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "cell_type": "code", |
| 233 | + "execution_count": null, |
| 234 | + "metadata": {}, |
| 235 | + "outputs": [], |
| 236 | + "source": [] |
| 237 | + } |
| 238 | + ], |
| 239 | + "metadata": { |
| 240 | + "kernelspec": { |
| 241 | + "display_name": "Python 3", |
| 242 | + "language": "python", |
| 243 | + "name": "python3" |
| 244 | + }, |
| 245 | + "language_info": { |
| 246 | + "codemirror_mode": { |
| 247 | + "name": "ipython", |
| 248 | + "version": 3 |
| 249 | + }, |
| 250 | + "file_extension": ".py", |
| 251 | + "mimetype": "text/x-python", |
| 252 | + "name": "python", |
| 253 | + "nbconvert_exporter": "python", |
| 254 | + "pygments_lexer": "ipython3", |
| 255 | + "version": "3.6.6" |
| 256 | + } |
| 257 | + }, |
| 258 | + "nbformat": 4, |
| 259 | + "nbformat_minor": 2 |
| 260 | +} |
0 commit comments