Source code for pysptools.skl.svm

##------------------------------------------------------------------------------# Copyright (c) 2013-2015, Christian Therien## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#------------------------------------------------------------------------------## svm.py - This file is part of the PySptools package.#importnumpyasnpfromsklearnimportpreprocessingfromsklearnimportsvmfrom.baseimportHyperScaledBaseClassifier

[docs]classHyperSVC(svm.SVC,HyperScaledBaseClassifier):""" Apply scikit-learn SVC on a hypercube. For the __init__ class contructor parameters: `see the sklearn.svm.SVC class parameters` The class is intrumented to be use with the scikit-learn cross validation. It use the plot and display methods from the class Output. Note: the class always do a preprocessing.scale before any processing. Note: the C parameter is set to 1, the result of this setting is that the class_weight is relative to C and that the first value of class_weight is the background. An example: you wish to fit two classes "1" and "2" with the help of one ROI for each, you declare class_weight like this: * class_weight={0:1,1:10,2:10} * 0: is always the background and is set to 1, 1: is the first class, * 2: is the second. A value of 10 for both classes give good results to start with. """def__init__(self,C=1.0,kernel='rbf',degree=3,gamma='auto',coef0=0.0,shrinking=True,probability=False,tol=1e-3,cache_size=200,class_weight=None,verbose=False,max_iter=-1,decision_function_shape=None,random_state=None):super(HyperSVC,self).__init__(C=C,kernel=kernel,degree=degree,gamma=gamma,coef0=coef0,shrinking=shrinking,probability=probability,tol=tol,cache_size=cache_size,class_weight=class_weight,verbose=verbose,max_iter=max_iter,decision_function_shape=decision_function_shape,random_state=random_state)HyperScaledBaseClassifier.__init__(self,'HyperSVC')

[docs]deffit(self,X,y):""" Same as the sklearn.svm.SVC fit call, but with preprocessing.scale call first. Parameters: X: `numpy array` A vector (n_samples, n_features) where each element *n_features* is a spectrum. y: `numpy array` Target values (n_samples,). A zero value is the background. A value of one or more is a class value. """super(HyperSVC,self)._set_n_clusters(int(np.max(y)))X_scaled=preprocessing.scale(X)super(HyperSVC,self).fit(X_scaled,y)

[docs]defpredict(self,X):""" Same as the sklearn.svm.SVC predict call, but with a call to preprocessing.scale first. Parameters: X: `numpy array` A vector where each element is a spectrum. """X_scaled=preprocessing.scale(X)y=super(HyperSVC,self).predict(X_scaled)returnself.classes_.take(np.asarray(y,dtype=np.intp))