×

SKLearn Model Selection

  • The model selections of SK learn has many functions with which we can work on.
  • It has functions to cross-validate the model and, it also provides validation and learning curves.
  • It is used for hyperparameters to tune the estimators.
  • Let us consider a list of functions the module provides.

The Splitter Classes

  • ms.GroupKFold([ n_splits ]): It is similar to cross-validation. This function is mainly used to form non – overlapping groups.
  • ms.GroupShuffleSplit([ ... ]): This function is mainly used to shuffle groups outcross validation test.
  • ms.KFold([ n_splits, shuffle, ... ]): We can use this function to perform kformcross-validation test.
  • ms.LeaveOneGroupOut( ): The aim of this function is used to perform the leave one group out cross-validation test.
  • ms.LeavePGroupsOut( n_groups ): This function uses p group out to perform its activity. It used this group to perform the test.
  • ms.LeaveOneOut( ):  The aim of the function is used to perform leave one out cross-validation.
  • ms.LeavePOut( p ): The main aim of this function is to perform the cross validator with leave one out.
  • ms.PredefinedSplit( test_fold ): The main aim of the function is used to perform the test on the p-outcross-validation test. This function is the original version of the leave-one-out test.
  • ms.RepeatedKFold( *[, n_splits, ... ] ): This function is used to perform test on the predefined split and it perform cross-validation test.
  • ms.RepeatedStratifiedKFold( *[, ... ] ): The main purpose of this function is to perform the test on k-fold cross-validation which is repeatedly stratified.
  • ms.ShuffleSplit([n_splits, ... ]): The aim of the function is used to perform tests on datasetsthat are shuffled silt with the help of random permutation.
  • ms.StratifiedKFold([ n_splits, ... ]): This function is used to perform test of stratified k fold cross validation test.
  • ms.StratifiedShuffleSplit([ ... ]): The aim of the function is used to perform a test on a dataset of shuffle split cross-validationtests.
  • ms.StratifiedGroupKFold([ ... ]): The aim of the function is used to perform a test on data of nonoverlappinggroups to perform k fold cross-validation test.
  • ms.TimeSeriesSplit([ n_splits, ... ]): This function is mainly used for time series of cross-validation test.

Splitter Functions

  • ms.check_cv([ cv, y, classifier ]): This function helps in performing cross validation test by checking the utility.
  • ms.train_test_split( *arrays[, ...] ): This function separates the matrices into the training and it tests the data sets based on the random which is used to perform the cross-validation test.

Hyperparameter Optimizers

  • ms.GridSearchCV( estimator, ... ): The main aim of the function is to perform a search for an estimator with the defined parameters.
  • ms.HalvingGridSearchCV( ...[, ...] ): The main aim of the function is to perform the search with the given parameters with the help of successive halving.
  • ms.ParameterGrid( param_grid ): The main aim of the parameter is used to perform the grid of parameters, There is a different range of values for every parameter.
  • msParameterSampler(  ..[, ...] ): This function is taken from the given sample distribution, which works as a generator on samples of parameters.
  • ms.RandomizedSearchCV( ...[, ...] ): This function is used to work on the hyperparameters, which helps to perform a random search.
  • ms.HalvingRandomSearchCV( ...[, ...] ): This function is used to work on the hyperparameters, which helps to perform a random search.

Program:

# Python program onk-fold cross-validation test   
 # Import the library required  
from sklearn.datasets import load_iris  
from sklearn.linear_model import LogisticRegression  
from sklearn.model_selection import cross_val_score, KFold  
 # Loading the dataset  
i = load_iris()    
 # Getting dependent and independent features  
A = i.data  
B= i.target  
print("The size of the Dataset is : ", len( A ))      
# Creating a object of logistic regression
log = LogisticRegression()     
# Perform the logistic regression on dataset of train
log.fit( X, Y )     
# Performing K-fold cross-validation test  
k = KFold(n_splits = 5)  
s= cross_val_score(log, A,B, cv=k)   
# Printing accuracy of the scores  
print("Validation scores ofK-fold Cross :", s)  
print("Validation score of Mean Cross : ", s.mean())

Output:

The size of the Dataset is: 100
Validation Scores of K-fold Cross: [2.  2  0.86333667 0.91111133 0.89999999]
Validation score of mean cross:  0.95688888889990

Related Topics

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

7 minutes read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

Python hasattr() function

Python hasattr() function The hasattr() function in Python returns a Boolean value ‘True’ if the given object has the specified attribute, else it returns False. Syntax hasattr(object, name) Parameter object: it is a required parameter which represents an object. attribute:...

1 minute read.

Python and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

Difference Between Python and Scala

What is Python? Python is a high-level general-purpose interpreted programing language. It is used for multi general-purpose work such as language construct as well as its object-oriented approach aims to help...

4 minutes read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Rank Based Percentile GUI Calculator using Tkinter in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

3 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.