_classes.py 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. import warnings
  2. from numbers import Integral, Real
  3. import numpy as np
  4. from ..base import BaseEstimator, OutlierMixin, RegressorMixin, _fit_context
  5. from ..linear_model._base import LinearClassifierMixin, LinearModel, SparseCoefMixin
  6. from ..utils import deprecated
  7. from ..utils._param_validation import Hidden, Interval, StrOptions
  8. from ..utils.multiclass import check_classification_targets
  9. from ..utils.validation import _num_samples
  10. from ._base import BaseLibSVM, BaseSVC, _fit_liblinear, _get_liblinear_solver_type
  11. def _validate_dual_parameter(dual, loss, penalty, multi_class, X):
  12. """Helper function to assign the value of dual parameter."""
  13. if dual == "auto":
  14. if X.shape[0] < X.shape[1]:
  15. try:
  16. _get_liblinear_solver_type(multi_class, penalty, loss, True)
  17. return True
  18. except ValueError: # dual not supported for the combination
  19. return False
  20. else:
  21. try:
  22. _get_liblinear_solver_type(multi_class, penalty, loss, False)
  23. return False
  24. except ValueError: # primal not supported by the combination
  25. return True
  26. # TODO 1.5
  27. elif dual == "warn":
  28. warnings.warn(
  29. (
  30. "The default value of `dual` will change from `True` to `'auto'` in"
  31. " 1.5. Set the value of `dual` explicitly to suppress the warning."
  32. ),
  33. FutureWarning,
  34. )
  35. return True
  36. else:
  37. return dual
  38. class LinearSVC(LinearClassifierMixin, SparseCoefMixin, BaseEstimator):
  39. """Linear Support Vector Classification.
  40. Similar to SVC with parameter kernel='linear', but implemented in terms of
  41. liblinear rather than libsvm, so it has more flexibility in the choice of
  42. penalties and loss functions and should scale better to large numbers of
  43. samples.
  44. The main differences between :class:`~sklearn.svm.LinearSVC` and
  45. :class:`~sklearn.svm.SVC` lie in the loss function used by default, and in
  46. the handling of intercept regularization between those two implementations.
  47. This class supports both dense and sparse input and the multiclass support
  48. is handled according to a one-vs-the-rest scheme.
  49. Read more in the :ref:`User Guide <svm_classification>`.
  50. Parameters
  51. ----------
  52. penalty : {'l1', 'l2'}, default='l2'
  53. Specifies the norm used in the penalization. The 'l2'
  54. penalty is the standard used in SVC. The 'l1' leads to ``coef_``
  55. vectors that are sparse.
  56. loss : {'hinge', 'squared_hinge'}, default='squared_hinge'
  57. Specifies the loss function. 'hinge' is the standard SVM loss
  58. (used e.g. by the SVC class) while 'squared_hinge' is the
  59. square of the hinge loss. The combination of ``penalty='l1'``
  60. and ``loss='hinge'`` is not supported.
  61. dual : "auto" or bool, default=True
  62. Select the algorithm to either solve the dual or primal
  63. optimization problem. Prefer dual=False when n_samples > n_features.
  64. `dual="auto"` will choose the value of the parameter automatically,
  65. based on the values of `n_samples`, `n_features`, `loss`, `multi_class`
  66. and `penalty`. If `n_samples` < `n_features` and optimizer supports
  67. chosen `loss`, `multi_class` and `penalty`, then dual will be set to True,
  68. otherwise it will be set to False.
  69. .. versionchanged:: 1.3
  70. The `"auto"` option is added in version 1.3 and will be the default
  71. in version 1.5.
  72. tol : float, default=1e-4
  73. Tolerance for stopping criteria.
  74. C : float, default=1.0
  75. Regularization parameter. The strength of the regularization is
  76. inversely proportional to C. Must be strictly positive.
  77. multi_class : {'ovr', 'crammer_singer'}, default='ovr'
  78. Determines the multi-class strategy if `y` contains more than
  79. two classes.
  80. ``"ovr"`` trains n_classes one-vs-rest classifiers, while
  81. ``"crammer_singer"`` optimizes a joint objective over all classes.
  82. While `crammer_singer` is interesting from a theoretical perspective
  83. as it is consistent, it is seldom used in practice as it rarely leads
  84. to better accuracy and is more expensive to compute.
  85. If ``"crammer_singer"`` is chosen, the options loss, penalty and dual
  86. will be ignored.
  87. fit_intercept : bool, default=True
  88. Whether or not to fit an intercept. If set to True, the feature vector
  89. is extended to include an intercept term: `[x_1, ..., x_n, 1]`, where
  90. 1 corresponds to the intercept. If set to False, no intercept will be
  91. used in calculations (i.e. data is expected to be already centered).
  92. intercept_scaling : float, default=1.0
  93. When `fit_intercept` is True, the instance vector x becomes ``[x_1,
  94. ..., x_n, intercept_scaling]``, i.e. a "synthetic" feature with a
  95. constant value equal to `intercept_scaling` is appended to the instance
  96. vector. The intercept becomes intercept_scaling * synthetic feature
  97. weight. Note that liblinear internally penalizes the intercept,
  98. treating it like any other term in the feature vector. To reduce the
  99. impact of the regularization on the intercept, the `intercept_scaling`
  100. parameter can be set to a value greater than 1; the higher the value of
  101. `intercept_scaling`, the lower the impact of regularization on it.
  102. Then, the weights become `[w_x_1, ..., w_x_n,
  103. w_intercept*intercept_scaling]`, where `w_x_1, ..., w_x_n` represent
  104. the feature weights and the intercept weight is scaled by
  105. `intercept_scaling`. This scaling allows the intercept term to have a
  106. different regularization behavior compared to the other features.
  107. class_weight : dict or 'balanced', default=None
  108. Set the parameter C of class i to ``class_weight[i]*C`` for
  109. SVC. If not given, all classes are supposed to have
  110. weight one.
  111. The "balanced" mode uses the values of y to automatically adjust
  112. weights inversely proportional to class frequencies in the input data
  113. as ``n_samples / (n_classes * np.bincount(y))``.
  114. verbose : int, default=0
  115. Enable verbose output. Note that this setting takes advantage of a
  116. per-process runtime setting in liblinear that, if enabled, may not work
  117. properly in a multithreaded context.
  118. random_state : int, RandomState instance or None, default=None
  119. Controls the pseudo random number generation for shuffling the data for
  120. the dual coordinate descent (if ``dual=True``). When ``dual=False`` the
  121. underlying implementation of :class:`LinearSVC` is not random and
  122. ``random_state`` has no effect on the results.
  123. Pass an int for reproducible output across multiple function calls.
  124. See :term:`Glossary <random_state>`.
  125. max_iter : int, default=1000
  126. The maximum number of iterations to be run.
  127. Attributes
  128. ----------
  129. coef_ : ndarray of shape (1, n_features) if n_classes == 2 \
  130. else (n_classes, n_features)
  131. Weights assigned to the features (coefficients in the primal
  132. problem).
  133. ``coef_`` is a readonly property derived from ``raw_coef_`` that
  134. follows the internal memory layout of liblinear.
  135. intercept_ : ndarray of shape (1,) if n_classes == 2 else (n_classes,)
  136. Constants in decision function.
  137. classes_ : ndarray of shape (n_classes,)
  138. The unique classes labels.
  139. n_features_in_ : int
  140. Number of features seen during :term:`fit`.
  141. .. versionadded:: 0.24
  142. feature_names_in_ : ndarray of shape (`n_features_in_`,)
  143. Names of features seen during :term:`fit`. Defined only when `X`
  144. has feature names that are all strings.
  145. .. versionadded:: 1.0
  146. n_iter_ : int
  147. Maximum number of iterations run across all classes.
  148. See Also
  149. --------
  150. SVC : Implementation of Support Vector Machine classifier using libsvm:
  151. the kernel can be non-linear but its SMO algorithm does not
  152. scale to large number of samples as LinearSVC does.
  153. Furthermore SVC multi-class mode is implemented using one
  154. vs one scheme while LinearSVC uses one vs the rest. It is
  155. possible to implement one vs the rest with SVC by using the
  156. :class:`~sklearn.multiclass.OneVsRestClassifier` wrapper.
  157. Finally SVC can fit dense data without memory copy if the input
  158. is C-contiguous. Sparse data will still incur memory copy though.
  159. sklearn.linear_model.SGDClassifier : SGDClassifier can optimize the same
  160. cost function as LinearSVC
  161. by adjusting the penalty and loss parameters. In addition it requires
  162. less memory, allows incremental (online) learning, and implements
  163. various loss functions and regularization regimes.
  164. Notes
  165. -----
  166. The underlying C implementation uses a random number generator to
  167. select features when fitting the model. It is thus not uncommon
  168. to have slightly different results for the same input data. If
  169. that happens, try with a smaller ``tol`` parameter.
  170. The underlying implementation, liblinear, uses a sparse internal
  171. representation for the data that will incur a memory copy.
  172. Predict output may not match that of standalone liblinear in certain
  173. cases. See :ref:`differences from liblinear <liblinear_differences>`
  174. in the narrative documentation.
  175. References
  176. ----------
  177. `LIBLINEAR: A Library for Large Linear Classification
  178. <https://www.csie.ntu.edu.tw/~cjlin/liblinear/>`__
  179. Examples
  180. --------
  181. >>> from sklearn.svm import LinearSVC
  182. >>> from sklearn.pipeline import make_pipeline
  183. >>> from sklearn.preprocessing import StandardScaler
  184. >>> from sklearn.datasets import make_classification
  185. >>> X, y = make_classification(n_features=4, random_state=0)
  186. >>> clf = make_pipeline(StandardScaler(),
  187. ... LinearSVC(dual="auto", random_state=0, tol=1e-5))
  188. >>> clf.fit(X, y)
  189. Pipeline(steps=[('standardscaler', StandardScaler()),
  190. ('linearsvc', LinearSVC(dual='auto', random_state=0, tol=1e-05))])
  191. >>> print(clf.named_steps['linearsvc'].coef_)
  192. [[0.141... 0.526... 0.679... 0.493...]]
  193. >>> print(clf.named_steps['linearsvc'].intercept_)
  194. [0.1693...]
  195. >>> print(clf.predict([[0, 0, 0, 0]]))
  196. [1]
  197. """
  198. _parameter_constraints: dict = {
  199. "penalty": [StrOptions({"l1", "l2"})],
  200. "loss": [StrOptions({"hinge", "squared_hinge"})],
  201. "dual": ["boolean", StrOptions({"auto"}), Hidden(StrOptions({"warn"}))],
  202. "tol": [Interval(Real, 0.0, None, closed="neither")],
  203. "C": [Interval(Real, 0.0, None, closed="neither")],
  204. "multi_class": [StrOptions({"ovr", "crammer_singer"})],
  205. "fit_intercept": ["boolean"],
  206. "intercept_scaling": [Interval(Real, 0, None, closed="neither")],
  207. "class_weight": [None, dict, StrOptions({"balanced"})],
  208. "verbose": ["verbose"],
  209. "random_state": ["random_state"],
  210. "max_iter": [Interval(Integral, 0, None, closed="left")],
  211. }
  212. def __init__(
  213. self,
  214. penalty="l2",
  215. loss="squared_hinge",
  216. *,
  217. dual="warn",
  218. tol=1e-4,
  219. C=1.0,
  220. multi_class="ovr",
  221. fit_intercept=True,
  222. intercept_scaling=1,
  223. class_weight=None,
  224. verbose=0,
  225. random_state=None,
  226. max_iter=1000,
  227. ):
  228. self.dual = dual
  229. self.tol = tol
  230. self.C = C
  231. self.multi_class = multi_class
  232. self.fit_intercept = fit_intercept
  233. self.intercept_scaling = intercept_scaling
  234. self.class_weight = class_weight
  235. self.verbose = verbose
  236. self.random_state = random_state
  237. self.max_iter = max_iter
  238. self.penalty = penalty
  239. self.loss = loss
  240. @_fit_context(prefer_skip_nested_validation=True)
  241. def fit(self, X, y, sample_weight=None):
  242. """Fit the model according to the given training data.
  243. Parameters
  244. ----------
  245. X : {array-like, sparse matrix} of shape (n_samples, n_features)
  246. Training vector, where `n_samples` is the number of samples and
  247. `n_features` is the number of features.
  248. y : array-like of shape (n_samples,)
  249. Target vector relative to X.
  250. sample_weight : array-like of shape (n_samples,), default=None
  251. Array of weights that are assigned to individual
  252. samples. If not provided,
  253. then each sample is given unit weight.
  254. .. versionadded:: 0.18
  255. Returns
  256. -------
  257. self : object
  258. An instance of the estimator.
  259. """
  260. X, y = self._validate_data(
  261. X,
  262. y,
  263. accept_sparse="csr",
  264. dtype=np.float64,
  265. order="C",
  266. accept_large_sparse=False,
  267. )
  268. check_classification_targets(y)
  269. self.classes_ = np.unique(y)
  270. _dual = _validate_dual_parameter(
  271. self.dual, self.loss, self.penalty, self.multi_class, X
  272. )
  273. self.coef_, self.intercept_, n_iter_ = _fit_liblinear(
  274. X,
  275. y,
  276. self.C,
  277. self.fit_intercept,
  278. self.intercept_scaling,
  279. self.class_weight,
  280. self.penalty,
  281. _dual,
  282. self.verbose,
  283. self.max_iter,
  284. self.tol,
  285. self.random_state,
  286. self.multi_class,
  287. self.loss,
  288. sample_weight=sample_weight,
  289. )
  290. # Backward compatibility: _fit_liblinear is used both by LinearSVC/R
  291. # and LogisticRegression but LogisticRegression sets a structured
  292. # `n_iter_` attribute with information about the underlying OvR fits
  293. # while LinearSVC/R only reports the maximum value.
  294. self.n_iter_ = n_iter_.max().item()
  295. if self.multi_class == "crammer_singer" and len(self.classes_) == 2:
  296. self.coef_ = (self.coef_[1] - self.coef_[0]).reshape(1, -1)
  297. if self.fit_intercept:
  298. intercept = self.intercept_[1] - self.intercept_[0]
  299. self.intercept_ = np.array([intercept])
  300. return self
  301. def _more_tags(self):
  302. return {
  303. "_xfail_checks": {
  304. "check_sample_weights_invariance": (
  305. "zero sample_weight is not equivalent to removing samples"
  306. ),
  307. }
  308. }
  309. class LinearSVR(RegressorMixin, LinearModel):
  310. """Linear Support Vector Regression.
  311. Similar to SVR with parameter kernel='linear', but implemented in terms of
  312. liblinear rather than libsvm, so it has more flexibility in the choice of
  313. penalties and loss functions and should scale better to large numbers of
  314. samples.
  315. The main differences between :class:`~sklearn.svm.LinearSVR` and
  316. :class:`~sklearn.svm.SVR` lie in the loss function used by default, and in
  317. the handling of intercept regularization between those two implementations.
  318. This class supports both dense and sparse input.
  319. Read more in the :ref:`User Guide <svm_regression>`.
  320. .. versionadded:: 0.16
  321. Parameters
  322. ----------
  323. epsilon : float, default=0.0
  324. Epsilon parameter in the epsilon-insensitive loss function. Note
  325. that the value of this parameter depends on the scale of the target
  326. variable y. If unsure, set ``epsilon=0``.
  327. tol : float, default=1e-4
  328. Tolerance for stopping criteria.
  329. C : float, default=1.0
  330. Regularization parameter. The strength of the regularization is
  331. inversely proportional to C. Must be strictly positive.
  332. loss : {'epsilon_insensitive', 'squared_epsilon_insensitive'}, \
  333. default='epsilon_insensitive'
  334. Specifies the loss function. The epsilon-insensitive loss
  335. (standard SVR) is the L1 loss, while the squared epsilon-insensitive
  336. loss ('squared_epsilon_insensitive') is the L2 loss.
  337. fit_intercept : bool, default=True
  338. Whether or not to fit an intercept. If set to True, the feature vector
  339. is extended to include an intercept term: `[x_1, ..., x_n, 1]`, where
  340. 1 corresponds to the intercept. If set to False, no intercept will be
  341. used in calculations (i.e. data is expected to be already centered).
  342. intercept_scaling : float, default=1.0
  343. When `fit_intercept` is True, the instance vector x becomes `[x_1, ...,
  344. x_n, intercept_scaling]`, i.e. a "synthetic" feature with a constant
  345. value equal to `intercept_scaling` is appended to the instance vector.
  346. The intercept becomes intercept_scaling * synthetic feature weight.
  347. Note that liblinear internally penalizes the intercept, treating it
  348. like any other term in the feature vector. To reduce the impact of the
  349. regularization on the intercept, the `intercept_scaling` parameter can
  350. be set to a value greater than 1; the higher the value of
  351. `intercept_scaling`, the lower the impact of regularization on it.
  352. Then, the weights become `[w_x_1, ..., w_x_n,
  353. w_intercept*intercept_scaling]`, where `w_x_1, ..., w_x_n` represent
  354. the feature weights and the intercept weight is scaled by
  355. `intercept_scaling`. This scaling allows the intercept term to have a
  356. different regularization behavior compared to the other features.
  357. dual : "auto" or bool, default=True
  358. Select the algorithm to either solve the dual or primal
  359. optimization problem. Prefer dual=False when n_samples > n_features.
  360. `dual="auto"` will choose the value of the parameter automatically,
  361. based on the values of `n_samples`, `n_features` and `loss`. If
  362. `n_samples` < `n_features` and optimizer supports chosen `loss`,
  363. then dual will be set to True, otherwise it will be set to False.
  364. .. versionchanged:: 1.3
  365. The `"auto"` option is added in version 1.3 and will be the default
  366. in version 1.5.
  367. verbose : int, default=0
  368. Enable verbose output. Note that this setting takes advantage of a
  369. per-process runtime setting in liblinear that, if enabled, may not work
  370. properly in a multithreaded context.
  371. random_state : int, RandomState instance or None, default=None
  372. Controls the pseudo random number generation for shuffling the data.
  373. Pass an int for reproducible output across multiple function calls.
  374. See :term:`Glossary <random_state>`.
  375. max_iter : int, default=1000
  376. The maximum number of iterations to be run.
  377. Attributes
  378. ----------
  379. coef_ : ndarray of shape (n_features) if n_classes == 2 \
  380. else (n_classes, n_features)
  381. Weights assigned to the features (coefficients in the primal
  382. problem).
  383. `coef_` is a readonly property derived from `raw_coef_` that
  384. follows the internal memory layout of liblinear.
  385. intercept_ : ndarray of shape (1) if n_classes == 2 else (n_classes)
  386. Constants in decision function.
  387. n_features_in_ : int
  388. Number of features seen during :term:`fit`.
  389. .. versionadded:: 0.24
  390. feature_names_in_ : ndarray of shape (`n_features_in_`,)
  391. Names of features seen during :term:`fit`. Defined only when `X`
  392. has feature names that are all strings.
  393. .. versionadded:: 1.0
  394. n_iter_ : int
  395. Maximum number of iterations run across all classes.
  396. See Also
  397. --------
  398. LinearSVC : Implementation of Support Vector Machine classifier using the
  399. same library as this class (liblinear).
  400. SVR : Implementation of Support Vector Machine regression using libsvm:
  401. the kernel can be non-linear but its SMO algorithm does not scale to
  402. large number of samples as :class:`~sklearn.svm.LinearSVR` does.
  403. sklearn.linear_model.SGDRegressor : SGDRegressor can optimize the same cost
  404. function as LinearSVR
  405. by adjusting the penalty and loss parameters. In addition it requires
  406. less memory, allows incremental (online) learning, and implements
  407. various loss functions and regularization regimes.
  408. Examples
  409. --------
  410. >>> from sklearn.svm import LinearSVR
  411. >>> from sklearn.pipeline import make_pipeline
  412. >>> from sklearn.preprocessing import StandardScaler
  413. >>> from sklearn.datasets import make_regression
  414. >>> X, y = make_regression(n_features=4, random_state=0)
  415. >>> regr = make_pipeline(StandardScaler(),
  416. ... LinearSVR(dual="auto", random_state=0, tol=1e-5))
  417. >>> regr.fit(X, y)
  418. Pipeline(steps=[('standardscaler', StandardScaler()),
  419. ('linearsvr', LinearSVR(dual='auto', random_state=0, tol=1e-05))])
  420. >>> print(regr.named_steps['linearsvr'].coef_)
  421. [18.582... 27.023... 44.357... 64.522...]
  422. >>> print(regr.named_steps['linearsvr'].intercept_)
  423. [-4...]
  424. >>> print(regr.predict([[0, 0, 0, 0]]))
  425. [-2.384...]
  426. """
  427. _parameter_constraints: dict = {
  428. "epsilon": [Real],
  429. "tol": [Interval(Real, 0.0, None, closed="neither")],
  430. "C": [Interval(Real, 0.0, None, closed="neither")],
  431. "loss": [StrOptions({"epsilon_insensitive", "squared_epsilon_insensitive"})],
  432. "fit_intercept": ["boolean"],
  433. "intercept_scaling": [Interval(Real, 0, None, closed="neither")],
  434. "dual": ["boolean", StrOptions({"auto"}), Hidden(StrOptions({"warn"}))],
  435. "verbose": ["verbose"],
  436. "random_state": ["random_state"],
  437. "max_iter": [Interval(Integral, 0, None, closed="left")],
  438. }
  439. def __init__(
  440. self,
  441. *,
  442. epsilon=0.0,
  443. tol=1e-4,
  444. C=1.0,
  445. loss="epsilon_insensitive",
  446. fit_intercept=True,
  447. intercept_scaling=1.0,
  448. dual="warn",
  449. verbose=0,
  450. random_state=None,
  451. max_iter=1000,
  452. ):
  453. self.tol = tol
  454. self.C = C
  455. self.epsilon = epsilon
  456. self.fit_intercept = fit_intercept
  457. self.intercept_scaling = intercept_scaling
  458. self.verbose = verbose
  459. self.random_state = random_state
  460. self.max_iter = max_iter
  461. self.dual = dual
  462. self.loss = loss
  463. @_fit_context(prefer_skip_nested_validation=True)
  464. def fit(self, X, y, sample_weight=None):
  465. """Fit the model according to the given training data.
  466. Parameters
  467. ----------
  468. X : {array-like, sparse matrix} of shape (n_samples, n_features)
  469. Training vector, where `n_samples` is the number of samples and
  470. `n_features` is the number of features.
  471. y : array-like of shape (n_samples,)
  472. Target vector relative to X.
  473. sample_weight : array-like of shape (n_samples,), default=None
  474. Array of weights that are assigned to individual
  475. samples. If not provided,
  476. then each sample is given unit weight.
  477. .. versionadded:: 0.18
  478. Returns
  479. -------
  480. self : object
  481. An instance of the estimator.
  482. """
  483. X, y = self._validate_data(
  484. X,
  485. y,
  486. accept_sparse="csr",
  487. dtype=np.float64,
  488. order="C",
  489. accept_large_sparse=False,
  490. )
  491. penalty = "l2" # SVR only accepts l2 penalty
  492. _dual = _validate_dual_parameter(self.dual, self.loss, penalty, "ovr", X)
  493. self.coef_, self.intercept_, n_iter_ = _fit_liblinear(
  494. X,
  495. y,
  496. self.C,
  497. self.fit_intercept,
  498. self.intercept_scaling,
  499. None,
  500. penalty,
  501. _dual,
  502. self.verbose,
  503. self.max_iter,
  504. self.tol,
  505. self.random_state,
  506. loss=self.loss,
  507. epsilon=self.epsilon,
  508. sample_weight=sample_weight,
  509. )
  510. self.coef_ = self.coef_.ravel()
  511. # Backward compatibility: _fit_liblinear is used both by LinearSVC/R
  512. # and LogisticRegression but LogisticRegression sets a structured
  513. # `n_iter_` attribute with information about the underlying OvR fits
  514. # while LinearSVC/R only reports the maximum value.
  515. self.n_iter_ = n_iter_.max().item()
  516. return self
  517. def _more_tags(self):
  518. return {
  519. "_xfail_checks": {
  520. "check_sample_weights_invariance": (
  521. "zero sample_weight is not equivalent to removing samples"
  522. ),
  523. }
  524. }
  525. class SVC(BaseSVC):
  526. """C-Support Vector Classification.
  527. The implementation is based on libsvm. The fit time scales at least
  528. quadratically with the number of samples and may be impractical
  529. beyond tens of thousands of samples. For large datasets
  530. consider using :class:`~sklearn.svm.LinearSVC` or
  531. :class:`~sklearn.linear_model.SGDClassifier` instead, possibly after a
  532. :class:`~sklearn.kernel_approximation.Nystroem` transformer or
  533. other :ref:`kernel_approximation`.
  534. The multiclass support is handled according to a one-vs-one scheme.
  535. For details on the precise mathematical formulation of the provided
  536. kernel functions and how `gamma`, `coef0` and `degree` affect each
  537. other, see the corresponding section in the narrative documentation:
  538. :ref:`svm_kernels`.
  539. Read more in the :ref:`User Guide <svm_classification>`.
  540. Parameters
  541. ----------
  542. C : float, default=1.0
  543. Regularization parameter. The strength of the regularization is
  544. inversely proportional to C. Must be strictly positive. The penalty
  545. is a squared l2 penalty.
  546. kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable, \
  547. default='rbf'
  548. Specifies the kernel type to be used in the algorithm. If
  549. none is given, 'rbf' will be used. If a callable is given it is used to
  550. pre-compute the kernel matrix from data matrices; that matrix should be
  551. an array of shape ``(n_samples, n_samples)``. For an intuitive
  552. visualization of different kernel types see
  553. :ref:`sphx_glr_auto_examples_svm_plot_svm_kernels.py`.
  554. degree : int, default=3
  555. Degree of the polynomial kernel function ('poly').
  556. Must be non-negative. Ignored by all other kernels.
  557. gamma : {'scale', 'auto'} or float, default='scale'
  558. Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
  559. - if ``gamma='scale'`` (default) is passed then it uses
  560. 1 / (n_features * X.var()) as value of gamma,
  561. - if 'auto', uses 1 / n_features
  562. - if float, must be non-negative.
  563. .. versionchanged:: 0.22
  564. The default value of ``gamma`` changed from 'auto' to 'scale'.
  565. coef0 : float, default=0.0
  566. Independent term in kernel function.
  567. It is only significant in 'poly' and 'sigmoid'.
  568. shrinking : bool, default=True
  569. Whether to use the shrinking heuristic.
  570. See the :ref:`User Guide <shrinking_svm>`.
  571. probability : bool, default=False
  572. Whether to enable probability estimates. This must be enabled prior
  573. to calling `fit`, will slow down that method as it internally uses
  574. 5-fold cross-validation, and `predict_proba` may be inconsistent with
  575. `predict`. Read more in the :ref:`User Guide <scores_probabilities>`.
  576. tol : float, default=1e-3
  577. Tolerance for stopping criterion.
  578. cache_size : float, default=200
  579. Specify the size of the kernel cache (in MB).
  580. class_weight : dict or 'balanced', default=None
  581. Set the parameter C of class i to class_weight[i]*C for
  582. SVC. If not given, all classes are supposed to have
  583. weight one.
  584. The "balanced" mode uses the values of y to automatically adjust
  585. weights inversely proportional to class frequencies in the input data
  586. as ``n_samples / (n_classes * np.bincount(y))``.
  587. verbose : bool, default=False
  588. Enable verbose output. Note that this setting takes advantage of a
  589. per-process runtime setting in libsvm that, if enabled, may not work
  590. properly in a multithreaded context.
  591. max_iter : int, default=-1
  592. Hard limit on iterations within solver, or -1 for no limit.
  593. decision_function_shape : {'ovo', 'ovr'}, default='ovr'
  594. Whether to return a one-vs-rest ('ovr') decision function of shape
  595. (n_samples, n_classes) as all other classifiers, or the original
  596. one-vs-one ('ovo') decision function of libsvm which has shape
  597. (n_samples, n_classes * (n_classes - 1) / 2). However, note that
  598. internally, one-vs-one ('ovo') is always used as a multi-class strategy
  599. to train models; an ovr matrix is only constructed from the ovo matrix.
  600. The parameter is ignored for binary classification.
  601. .. versionchanged:: 0.19
  602. decision_function_shape is 'ovr' by default.
  603. .. versionadded:: 0.17
  604. *decision_function_shape='ovr'* is recommended.
  605. .. versionchanged:: 0.17
  606. Deprecated *decision_function_shape='ovo' and None*.
  607. break_ties : bool, default=False
  608. If true, ``decision_function_shape='ovr'``, and number of classes > 2,
  609. :term:`predict` will break ties according to the confidence values of
  610. :term:`decision_function`; otherwise the first class among the tied
  611. classes is returned. Please note that breaking ties comes at a
  612. relatively high computational cost compared to a simple predict.
  613. .. versionadded:: 0.22
  614. random_state : int, RandomState instance or None, default=None
  615. Controls the pseudo random number generation for shuffling the data for
  616. probability estimates. Ignored when `probability` is False.
  617. Pass an int for reproducible output across multiple function calls.
  618. See :term:`Glossary <random_state>`.
  619. Attributes
  620. ----------
  621. class_weight_ : ndarray of shape (n_classes,)
  622. Multipliers of parameter C for each class.
  623. Computed based on the ``class_weight`` parameter.
  624. classes_ : ndarray of shape (n_classes,)
  625. The classes labels.
  626. coef_ : ndarray of shape (n_classes * (n_classes - 1) / 2, n_features)
  627. Weights assigned to the features (coefficients in the primal
  628. problem). This is only available in the case of a linear kernel.
  629. `coef_` is a readonly property derived from `dual_coef_` and
  630. `support_vectors_`.
  631. dual_coef_ : ndarray of shape (n_classes -1, n_SV)
  632. Dual coefficients of the support vector in the decision
  633. function (see :ref:`sgd_mathematical_formulation`), multiplied by
  634. their targets.
  635. For multiclass, coefficient for all 1-vs-1 classifiers.
  636. The layout of the coefficients in the multiclass case is somewhat
  637. non-trivial. See the :ref:`multi-class section of the User Guide
  638. <svm_multi_class>` for details.
  639. fit_status_ : int
  640. 0 if correctly fitted, 1 otherwise (will raise warning)
  641. intercept_ : ndarray of shape (n_classes * (n_classes - 1) / 2,)
  642. Constants in decision function.
  643. n_features_in_ : int
  644. Number of features seen during :term:`fit`.
  645. .. versionadded:: 0.24
  646. feature_names_in_ : ndarray of shape (`n_features_in_`,)
  647. Names of features seen during :term:`fit`. Defined only when `X`
  648. has feature names that are all strings.
  649. .. versionadded:: 1.0
  650. n_iter_ : ndarray of shape (n_classes * (n_classes - 1) // 2,)
  651. Number of iterations run by the optimization routine to fit the model.
  652. The shape of this attribute depends on the number of models optimized
  653. which in turn depends on the number of classes.
  654. .. versionadded:: 1.1
  655. support_ : ndarray of shape (n_SV)
  656. Indices of support vectors.
  657. support_vectors_ : ndarray of shape (n_SV, n_features)
  658. Support vectors. An empty array if kernel is precomputed.
  659. n_support_ : ndarray of shape (n_classes,), dtype=int32
  660. Number of support vectors for each class.
  661. probA_ : ndarray of shape (n_classes * (n_classes - 1) / 2)
  662. probB_ : ndarray of shape (n_classes * (n_classes - 1) / 2)
  663. If `probability=True`, it corresponds to the parameters learned in
  664. Platt scaling to produce probability estimates from decision values.
  665. If `probability=False`, it's an empty array. Platt scaling uses the
  666. logistic function
  667. ``1 / (1 + exp(decision_value * probA_ + probB_))``
  668. where ``probA_`` and ``probB_`` are learned from the dataset [2]_. For
  669. more information on the multiclass case and training procedure see
  670. section 8 of [1]_.
  671. shape_fit_ : tuple of int of shape (n_dimensions_of_X,)
  672. Array dimensions of training vector ``X``.
  673. See Also
  674. --------
  675. SVR : Support Vector Machine for Regression implemented using libsvm.
  676. LinearSVC : Scalable Linear Support Vector Machine for classification
  677. implemented using liblinear. Check the See Also section of
  678. LinearSVC for more comparison element.
  679. References
  680. ----------
  681. .. [1] `LIBSVM: A Library for Support Vector Machines
  682. <http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf>`_
  683. .. [2] `Platt, John (1999). "Probabilistic Outputs for Support Vector
  684. Machines and Comparisons to Regularized Likelihood Methods"
  685. <https://citeseerx.ist.psu.edu/doc_view/pid/42e5ed832d4310ce4378c44d05570439df28a393>`_
  686. Examples
  687. --------
  688. >>> import numpy as np
  689. >>> from sklearn.pipeline import make_pipeline
  690. >>> from sklearn.preprocessing import StandardScaler
  691. >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
  692. >>> y = np.array([1, 1, 2, 2])
  693. >>> from sklearn.svm import SVC
  694. >>> clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))
  695. >>> clf.fit(X, y)
  696. Pipeline(steps=[('standardscaler', StandardScaler()),
  697. ('svc', SVC(gamma='auto'))])
  698. >>> print(clf.predict([[-0.8, -1]]))
  699. [1]
  700. """
  701. _impl = "c_svc"
  702. def __init__(
  703. self,
  704. *,
  705. C=1.0,
  706. kernel="rbf",
  707. degree=3,
  708. gamma="scale",
  709. coef0=0.0,
  710. shrinking=True,
  711. probability=False,
  712. tol=1e-3,
  713. cache_size=200,
  714. class_weight=None,
  715. verbose=False,
  716. max_iter=-1,
  717. decision_function_shape="ovr",
  718. break_ties=False,
  719. random_state=None,
  720. ):
  721. super().__init__(
  722. kernel=kernel,
  723. degree=degree,
  724. gamma=gamma,
  725. coef0=coef0,
  726. tol=tol,
  727. C=C,
  728. nu=0.0,
  729. shrinking=shrinking,
  730. probability=probability,
  731. cache_size=cache_size,
  732. class_weight=class_weight,
  733. verbose=verbose,
  734. max_iter=max_iter,
  735. decision_function_shape=decision_function_shape,
  736. break_ties=break_ties,
  737. random_state=random_state,
  738. )
  739. def _more_tags(self):
  740. return {
  741. "_xfail_checks": {
  742. "check_sample_weights_invariance": (
  743. "zero sample_weight is not equivalent to removing samples"
  744. ),
  745. }
  746. }
  747. class NuSVC(BaseSVC):
  748. """Nu-Support Vector Classification.
  749. Similar to SVC but uses a parameter to control the number of support
  750. vectors.
  751. The implementation is based on libsvm.
  752. Read more in the :ref:`User Guide <svm_classification>`.
  753. Parameters
  754. ----------
  755. nu : float, default=0.5
  756. An upper bound on the fraction of margin errors (see :ref:`User Guide
  757. <nu_svc>`) and a lower bound of the fraction of support vectors.
  758. Should be in the interval (0, 1].
  759. kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable, \
  760. default='rbf'
  761. Specifies the kernel type to be used in the algorithm.
  762. If none is given, 'rbf' will be used. If a callable is given it is
  763. used to precompute the kernel matrix. For an intuitive
  764. visualization of different kernel types see
  765. :ref:`sphx_glr_auto_examples_svm_plot_svm_kernels.py`.
  766. degree : int, default=3
  767. Degree of the polynomial kernel function ('poly').
  768. Must be non-negative. Ignored by all other kernels.
  769. gamma : {'scale', 'auto'} or float, default='scale'
  770. Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
  771. - if ``gamma='scale'`` (default) is passed then it uses
  772. 1 / (n_features * X.var()) as value of gamma,
  773. - if 'auto', uses 1 / n_features
  774. - if float, must be non-negative.
  775. .. versionchanged:: 0.22
  776. The default value of ``gamma`` changed from 'auto' to 'scale'.
  777. coef0 : float, default=0.0
  778. Independent term in kernel function.
  779. It is only significant in 'poly' and 'sigmoid'.
  780. shrinking : bool, default=True
  781. Whether to use the shrinking heuristic.
  782. See the :ref:`User Guide <shrinking_svm>`.
  783. probability : bool, default=False
  784. Whether to enable probability estimates. This must be enabled prior
  785. to calling `fit`, will slow down that method as it internally uses
  786. 5-fold cross-validation, and `predict_proba` may be inconsistent with
  787. `predict`. Read more in the :ref:`User Guide <scores_probabilities>`.
  788. tol : float, default=1e-3
  789. Tolerance for stopping criterion.
  790. cache_size : float, default=200
  791. Specify the size of the kernel cache (in MB).
  792. class_weight : {dict, 'balanced'}, default=None
  793. Set the parameter C of class i to class_weight[i]*C for
  794. SVC. If not given, all classes are supposed to have
  795. weight one. The "balanced" mode uses the values of y to automatically
  796. adjust weights inversely proportional to class frequencies as
  797. ``n_samples / (n_classes * np.bincount(y))``.
  798. verbose : bool, default=False
  799. Enable verbose output. Note that this setting takes advantage of a
  800. per-process runtime setting in libsvm that, if enabled, may not work
  801. properly in a multithreaded context.
  802. max_iter : int, default=-1
  803. Hard limit on iterations within solver, or -1 for no limit.
  804. decision_function_shape : {'ovo', 'ovr'}, default='ovr'
  805. Whether to return a one-vs-rest ('ovr') decision function of shape
  806. (n_samples, n_classes) as all other classifiers, or the original
  807. one-vs-one ('ovo') decision function of libsvm which has shape
  808. (n_samples, n_classes * (n_classes - 1) / 2). However, one-vs-one
  809. ('ovo') is always used as multi-class strategy. The parameter is
  810. ignored for binary classification.
  811. .. versionchanged:: 0.19
  812. decision_function_shape is 'ovr' by default.
  813. .. versionadded:: 0.17
  814. *decision_function_shape='ovr'* is recommended.
  815. .. versionchanged:: 0.17
  816. Deprecated *decision_function_shape='ovo' and None*.
  817. break_ties : bool, default=False
  818. If true, ``decision_function_shape='ovr'``, and number of classes > 2,
  819. :term:`predict` will break ties according to the confidence values of
  820. :term:`decision_function`; otherwise the first class among the tied
  821. classes is returned. Please note that breaking ties comes at a
  822. relatively high computational cost compared to a simple predict.
  823. .. versionadded:: 0.22
  824. random_state : int, RandomState instance or None, default=None
  825. Controls the pseudo random number generation for shuffling the data for
  826. probability estimates. Ignored when `probability` is False.
  827. Pass an int for reproducible output across multiple function calls.
  828. See :term:`Glossary <random_state>`.
  829. Attributes
  830. ----------
  831. class_weight_ : ndarray of shape (n_classes,)
  832. Multipliers of parameter C of each class.
  833. Computed based on the ``class_weight`` parameter.
  834. classes_ : ndarray of shape (n_classes,)
  835. The unique classes labels.
  836. coef_ : ndarray of shape (n_classes * (n_classes -1) / 2, n_features)
  837. Weights assigned to the features (coefficients in the primal
  838. problem). This is only available in the case of a linear kernel.
  839. `coef_` is readonly property derived from `dual_coef_` and
  840. `support_vectors_`.
  841. dual_coef_ : ndarray of shape (n_classes - 1, n_SV)
  842. Dual coefficients of the support vector in the decision
  843. function (see :ref:`sgd_mathematical_formulation`), multiplied by
  844. their targets.
  845. For multiclass, coefficient for all 1-vs-1 classifiers.
  846. The layout of the coefficients in the multiclass case is somewhat
  847. non-trivial. See the :ref:`multi-class section of the User Guide
  848. <svm_multi_class>` for details.
  849. fit_status_ : int
  850. 0 if correctly fitted, 1 if the algorithm did not converge.
  851. intercept_ : ndarray of shape (n_classes * (n_classes - 1) / 2,)
  852. Constants in decision function.
  853. n_features_in_ : int
  854. Number of features seen during :term:`fit`.
  855. .. versionadded:: 0.24
  856. feature_names_in_ : ndarray of shape (`n_features_in_`,)
  857. Names of features seen during :term:`fit`. Defined only when `X`
  858. has feature names that are all strings.
  859. .. versionadded:: 1.0
  860. n_iter_ : ndarray of shape (n_classes * (n_classes - 1) // 2,)
  861. Number of iterations run by the optimization routine to fit the model.
  862. The shape of this attribute depends on the number of models optimized
  863. which in turn depends on the number of classes.
  864. .. versionadded:: 1.1
  865. support_ : ndarray of shape (n_SV,)
  866. Indices of support vectors.
  867. support_vectors_ : ndarray of shape (n_SV, n_features)
  868. Support vectors.
  869. n_support_ : ndarray of shape (n_classes,), dtype=int32
  870. Number of support vectors for each class.
  871. fit_status_ : int
  872. 0 if correctly fitted, 1 if the algorithm did not converge.
  873. probA_ : ndarray of shape (n_classes * (n_classes - 1) / 2,)
  874. probB_ : ndarray of shape (n_classes * (n_classes - 1) / 2,)
  875. If `probability=True`, it corresponds to the parameters learned in
  876. Platt scaling to produce probability estimates from decision values.
  877. If `probability=False`, it's an empty array. Platt scaling uses the
  878. logistic function
  879. ``1 / (1 + exp(decision_value * probA_ + probB_))``
  880. where ``probA_`` and ``probB_`` are learned from the dataset [2]_. For
  881. more information on the multiclass case and training procedure see
  882. section 8 of [1]_.
  883. shape_fit_ : tuple of int of shape (n_dimensions_of_X,)
  884. Array dimensions of training vector ``X``.
  885. See Also
  886. --------
  887. SVC : Support Vector Machine for classification using libsvm.
  888. LinearSVC : Scalable linear Support Vector Machine for classification using
  889. liblinear.
  890. References
  891. ----------
  892. .. [1] `LIBSVM: A Library for Support Vector Machines
  893. <http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf>`_
  894. .. [2] `Platt, John (1999). "Probabilistic Outputs for Support Vector
  895. Machines and Comparisons to Regularized Likelihood Methods"
  896. <https://citeseerx.ist.psu.edu/doc_view/pid/42e5ed832d4310ce4378c44d05570439df28a393>`_
  897. Examples
  898. --------
  899. >>> import numpy as np
  900. >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
  901. >>> y = np.array([1, 1, 2, 2])
  902. >>> from sklearn.pipeline import make_pipeline
  903. >>> from sklearn.preprocessing import StandardScaler
  904. >>> from sklearn.svm import NuSVC
  905. >>> clf = make_pipeline(StandardScaler(), NuSVC())
  906. >>> clf.fit(X, y)
  907. Pipeline(steps=[('standardscaler', StandardScaler()), ('nusvc', NuSVC())])
  908. >>> print(clf.predict([[-0.8, -1]]))
  909. [1]
  910. """
  911. _impl = "nu_svc"
  912. _parameter_constraints: dict = {
  913. **BaseSVC._parameter_constraints,
  914. "nu": [Interval(Real, 0.0, 1.0, closed="right")],
  915. }
  916. _parameter_constraints.pop("C")
  917. def __init__(
  918. self,
  919. *,
  920. nu=0.5,
  921. kernel="rbf",
  922. degree=3,
  923. gamma="scale",
  924. coef0=0.0,
  925. shrinking=True,
  926. probability=False,
  927. tol=1e-3,
  928. cache_size=200,
  929. class_weight=None,
  930. verbose=False,
  931. max_iter=-1,
  932. decision_function_shape="ovr",
  933. break_ties=False,
  934. random_state=None,
  935. ):
  936. super().__init__(
  937. kernel=kernel,
  938. degree=degree,
  939. gamma=gamma,
  940. coef0=coef0,
  941. tol=tol,
  942. C=0.0,
  943. nu=nu,
  944. shrinking=shrinking,
  945. probability=probability,
  946. cache_size=cache_size,
  947. class_weight=class_weight,
  948. verbose=verbose,
  949. max_iter=max_iter,
  950. decision_function_shape=decision_function_shape,
  951. break_ties=break_ties,
  952. random_state=random_state,
  953. )
  954. def _more_tags(self):
  955. return {
  956. "_xfail_checks": {
  957. "check_methods_subset_invariance": (
  958. "fails for the decision_function method"
  959. ),
  960. "check_class_weight_classifiers": "class_weight is ignored.",
  961. "check_sample_weights_invariance": (
  962. "zero sample_weight is not equivalent to removing samples"
  963. ),
  964. "check_classifiers_one_label_sample_weights": (
  965. "specified nu is infeasible for the fit."
  966. ),
  967. }
  968. }
  969. class SVR(RegressorMixin, BaseLibSVM):
  970. """Epsilon-Support Vector Regression.
  971. The free parameters in the model are C and epsilon.
  972. The implementation is based on libsvm. The fit time complexity
  973. is more than quadratic with the number of samples which makes it hard
  974. to scale to datasets with more than a couple of 10000 samples. For large
  975. datasets consider using :class:`~sklearn.svm.LinearSVR` or
  976. :class:`~sklearn.linear_model.SGDRegressor` instead, possibly after a
  977. :class:`~sklearn.kernel_approximation.Nystroem` transformer or
  978. other :ref:`kernel_approximation`.
  979. Read more in the :ref:`User Guide <svm_regression>`.
  980. Parameters
  981. ----------
  982. kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable, \
  983. default='rbf'
  984. Specifies the kernel type to be used in the algorithm.
  985. If none is given, 'rbf' will be used. If a callable is given it is
  986. used to precompute the kernel matrix.
  987. degree : int, default=3
  988. Degree of the polynomial kernel function ('poly').
  989. Must be non-negative. Ignored by all other kernels.
  990. gamma : {'scale', 'auto'} or float, default='scale'
  991. Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
  992. - if ``gamma='scale'`` (default) is passed then it uses
  993. 1 / (n_features * X.var()) as value of gamma,
  994. - if 'auto', uses 1 / n_features
  995. - if float, must be non-negative.
  996. .. versionchanged:: 0.22
  997. The default value of ``gamma`` changed from 'auto' to 'scale'.
  998. coef0 : float, default=0.0
  999. Independent term in kernel function.
  1000. It is only significant in 'poly' and 'sigmoid'.
  1001. tol : float, default=1e-3
  1002. Tolerance for stopping criterion.
  1003. C : float, default=1.0
  1004. Regularization parameter. The strength of the regularization is
  1005. inversely proportional to C. Must be strictly positive.
  1006. The penalty is a squared l2 penalty.
  1007. epsilon : float, default=0.1
  1008. Epsilon in the epsilon-SVR model. It specifies the epsilon-tube
  1009. within which no penalty is associated in the training loss function
  1010. with points predicted within a distance epsilon from the actual
  1011. value. Must be non-negative.
  1012. shrinking : bool, default=True
  1013. Whether to use the shrinking heuristic.
  1014. See the :ref:`User Guide <shrinking_svm>`.
  1015. cache_size : float, default=200
  1016. Specify the size of the kernel cache (in MB).
  1017. verbose : bool, default=False
  1018. Enable verbose output. Note that this setting takes advantage of a
  1019. per-process runtime setting in libsvm that, if enabled, may not work
  1020. properly in a multithreaded context.
  1021. max_iter : int, default=-1
  1022. Hard limit on iterations within solver, or -1 for no limit.
  1023. Attributes
  1024. ----------
  1025. class_weight_ : ndarray of shape (n_classes,)
  1026. Multipliers of parameter C for each class.
  1027. Computed based on the ``class_weight`` parameter.
  1028. .. deprecated:: 1.2
  1029. `class_weight_` was deprecated in version 1.2 and will be removed in 1.4.
  1030. coef_ : ndarray of shape (1, n_features)
  1031. Weights assigned to the features (coefficients in the primal
  1032. problem). This is only available in the case of a linear kernel.
  1033. `coef_` is readonly property derived from `dual_coef_` and
  1034. `support_vectors_`.
  1035. dual_coef_ : ndarray of shape (1, n_SV)
  1036. Coefficients of the support vector in the decision function.
  1037. fit_status_ : int
  1038. 0 if correctly fitted, 1 otherwise (will raise warning)
  1039. intercept_ : ndarray of shape (1,)
  1040. Constants in decision function.
  1041. n_features_in_ : int
  1042. Number of features seen during :term:`fit`.
  1043. .. versionadded:: 0.24
  1044. feature_names_in_ : ndarray of shape (`n_features_in_`,)
  1045. Names of features seen during :term:`fit`. Defined only when `X`
  1046. has feature names that are all strings.
  1047. .. versionadded:: 1.0
  1048. n_iter_ : int
  1049. Number of iterations run by the optimization routine to fit the model.
  1050. .. versionadded:: 1.1
  1051. n_support_ : ndarray of shape (1,), dtype=int32
  1052. Number of support vectors.
  1053. shape_fit_ : tuple of int of shape (n_dimensions_of_X,)
  1054. Array dimensions of training vector ``X``.
  1055. support_ : ndarray of shape (n_SV,)
  1056. Indices of support vectors.
  1057. support_vectors_ : ndarray of shape (n_SV, n_features)
  1058. Support vectors.
  1059. See Also
  1060. --------
  1061. NuSVR : Support Vector Machine for regression implemented using libsvm
  1062. using a parameter to control the number of support vectors.
  1063. LinearSVR : Scalable Linear Support Vector Machine for regression
  1064. implemented using liblinear.
  1065. References
  1066. ----------
  1067. .. [1] `LIBSVM: A Library for Support Vector Machines
  1068. <http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf>`_
  1069. .. [2] `Platt, John (1999). "Probabilistic Outputs for Support Vector
  1070. Machines and Comparisons to Regularized Likelihood Methods"
  1071. <https://citeseerx.ist.psu.edu/doc_view/pid/42e5ed832d4310ce4378c44d05570439df28a393>`_
  1072. Examples
  1073. --------
  1074. >>> from sklearn.svm import SVR
  1075. >>> from sklearn.pipeline import make_pipeline
  1076. >>> from sklearn.preprocessing import StandardScaler
  1077. >>> import numpy as np
  1078. >>> n_samples, n_features = 10, 5
  1079. >>> rng = np.random.RandomState(0)
  1080. >>> y = rng.randn(n_samples)
  1081. >>> X = rng.randn(n_samples, n_features)
  1082. >>> regr = make_pipeline(StandardScaler(), SVR(C=1.0, epsilon=0.2))
  1083. >>> regr.fit(X, y)
  1084. Pipeline(steps=[('standardscaler', StandardScaler()),
  1085. ('svr', SVR(epsilon=0.2))])
  1086. """
  1087. _impl = "epsilon_svr"
  1088. _parameter_constraints: dict = {**BaseLibSVM._parameter_constraints}
  1089. for unused_param in ["class_weight", "nu", "probability", "random_state"]:
  1090. _parameter_constraints.pop(unused_param)
  1091. def __init__(
  1092. self,
  1093. *,
  1094. kernel="rbf",
  1095. degree=3,
  1096. gamma="scale",
  1097. coef0=0.0,
  1098. tol=1e-3,
  1099. C=1.0,
  1100. epsilon=0.1,
  1101. shrinking=True,
  1102. cache_size=200,
  1103. verbose=False,
  1104. max_iter=-1,
  1105. ):
  1106. super().__init__(
  1107. kernel=kernel,
  1108. degree=degree,
  1109. gamma=gamma,
  1110. coef0=coef0,
  1111. tol=tol,
  1112. C=C,
  1113. nu=0.0,
  1114. epsilon=epsilon,
  1115. verbose=verbose,
  1116. shrinking=shrinking,
  1117. probability=False,
  1118. cache_size=cache_size,
  1119. class_weight=None,
  1120. max_iter=max_iter,
  1121. random_state=None,
  1122. )
  1123. # TODO(1.4): Remove
  1124. @deprecated( # type: ignore
  1125. "Attribute `class_weight_` was deprecated in version 1.2 and will be removed in"
  1126. " 1.4."
  1127. )
  1128. @property
  1129. def class_weight_(self):
  1130. return np.empty(0)
  1131. def _more_tags(self):
  1132. return {
  1133. "_xfail_checks": {
  1134. "check_sample_weights_invariance": (
  1135. "zero sample_weight is not equivalent to removing samples"
  1136. ),
  1137. }
  1138. }
  1139. class NuSVR(RegressorMixin, BaseLibSVM):
  1140. """Nu Support Vector Regression.
  1141. Similar to NuSVC, for regression, uses a parameter nu to control
  1142. the number of support vectors. However, unlike NuSVC, where nu
  1143. replaces C, here nu replaces the parameter epsilon of epsilon-SVR.
  1144. The implementation is based on libsvm.
  1145. Read more in the :ref:`User Guide <svm_regression>`.
  1146. Parameters
  1147. ----------
  1148. nu : float, default=0.5
  1149. An upper bound on the fraction of training errors and a lower bound of
  1150. the fraction of support vectors. Should be in the interval (0, 1]. By
  1151. default 0.5 will be taken.
  1152. C : float, default=1.0
  1153. Penalty parameter C of the error term.
  1154. kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable, \
  1155. default='rbf'
  1156. Specifies the kernel type to be used in the algorithm.
  1157. If none is given, 'rbf' will be used. If a callable is given it is
  1158. used to precompute the kernel matrix.
  1159. degree : int, default=3
  1160. Degree of the polynomial kernel function ('poly').
  1161. Must be non-negative. Ignored by all other kernels.
  1162. gamma : {'scale', 'auto'} or float, default='scale'
  1163. Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
  1164. - if ``gamma='scale'`` (default) is passed then it uses
  1165. 1 / (n_features * X.var()) as value of gamma,
  1166. - if 'auto', uses 1 / n_features
  1167. - if float, must be non-negative.
  1168. .. versionchanged:: 0.22
  1169. The default value of ``gamma`` changed from 'auto' to 'scale'.
  1170. coef0 : float, default=0.0
  1171. Independent term in kernel function.
  1172. It is only significant in 'poly' and 'sigmoid'.
  1173. shrinking : bool, default=True
  1174. Whether to use the shrinking heuristic.
  1175. See the :ref:`User Guide <shrinking_svm>`.
  1176. tol : float, default=1e-3
  1177. Tolerance for stopping criterion.
  1178. cache_size : float, default=200
  1179. Specify the size of the kernel cache (in MB).
  1180. verbose : bool, default=False
  1181. Enable verbose output. Note that this setting takes advantage of a
  1182. per-process runtime setting in libsvm that, if enabled, may not work
  1183. properly in a multithreaded context.
  1184. max_iter : int, default=-1
  1185. Hard limit on iterations within solver, or -1 for no limit.
  1186. Attributes
  1187. ----------
  1188. class_weight_ : ndarray of shape (n_classes,)
  1189. Multipliers of parameter C for each class.
  1190. Computed based on the ``class_weight`` parameter.
  1191. .. deprecated:: 1.2
  1192. `class_weight_` was deprecated in version 1.2 and will be removed in 1.4.
  1193. coef_ : ndarray of shape (1, n_features)
  1194. Weights assigned to the features (coefficients in the primal
  1195. problem). This is only available in the case of a linear kernel.
  1196. `coef_` is readonly property derived from `dual_coef_` and
  1197. `support_vectors_`.
  1198. dual_coef_ : ndarray of shape (1, n_SV)
  1199. Coefficients of the support vector in the decision function.
  1200. fit_status_ : int
  1201. 0 if correctly fitted, 1 otherwise (will raise warning)
  1202. intercept_ : ndarray of shape (1,)
  1203. Constants in decision function.
  1204. n_features_in_ : int
  1205. Number of features seen during :term:`fit`.
  1206. .. versionadded:: 0.24
  1207. feature_names_in_ : ndarray of shape (`n_features_in_`,)
  1208. Names of features seen during :term:`fit`. Defined only when `X`
  1209. has feature names that are all strings.
  1210. .. versionadded:: 1.0
  1211. n_iter_ : int
  1212. Number of iterations run by the optimization routine to fit the model.
  1213. .. versionadded:: 1.1
  1214. n_support_ : ndarray of shape (1,), dtype=int32
  1215. Number of support vectors.
  1216. shape_fit_ : tuple of int of shape (n_dimensions_of_X,)
  1217. Array dimensions of training vector ``X``.
  1218. support_ : ndarray of shape (n_SV,)
  1219. Indices of support vectors.
  1220. support_vectors_ : ndarray of shape (n_SV, n_features)
  1221. Support vectors.
  1222. See Also
  1223. --------
  1224. NuSVC : Support Vector Machine for classification implemented with libsvm
  1225. with a parameter to control the number of support vectors.
  1226. SVR : Epsilon Support Vector Machine for regression implemented with
  1227. libsvm.
  1228. References
  1229. ----------
  1230. .. [1] `LIBSVM: A Library for Support Vector Machines
  1231. <http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf>`_
  1232. .. [2] `Platt, John (1999). "Probabilistic Outputs for Support Vector
  1233. Machines and Comparisons to Regularized Likelihood Methods"
  1234. <https://citeseerx.ist.psu.edu/doc_view/pid/42e5ed832d4310ce4378c44d05570439df28a393>`_
  1235. Examples
  1236. --------
  1237. >>> from sklearn.svm import NuSVR
  1238. >>> from sklearn.pipeline import make_pipeline
  1239. >>> from sklearn.preprocessing import StandardScaler
  1240. >>> import numpy as np
  1241. >>> n_samples, n_features = 10, 5
  1242. >>> np.random.seed(0)
  1243. >>> y = np.random.randn(n_samples)
  1244. >>> X = np.random.randn(n_samples, n_features)
  1245. >>> regr = make_pipeline(StandardScaler(), NuSVR(C=1.0, nu=0.1))
  1246. >>> regr.fit(X, y)
  1247. Pipeline(steps=[('standardscaler', StandardScaler()),
  1248. ('nusvr', NuSVR(nu=0.1))])
  1249. """
  1250. _impl = "nu_svr"
  1251. _parameter_constraints: dict = {**BaseLibSVM._parameter_constraints}
  1252. for unused_param in ["class_weight", "epsilon", "probability", "random_state"]:
  1253. _parameter_constraints.pop(unused_param)
  1254. def __init__(
  1255. self,
  1256. *,
  1257. nu=0.5,
  1258. C=1.0,
  1259. kernel="rbf",
  1260. degree=3,
  1261. gamma="scale",
  1262. coef0=0.0,
  1263. shrinking=True,
  1264. tol=1e-3,
  1265. cache_size=200,
  1266. verbose=False,
  1267. max_iter=-1,
  1268. ):
  1269. super().__init__(
  1270. kernel=kernel,
  1271. degree=degree,
  1272. gamma=gamma,
  1273. coef0=coef0,
  1274. tol=tol,
  1275. C=C,
  1276. nu=nu,
  1277. epsilon=0.0,
  1278. shrinking=shrinking,
  1279. probability=False,
  1280. cache_size=cache_size,
  1281. class_weight=None,
  1282. verbose=verbose,
  1283. max_iter=max_iter,
  1284. random_state=None,
  1285. )
  1286. # TODO(1.4): Remove
  1287. @deprecated( # type: ignore
  1288. "Attribute `class_weight_` was deprecated in version 1.2 and will be removed in"
  1289. " 1.4."
  1290. )
  1291. @property
  1292. def class_weight_(self):
  1293. return np.empty(0)
  1294. def _more_tags(self):
  1295. return {
  1296. "_xfail_checks": {
  1297. "check_sample_weights_invariance": (
  1298. "zero sample_weight is not equivalent to removing samples"
  1299. ),
  1300. }
  1301. }
  1302. class OneClassSVM(OutlierMixin, BaseLibSVM):
  1303. """Unsupervised Outlier Detection.
  1304. Estimate the support of a high-dimensional distribution.
  1305. The implementation is based on libsvm.
  1306. Read more in the :ref:`User Guide <outlier_detection>`.
  1307. Parameters
  1308. ----------
  1309. kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable, \
  1310. default='rbf'
  1311. Specifies the kernel type to be used in the algorithm.
  1312. If none is given, 'rbf' will be used. If a callable is given it is
  1313. used to precompute the kernel matrix.
  1314. degree : int, default=3
  1315. Degree of the polynomial kernel function ('poly').
  1316. Must be non-negative. Ignored by all other kernels.
  1317. gamma : {'scale', 'auto'} or float, default='scale'
  1318. Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
  1319. - if ``gamma='scale'`` (default) is passed then it uses
  1320. 1 / (n_features * X.var()) as value of gamma,
  1321. - if 'auto', uses 1 / n_features
  1322. - if float, must be non-negative.
  1323. .. versionchanged:: 0.22
  1324. The default value of ``gamma`` changed from 'auto' to 'scale'.
  1325. coef0 : float, default=0.0
  1326. Independent term in kernel function.
  1327. It is only significant in 'poly' and 'sigmoid'.
  1328. tol : float, default=1e-3
  1329. Tolerance for stopping criterion.
  1330. nu : float, default=0.5
  1331. An upper bound on the fraction of training
  1332. errors and a lower bound of the fraction of support
  1333. vectors. Should be in the interval (0, 1]. By default 0.5
  1334. will be taken.
  1335. shrinking : bool, default=True
  1336. Whether to use the shrinking heuristic.
  1337. See the :ref:`User Guide <shrinking_svm>`.
  1338. cache_size : float, default=200
  1339. Specify the size of the kernel cache (in MB).
  1340. verbose : bool, default=False
  1341. Enable verbose output. Note that this setting takes advantage of a
  1342. per-process runtime setting in libsvm that, if enabled, may not work
  1343. properly in a multithreaded context.
  1344. max_iter : int, default=-1
  1345. Hard limit on iterations within solver, or -1 for no limit.
  1346. Attributes
  1347. ----------
  1348. class_weight_ : ndarray of shape (n_classes,)
  1349. Multipliers of parameter C for each class.
  1350. Computed based on the ``class_weight`` parameter.
  1351. .. deprecated:: 1.2
  1352. `class_weight_` was deprecated in version 1.2 and will be removed in 1.4.
  1353. coef_ : ndarray of shape (1, n_features)
  1354. Weights assigned to the features (coefficients in the primal
  1355. problem). This is only available in the case of a linear kernel.
  1356. `coef_` is readonly property derived from `dual_coef_` and
  1357. `support_vectors_`.
  1358. dual_coef_ : ndarray of shape (1, n_SV)
  1359. Coefficients of the support vectors in the decision function.
  1360. fit_status_ : int
  1361. 0 if correctly fitted, 1 otherwise (will raise warning)
  1362. intercept_ : ndarray of shape (1,)
  1363. Constant in the decision function.
  1364. n_features_in_ : int
  1365. Number of features seen during :term:`fit`.
  1366. .. versionadded:: 0.24
  1367. feature_names_in_ : ndarray of shape (`n_features_in_`,)
  1368. Names of features seen during :term:`fit`. Defined only when `X`
  1369. has feature names that are all strings.
  1370. .. versionadded:: 1.0
  1371. n_iter_ : int
  1372. Number of iterations run by the optimization routine to fit the model.
  1373. .. versionadded:: 1.1
  1374. n_support_ : ndarray of shape (n_classes,), dtype=int32
  1375. Number of support vectors for each class.
  1376. offset_ : float
  1377. Offset used to define the decision function from the raw scores.
  1378. We have the relation: decision_function = score_samples - `offset_`.
  1379. The offset is the opposite of `intercept_` and is provided for
  1380. consistency with other outlier detection algorithms.
  1381. .. versionadded:: 0.20
  1382. shape_fit_ : tuple of int of shape (n_dimensions_of_X,)
  1383. Array dimensions of training vector ``X``.
  1384. support_ : ndarray of shape (n_SV,)
  1385. Indices of support vectors.
  1386. support_vectors_ : ndarray of shape (n_SV, n_features)
  1387. Support vectors.
  1388. See Also
  1389. --------
  1390. sklearn.linear_model.SGDOneClassSVM : Solves linear One-Class SVM using
  1391. Stochastic Gradient Descent.
  1392. sklearn.neighbors.LocalOutlierFactor : Unsupervised Outlier Detection using
  1393. Local Outlier Factor (LOF).
  1394. sklearn.ensemble.IsolationForest : Isolation Forest Algorithm.
  1395. Examples
  1396. --------
  1397. >>> from sklearn.svm import OneClassSVM
  1398. >>> X = [[0], [0.44], [0.45], [0.46], [1]]
  1399. >>> clf = OneClassSVM(gamma='auto').fit(X)
  1400. >>> clf.predict(X)
  1401. array([-1, 1, 1, 1, -1])
  1402. >>> clf.score_samples(X)
  1403. array([1.7798..., 2.0547..., 2.0556..., 2.0561..., 1.7332...])
  1404. """
  1405. _impl = "one_class"
  1406. _parameter_constraints: dict = {**BaseLibSVM._parameter_constraints}
  1407. for unused_param in ["C", "class_weight", "epsilon", "probability", "random_state"]:
  1408. _parameter_constraints.pop(unused_param)
  1409. def __init__(
  1410. self,
  1411. *,
  1412. kernel="rbf",
  1413. degree=3,
  1414. gamma="scale",
  1415. coef0=0.0,
  1416. tol=1e-3,
  1417. nu=0.5,
  1418. shrinking=True,
  1419. cache_size=200,
  1420. verbose=False,
  1421. max_iter=-1,
  1422. ):
  1423. super().__init__(
  1424. kernel,
  1425. degree,
  1426. gamma,
  1427. coef0,
  1428. tol,
  1429. 0.0,
  1430. nu,
  1431. 0.0,
  1432. shrinking,
  1433. False,
  1434. cache_size,
  1435. None,
  1436. verbose,
  1437. max_iter,
  1438. random_state=None,
  1439. )
  1440. # TODO(1.4): Remove
  1441. @deprecated( # type: ignore
  1442. "Attribute `class_weight_` was deprecated in version 1.2 and will be removed in"
  1443. " 1.4."
  1444. )
  1445. @property
  1446. def class_weight_(self):
  1447. return np.empty(0)
  1448. def fit(self, X, y=None, sample_weight=None):
  1449. """Detect the soft boundary of the set of samples X.
  1450. Parameters
  1451. ----------
  1452. X : {array-like, sparse matrix} of shape (n_samples, n_features)
  1453. Set of samples, where `n_samples` is the number of samples and
  1454. `n_features` is the number of features.
  1455. y : Ignored
  1456. Not used, present for API consistency by convention.
  1457. sample_weight : array-like of shape (n_samples,), default=None
  1458. Per-sample weights. Rescale C per sample. Higher weights
  1459. force the classifier to put more emphasis on these points.
  1460. Returns
  1461. -------
  1462. self : object
  1463. Fitted estimator.
  1464. Notes
  1465. -----
  1466. If X is not a C-ordered contiguous array it is copied.
  1467. """
  1468. super().fit(X, np.ones(_num_samples(X)), sample_weight=sample_weight)
  1469. self.offset_ = -self._intercept_
  1470. return self
  1471. def decision_function(self, X):
  1472. """Signed distance to the separating hyperplane.
  1473. Signed distance is positive for an inlier and negative for an outlier.
  1474. Parameters
  1475. ----------
  1476. X : array-like of shape (n_samples, n_features)
  1477. The data matrix.
  1478. Returns
  1479. -------
  1480. dec : ndarray of shape (n_samples,)
  1481. Returns the decision function of the samples.
  1482. """
  1483. dec = self._decision_function(X).ravel()
  1484. return dec
  1485. def score_samples(self, X):
  1486. """Raw scoring function of the samples.
  1487. Parameters
  1488. ----------
  1489. X : array-like of shape (n_samples, n_features)
  1490. The data matrix.
  1491. Returns
  1492. -------
  1493. score_samples : ndarray of shape (n_samples,)
  1494. Returns the (unshifted) scoring function of the samples.
  1495. """
  1496. return self.decision_function(X) + self.offset_
  1497. def predict(self, X):
  1498. """Perform classification on samples in X.
  1499. For a one-class model, +1 or -1 is returned.
  1500. Parameters
  1501. ----------
  1502. X : {array-like, sparse matrix} of shape (n_samples, n_features) or \
  1503. (n_samples_test, n_samples_train)
  1504. For kernel="precomputed", the expected shape of X is
  1505. (n_samples_test, n_samples_train).
  1506. Returns
  1507. -------
  1508. y_pred : ndarray of shape (n_samples,)
  1509. Class labels for samples in X.
  1510. """
  1511. y = super().predict(X)
  1512. return np.asarray(y, dtype=np.intp)
  1513. def _more_tags(self):
  1514. return {
  1515. "_xfail_checks": {
  1516. "check_sample_weights_invariance": (
  1517. "zero sample_weight is not equivalent to removing samples"
  1518. ),
  1519. }
  1520. }