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

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.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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.

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.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.