How to prepare a machine learning workflow in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to prepare a machine learning workflow in Python?

How to prepare a machine learning workflow in Python?

This recipe helps you prepare a machine learning workflow in Python

0

Recipe Objective

Preparing a Machine learning workflow helps us for better performance and we can do this by using perceptron.

So this is the recipe on how we can prepare a machine leaning workflow in Python.

Step 1 - Import the library

from sklearn import datasets from sklearn.preprocessing import StandardScaler from sklearn.linear_model import Perceptron from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, confusion_matrix

We have only imported datasets, perceptron, confusion_matrix, accuracy_score, train_test_split and standardscaler which is needed.

Step 2 - Setting up the Data

We have imported an inbuilt iris dataset to use test_train_split. We have stored data in X and target in y. iris = datasets.load_iris() X = iris.data y = iris.target

Step 3 - Splitting the Data

So now we are using test_train_split to split the data. We have passed test_size as 0.33 which means 33% of data will be in the test part and rest will be in train part. Parameter random_state signifies the random splitting of data into the two parts. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

Step 4 - Using StandardScaler

StandardScaler is used to remove the outliners and scale the data by making the mean of the data 0 and standard deviation as 1. So we are creating an object std_scl to use standardScaler.
We have fitted the train data and transformed train and test data form standard scaler. Finally we have printed first five elements of test, train, scaled train and scaled test. sc = StandardScaler(with_mean=True, with_std=True) sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test)

Step 5 - Using Perceptron

We have used perceptron with different parameters like alpha, class_weight, fit_intercept , etc. We have fiuued it and predicted the output for it. ppn = Perceptron(alpha=0.0001, class_weight=None, eta0=0.1, fit_intercept=True, n_iter=40, n_jobs=4, penalty=None, random_state=0, shuffle=True, verbose=0, warm_start=False) ppn.fit(X_train_std, y_train) y_pred = ppn.predict(X_test_std) print("y_pred: ", y_pred) print("y_test: ", y_test) We are printing Accuracy and Confusion Matrix for the test and predicted target value. print("Accuracy: %.2f" % accuracy_score(y_test, y_pred)) print("Comfusion Matrix: ", confusion_matrix(y_test, y_pred)) As an output we get

y_pred:  [1 2 2 2 0 2 2 1 2 2 2 1 1 1 0 2 2 0 0 1 2 2 0 1 0 0 0 0 1 2 0 0 0 1 0 0 2
 0 1 1 1 1 2 2 2]

y_test:  [1 1 2 2 0 1 2 0 2 1 2 1 0 1 0 2 2 0 0 1 2 2 0 1 0 1 0 0 1 2 0 1 1 1 0 0 2
 0 0 0 2 1 2 2 2]

Accuracy: 0.76

Comfusion Matrix:
 [[12  4  0]
 [ 3  8  3]
 [ 0  1 14]]

Relevant Projects

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.