GithubHelp home page GithubHelp logo

ryan102590 / stacked_generalization Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dustinstansbury/stacked_generalization

0.0 0.0 0.0 26 KB

Python implementation of stacked generalization classifier. Plays nice with sklearn.

License: GNU General Public License v3.0

Python 100.00%

stacked_generalization's Introduction

stacked_generalization

Python implementation of stacked generalization classifier, as described here.

Plays nice with sklearn classifiers, or any model classes that have both .fit and .predict methods.

Installation

Currently the package is not on PyPi, but is easy to install directly from github via pip using the following command.

pip install -e 'git+http://github.com/dustinstansbury/stacked_generalization.git#egg=stacked_generalization'

Example usage

The following example builds a stacked generalizer model to classify the digits dataset available in scikits-learn. The three base models (two RandomForest classifiers with different optimization criterion, and a ExtraTreesClassifier) are estimated with 5-fold cross-validation. The outputs of the fit base models are used as features inputs to the LogisticRegression blending model, which is also trained with 5-fold cross-validation. The models are trained on 80 percent of the digits dataset and accuracy is evaluated on the remaining 20 percent.

from sklearn.datasets import load_digits
from stacked_generalizer import StackedGeneralizer
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.linear_model import LogisticRegression
import numpy as np

VERBOSE = True
N_FOLDS = 5

# load data and shuffle observations
data = load_digits()

X = data.data
y = data.target

shuffle_idx = np.random.permutation(y.size)

X = X[shuffle_idx]
y = y[shuffle_idx]

# hold out 20 percent of data for testing accuracy
train_prct = 0.8
n_train = int(round(X.shape[0]*train_prct))

# define base models
base_models = [RandomForestClassifier(n_estimators=100, n_jobs=-1, criterion='gini'),
               RandomForestClassifier(n_estimators=100, n_jobs=-1, criterion='entropy'),
               ExtraTreesClassifier(n_estimators=100, n_jobs=-1, criterion='gini')]

# define blending model
blending_model = LogisticRegression()

# initialize multi-stage model
sg = StackedGeneralizer(base_models, blending_model, 
	                    n_folds=N_FOLDS, verbose=VERBOSE)

# fit model
sg.fit(X[:n_train],y[:n_train])

# test accuracy
pred = sg.predict(X[n_train:])
pred_classes = [np.argmax(p) for p in pred]

_ = sg.evaluate(y[n_train:], pred_classes)


       		 precision    recall  f1-score   support

          0       1.00      1.00      1.00        31
          1       0.95      1.00      0.97        39
          2       1.00      1.00      1.00        40
          3       1.00      0.97      0.99        38
          4       1.00      0.97      0.99        37
          5       1.00      0.97      0.99        35
          6       1.00      1.00      1.00        32
          7       0.95      1.00      0.97        37
          8       1.00      0.94      0.97        35
          9       0.92      0.94      0.93        35

avg / total       0.98      0.98      0.98       359

Confusion Matrix:
[[31  0  0  0  0  0  0  0  0  0]
 [ 0 39  0  0  0  0  0  0  0  0]
 [ 0  0 40  0  0  0  0  0  0  0]
 [ 0  0  0 37  0  0  0  1  0  0]
 [ 0  0  0  0 36  0  0  0  0  1]
 [ 0  0  0  0  0 34  0  0  0  1]
 [ 0  0  0  0  0  0 32  0  0  0]
 [ 0  0  0  0  0  0  0 37  0  0]
 [ 0  1  0  0  0  0  0  0 33  1]
 [ 0  1  0  0  0  0  0  1  0 33]]

stacked_generalization's People

Contributors

8tracks-datascience avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.