Class: Rumale::LinearModel::LogisticRegression
- Inherits:
-
BaseEstimator
- Object
- Base::Estimator
- BaseEstimator
- Rumale::LinearModel::LogisticRegression
- Includes:
- Base::Classifier
- Defined in:
- rumale-linear_model/lib/rumale/linear_model/logistic_regression.rb
Overview
Note:
Rumale::SVM provides Logistic Regression based on LIBLINEAR. If you prefer execution speed, you should use Rumale::SVM::LogisticRegression. github.com/yoshoku/rumale-svm
LogisticRegression is a class that implements (multinomial) Logistic Regression.
Instance Attribute Summary collapse
-
#classes ⇒ Numo::Int32
readonly
Return the class labels.
Attributes inherited from BaseEstimator
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ LogisticRegression
Fit the model with given training data.
-
#initialize(reg_param: 1.0, fit_bias: true, bias_scale: 1.0, max_iter: 1000, tol: 1e-4, n_jobs: nil, verbose: false) ⇒ LogisticRegression
constructor
Create a new classifier with Logisitc Regression.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
-
#predict_proba(x) ⇒ Numo::DFloat
Predict probability for samples.
Methods included from Base::Classifier
Constructor Details
#initialize(reg_param: 1.0, fit_bias: true, bias_scale: 1.0, max_iter: 1000, tol: 1e-4, n_jobs: nil, verbose: false) ⇒ LogisticRegression
Create a new classifier with Logisitc Regression.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'rumale-linear_model/lib/rumale/linear_model/logistic_regression.rb', line 48 def initialize(reg_param: 1.0, fit_bias: true, bias_scale: 1.0, max_iter: 1000, tol: 1e-4, n_jobs: nil, verbose: false) super() @params = { reg_param: reg_param, fit_bias: fit_bias, bias_scale: bias_scale, max_iter: max_iter, tol: tol, n_jobs: n_jobs, verbose: verbose } end |
Instance Attribute Details
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
31 32 33 |
# File 'rumale-linear_model/lib/rumale/linear_model/logistic_regression.rb', line 31 def classes @classes end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
81 82 83 84 85 |
# File 'rumale-linear_model/lib/rumale/linear_model/logistic_regression.rb', line 81 def decision_function(x) x = Rumale::Validation.check_convert_sample_array(x) x.dot(@weight_vec.transpose) + @bias_term end |
#fit(x, y) ⇒ LogisticRegression
Fit the model with given training data.
66 67 68 69 70 71 72 73 74 75 |
# File 'rumale-linear_model/lib/rumale/linear_model/logistic_regression.rb', line 66 def fit(x, y) x = Rumale::Validation.check_convert_sample_array(x) y = Rumale::Validation.check_convert_label_array(y) Rumale::Validation.check_sample_size(x, y) @classes = Numo::Int32[*y.to_a.uniq.sort] @weight_vec, @bias_term = partial_fit(x, y) self end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'rumale-linear_model/lib/rumale/linear_model/logistic_regression.rb', line 91 def predict(x) x = Rumale::Validation.check_convert_sample_array(x) n_samples, = x.shape decision_values = predict_proba(x) predicted = if enable_parallel? parallel_map(n_samples) { |n| @classes[decision_values[n, true].max_index] } else Array.new(n_samples) { |n| @classes[decision_values[n, true].max_index] } end Numo::Int32.asarray(predicted) end |
#predict_proba(x) ⇒ Numo::DFloat
Predict probability for samples.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'rumale-linear_model/lib/rumale/linear_model/logistic_regression.rb', line 108 def predict_proba(x) x = Rumale::Validation.check_convert_sample_array(x) proba = 1.0 / (Numo::NMath.exp(-decision_function(x)) + 1.0) return (proba.transpose / proba.sum(axis: 1)).transpose.dup if multiclass_problem? n_samples, = x.shape probs = Numo::DFloat.zeros(n_samples, 2) probs[true, 1] = proba probs[true, 0] = 1.0 - proba probs end |