Skip to content

kaushalshetty/Stacking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation


Stacked Generalizer Classifier
Trains a series of base models using K-fold cross-validation, then combines
the predictions of each model into a set of features that are used to train
a high-level classifier model.




Usage

# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
from stacking import Stacking
from sklearn.model_selection import train_test_split


# In[2]:

df = pd.read_csv("train.csv")


# In[4]:

le = LabelEncoder()
le.fit(df.type)
y = le.transform(df.type)


# In[7]:

df.drop('id',1,inplace=True)
df.drop('color',1,inplace=True)
df.drop('type',1,inplace=True)


# In[11]:




# In[14]:

x = np.array(df,dtype=float)


X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)
estimators = [LogisticRegression(C=0.8),RandomForestClassifier(n_estimators=500)]
stack_model = LogisticRegression()
stk = Stacking(estimators,stack_model,use_prob=False,n_splits=5,verbose=1)
stk.fit(X_train,y_train)
y_pred = stk.predict(X_test)
from sklearn.metrics import classification_report,f1_score,accuracy_score,confusion_matrix
print "class rep",classification_report(y_test,y_pred)
print "confusion_matrix",confusion_matrix(y_test,y_pred)
print "accuracy_score",accuracy_score(y_test,y_pred)