{ "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%2F47_ensemble_tuning.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/47_ensemble_tuning.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #47 ([video](https://www.youtube.com/watch?v=fvY3InlnOh8&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=47))\n", "\n", "Want to improve the accuracy of your VotingClassifier? Try tuning the 'voting' and 'weights' parameters to change how predictions are combined!\n", "\n", "See example 👇\n", "\n", "P.S. If you're using VotingRegressor, just tune the 'weights' parameter" ] }, { "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": [ "cols = ['Pclass', 'Parch', 'SibSp', 'Fare']\n", "X = df[cols]\n", "y = df['Survived']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn.linear_model import LogisticRegression\n", "from sklearn.ensemble import RandomForestClassifier, VotingClassifier\n", "from sklearn.naive_bayes import MultinomialNB\n", "from sklearn.model_selection import cross_val_score, GridSearchCV" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "lr = LogisticRegression(solver='liblinear', random_state=1)\n", "rf = RandomForestClassifier(max_features=None, random_state=1)\n", "nb = MultinomialNB()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6970560542338836" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# create an ensemble of 3 classifiers\n", "vc = VotingClassifier([('clf1', lr), ('clf2', rf), ('clf3', nb)])\n", "cross_val_score(vc, X, y).mean()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# define VotingClassifier parameters to search\n", "params = {'voting':['hard', 'soft'],\n", " 'weights':[(1,1,1), (2,1,1), (1,2,1), (1,1,2)]}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'voting': 'soft', 'weights': (1, 2, 1)}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# find the best set of parameters\n", "grid = GridSearchCV(vc, params)\n", "grid.fit(X, y)\n", "grid.best_params_" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7262820915196786" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# accuracy has improved\n", "grid.best_score_" ] }, { "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 }