{ "cells": [ { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import make_regression,make_classification\n", "\n", "from sklearn.metrics import accuracy_score\n", "from sklearn.model_selection import train_test_split\n", "\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.linear_model import LogisticRegression\n", "\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "X, y = make_classification(n_samples=100,n_features=10,n_informative=2)\n", "\n", "X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((67, 10), (33, 10), (67,), (33,))" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train.shape, X_test.shape, y_train.shape, y_test.shape" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Pipeline(memory=None,\n", " steps=[('scaler', StandardScaler(copy=True, with_mean=True, with_std=True)), ('clf', LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n", " intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n", " penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n", " verbose=0, warm_start=False))])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# it takes a list of tuples as parameter\n", "pipeline = Pipeline([\n", " ('scaler',StandardScaler()),\n", " ('clf', LogisticRegression())\n", "])\n", "\n", "# use the pipeline object as you would\n", "# a regular classifier\n", "pipeline.fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "y_preds = pipeline.predict(X_test)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.84848484848484851" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "accuracy_score(y_test,y_preds)" ] } ], "metadata": { "kernelspec": { "display_name": "Global TF Kernel (Python 3)", "language": "python", "name": "global-tf-python-3" }, "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 }