{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import matplotlib as plt\n", "import numpy as np\n", "\n", "from autotagger.helpers.preprocess import load_dataset\n", "from sklearn import cross_validation,linear_model\n", "from sklearn.datasets import make_multilabel_classification\n", "from sklearn.metrics import f1_score, precision_score, recall_score\n", "from sklearn.multiclass import OneVsRestClassifier" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "X,Y = load_dataset(\"stackoverflow\", max_features=100)\n", "X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(X,Y,test_size=0.80, random_state=42)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "clf = linear_model.LogisticRegression()\n", "meta_clf = OneVsRestClassifier(clf)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "OneVsRestClassifier(estimator=LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n", " intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n", " penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n", " verbose=0, warm_start=False),\n", " n_jobs=1)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "meta_clf.fit(X_train,Y_train)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Y_pred = meta_clf.predict(X_test)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/felipe/auto-tagger/venv3/lib/python3.4/site-packages/sklearn/metrics/classification.py:1074: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples.\n", " 'precision', 'predicted', average, warn_for)\n" ] }, { "data": { "text/plain": [ "0.037940736071986787" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# macro average refers to the average f1_score for each label\n", "f1_score(Y_test,Y_pred, average='macro')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/felipe/auto-tagger/venv3/lib/python3.4/site-packages/sklearn/metrics/classification.py:1074: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples.\n", " 'precision', 'predicted', average, warn_for)\n" ] }, { "data": { "text/plain": [ "0.10539093353329665" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# if we just consider the labels that have had at least one instance predicted,\n", "# our score goes up:\n", "\n", "label_scores = f1_score(Y_test,Y_pred,average=None)\n", "valid_label_indices = np.nonzero(label_scores)[0]\n", "f1_score(Y_test,Y_pred,average='macro',labels=valid_label_indices)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.18395809178395894" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# micro average refers to the average f1_score for each instance\n", "f1_score(Y_test,Y_pred,average='micro')" ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }