Python numpy.var() Examples

The following are 30 code examples for showing how to use numpy.var(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: python--   Author: Leezhen2014   File: BlurDetection.py    License: GNU General Public License v3.0 10 votes vote down vote up
def _lapulaseDetection(self, imgName):
        """
        :param strdir: 文件所在的目录
        :param name: 文件名称
        :return: 检测模糊后的分数
        """
        # step1: 预处理
        img2gray, reImg = self.preImgOps(imgName)
        # step2: laplacian算子 获取评分
        resLap = cv2.Laplacian(img2gray, cv2.CV_64F)
        score = resLap.var()
        print("Laplacian %s score of given image is %s", str(score))
        # strp3: 绘制图片并保存  不应该写在这里  抽象出来   这是共有的部分
        newImg = self._drawImgFonts(reImg, str(score))
        newDir = self.strDir + "/_lapulaseDetection_/"
        if not os.path.exists(newDir):
            os.makedirs(newDir)
        newPath = newDir + imgName
        # 显示
        cv2.imwrite(newPath, newImg)  # 保存图片
        cv2.imshow(imgName, newImg)
        cv2.waitKey(0)

        # step3: 返回分数
        return score 
Example 2
Project: TradzQAI   Author: kkuette   File: standard_variance.py    License: Apache License 2.0 6 votes vote down vote up
def standard_variance(data, period):
    """
    Standard Variance.

    Formula:
    (Ct - AVGt)^2 / N
    """
    check_for_period_error(data, period)
    sv = list(map(
        lambda idx:
        np.var(data[idx+1-period:idx+1], ddof=1),
        range(period-1, len(data))
        ))
    sv = fill_for_noncomputable_vals(data, sv)

    return sv 
Example 3
Project: lirpg   Author: Hwhitetooth   File: running_mean_std.py    License: MIT License 6 votes vote down vote up
def test_runningmeanstd():
    for (x1, x2, x3) in [
        (np.random.randn(3), np.random.randn(4), np.random.randn(5)),
        (np.random.randn(3,2), np.random.randn(4,2), np.random.randn(5,2)),
        ]:

        rms = RunningMeanStd(epsilon=0.0, shape=x1.shape[1:])

        x = np.concatenate([x1, x2, x3], axis=0)
        ms1 = [x.mean(axis=0), x.var(axis=0)]
        rms.update(x1)
        rms.update(x2)
        rms.update(x3)
        ms2 = [rms.mean, rms.var]

        assert np.allclose(ms1, ms2) 
Example 4
Project: HardRLWithYoutube   Author: MaxSobolMark   File: running_mean_std.py    License: MIT License 6 votes vote down vote up
def test_tf_runningmeanstd():
    for (x1, x2, x3) in [
        (np.random.randn(3), np.random.randn(4), np.random.randn(5)),
        (np.random.randn(3,2), np.random.randn(4,2), np.random.randn(5,2)),
        ]:

        rms = TfRunningMeanStd(epsilon=0.0, shape=x1.shape[1:], scope='running_mean_std' + str(np.random.randint(0, 128)))

        x = np.concatenate([x1, x2, x3], axis=0)
        ms1 = [x.mean(axis=0), x.var(axis=0)]
        rms.update(x1)
        rms.update(x2)
        rms.update(x3)
        ms2 = [rms.mean, rms.var]

        np.testing.assert_allclose(ms1, ms2) 
Example 5
Project: python--   Author: Leezhen2014   File: BlurDetection.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _Variance(self, imgName):
        """
               灰度方差乘积
               :param imgName:
               :return:
               """
        # step 1 图像的预处理
        img2gray, reImg = self.preImgOps(imgName)
        f = self._imageToMatrix(img2gray)

        # strp3: 绘制图片并保存  不应该写在这里  抽象出来   这是共有的部分
        score = np.var(f)
        newImg = self._drawImgFonts(reImg, str(score))
        newDir = self.strDir + "/_Variance_/"
        if not os.path.exists(newDir):
            os.makedirs(newDir)
        newPath = newDir + imgName
        cv2.imwrite(newPath, newImg)  # 保存图片
        cv2.imshow(imgName, newImg)
        cv2.waitKey(0)
        return score 
Example 6
Project: NeuroKit   Author: neuropsychology   File: signal_changepoints.py    License: MIT License 6 votes vote down vote up
def _signal_changepoints_cost_mean(signal):
    """Cost function for a normally distributed signal with a changing mean."""
    i_variance_2 = 1 / (np.var(signal) ** 2)
    cmm = [0.0]
    cmm.extend(np.cumsum(signal))

    cmm2 = [0.0]
    cmm2.extend(np.cumsum(np.abs(signal)))

    def cost(start, end):
        cmm2_diff = cmm2[end] - cmm2[start]
        cmm_diff = pow(cmm[end] - cmm[start], 2)
        i_diff = end - start
        diff = cmm2_diff - cmm_diff
        return (diff / i_diff) * i_variance_2

    return cost 
Example 7
Project: discomll   Author: romanorac   File: naivebayes.py    License: Apache License 2.0 5 votes vote down vote up
def map_fit(interface, state, label, inp):
    """
    Function counts occurrences of feature values for every row in given data chunk. For continuous features it returns
    number of values and it calculates mean and variance for every feature.
    For discrete features it counts occurrences of labels and values for every feature. It returns occurrences of pairs:
    label, feature index, feature values.
    """
    import numpy as np
    combiner = {}  # combiner used for joining of intermediate pairs
    out = interface.output(0)  # all outputted pairs have the same output label

    for row in inp:  # for every row in data chunk
        row = row.strip().split(state["delimiter"])  # split row
        if len(row) > 1:  # check if row is empty
            for i, j in enumerate(state["X_indices"]):  # for defined features
                if row[j] not in state["missing_vals"]:  # check missing values
                    # creates a pair - label, feature index
                    pair = row[state["y_index"]] + state["delimiter"] + str(j)

                    if state["X_meta"][i] == "c":  # continuous features
                        if pair in combiner:
                            # convert to float and store value
                            combiner[pair].append(np.float32(row[j]))
                        else:
                            combiner[pair] = [np.float32(row[j])]

                    else:  # discrete features
                        # add feature value to pair
                        pair += state["delimiter"] + row[j]
                        # increase counts of current pair
                        combiner[pair] = combiner.get(pair, 0) + 1

                    # increase label counts
                    combiner[row[state["y_index"]]] = combiner.get(row[state["y_index"]], 0) + 1

    for k, v in combiner.iteritems():  # all pairs in combiner are output
        if len(k.split(state["delimiter"])) == 2:  # continous features
            # number of elements, partial mean and variance
            out.add(k, (np.size(v), np.mean(v, dtype=np.float32), np.var(v, dtype=np.float32)))
        else:  # discrete features and labels
            out.add(k, v) 
Example 8
Project: DOTA_models   Author: ringringyi   File: ops_test.py    License: Apache License 2.0 5 votes vote down vote up
def testComputeMovingVars(self):
    height, width = 3, 3
    with self.test_session() as sess:
      image_shape = (10, height, width, 3)
      image_values = np.random.rand(*image_shape)
      expected_mean = np.mean(image_values, axis=(0, 1, 2))
      expected_var = np.var(image_values, axis=(0, 1, 2))
      images = tf.constant(image_values, shape=image_shape, dtype=tf.float32)
      output = ops.batch_norm(images, decay=0.1)
      update_ops = tf.get_collection(ops.UPDATE_OPS_COLLECTION)
      with tf.control_dependencies(update_ops):
        output = tf.identity(output)
      # Initialize all variables
      sess.run(tf.global_variables_initializer())
      moving_mean = variables.get_variables('BatchNorm/moving_mean')[0]
      moving_variance = variables.get_variables('BatchNorm/moving_variance')[0]
      mean, variance = sess.run([moving_mean, moving_variance])
      # After initialization moving_mean == 0 and moving_variance == 1.
      self.assertAllClose(mean, [0] * 3)
      self.assertAllClose(variance, [1] * 3)
      for _ in range(10):
        sess.run([output])
      mean = moving_mean.eval()
      variance = moving_variance.eval()
      # After 10 updates with decay 0.1 moving_mean == expected_mean and
      # moving_variance == expected_var.
      self.assertAllClose(mean, expected_mean)
      self.assertAllClose(variance, expected_var) 
Example 9
Project: DOTA_models   Author: ringringyi   File: ops_test.py    License: Apache License 2.0 5 votes vote down vote up
def testEvalMovingVars(self):
    height, width = 3, 3
    with self.test_session() as sess:
      image_shape = (10, height, width, 3)
      image_values = np.random.rand(*image_shape)
      expected_mean = np.mean(image_values, axis=(0, 1, 2))
      expected_var = np.var(image_values, axis=(0, 1, 2))
      images = tf.constant(image_values, shape=image_shape, dtype=tf.float32)
      output = ops.batch_norm(images, decay=0.1, is_training=False)
      update_ops = tf.get_collection(ops.UPDATE_OPS_COLLECTION)
      with tf.control_dependencies(update_ops):
        output = tf.identity(output)
      # Initialize all variables
      sess.run(tf.global_variables_initializer())
      moving_mean = variables.get_variables('BatchNorm/moving_mean')[0]
      moving_variance = variables.get_variables('BatchNorm/moving_variance')[0]
      mean, variance = sess.run([moving_mean, moving_variance])
      # After initialization moving_mean == 0 and moving_variance == 1.
      self.assertAllClose(mean, [0] * 3)
      self.assertAllClose(variance, [1] * 3)
      # Simulate assigment from saver restore.
      init_assigns = [tf.assign(moving_mean, expected_mean),
                      tf.assign(moving_variance, expected_var)]
      sess.run(init_assigns)
      for _ in range(10):
        sess.run([output], {images: np.random.rand(*image_shape)})
      mean = moving_mean.eval()
      variance = moving_variance.eval()
      # Although we feed different images, the moving_mean and moving_variance
      # shouldn't change.
      self.assertAllClose(mean, expected_mean)
      self.assertAllClose(variance, expected_var) 
Example 10
Project: DOTA_models   Author: ringringyi   File: ops_test.py    License: Apache License 2.0 5 votes vote down vote up
def testReuseVars(self):
    height, width = 3, 3
    with self.test_session() as sess:
      image_shape = (10, height, width, 3)
      image_values = np.random.rand(*image_shape)
      expected_mean = np.mean(image_values, axis=(0, 1, 2))
      expected_var = np.var(image_values, axis=(0, 1, 2))
      images = tf.constant(image_values, shape=image_shape, dtype=tf.float32)
      output = ops.batch_norm(images, decay=0.1, is_training=False)
      update_ops = tf.get_collection(ops.UPDATE_OPS_COLLECTION)
      with tf.control_dependencies(update_ops):
        output = tf.identity(output)
      # Initialize all variables
      sess.run(tf.global_variables_initializer())
      moving_mean = variables.get_variables('BatchNorm/moving_mean')[0]
      moving_variance = variables.get_variables('BatchNorm/moving_variance')[0]
      mean, variance = sess.run([moving_mean, moving_variance])
      # After initialization moving_mean == 0 and moving_variance == 1.
      self.assertAllClose(mean, [0] * 3)
      self.assertAllClose(variance, [1] * 3)
      # Simulate assigment from saver restore.
      init_assigns = [tf.assign(moving_mean, expected_mean),
                      tf.assign(moving_variance, expected_var)]
      sess.run(init_assigns)
      for _ in range(10):
        sess.run([output], {images: np.random.rand(*image_shape)})
      mean = moving_mean.eval()
      variance = moving_variance.eval()
      # Although we feed different images, the moving_mean and moving_variance
      # shouldn't change.
      self.assertAllClose(mean, expected_mean)
      self.assertAllClose(variance, expected_var) 
Example 11
Project: DOTA_models   Author: ringringyi   File: hyperparams_builder_test.py    License: Apache License 2.0 5 votes vote down vote up
def _assert_variance_in_range(self, initializer, shape, variance,
                                tol=1e-2):
    with tf.Graph().as_default() as g:
      with self.test_session(graph=g) as sess:
        var = tf.get_variable(
            name='test',
            shape=shape,
            dtype=tf.float32,
            initializer=initializer)
        sess.run(tf.global_variables_initializer())
        values = sess.run(var)
        self.assertAllClose(np.var(values), variance, tol, tol) 
Example 12
Project: soccer-matlab   Author: utra-robosoccer   File: filter.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def var(self):
        return 1 
Example 13
Project: soccer-matlab   Author: utra-robosoccer   File: filter.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def var(self):
        return self._S / (self._n - 1) if self._n > 1 else np.square(self._M) 
Example 14
Project: soccer-matlab   Author: utra-robosoccer   File: filter.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def std(self):
        return np.sqrt(self.var) 
Example 15
Project: soccer-matlab   Author: utra-robosoccer   File: filter.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_running_stat():
    for shp in ((), (3,), (3, 4)):
        li = []
        rs = RunningStat(shp)
        for _ in range(5):
            val = np.random.randn(*shp)
            rs.push(val)
            li.append(val)
            m = np.mean(li, axis=0)
            assert np.allclose(rs.mean, m)
            v = np.square(m) if (len(li) == 1) else np.var(li, ddof=1, axis=0)
            assert np.allclose(rs.var, v) 
Example 16
Project: lirpg   Author: Hwhitetooth   File: running_stat.py    License: MIT License 5 votes vote down vote up
def var(self):
        return self._S/(self._n - 1) if self._n > 1 else np.square(self._M) 
Example 17
Project: lirpg   Author: Hwhitetooth   File: running_stat.py    License: MIT License 5 votes vote down vote up
def std(self):
        return np.sqrt(self.var) 
Example 18
Project: lirpg   Author: Hwhitetooth   File: running_stat.py    License: MIT License 5 votes vote down vote up
def test_running_stat():
    for shp in ((), (3,), (3,4)):
        li = []
        rs = RunningStat(shp)
        for _ in range(5):
            val = np.random.randn(*shp)
            rs.push(val)
            li.append(val)
            m = np.mean(li, axis=0)
            assert np.allclose(rs.mean, m)
            v = np.square(m) if (len(li) == 1) else np.var(li, ddof=1, axis=0)
            assert np.allclose(rs.var, v) 
Example 19
Project: lirpg   Author: Hwhitetooth   File: math_util.py    License: MIT License 5 votes vote down vote up
def explained_variance(ypred,y):
    """
    Computes fraction of variance that ypred explains about y.
    Returns 1 - Var[y-ypred] / Var[y]

    interpretation:
        ev=0  =>  might as well have predicted zero
        ev=1  =>  perfect prediction
        ev<0  =>  worse than just predicting zero

    """
    assert y.ndim == 1 and ypred.ndim == 1
    vary = np.var(y)
    return np.nan if vary==0 else 1 - np.var(y-ypred)/vary 
Example 20
Project: lirpg   Author: Hwhitetooth   File: math_util.py    License: MIT License 5 votes vote down vote up
def explained_variance_2d(ypred, y):
    assert y.ndim == 2 and ypred.ndim == 2
    vary = np.var(y, axis=0)
    out = 1 - np.var(y-ypred)/vary
    out[vary < 1e-10] = 0
    return out 
Example 21
Project: lirpg   Author: Hwhitetooth   File: running_mean_std.py    License: MIT License 5 votes vote down vote up
def update(self, x):
        batch_mean = np.mean(x, axis=0)
        batch_var = np.var(x, axis=0)
        batch_count = x.shape[0]
        self.update_from_moments(batch_mean, batch_var, batch_count) 
Example 22
Project: lirpg   Author: Hwhitetooth   File: running_mean_std.py    License: MIT License 5 votes vote down vote up
def update_from_moments(self, batch_mean, batch_var, batch_count):
        delta = batch_mean - self.mean
        tot_count = self.count + batch_count

        new_mean = self.mean + delta * batch_count / tot_count        
        m_a = self.var * (self.count)
        m_b = batch_var * (batch_count)
        M2 = m_a + m_b + np.square(delta) * self.count * batch_count / (self.count + batch_count)
        new_var = M2 / (self.count + batch_count)

        new_count = batch_count + self.count

        self.mean = new_mean
        self.var = new_var
        self.count = new_count 
Example 23
Project: trees   Author: gdanezis   File: malware.py    License: Apache License 2.0 5 votes vote down vote up
def graph_ROC(max_ACC, TP, FP, name="STD"):
    aTP = np.vstack(TP)
    n = len(TP)
    mean_TP = np.mean(aTP, axis=0)
    stderr_TP = np.std(aTP, axis=0) / (n ** 0.5)
    var_TP = np.var(aTP, axis=0)
    max_TP = mean_TP + 3 * stderr_TP
    min_TP = mean_TP - 3 * stderr_TP

    # sTP = sum(TP) / len(TP)
    sFP = FP[0]
    print len(sFP), len(mean_TP), len(TP[0])
    smax_ACC = np.mean(max_ACC)

    plt.cla()
    plt.clf()
    plt.close()

    plt.plot(sFP, mean_TP)
    plt.fill_between(sFP, min_TP, max_TP, color='black', alpha=0.2)
    plt.xlim((0,0.1))
    plt.ylim((0,1))
    plt.title('ROC Curve (accuracy=%.3f)' % smax_ACC)
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.savefig(r"../scratch/"+name+"_ROC_curve.pdf", bbox_inches='tight')

    # Write the data to the file
    f = file(r"../scratch/"+name+"_ROC_curve.csv", "w")
    f.write("FalsePositive,TruePositive,std_err, var, n\n")
    for fp, tp, err, var in zip(sFP, mean_TP, stderr_TP, var_TP):
        f.write("%s, %s, %s, %s, %s\n" % (fp, tp, err, var, n))
    f.close() 
Example 24
Project: HardRLWithYoutube   Author: MaxSobolMark   File: math_util.py    License: MIT License 5 votes vote down vote up
def explained_variance(ypred,y):
    """
    Computes fraction of variance that ypred explains about y.
    Returns 1 - Var[y-ypred] / Var[y]

    interpretation:
        ev=0  =>  might as well have predicted zero
        ev=1  =>  perfect prediction
        ev<0  =>  worse than just predicting zero

    """
    assert y.ndim == 1 and ypred.ndim == 1
    vary = np.var(y)
    return np.nan if vary==0 else 1 - np.var(y-ypred)/vary 
Example 25
Project: HardRLWithYoutube   Author: MaxSobolMark   File: math_util.py    License: MIT License 5 votes vote down vote up
def explained_variance_2d(ypred, y):
    assert y.ndim == 2 and ypred.ndim == 2
    vary = np.var(y, axis=0)
    out = 1 - np.var(y-ypred)/vary
    out[vary < 1e-10] = 0
    return out 
Example 26
Project: HardRLWithYoutube   Author: MaxSobolMark   File: running_stat.py    License: MIT License 5 votes vote down vote up
def var(self):
        return self._S/(self._n - 1) if self._n > 1 else np.square(self._M) 
Example 27
Project: HardRLWithYoutube   Author: MaxSobolMark   File: running_stat.py    License: MIT License 5 votes vote down vote up
def std(self):
        return np.sqrt(self.var) 
Example 28
Project: HardRLWithYoutube   Author: MaxSobolMark   File: running_stat.py    License: MIT License 5 votes vote down vote up
def test_running_stat():
    for shp in ((), (3,), (3,4)):
        li = []
        rs = RunningStat(shp)
        for _ in range(5):
            val = np.random.randn(*shp)
            rs.push(val)
            li.append(val)
            m = np.mean(li, axis=0)
            assert np.allclose(rs.mean, m)
            v = np.square(m) if (len(li) == 1) else np.var(li, ddof=1, axis=0)
            assert np.allclose(rs.var, v) 
Example 29
Project: HardRLWithYoutube   Author: MaxSobolMark   File: running_mean_std.py    License: MIT License 5 votes vote down vote up
def update(self, x):
        batch_mean = np.mean(x, axis=0)
        batch_var = np.var(x, axis=0)
        batch_count = x.shape[0]
        self.update_from_moments(batch_mean, batch_var, batch_count) 
Example 30
Project: HardRLWithYoutube   Author: MaxSobolMark   File: running_mean_std.py    License: MIT License 5 votes vote down vote up
def update_from_moments(self, batch_mean, batch_var, batch_count):
        self.mean, self.var, self.count = update_mean_var_count_from_moments(
            self.mean, self.var, self.count, batch_mean, batch_var, batch_count)