Thundersvm source code viewing and learning
Article Directory
Estimator and Mixin
In the library introduction section, see that two classes are inherited thundersvm
from itsklearn.base
One is BaseEstimator
the base evaluator class and the other is Mixin
a helper class
In scikit-learn, an estimator is an object that implements a machine learning algorithm for fitting and predicting data . For example, linear regression models, decision tree classifiers, etc. are all estimators.
Mixin is a technique for mixing (mix-in) functions in a class , and only two kinds of Mixin are introduced here : RegressionMixin
and ClassfierMixin
, namely, regression tasks and classification tasks
The main functions provided by these two mixin classes are as follows:
RegressorMixin
Provides methods and attributes commonly used in regression tasksfit
, such as (fitting the model),predict
(making predictions),score
(calculating the performance score of the model), etc.ClassifierMixin
Provides methods and attributes commonly used in classification tasksfit
, such as ,predict
,score
, and other classification-specific methods, such aspredict_proba
(returns the probability that a sample belongs to each category) , **decision_function
(returns the decision function value of a sample)**, etc.
In the source code of the evaluator sklearn.base
, you can see the following functions
Basically, there are functions related to the acquisition and setting ofBaseEstimator
some parameters , that is, BaseEstimator mainly realizes the functions related to the input and setting of parameters
There are many types of Mixin , which mainly implement related functions such as model training and evaluation
question
Then in the initial introduction, it is said that the evaluator is used to fit and predict the data, but seeing the source code, it implements some functions related to parameter passing and setting
- So how does fitting and prediction work in the estimator?
- And Mixin mainly implements functions such as model training and evaluation, what is the relationship between it and Mixin auxiliary classes?
- What is its specific combination and calling method?
understand
Obviously, the evaluator calls a function in the helper class .
The estimator class usually defines some general methods, such as functions for passing in and returning parameters, but does not directly define specific training and evaluation functions . Instead, the estimator class leverages inherited helper classes for specific training and evaluation functionality.
That is to say, sklearn.base
the idea of function construction is based on the following points:
- Separate the basic functions such as parameter input settings from the definition of the function in the training . Because there are many types of training tasks, the relevant function definitions are made for each training type, and then implemented in the specific training function call
- Pass parameters into basic functions such as settings as the main part of the evaluator as baseEstimator
- Inherit the variable and function definitions in BaseEstimator in the specific Estimator to implement specific training details
- For different types of training tasks, considering that the required functions are not uniform, some tasks require specific functions , so define different types of Mixin base classes
In a specific task, such as svm, it is necessary to build an svm evaluator, which inherits BaseEstimator, and then calls the corresponding Mixin auxiliary class to train and evaluate the model according to the task type of svm training
And thundersvm is exactly this way of implementation
After naming it, inherit ThundersvmBase in the svm model, which is BaseEstimator
Then pass in parameters and implement related functions
OS verification identification
You can see that Thundersvm is supported for Windows , Linux and Mac
Darwin refers to Darwin, which is Mac OS , and has nothing to do with Windows
function implementation
It only implements the detailed implementation of SvmModel, and adopts the method of inheriting SvmModel for other algorithms, and adds the inheritance of Mixin auxiliary class according to the algorithm and the corresponding task type