Skip to content

FIX handle properly null weights in SVC #27763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sklearn/svm/_libsvm_sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def libsvm_sparse_train (int n_features,
blas_functions.dot = _dot[double]
# call svm_train, this does the real work
cdef int fit_status = 0

with nogil:
model = svm_csr_train(problem, param, &fit_status, &blas_functions)

Expand Down
39 changes: 20 additions & 19 deletions sklearn/svm/src/liblinear/linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2424,26 +2424,27 @@ static int train_one(const problem *prob, const parameter *param, double *w, dou
//
// Remove zero weighed data as libsvm and some liblinear solvers require C > 0.
//
static void remove_zero_weight(problem *newprob, const problem *prob)
static void remove_negative_and_null_weights(problem *newprob, const problem *prob)
{
int i;
int l = 0;
for(i=0;i<prob->l;i++)
if(prob->W[i] > 0) l++;
*newprob = *prob;
newprob->l = l;
newprob->x = Malloc(feature_node*,l);
newprob->y = Malloc(double,l);
newprob->W = Malloc(double,l);
int sample_idx;
int n_samples = prob->l;
int n_positive_samples = 0;
for(sample_idx=0; sample_idx < n_samples; sample_idx++)
if(prob->W[sample_idx] > 0) n_positive_samples++;

int j = 0;
for(i=0;i<prob->l;i++)
if(prob->W[i] > 0)
{
newprob->x[j] = prob->x[i];
newprob->y[j] = prob->y[i];
newprob->W[j] = prob->W[i];
j++;
*newprob = *prob;
newprob->l = n_positive_samples;
newprob->x = Malloc(feature_node*, n_positive_samples);
newprob->y = Malloc(double, n_positive_samples);
newprob->W = Malloc(double, n_positive_samples);

int subsample_idx = 0;
for(sample_idx=0; sample_idx < n_samples; sample_idx++)
if(prob->W[sample_idx] > 0) {
newprob->x[subsample_idx] = prob->x[sample_idx];
newprob->y[subsample_idx] = prob->y[sample_idx];
newprob->W[subsample_idx] = prob->W[sample_idx];
subsample_idx++;
}
}

Expand All @@ -2453,7 +2454,7 @@ static void remove_zero_weight(problem *newprob, const problem *prob)
model* train(const problem *prob, const parameter *param, BlasFunctions *blas_functions)
{
problem newprob;
remove_zero_weight(&newprob, prob);
remove_negative_and_null_weights(&newprob, prob);
prob = &newprob;
int i,j;
int l = prob->l;
Expand Down
Loading