value error happens when using GridSearchCV

2024/12/9 20:39:06

I am using GridSearchCV to do classification and my codes are:

parameter_grid_SVM = {'dual':[True,False],'loss':["squared_hinge","hinge"],'penalty':["l1","l2"] }
clf = GridSearchCV(LinearSVC(),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)

And then, I meet the error

ValueError: Unsupported set of arguments: penalty='l1' is only supported when dual='false'., Parameters: penalty='l1', loss='hinge', dual=False

later on I change my code to :

clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),verbose=2)

And I meet the error

TypeError: init() takes at least 3 arguments (3 given)

I also tried:

parameter_grid_SVM = {'loss':["squared_hinge"]}
clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)

However, I still have the error

ValueError: Unsupported set of arguments: penalty='l1' is only supported when dual='false'., Parameters: penalty='l1', loss='squared_hinge', dual=False

Anyone has idea what I should do to deal with that?

Answer

I also met this problem when doing sparse SVM. I find one piece of working demo code in this page SVM module explanation. Hope it might help.

clf = LinearSVC(loss='l2', penalty='l1', dual=False)

https://en.xdnf.cn/q/72900.html

Related Q&A

How to remove english text from arabic string in python?

I have an Arabic string with English text and punctuations. I need to filter Arabic text and I tried removing punctuations and English words using sting. However, I lost the spacing between Arabic word…

python module pandas has no attribute plotting

I am a beginner of Python. I follow the machine learning course of Intel. And I encounter some troubles in coding. I run the code below in Jupyter and it raises an AttributeError.import pandas as pd st…

pandass resample with fill_method: Need to know data from which row was copied?

I am trying to use resample method to fill the gaps in timeseries data. But I also want to know which row was used to fill the missed data.This is my input series.In [28]: data Out[28]: Date 2002-09-0…

Inefficient multiprocessing of numpy-based calculations

Im trying to parallelize some calculations that use numpy with the help of Pythons multiprocessing module. Consider this simplified example:import time import numpyfrom multiprocessing import Pooldef t…

SQLite: return only top 2 results within each group

I checked other solutions to similar problems, but sqlite does not support row_number() and rank() functions or there are no examples which involve joining multiple tables, grouping them by multiple co…

Python list.append if not in list vs set.add performance [duplicate]

This question already has answers here:Which is faster and why? Set or List?(3 answers)Closed 6 years ago.Which is more performant, and what is asymptotic complexity (or are they equivalent) in Pytho…

using the hardware rng from python

Are there any ready made libraries so that the intel hardware prng (rdrand) can be used by numpy programs to fill buffers of random numbers?Failing this can someone point me in the right direction for…

How do I revert sys.stdout.close()?

In the interactive console:>>> import sys >>> sys.stdout <open file <stdout>, mode w at 0xb7810078> >>> sys.stdout.close() >>> sys.stdout # confirming th…

Find a value from x axis that correspond to y axis in matplotlib python

I am trying to do simple task such as to read values of x axis that corresponds to value of y axis in matplotlib and I cannot see what is wrong. In this case I am interested for example to find which v…

Django accessing OneToOneField

Made a view that extended User:class Client(models.Model):user = models.OneToOneField(User, related_name=user)def __unicode__(self):return "%s" % (self.user) I am currently trying to access…