{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Usando scikit-learn\n", "\n", "Aqui mostramos como usar alguns classificadores disponíveis no scikit-learn\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Gerar os dados" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from funcoes import geraDados2DfronteiraNonLinear\n", "\n", "%matplotlib inline\n", "\n", "N = 100\n", "X, y, x, fx = geraDados2DfronteiraNonLinear(N)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Usar regressão logística do scikit-learn\n", "\n", "http://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "from sklearn.linear_model import LogisticRegression\n", "\n", "clf = LogisticRegression(max_iter=100) # default max_iter=100\n", "clf.fit(X,y)\n", "\n", "z = clf.predict(X)\n", "\n", "print(z)\n", "\n", "for i in range(N):\n", " if z[i] > 0.5:\n", " plt.plot(X[i,1], X[i,2], 'bx')\n", " else:\n", " plt.plot(X[i,1], X[i,2], 'ro')\n", "\n", "plt.plot(x, fx, lw=2)\n", "plt.xlabel('x1')\n", "plt.ylabel('x2')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Usar SVM do scikit-learn\n", "\n", "http://scikit-learn.org/stable/modules/svm.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "from sklearn import svm\n", "\n", "clf = svm.SVC(gamma=0.3, C=100.)\n", "clf.fit(X,y)\n", "\n", "z = clf.predict(X)\n", "\n", "print(z)\n", "\n", "for i in range(N):\n", " if z[i] > 0.5:\n", " plt.plot(X[i,1], X[i,2], 'bx')\n", " else:\n", " plt.plot(X[i,1], X[i,2], 'ro')\n", " \n", "plt.plot(x, fx, lw=2)\n", "plt.xlabel('x1')\n", "plt.ylabel('x2')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Redes neurais multicamadas\n", "\n", "http://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.neural_network import MLPClassifier\n", "\n", "clf = MLPClassifier(solver='sgd', learning_rate_init=0.01, max_iter=500, hidden_layer_sizes=(10), random_state=1)\n", "\n", "clf.fit(X, y)\n", "\n", "z = clf.predict(X)\n", "print(z)\n", "\n", "for i in range(N):\n", " if z[i] > 0.5:\n", " plt.plot(X[i,1], X[i,2], 'bx')\n", " else:\n", " plt.plot(X[i,1], X[i,2], 'ro')\n", " \n", "plt.plot(x, fx, lw=2)\n", "plt.xlabel('x1')\n", "plt.ylabel('x2')\n", "plt.show()" ] } ], "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 }