Python scipy 模块,mean() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用scipy.mean()

项目:nimo    作者:wolfram2012    | 项目源码 | 文件源码
def getForegroundMask(self):
        '''
        @return: A mask image indicating which pixels are considered foreground.
          Depending on whether soft-thresholding is used, this may be a binary image
          with values of [0 or 255], or image of weights [0.0-255.0], which will
          have to be divided by 255 to get weights [0.0-1.0].         
        @note: One may wish to perform additional morphological operations
            on the foreground mask prior to use.
        '''
        diff = self._computeBGDiff()
        if self._softThreshold:
            mask = 1 - (math.e)**(-(1.0*diff)/self._threshold)  #element-wise exp weighting
            #mask = (diff > self._threshold)   
        else:
            mask = (sp.absolute(diff) > self._threshold)    
            #mu = sp.mean(diff)
            #sigma = sp.std(diff)
            #mask = sp.absolute((diff-mu)/sigma) > self._threshold
        return pv.Image(mask*255.0)
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_html(self, base_file_name: str, h_level: int) -> str:
        sp = None # type: SingleProperty
        columns = [
            BOTableColumn("n", "{:5d}", lambda sp, _: sp.observations(), first),
            BOTableColumn("mean", "{:10.5f}", lambda sp, _: sp.mean(), first),
            BOTableColumn("mean / best mean", "{:5.5%}", lambda sp, means: sp.mean() / min(means), first),
            BOTableColumn("mean / mean of first impl", "{:5.5%}", lambda sp, means: sp.mean() / means[0], first),
            BOTableColumn("std / mean", "{:5.5%}", lambda sp, _: sp.std_dev_per_mean(), first),
            BOTableColumn("std / best mean", "{:5.5%}", lambda sp, means: sp.std_dev() / min(means), first),
            BOTableColumn("std / mean of first impl", "{:5.5%}", lambda sp, means: sp.std_dev() / means[0], first),
            BOTableColumn("median", "{:5.5f}", lambda sp, _: sp.median(), first)
        ]
        html = """
        <h{h}>Input: {input}</h{h}>
        The following plot shows the actual distribution of the measurements for each implementation.
        {box_plot}
        """.format(h=h_level, input=repr(self.input), box_plot=self.get_box_plot_html(base_file_name))
        html += self.table_html_for_vals_per_impl(columns, base_file_name)
        return html
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_x_per_impl(self, property: StatProperty) -> t.Dict[str, t.List[float]]:
        """
        Returns a list of [property] for each implementation.

        :param property: property function that gets a SingleProperty object and a list of all means and returns a float
        """
        means = [x.mean() for x in self.impls.values()]  # type: t.List[float]
        singles = [x.get_single_property() for x in self.impls.values()]
        ret = InsertionTimeOrderedDict() # t.Dict[str, t.List[float]]
        property_arg_number = min(len(inspect.signature(property).parameters), 4)
        for (i, impl) in enumerate(self.impls):
            args = [singles[i], means, singles, i]
            ret[impl] = [property(*args[:property_arg_number])]
        #pprint(ret._dict)
        typecheck(ret._dict, Dict(key_type=Str(), value_type=List(Float()|Int()), all_keys=False))
        return ret
项目:prml    作者:Yevgnen    | 项目源码 | 文件源码
def __init__(self,
                 kernel='gaussian',
                 mean=None,
                 cov=None,
                 beta=None):
        """
        Build alpha relenvance vector machine mode.

        The kernel function is Gaussian default.
        The prior mean ``mean`` is alpha zero vector by default.
        The prior cov matrix ``precison`` should be given by alpha 1d numpy array.
        The cov of the distribution of target conditioning on the isput is given by ``beta``.
        """
        self.supported_kernel = {
            'gaussian': GaussianKernel(sigma=0.2),  # FIXED me!
            'linear': LinearKernel()
        }
        self.kernel = self.supported_kernel[kernel]
        self.rv_indices = None
        self.cov = cov
        self.mean = mean
        self.beta = beta
项目:Python-for-Finance-Second-Edition    作者:PacktPublishing    | 项目源码 | 文件源码
def sharpeRatio(ticker,begdate=(2012,1,1),enddate=(2016,12,31)):
    """Objective: estimate Sharpe ratio for stock
        ticker  : stock symbol 
        begdate : beginning date
        enddate : ending date

       Example #1: sharpeRatio("ibm")
                     0.0068655583807256159

       Example #2: date1=(1990,1,1)
                   date2=(2015,12,23)
                   sharpeRatio("ibm",date1,date2)
                     0.027831010497755326
    """
    import scipy as sp
    from matplotlib.finance import quotes_historical_yahoo_ochl as getData
    p = getData(ticker,begdate, enddate,asobject=True,adjusted=True)
    ret=p.aclose[1:]/p.aclose[:-1]-1
    return sp.mean(ret)/sp.std(ret)
项目:SLIC_cityscapes    作者:wpqmanu    | 项目源码 | 文件源码
def update(self, centers):
        # sums = [scipy.zeros(5) for i in range(len(centers))]
        # nums = [0 for i in range(len(centers))]
        # width, height = self.img.shape[:2]
        print "E step"
        new_centers=[]
        nan_record=[]

        for i in xrange(len(centers)):
            current_region=self.xylab[self.assignedindex == i]
            if current_region.size>0: #non-empty region
                new_centers.append(scipy.mean(current_region, 0))
            else: # empty region
                nan_record.append(i)

        # after we get full nan_record list, update assignment index (elimnate those indexes in reverse order)
        for nan_value in nan_record[::-1]:
            self.assignedindex[self.assignedindex>nan_value]=self.assignedindex[self.assignedindex>nan_value]-1


        for new_center_index in range(len(new_centers)):
            # print new_center_index
            new_centers[new_center_index][2:]=self.labimg[math.floor(new_centers[new_center_index][0])][math.floor(new_centers[new_center_index][1])]

        return new_centers,nan_record
项目:HistoricalMap    作者:lennepkade    | 项目源码 | 文件源码
def __init__(self):
        self.ni = []
        self.prop = []
        self.mean = []
        self.cov =[]
        self.Q = []
        self.L = []
        self.classnum = [] # to keep right labels
        self.tau = 0.0
项目:HistoricalMap    作者:lennepkade    | 项目源码 | 文件源码
def learn(self,x,y):
        '''
        Function that learns the GMM with ridge regularizationb from training samples
        Input:
            x : the training samples
            y :  the labels
        Output:
            the mean, covariance and proportion of each class, as well as the spectral decomposition of the covariance matrix
        '''

        ## Get information from the data
        C = sp.unique(y).shape[0]
        #C = int(y.max(0))  # Number of classes
        n = x.shape[0]  # Number of samples
        d = x.shape[1]  # Number of variables
        eps = sp.finfo(sp.float64).eps

        ## Initialization
        self.ni = sp.empty((C,1))    # Vector of number of samples for each class
        self.prop = sp.empty((C,1))  # Vector of proportion
        self.mean = sp.empty((C,d))  # Vector of means
        self.cov = sp.empty((C,d,d)) # Matrix of covariance
        self.Q = sp.empty((C,d,d)) # Matrix of eigenvectors
        self.L = sp.empty((C,d)) # Vector of eigenvalues
        self.classnum = sp.empty(C).astype('uint8')

        ## Learn the parameter of the model for each class
        for c,cR in enumerate(sp.unique(y)):

            j = sp.where(y==(cR))[0]

            self.classnum[c] = cR # Save the right label
            self.ni[c] = float(j.size)    
            self.prop[c] = self.ni[c]/n
            self.mean[c,:] = sp.mean(x[j,:],axis=0)
            self.cov[c,:,:] = sp.cov(x[j,:],bias=1,rowvar=0)  # Normalize by ni to be consistent with the update formulae

            # Spectral decomposition
            L,Q = linalg.eigh(self.cov[c,:,:])
            idx = L.argsort()[::-1]
            self.L[c,:] = L[idx]
            self.Q[c,:,:]=Q[:,idx]
项目:HistoricalMap    作者:lennepkade    | 项目源码 | 文件源码
def predict(self,xt,tau=None,proba=None):
        '''
        Function that predict the label for sample xt using the learned model
        Inputs:
            xt: the samples to be classified
        Outputs:
            y: the class
            K: the decision value for each class
        '''
        ## Get information from the data
        nt = xt.shape[0]        # Number of testing samples
        C = self.ni.shape[0]    # Number of classes

        ## Initialization
        K = sp.empty((nt,C))

        if tau is None:
            TAU=self.tau
        else:
            TAU=tau

        for c in range(C):
            invCov,logdet = self.compute_inverse_logdet(c,TAU)
            cst = logdet - 2*sp.log(self.prop[c]) # Pre compute the constant

            xtc = xt-self.mean[c,:]
            temp = sp.dot(invCov,xtc.T).T
            K[:,c] = sp.sum(xtc*temp,axis=1)+cst
            del temp,xtc

        ##

        ## Assign the label save in classnum to the minimum value of K 
        yp = self.classnum[sp.argmin(K,1)]

        ## Reassign label with real value

        if proba is None:
            return yp
        else:
            return yp,K
项目:HistoricalMap    作者:lennepkade    | 项目源码 | 文件源码
def BIC(self,x,y,tau=None):
        '''
        Computes the Bayesian Information Criterion of the model
        '''
        ## Get information from the data
        C,d = self.mean.shape
        n = x.shape[0]

        ## Initialization
        if tau is None:
            TAU=self.tau
        else:
            TAU=tau

        ## Penalization
        P = C*(d*(d+3)/2) + (C-1)
        P *= sp.log(n)

        ## Compute the log-likelihood
        L = 0
        for c in range(C):
            j = sp.where(y==(c+1))[0]
            xi = x[j,:]
            invCov,logdet = self.compute_inverse_logdet(c,TAU)
            cst = logdet - 2*sp.log(self.prop[c]) # Pre compute the constant
            xi -= self.mean[c,:]
            temp = sp.dot(invCov,xi.T).T
            K = sp.sum(xi*temp,axis=1)+cst
            L +=sp.sum(K)
            del K,xi

        return L + P
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def amean_std(values: t.List[float]) -> float:
    """
    Calculates the arithmetic mean.
    """
    return sp.std(values)
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def used_summarize_mean(values: t.List[float]) -> float:
    if CALC_MODE in [Mode.geom_mean_rel_to_best, Mode.mean_rel_to_one]:
        return stats.gmean(values)
    elif CALC_MODE in [Mode.mean_rel_to_first]:
        return sp.mean(values)
    assert False
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def rel_mean_property(single: SingleProperty, means: t.List[float], *args) -> float:
    """
    A property function that returns the relative mean (the mean of the single / minimum of means)
    """
    return single.mean() / min(means)
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def rel_std_property(single: SingleProperty, means: t.List[float], *args) -> float:
    """
    A property function that returns the relative standard deviation (relative to single's mean)
    """
    return single.std_dev_per_mean()
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def used_rel_mean_property(single: SingleProperty, means: t.List[float], *args) -> float:
    if CALC_MODE == Mode.geom_mean_rel_to_best:
        return single.mean() / min(means)
    elif CALC_MODE == Mode.mean_rel_to_first:
        return single.mean() / means[0]
    elif CALC_MODE == Mode.mean_rel_to_one:
        return single.mean()
    assert False
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_statistical_properties_for_each(self, func: StatisticalPropertyFunc) -> t.Dict[str, float]:
        sps = self.get_single_properties()
        means = [sp.mean() for (impl, sp) in sps]
        d = InsertionTimeOrderedDict()
        for (impl, sp) in sps:
            d[impl] = func(sp, means)
        return d
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_box_plot_html(self, base_file_name: str) -> str:
        return self.boxplot_html_for_data("mean score", base_file_name + "_program" + html_escape_property(self.name),
                                          self.get_statistical_property_scores(rel_mean_func))
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_html2(self, base_file_name: str, h_level: int):
        base_file_name += "__program_" + html_escape_property(self.name)
        html = """
            <h{}>Program: {!r}</h{}>
            The following plot shows the rel means (means / min means) per input distribution for every implementation.
        """.format(h_level, self.name, h_level)
        html += self.boxplot_html_for_data("mean score", base_file_name, self.get_x_per_impl(used_rel_mean_property))
        html += self.table_html_for_vals_per_impl(common_columns, base_file_name)
        for (i, input) in enumerate(self.prog_inputs.keys()):
            app = html_escape_property(input)
            if len(app) > 20:
                app = str(i)
            html += self.prog_inputs[input].get_html2(base_file_name + "_" + app, h_level + 1)
        return html
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_impl_mean_scores(self) -> t.Dict[str, t.List[float]]:
        """
        Geometric mean over the means relative to best per implementation (per input).
        """
        return self.get_statistical_property_scores(rel_mean_func)
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_box_plot_html(self, base_file_name: str) -> str: # a box plot over the mean scores per sub program
        scores_per_impl = self.get_scores_per_impl()
        singles = []
        for impl in scores_per_impl:
            scores = scores_per_impl[impl]
            name = "mean score"
            data = RunData({name: scores}, {"description": impl})
            singles.append(SingleProperty(Single(data), data, name))
        return self.boxplot_html(base_file_name, singles)
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_html2(self, base_file_name: str, h_level: int):
        base_file_name += "__cat_" + html_escape_property(self.name)
        html = """
            <h{}>{}</h{}>
        """.format(h_level, self.name, h_level)
        if len(self.children) > 1:
            html += self.boxplot_html_for_data("mean score", base_file_name, self.get_x_per_impl(used_rel_mean_property))
            html += self.table_html_for_vals_per_impl(common_columns, base_file_name)
            if len(self.get_input_strs()) > 1:
                html += """
                <h{h}> Mean scores per input</h{h}>
                """.format(h=h_level + 1)
                for input in self.get_input_strs():
                    html += """
                        <h{h}>Mean scores for input {!r}</h{h}>
                        The plot shows the distribution of mean scores per program for each implementation.
                        <p>
                    """.format(input, h=h_level + 2)
                    file_name = base_file_name + "__input_" + html_escape_property(input)
                    html += self.boxplot_html_for_data("mean score", file_name,
                                          self.get_x_per_impl_and_input(used_rel_mean_property, input))
                    html += self.table_html_for_vals_per_impl(common_columns, file_name,
                                                              lambda property: self.get_x_per_impl_and_input(property,                                                                                       input))
        for (i, prog) in enumerate(self.programs):
            html += self.programs[prog].get_html2(base_file_name + "_" + html_escape_property(prog), h_level + 1)
        return html
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_box_plot_per_input_per_impl_html(self, base_file_name: str, input: str) -> str:
        """
        A box plot for each input that shows the mean scores (over all programs) for each implementation.
        """
        return self.boxplot_html_for_data("mean score", base_file_name + "__input_" + html_escape_property(input),
                                          self.get_statistical_property_scores_per_input_per_impl(rel_mean_func, input))
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def get_box_plot_per_input_per_impl_html(self, base_file_name: str, input_num: int) -> str:
        """
        A box plot for each input that shows the mean scores (over all programs) for each implementation.
        """
        return self.boxplot_html_for_data("mean score", base_file_name + "__input_" + str(input_num),
                                          self.get_statistical_property_scores_per_input_per_impl(rel_mean_func, input_num))
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def _speed_up(self, property: str, data1: RunData, data2: RunData):
        """
        Calculates the speed up from the second to the first
        (e.g. the first is RESULT * 100 % faster than the second).
        """
        return (scipy.mean(data1[property]) - scipy.mean(data2[property])) \
               / scipy.mean(data1[property])
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def _estimate_time_for_run_datas(self, run_bin_size: int, data1: RunData, data2: RunData,
                                     min_runs: int, max_runs: int) -> float:
        if min(len(data1), len(data2)) == 0 \
                or "__ov-time" not in data1.properties \
                or "__ov-time" not in data2.properties:
            return max_runs
        needed_runs = []
        for prop in set(data1.properties).intersection(data2.properties):
            estimate = self.tester.estimate_needed_runs(data1[prop], data2[prop],
                                                                run_bin_size, min_runs, max_runs)
            needed_runs.append(estimate)
        avg_time = max(scipy.mean(data1["__ov-time"]), scipy.mean(data2["__ov-time"]))
        return max(needed_runs) * avg_time
项目:temci    作者:parttimenerd    | 项目源码 | 文件源码
def estimate_time_for_next_round(self, run_bin_size: int, all: bool) -> float:
        """
        Roughly estimates the time needed for the next benchmarking round.

        :param run_bin_size: times a program block is benchmarked in a single block of time and the size of a round
        :param all: expect all program block to be benchmarked
        :return: estimated time in seconds
        """
        if "__ov-time" not in self.properties():
            return -1
        summed = 0
        to_bench = range(0, len(self.runs)) if all else self.get_program_ids_to_bench()
        for i in to_bench:
            summed += scipy.mean(self.runs[i]["__ov-time"] if "__ov_time" in self.runs[i].data else 0) * run_bin_size
        return summed

    #ef add_run_data(self, data: t.Dict[str, t.List[Number]] = None, attributes: t.Dict[str, str] = None,
    #                property_descriptions: t.Dict[str, str] = None) -> int:
    #   """
    #   Adds a new run data (corresponding to a program block) and returns its id.
    #
    #   :param data: benchmarking data of the new run data object
    #   :param attributes: attributes of the new run data object
    #   :param property_descriptions: mapping of property to a description
    #   :return: id of the run data object (and its corresponding program block)
    #   """
    #   self.runs.append(RunData(data, attributes=attributes, property_descriptions))
    #   return len(self.runs) - 1
项目:OSDN    作者:abhijitbendale    | 项目源码 | 文件源码
def compute_mean_vector(category_name, labellist, layer = 'fc8'):
    print category_name
    featurefile_list = glob.glob('%s/%s/*.mat' %(featurefilepath, category_name))

    # gather all the training samples for which predicted category
    # was the category under consideration
    correct_features = []
    for featurefile in featurefile_list:
        try:
            img_arr = loadmat(featurefile)
            predicted_category = labellist[img_arr['scores'].argmax()]
            if predicted_category == category_name:
                correct_features += [img_arr[layer]]
        except TypeError:
            continue

    # Now compute channel wise mean vector
    channel_mean_vec = []
    for channelid in range(correct_features[0].shape[0]):
        channel = []
        for feature in correct_features:
            channel += [feature[channelid, :]]
        channel = sp.asarray(channel)
        assert len(correct_features) == channel.shape[0]
        # Gather mean over each channel, to get mean channel vector
        channel_mean_vec += [sp.mean(channel, axis=0)]

    # this vector contains mean computed over correct classifications
    # for each channel separately
    channel_mean_vec = sp.asarray(channel_mean_vec)
    savemat('%s.mat' %category_name, {'%s'%category_name: channel_mean_vec})
项目:OSDN    作者:abhijitbendale    | 项目源码 | 文件源码
def computeOpenMaxProbability(openmax_fc8, openmax_score_u):
    """ Convert the scores in probability value using openmax

    Input:
    ---------------
    openmax_fc8 : modified FC8 layer from Weibull based computation
    openmax_score_u : degree

    Output:
    ---------------
    modified_scores : probability values modified using OpenMax framework,
    by incorporating degree of uncertainity/openness for a given class

    """
    prob_scores, prob_unknowns = [], []
    for channel in range(NCHANNELS):
        channel_scores, channel_unknowns = [], []
        for category in range(NCLASSES):
            channel_scores += [sp.exp(openmax_fc8[channel, category])]

        total_denominator = sp.sum(sp.exp(openmax_fc8[channel, :])) + sp.exp(sp.sum(openmax_score_u[channel, :]))
        prob_scores += [channel_scores/total_denominator ]
        prob_unknowns += [sp.exp(sp.sum(openmax_score_u[channel, :]))/total_denominator]

    prob_scores = sp.asarray(prob_scores)
    prob_unknowns = sp.asarray(prob_unknowns)

    scores = sp.mean(prob_scores, axis = 0)
    unknowns = sp.mean(prob_unknowns, axis=0)
    modified_scores =  scores.tolist() + [unknowns]
    assert len(modified_scores) == 1001
    return modified_scores

#---------------------------------------------------------------------------------
项目:Wind-Turbine-Design    作者:doublerob7    | 项目源码 | 文件源码
def mean(self):
        if self._mean is None:
            from scipy import mean
            self._mean = mean(self.speed)
        return self._mean
项目:Wind-Turbine-Design    作者:doublerob7    | 项目源码 | 文件源码
def mean(self):
        if self._mean is None:
            from scipy import mean
            self._mean = mean(self.speed)
        return self._mean
项目:prml    作者:Yevgnen    | 项目源码 | 文件源码
def _init_hyperparameters(self, X, T):
        n_samples = X.shape[0]

        if (self.mean is None):
            self.mean = sp.zeros(n_samples + 1)

        if (self.cov is None):
            self.cov = sp.ones(n_samples + 1)

        if (self.beta is None):
            self.beta = 1

        return
项目:prml    作者:Yevgnen    | 项目源码 | 文件源码
def predict(self, X, T, X_new):
        """Predict ``X_new`` with given traning data ``(X, T)``."""
        n_tests = X_new.shape[0]
        phi = sp.r_[sp.ones(n_tests).reshape(1, -1), self._compute_design_matrix(X_new, X)]  # Add x0
        phi = phi[self.rv_indices, :]

        predict_mean = sp.dot(self.mean, phi)
        predict_cov = 1 / self.beta + sp.dot(phi.T, sp.dot(self.cov, phi)).diagonal()

        return predict_mean, predict_cov
项目:prml    作者:Yevgnen    | 项目源码 | 文件源码
def score(self, X_train, T_train, X_test, T_test):
        Y = self.predict(X_train, T_train, X_test)

        return sp.mean(sp.isclose(Y, T_test))
项目:DGA-Detection    作者:philarkwright    | 项目源码 | 文件源码
def check_domain(input_domain):

    baseline, total_bigrams_settings = load_settings()

    if os.path.isfile('data/database.json'):
        with open('data/database.json', 'r') as f:
            try:
                bigram_dict = json.load(f)
            # if the file is empty the ValueError will be thrown
            except ValueError:
                bigram_dict = {}

    percentage = []

    for  bigram_position in xrange(len(input_domain) - 1): #Run through each bigram in the data
        if input_domain[bigram_position:bigram_position + 2] in bigram_dict: #Check if bigram is in dictionary 
            percentage.append((bigram_dict[input_domain[bigram_position:bigram_position + 2]] / total_bigrams_settings) * 100) #Get bigram dictionary value and convert to percantage
        else:
            percentage.append(0) #Bigram value is 0 as it doesn't exist

    if baseline >= scipy.mean(percentage):
        print 67 * "*"
        print 'Baseline:', baseline, 'Domain Average Bigram Percentage:',scipy.mean(percentage)
        return 1
    else:
        return 0

    percentage = [] #Clear percentage list
项目:dzetsaka    作者:lennepkade    | 项目源码 | 文件源码
def transform_3d(self, X):
            X_resampled = sp.zeros((X.shape[0], self.n_samples, X.shape[2]))
            xnew = sp.linspace(0, 1, self.n_samples)
            for i in range(X.shape[0]):
                end = last_index(X[i])
                for j in range(X.shape[2]):
                    X_resampled[i, :, j] = resampled(X[i, :end, j], n_samples=self.n_samples, kind=self.interp_kind)
                # Compute indices based on alignment of dimension self.scaling_col_idx with the reference
                indices_xy = [[] for _ in range(self.n_samples)]

                if self.save_path and len(DTWSampler.saved_dtw_path)==(self.d+1): # verify if full dtw path already exists
                    current_path = DTWSampler.saved_dtw_path[i]
                else:
                    # append path
                    current_path = dtw_path(X_resampled[i, :, self.scaling_col_idx], self.reference_series)           
                    if self.save_path: # save current path is asked
                        DTWSampler.saved_dtw_path.append(current_path)                

                for t_current, t_ref in current_path:
                    indices_xy[t_ref].append(t_current)
                for j in range(X.shape[2]):
                    if False and j == self.scaling_col_idx:
                        X_resampled[i, :, j] = xnew
                    else:
                        ynew = sp.array([sp.mean(X_resampled[i, indices, j]) for indices in indices_xy])
                        X_resampled[i, :, j] = ynew
            return X_resampled
项目:dzetsaka    作者:lennepkade    | 项目源码 | 文件源码
def __init__(self):
        self.ni = []
        self.prop = []
        self.mean = []
        self.cov =[]
        self.Q = []
        self.L = []
        self.classnum = [] # to keep right labels
        self.tau = 0.0
项目:dzetsaka    作者:lennepkade    | 项目源码 | 文件源码
def BIC(self,x,y,tau=None):
        '''
        Computes the Bayesian Information Criterion of the model
        '''
        ## Get information from the data
        C,d = self.mean.shape
        n = x.shape[0]

        ## Initialization
        if tau is None:
            TAU=self.tau
        else:
            TAU=tau

        ## Penalization
        P = C*(d*(d+3)/2) + (C-1)
        P *= sp.log(n)

        ## Compute the log-likelihood
        L = 0
        for c in range(C):
            j = sp.where(y==(c+1))[0]
            xi = x[j,:]
            invCov,logdet = self.compute_inverse_logdet(c,TAU)
            cst = logdet - 2*sp.log(self.prop[c]) # Pre compute the constant
            xi -= self.mean[c,:]
            temp = sp.dot(invCov,xi.T).T
            K = sp.sum(xi*temp,axis=1)+cst
            L +=sp.sum(K)
            del K,xi

        return L + P
项目:Python-for-Finance-Second-Edition    作者:PacktPublishing    | 项目源码 | 文件源码
def myUtilityFunction(ret,A=1):
    meanDaily=sp.mean(ret)
    varDaily=sp.var(ret)
    meanAnnual=(1+meanDaily)**252
    varAnnual=varDaily*252
    return meanAnnual- 0.5*A*varAnnual
项目:Python-for-Finance-Second-Edition    作者:PacktPublishing    | 项目源码 | 文件源码
def treynor(R,w):
    betaP=portfolioBeta(betaGiven,w)
    mean_return=sp.mean(R,axis=0)
    ret = sp.array(mean_return)
    return (sp.dot(w,ret) - rf)/betaP
# function 4: for given n-1 weights, return a negative sharpe ratio
项目:Python-for-Finance-Second-Edition    作者:PacktPublishing    | 项目源码 | 文件源码
def treynor(R,w):
    betaP=portfolioBeta(betaGiven,w)
    mean_return=sp.mean(R,axis=0)
    ret = sp.array(mean_return)
    return (sp.dot(w,ret) - rf)/betaP
#
# function 4: for given n-1 weights, return a negative sharpe ratio
项目:Python-for-Finance-Second-Edition    作者:PacktPublishing    | 项目源码 | 文件源码
def portfolioRet(R,w):
    mean_return=sp.mean(R,axis=0)
    ret = sp.array(mean_return)
    return sp.dot(w,ret)
项目:Python-for-Finance-Second-Edition    作者:PacktPublishing    | 项目源码 | 文件源码
def sharpe(R,w):
    var = portfolio_var(R,w)
    mean_return=sp.mean(R,axis=0)
    ret = sp.array(mean_return)
    return (sp.dot(w,ret) - rf)/sp.sqrt(var)
# function 4: for given n-1 weights, return a negative sharpe ratio
项目:Python-for-Finance-Second-Edition    作者:PacktPublishing    | 项目源码 | 文件源码
def meanVarAnnual(ret):
    meanDaily=sp.mean(ret)
    varDaily=sp.var(ret)
    meanAnnual=(1+meanDaily)**252
    varAnnual=varDaily*252
    return meanAnnual, varAnnual
项目:RFCN    作者:zengxianyu    | 项目源码 | 文件源码
def __MR_superpixel_mean_vector(self,img,labels):
        s = sp.amax(labels)+1
        vec = sp.zeros((s,3)).astype(float)
        for i in range(s):
            mask = labels == i
            super_v = img[mask].astype(float)
            mean = sp.mean(super_v,0)
            vec[i] = mean
        return vec
项目:astroEMPEROR    作者:ReddTea    | 项目源码 | 文件源码
def read_data(instruments):
    '''
    Data pre-processing
    '''
    nins = len(instruments)
    instruments = sp.array([sp.loadtxt('datafiles/'+x) for x in instruments])
    def data(data, ins_no):
        Time, Radial_Velocity, Err = data.T[:3]  # el error de la rv
        Radial_Velocity -= sp.mean(Radial_Velocity)
        Flag = sp.ones(len(Time)) * ins_no  # marca el instrumento al q pertenece
        Staract = data.T[3:]
        return sp.array([Time, Radial_Velocity, Err, Flag, Staract])

    def sortstuff(tryin):
        t, rv, er, flag = tryin
        order = sp.argsort(t)
        return sp.array([x[order] for x in [t, rv, er, flag]])

    fd = sp.array([]), sp.array([]), sp.array([]), sp.array([])

    for k in range(len(instruments)):  # appends all the data in megarg
        t, rv, er, flag, star = data(instruments[k], k)
        fd = sp.hstack((fd, [t, rv, er, flag] ))  # ojo this, list not array

    fd[0] = fd[0] - min(fd[0])
    alldat = sp.array([])
    try:
        staract = sp.array([data(instruments[i], i)[4] for i in range(nins)])
    except:
        staract = sp.array([sp.array([]) for i in range(nins)])
    starflag = sp.array([sp.array([i for k in range(len(staract[i]))]) for i in range(len(staract))])
    tryin = sortstuff(fd)
    for i in range(len(starflag)):
        for j in range(len(starflag[i])):
            staract[i][j] -= sp.mean(staract[i][j])
    totcornum = 0
    for correlations in starflag:
        if len(correlations) > 0:
            totcornum += len(correlations)

    return fd, staract, starflag, totcornum
项目:astroEMPEROR    作者:ReddTea    | 项目源码 | 文件源码
def normal_pdf(x, mean, variance):
    var = 2 * variance
    return ( - (x - mean) ** 2 / var)
项目:SLIC_cityscapes    作者:wpqmanu    | 项目源码 | 文件源码
def update(self, centers):
        # sums = [scipy.zeros(5) for i in range(len(centers))]
        # nums = [0 for i in range(len(centers))]
        # width, height = self.img.shape[:2]
        print "E step"
        new_centers=[]
        nan_record=[]

        for i in xrange(len(centers)):
            current_region=self.xylab[self.assignedindex == i]
            if current_region.size>0: #non-empty region
                new_centers.append(scipy.mean(current_region, 0))
            else: # empty region
                nan_record.append(i)

        # after we get full nan_record list, update assignment index (elimnate those indexes in reverse order)
        for nan_value in nan_record[::-1]:
            self.assignedindex[self.assignedindex>nan_value]=self.assignedindex[self.assignedindex>nan_value]-1


        for new_center_index in range(len(new_centers)):
            # print new_center_index
            new_centers[new_center_index][0] = math.floor(new_centers[new_center_index][0])
            new_centers[new_center_index][1] = math.floor(new_centers[new_center_index][1])
            new_centers[new_center_index][2:]=self.labimg[math.floor(new_centers[new_center_index][0])][math.floor(new_centers[new_center_index][1])]

        return new_centers,nan_record
项目:SLIC_cityscapes    作者:wpqmanu    | 项目源码 | 文件源码
def update(self, centers):
        sums = [scipy.zeros(5) for i in range(len(centers))]
        nums = [0 for i in range(len(centers))]
        width, height = self.img.shape[:2]
        print "E step"
        return [scipy.mean(self.xylab[self.assignedindex == i], 0) for i in xrange(len(centers))]
项目:SLIC_cityscapes    作者:wpqmanu    | 项目源码 | 文件源码
def update(self, centers):
        # sums = [scipy.zeros(5) for i in range(len(centers))]
        # nums = [0 for i in range(len(centers))]
        # width, height = self.img.shape[:2]
        print "E step"
        new_centers=[]
        nan_record=[]

        for i in xrange(len(centers)):
            current_region=self.xylab[self.assignedindex == i]
            if current_region.size>0: #non-empty region
                new_centers.append(scipy.mean(current_region, 0))
            else: # empty region
                nan_record.append(i)

        # after we get full nan_record list, update assignment index (elimnate those indexes in reverse order)
        for nan_value in nan_record[::-1]:
            self.assignedindex[self.assignedindex>nan_value]=self.assignedindex[self.assignedindex>nan_value]-1


        for new_center_index in range(len(new_centers)):
            # print new_center_index
            new_centers[new_center_index][0] = math.floor(new_centers[new_center_index][0])
            new_centers[new_center_index][1] = math.floor(new_centers[new_center_index][1])
            new_centers[new_center_index][2:]=self.labimg[math.floor(new_centers[new_center_index][0])][math.floor(new_centers[new_center_index][1])]

        return new_centers,nan_record
项目:SLIC_cityscapes    作者:wpqmanu    | 项目源码 | 文件源码
def update(self, centers):
        # sums = [scipy.zeros(5) for i in range(len(centers))]
        # nums = [0 for i in range(len(centers))]
        # width, height = self.img.shape[:2]
        print "E step"
        new_centers=[]
        nan_record=[]

        for i in xrange(len(centers)):
            current_region=self.xylab[self.assignedindex == i]
            if current_region.size>0: #non-empty region
                new_centers.append(scipy.mean(current_region, 0))
            else: # empty region
                nan_record.append(i)

        # after we get full nan_record list, update assignment index (elimnate those indexes in reverse order)
        for nan_value in nan_record[::-1]:
            self.assignedindex[self.assignedindex>nan_value]=self.assignedindex[self.assignedindex>nan_value]-1


        for new_center_index in range(len(new_centers)):
            # print new_center_index
            new_centers[new_center_index][0] = math.floor(new_centers[new_center_index][0])
            new_centers[new_center_index][1] = math.floor(new_centers[new_center_index][1])
            new_centers[new_center_index][2:]=self.labimg[math.floor(new_centers[new_center_index][0])][math.floor(new_centers[new_center_index][1])]

        return new_centers,nan_record