How to prepare a machine learning workflow in Python?

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

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 = y =

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) 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), 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]]

Download Materials

Relevant Projects

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Loan Eligibility Prediction in Python using
In this loan prediction project you will build predictive models in Python using to predict if an applicant is able to repay the loan or not.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Creating your own embeddings using Glove and Word2vec
We all at some point in time wished to create our own language as a child! But what if certain words always cooccur with another in a corpus? Thus you can make your own model which will understand which word goes with which one, which words are often coming together etc. This all can be done by building a custom embeddings model which we create in this project

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.