{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open in Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F17_randomized_search.ipynb)\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/justmarkham/scikit-learn-tips/blob/master/notebooks/17_randomized_search.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #17 ([video](https://www.youtube.com/watch?v=Q5dH5mOQ_ik&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=17))\n", "\n", "GridSearchCV taking too long? Try RandomizedSearchCV with a small number of iterations.\n", "\n", "Make sure to specify a distribution (instead of a list of values) for continuous parameters!\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "df = pd.read_csv('http://bit.ly/kaggletrain')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# use single brackets around \"Name\" because CountVectorizer expects 1-D input\n", "X = df['Name']\n", "y = df['Survived']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn.feature_extraction.text import CountVectorizer\n", "from sklearn.naive_bayes import MultinomialNB\n", "from sklearn.pipeline import make_pipeline" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "pipe = make_pipeline(CountVectorizer(), MultinomialNB())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8001820350260498" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# cross-validate the pipeline using default parameters\n", "from sklearn.model_selection import cross_val_score\n", "cross_val_score(pipe, X, y, cv=5, scoring='accuracy').mean()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# specify parameter values to search (use a distribution for any continuous parameters)\n", "import scipy as sp\n", "params = {}\n", "params['countvectorizer__min_df'] = [1, 2, 3, 4]\n", "params['countvectorizer__lowercase'] = [True, False]\n", "params['multinomialnb__alpha'] = sp.stats.uniform(scale=1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# try \"n_iter\" random combinations of those parameter values\n", "from sklearn.model_selection import RandomizedSearchCV\n", "rand = RandomizedSearchCV(pipe, params, n_iter=10, cv=5, scoring='accuracy', random_state=1)\n", "rand.fit(X, y);" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8080534806352395" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# what was the best score found during the search?\n", "rand.best_score_" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'countvectorizer__lowercase': False,\n", " 'countvectorizer__min_df': 3,\n", " 'multinomialnb__alpha': 0.1981014890848788}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# which combination of parameters produced the best score?\n", "rand.best_params_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Want more tips? [View all tips on GitHub](https://github.com/justmarkham/scikit-learn-tips) or [Sign up to receive 2 tips by email every week](https://scikit-learn.tips) 💌\n", "\n", "© 2020 [Data School](https://www.dataschool.io). All rights reserved." ] } ], "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.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }