Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

ensemble model using voting classifier

# Set seed for reproducibility
SEED=1
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
# Instantiate lr
lr = LogisticRegression(random_state=SEED)

# Instantiate knn
knn = KNN(n_neighbors=27)

# Instantiate dt
dt = DecisionTreeClassifier(min_samples_leaf=0.13, random_state=SEED)

# Define the list classifiers
classifiers = [('Logistic Regression', lr), ('K Nearest Neighbours', knn), ('Classification Tree', dt)]
# Iterate over the pre-defined list of classifiers
for clf_name, clf in classifiers:    
  
    # Fit clf to the training set
    clf.fit(X_train, y_train)    
  
    # Predict y_pred
    y_pred = clf.predict(X_test)
    
    # Calculate accuracy
    accuracy = accuracy_score(y_test, y_pred)
  
    # Evaluate clf's accuracy on the test set
    print('{:s} : {:.3f}'.format(clf_name, accuracy))
# Import VotingClassifier from sklearn.ensemble
from sklearn.ensemble import VotingClassifier

# Instantiate a VotingClassifier vc 
vc = VotingClassifier(estimators=classifiers)     

# Fit vc to the training set
vc.fit(X_train, y_train)   

# Evaluate the test set predictions
y_pred = vc.predict(X_test)

# Calculate accuracy score
accuracy = accuracy_score(y_test, y_pred)
print('Voting Classifier: {:.3f}'.format(accuracy))
Comment

PREVIOUS NEXT
Code Example
Python :: python open file check error 
Python :: python find string in string 
Python :: speech to text 
Python :: check whether number is even or odd 
Python :: plotly create plot 
Python :: h2o ai python 
Python :: create folders in python overwright existing 
Python :: how to calculate the google map distance in python 
Python :: mistborn series 
Python :: how to check whether input is string or not 
Python :: Using python-poppler 
Python :: class chain methods python 
Python :: normal discord.py codes 
Python :: hh:mm to mins in python 
Python :: convert string to float python 
Python :: flask get with parameters 
Python :: async asyncio input 
Python :: python check if string is float 
Python :: migrations.RunPython 
Python :: linear search implementation 
Python :: how to pre populate field flask wtforms 
Python :: python dictionary get vs setdefault 
Python :: python string is in list 
Python :: how to insert values to database with using dictionary in python 
Python :: python use math 
Python :: change xlabel python 
Python :: python convert float to whole part of number 
Python :: model coefficients 
Python :: python how to iterate through a list of lists 
Python :: hierarchy dendrogram 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =