当前位置: 首页>>代码示例>>Python>>正文


Python NuSVC.decision_function方法代码示例

本文整理汇总了Python中sklearn.svm.NuSVC.decision_function方法的典型用法代码示例。如果您正苦于以下问题:Python NuSVC.decision_function方法的具体用法?Python NuSVC.decision_function怎么用?Python NuSVC.decision_function使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.svm.NuSVC的用法示例。


在下文中一共展示了NuSVC.decision_function方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import decision_function [as 别名]
class Learner:
       
       #@input recurrence: Dimensionality of the feature-space
       #        with dimension corresponding to the last n returns
       #        this is a positive integer
       #@input realy_recurrent: default=False
       #        if true: the last decision is also a dimension in the
       #        feature space
       #@input label_par paramter used for labeling 'r' for returns
       #                                            'p' for prices
    def __init__(self, recurrence=30, w_size=20,hybrid = False):
        self.learner = NuSVC()
        
        #size of each training batch
        self.batch_size = w_size * (recurrence)
        #size of the sliding window for sharpé ratio
        self.window_size = 5 * self.batch_size
        
        #true if part of a hybrid learner
        self.hybrid = hybrid
        
        # the data matrix of a single batch
        # Data-Vector = r_1, ... r_n
        # with r_n := r_n - r_n-1
        self.returns = list()
        #training data for experimental apporach
        self.train_dat = list()
        self.labels = list()
        self.decisions = list()
        self.recurrence = recurrence
        
        self.last_decision = 0
        self.ready = False
        self.tstep = 0
        self.prices = list()
        return
       
    def predict(self,new_price,old_price,tstep = 0):
        #default decision value        
        decision = 0
        #Add prices to sliding window
        self.prices.append(new_price)
        if(len(self.prices) > self.window_size):
            self.prices.pop(0)
        latest_return = new_price - old_price
        #add next label
        if(self.tstep > self.recurrence):
            self.labels.append(self.label_returns(latest_return))
        #increment timer
        self.tstep += 1
        #add latest return to history
        self.returns.append(latest_return)
        if(self.tstep > self.window_size):
            if(len(self.returns) > self.window_size):
                self.returns.pop(0)
        #if batch is full, start training
        if(self.tstep%self.batch_size == 0 and self.tstep != 0):
            self.train()
            #disabled this, normally for predicting prices, but performance is
            #worse, so this is actually dead code
        #setup x-vector
        if(self.tstep > self.recurrence):
            x = self.returns[len(self.returns)-self.recurrence-1:len(self.returns)-1]
            #set up training matrix            
            x = np.array(x)
            x = x.reshape((len(x),1))
            self.train_dat.append(x)
            x = np.transpose(x)
            #create decision only if svm is trained
            if(self.ready):
                decision = np.tanh(self.learner.decision_function(x))
                decision = decision[0]
        #if the system is truly recurrent (uses the last decision input-vecotr)
        #append the decision
        self.last_decision = decision
        return  decision
    
    #calls partial_fit() on the svm to adjust it's internal model
    def train(self):
        #setup training matrix
        train_dat = np.zeros((len(self.labels),self.recurrence))
        for i in range(len(train_dat)):
            train_dat[i][:] = np.transpose(self.train_dat[i])
        #np.transpose(train_dat)
        self.learner.fit(train_dat, self.labels)
        #clear the training-related strzctures
        self.labels = list()
        self.train_dat = list()
        self.ready = True
        return
        #calls partial_fit() on the svm to adjust it's internal model
        
    #labeling function using the complete vector
    #very simple, since it only detects trends depending on the mu
    def label_set(self,return_list):
        mu_current = np.mean(return_list)
        mu_total = np.mean(self.returns)
        if(mu_current >= mu_total):
            return 1
        else:
#.........这里部分代码省略.........
开发者ID:Cypher42,项目名称:AutoBuffett,代码行数:103,代码来源:NLSVC.py

示例2: NuSVC

# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import decision_function [as 别名]
        """

    print 'standardization'
    #print trn_data
    #print tst_data
    #trn_data_scaled = preprocessing.scale(trn_data)
    #tst_data_scaled = preprocessing.scale(tst_data)
    scaler = preprocessing.StandardScaler().fit(trn_data)
    trn_data_scaled = scaler.transform(trn_data)
    tst_data_scaled = scaler.transform(tst_data)
    #print trn_data_scaled
    #print tst_data_scaled
    clf = NuSVC(nu=0.5, kernel = 'linear')
    clf.fit(trn_data_scaled, trn_label)
    pred_label = clf.predict(tst_data_scaled)
    print pred_label
    print clf.decision_function(tst_data_scaled)
    accu = sum(pred_label == tst_label)/float(len(pred_label))


    
    if args.align_algo in ['ppca_idvclas','pica_idvclas']:
        for it in range(11):
            np.savez_compressed(options['working_path']+opt_group_folder+args.align_algo+'_acc_'+str(it)+'.npz',accu = accu)
    else:
        np.savez_compressed(options['working_path']+opt_group_folder+args.align_algo+'_acc_'+str(itr)+'.npz',accu = accu)
        #np.savez_compressed(options['working_path']+opt_group_folder+args.align_algo+'_acc_'+str(10)+'.npz',accu = accu)
    print options['working_path']+opt_group_folder+args.align_algo+'_acc_'+str(itr)+'.npz'
    print np.mean(accu)

开发者ID:snastase,项目名称:SRM,代码行数:31,代码来源:run_exp_noLR_idvclas.py

示例3: FactorizationMachineClassifier

# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import decision_function [as 别名]
y[flip] = ~y[flip]

# fit the model
fm = FactorizationMachineClassifier(n_components=1, fit_linear=False,
                                    random_state=0)
fm.fit(X, y)

# fit a NuSVC for comparison
svc = NuSVC(kernel='poly', degree=2)
svc.fit(X, y)

# plot the decision function for each datapoint on the grid
Z = fm.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

Z_svc = svc.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z_svc = Z_svc.reshape(xx.shape)

plt.imshow(Z, interpolation='nearest',
           extent=(xx.min(), xx.max(), yy.min(), yy.max()), aspect='auto',
           origin='lower', cmap=plt.cm.PuOr_r)

contour_fm = plt.contour(xx, yy, Z, levels=[0], linewidths=2)

contour_svc = plt.contour(xx, yy, Z_svc, levels=[0], linestyles='dashed')

plt.scatter(X[:, 0], X[:, 1], s=30, c=y, cmap=plt.cm.Paired)
plt.xticks(())
plt.yticks(())
plt.axis([-3, 3, -3, 3])
plt.legend((contour_fm.collections[0], contour_svc.collections[0]),
开发者ID:Saikrishna41,项目名称:polylearn,代码行数:33,代码来源:plot_xor.py

示例4: __init__

# 需要导入模块: from sklearn.svm import NuSVC [as 别名]
# 或者: from sklearn.svm.NuSVC import decision_function [as 别名]
class Learner:
       
       #@input recurrence: Dimensionality of the feature-space
       #        with dimension corresponding to the last n returns
       #        this is a positive integer
       #@input realy_recurrent: default=False
       #        if true: the last decision is also a dimension in the
       #        feature space
       #@input label_par paramter used for labeling 'r' for returns
       #                                            'p' for prices
    def __init__(self,adaption = 0.5,transactionCost = 0.001, recurrence=35, realy_recurrent=False, w_size=20,label_par='r'):
        self.learner = NuSVC()
        self.transactionCost = transactionCost
        self.adaption = adaption
        
        #size of each training batch
        self.batch_size = 200 * (recurrence)
        #size of the sliding window for sharpé ratio
        self.window_size = w_size * self.batch_size
        
        # the data matrix of a single batch
        # Data-Vector = r_1, ... r_n, prediction_t-1
        # with r_n := r_n - r_n-1
        self.returns = list()
        self.labels = list()
        self.decisions = [0]
        self.weighted_returns = list()
        
        #self.rng = rnj.Learner()
        
        self.recurrence = recurrence
        self.last_decision = 0
        self.ready = False
        self.tstep = 0
        self.recurrent = realy_recurrent
        self.prices = list()
        self.label_par = label_par
        
        self.sharpeA_old = 1
        self.sharpeB_old = 1
        return
        
    def predict(self,new_price,old_price,tstep = 0):
        latest_return = new_price - old_price

        #Test differen classifier
        #if(self.tstep == 0):
        #    self.prices.append(old_price)
        self.prices.append(new_price)

        if(len(self.prices) > self.window_size):
            self.prices.pop(0)

        self.tstep += 1
        self.returns.append(latest_return)
        if(self.ready):
            x = self.returns[len(self.returns)-self.recurrence-1:len(self.returns)-1]
            if(self.recurrent):
                x.append(self.last_decision)            
            x = np.array(x)
            x = x.reshape((len(x),1))
            x = np.transpose(x)
            #maybe add previous decision later on
            decision = np.tanh(self.learner.decision_function(x))
        else:
            decision = 0.5#self.rng.predict()
        self.weighted_returns.append(self.last_decision * latest_return - (self.transactionCost*np.fabs(self.last_decision - decision)))
        if(self.tstep > self.window_size):
            if(len(self.returns) > self.window_size):
                self.returns.pop(0)
        if(self.tstep%self.batch_size == 0 and self.tstep != 0 and self.tstep%self.window_size==0):
            self.train()
            self.ready = True
        self.decisions.append(decision)
        if(len(self.decisions) > self.window_size):
            self.decisions.pop(0)
        self.last_decision = decision
        return  decision
    
    #calls partial_fit() on the svm to adjust it's internal model
    def train(self):
        returns = np.array(self.returns)
        returns = returns[len(returns)-(self.batch_size):]
#        returns = returns.reshape((100,self.recurrence))
        
        weighted_returns = np.array(self.weighted_returns)
        weighted_returns = weighted_returns[len(weighted_returns)-(self.batch_size):]
       #weighted_returns = weighted_returns.reshape((100,self.recurrence))
        
        decisions = np.array(self.decisions)
        decisions = decisions[len(decisions)-(self.batch_size):]
        #decisions = decisions.reshape((100,self.recurrence))
        
        trainingMatrix = list()
        self.labels = list()
        
        #for i in range(len(weighted_returns)):
            #self.labels.append(self.label_set(weighted_returns[i],decisions[i]))
        for i in range(self.recurrence,len(weighted_returns)-1):
            trainDat = weighted_returns[i-self.recurrence:i]
#.........这里部分代码省略.........
开发者ID:Cypher42,项目名称:AutoBuffett,代码行数:103,代码来源:NLSVM.py


注:本文中的sklearn.svm.NuSVC.decision_function方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。