How to build a network using PyBrain

This recipe helps you build a network using PyBrain

Recipe Objective - How to build a network using PyBrain?

For more related projects -

https://www.dezyre.com/projects/data-science-projects/tensorflow-projects
https://www.dezyre.com/projects/data-science-projects/keras-deep-learning-projects

Let's try to build a network on the Iris dataset-

# Importing all the necessary libraries
from sklearn import datasets
import matplotlib.pyplot as plt
from pybrain.datasets import ClassificationDataSet
from pybrain.utilities import percentError
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules import SoftmaxLayer
from numpy import ravel

# Loading iris dataset from sklearn datasets
iris = datasets.load_iris()

# Defining feature variables and target variable
X_data = iris.data
y_data = iris.target

# Defining classification dataset model
classification_dataset = ClassificationDataSet(4, 1, nb_classes=3)

# Adding sample into classification dataset
for i in range(len(X_data)):
  classification_dataset.addSample(ravel(X_data[i]), y_data[i])

# Spilling data into testing and training data with the ratio 7:3
testing_data, training_data = classification_dataset.splitWithProportion(0.3)

# Classification dataset for test data
test_data = ClassificationDataSet(4, 1, nb_classes=3)

# Adding sample into testing classification dataset
for n in range(0, testing_data.getLength()):
   test_data.addSample( testing_data.getSample(n)[0], testing_data.getSample(n)[1] )

# Classification dataset for train data
train_data = ClassificationDataSet(4, 1, nb_classes=3)

# Adding sample into training classification dataset
for n in range(0, training_data.getLength()):
   train_data.addSample( training_data.getSample(n)[0], training_data.getSample(n)[1] )

test_data._convertToOneOfMany()
train_data._convertToOneOfMany()

# Building network with outclass as SoftmaxLayer on training data
build_network = buildNetwork(train_data.indim, 4, train_data.outdim, outclass=SoftmaxLayer)

# Building a backproptrainer on training data
trainer = BackpropTrainer(build_network, dataset=train_data, learningrate=0.01, verbose=True)

# 20 iterations on training data
trainer.trainEpochs(20)

# Testing data
print('Error percentage on testing data=>',percentError(trainer.testOnClassData(dataset=test_data), test_data['class']))

Output -
Total error:  0.116173324197
Total error:  0.108456354663
Total error:  0.10245801191
Total error:  0.0977106095499
Total error:  0.0929660056728
Total error:  0.0883331491649
Total error:  0.0848566302306
Total error:  0.0811810786595
Total error:  0.0781061808762
Total error:  0.0758569722131
Total error:  0.0735541564513
Total error:  0.0714176535205
Total error:  0.0695884320282
Total error:  0.0680868021349
Total error:  0.0665663288663
Total error:  0.0654873247161
Total error:  0.0642748540764
Total error:  0.0634026381591
Total error:  0.0625583220368
Total error:  0.0618042876573
Error percentage on testing data=> 42.22222222222222

In this way, we can build a network using pybrain.

​

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

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.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

OSZAR »