{ "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%2F50_simple_ml_pattern.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/50_simple_ml_pattern.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #50 ([video](https://www.youtube.com/watch?v=gd-TZut-oto&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=50))\n", "\n", "Here's a simple pattern that can be adapted to solve many ML problems. It has plenty of shortcomings, but can work surprisingly well as-is!\n", "\n", "Check it out 👇\n", "\n", "Shortcomings include:\n", "\n", "- Assumes all columns have proper data types\n", "- May include irrelevant or improper features\n", "- Does not handle text or date columns well\n", "- Does not include feature engineering\n", "- Ordinal encoding may be better\n", "- Other imputation strategies may be better\n", "- Numeric features may not need scaling\n", "- A different model may be better\n", "- And so on..." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "cols = ['Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('http://bit.ly/kaggletrain')\n", "X = df[cols]\n", "y = df['Survived']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "df_new = pd.read_csv('http://bit.ly/kaggletest', nrows=10)\n", "X_new = df_new[cols]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from sklearn.impute import SimpleImputer\n", "from sklearn.preprocessing import StandardScaler, OneHotEncoder\n", "from sklearn.compose import make_column_selector, make_column_transformer\n", "from sklearn.pipeline import make_pipeline\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.model_selection import cross_val_score" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# set up preprocessing for numeric columns\n", "imp_median = SimpleImputer(strategy='median', add_indicator=True)\n", "scaler = StandardScaler()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# set up preprocessing for categorical columns\n", "imp_constant = SimpleImputer(strategy='constant')\n", "ohe = OneHotEncoder(handle_unknown='ignore')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# select columns by data type\n", "num_cols = make_column_selector(dtype_include='number')\n", "cat_cols = make_column_selector(dtype_exclude='number')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# do all preprocessing\n", "preprocessor = make_column_transformer(\n", " (make_pipeline(imp_median, scaler), num_cols),\n", " (make_pipeline(imp_constant, ohe), cat_cols))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# create a pipeline\n", "pipe = make_pipeline(preprocessor, LogisticRegression())" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8035904839620865" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# cross-validate the pipeline\n", "cross_val_score(pipe, X, y).mean()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 1, 0, 1, 0, 1, 0])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# fit the pipeline and make predictions\n", "pipe.fit(X, y)\n", "pipe.predict(X_new)" ] }, { "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 }