{ "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%2F46_ensembling.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/46_ensembling.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #46 ([video](https://www.youtube.com/watch?v=2lq2k6J3GW4&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=46))\n", "\n", "Want to improve your classifier's accuracy? Create multiple models and ensemble them using VotingClassifier!\n", "\n", "See example 👇\n", "\n", "P.S. VotingRegressor is also available" ] }, { "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.model_selection import cross_val_score" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6835791852363318" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lr = LogisticRegression(solver='liblinear', random_state=1)\n", "cross_val_score(lr, X, y).mean()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6947774778733288" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rf = RandomForestClassifier(max_features=None, random_state=1)\n", "cross_val_score(rf, X, y).mean()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7251020023852865" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# create an ensemble for improved accuracy\n", "vc = VotingClassifier([('clf1', lr), ('clf2', rf)], voting='soft')\n", "cross_val_score(vc, X, y).mean()" ] }, { "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 }