from sklearn.model_selection import KFold from sklearn.model_selection import cross_val_score from sklearn.datasets import make_classification from deslib.dcs.lca import LCA X, y = make_classification(n_samples=200, n_features=5, n_informative=4, n_redundant=1, n_repeated=0, n_classes=3, shuffle=True, random_state=1) model = LCA() kfold = KFold(n_splits=10, shuffle=True, random_state=1) scores = cross_val_score(model, X, y, cv=kfold, scoring="accuracy") print("Accuracy: ", scores.mean())
We are here using the deslib library for DCS with LCA. Firstly, we are generating two ndarrays X and y using the function make_classification (How to generate a dataset using make_classification()?). X contains all the features, and y contains the target variable.
X, y = make_classification(n_samples=200, n_features=5, n_informative=4, n_redundant=1, n_repeated=0, n_classes=3, shuffle=True, random_state=1)
Now, we are initializing the model using the LCA class. Please note that if we do not provide a list of models, then bagging classifiers are used by default.
model = LCA()
We are using k-fold cross-validation with 10 splits. We are shuffling the data before splitting, and the argument random_state is used to initialize the pseudo-random number generator that is used for randomization.
kfold = KFold(n_splits=10, shuffle=True, random_state=1)
Now, we are using the cross_val_score() function to evaluate the performance of the model. We are using the accuracy score here (What is the accuracy score in machine learning?)
scores = cross_val_score(model, X, y, cv=kfold, scoring="accuracy")
Then, we are printing the average accuracy score for the model. The output of the given program will be:
Accuracy: 0.655






0 Comments