python类mean()的实例源码

BackgroundSubtraction.py 文件源码 项目:nimo 作者: wolfram2012 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
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)
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
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
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
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
rvm.py 文件源码 项目:prml 作者: Yevgnen 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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
c7_16_def_sharpe_ratio.py 文件源码 项目:Python-for-Finance-Second-Edition 作者: PacktPublishing 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
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_new.py 文件源码 项目:SLIC_cityscapes 作者: wpqmanu 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
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
gmm_ridge.py 文件源码 项目:HistoricalMap 作者: lennepkade 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
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
gmm_ridge.py 文件源码 项目:HistoricalMap 作者: lennepkade 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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]
gmm_ridge.py 文件源码 项目:HistoricalMap 作者: lennepkade 项目源码 文件源码 阅读 46 收藏 0 点赞 0 评论 0
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
gmm_ridge.py 文件源码 项目:HistoricalMap 作者: lennepkade 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
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
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 43 收藏 0 点赞 0 评论 0
def amean_std(values: t.List[float]) -> float:
    """
    Calculates the arithmetic mean.
    """
    return sp.std(values)
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
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
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
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)
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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()
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
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
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
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))
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
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
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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)
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
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)
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
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
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
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))
game.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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))
rundata.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
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])
rundata.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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
rundata.py 文件源码 项目:temci 作者: parttimenerd 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
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
MAV_Compute.py 文件源码 项目:OSDN 作者: abhijitbendale 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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})
compute_openmax.py 文件源码 项目:OSDN 作者: abhijitbendale 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
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

#---------------------------------------------------------------------------------
Step_2.py 文件源码 项目:Wind-Turbine-Design 作者: doublerob7 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def mean(self):
        if self._mean is None:
            from scipy import mean
            self._mean = mean(self.speed)
        return self._mean
functions.py 文件源码 项目:Wind-Turbine-Design 作者: doublerob7 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def mean(self):
        if self._mean is None:
            from scipy import mean
            self._mean = mean(self.speed)
        return self._mean


问题


面经


文章

微信
公众号

扫码关注公众号