Here are the examples of the python api numpy.std taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
161 Examples
3
Example 1
def _new_update_deltas(self, network, parameter_vws, grads):
learning_rate = network.find_hyperparameter(["sgd_learning_rate",
"learning_rate"],
0.1)
# HACK changes the rest of this node... mostly restructuring
deltas = {}
for vw, grad in zip(parameter_vws, grads):
initial_std = np.std(vw.value)
# prevent multiplying by 0 std
if initial_std == 0:
initial_std = 1.0
factor = treeano.utils.as_fX(-learning_rate * initial_std ** 2)
deltas[vw.variable] = factor * grad
return treeano.UpdateDeltas(deltas)
3
Example 2
Project: intuition Source File: finance.py
def qstk_get_sharpe_ratio(rets, risk_free=0.00):
"""
@summary Returns the daily Sharpe ratio of the returns.
@param rets: 1d numpy array or fund list of daily returns (centered on 0)
@param risk_free: risk free returns, default is 0%
@return Annualized rate of return, not converted to percent
"""
f_dev = np.std(rets, axis=0)
f_mean = np.mean(rets, axis=0)
f_sharpe = (f_mean * 252 - risk_free) / (f_dev * np.sqrt(252))
return f_sharpe
3
Example 3
def zscore(self, axis=1):
"""
Subtract the mean and divide by standard deviation within or across records.
Parameters
----------
axis : int, optional, default = 0
Which axis to zscore along, within (1) or across (0) records
"""
if axis == 1:
return self.map(lambda x: (x - mean(x)) / std(x))
elif axis == 0:
meanval = self.mean().toarray()
stdval = self.std().toarray()
return self.map(lambda x: (x - meanval) / stdval)
else:
raise Exception('Axis must be 0 or 1')
3
Example 4
Project: spc Source File: spc.py
def get_stats_x_bar_s_s(data, size):
n = size
assert n >= 2
assert n <= 10
Sbar = numpy.mean(numpy.std(data, 1, ddof=1))
center = Sbar
lcl = B3[n]*Sbar
ucl = B4[n]*Sbar
return center, lcl, ucl
3
Example 5
Project: neleval Source File: significance.py
def calibrate_trials(self, trials=[100, 250, 500, 1000, 2500, 5000, 10000],
max_trials=20000):
import numpy as np
tmp_trials, self.trials = self.trials, max_trials
matrices = self._read_to_matrices()
print('measure', 'metric', 'pct', 'trials', 'stdev', sep='\t')
for measure in self.measures:
history = self.run_trials(matrices[measure][0])
for metric in self.metrics:
X = history[metric]
for p in self.percentiles:
v = (100 - p) / 2
for n in trials:
stats = [_percentile(sorted(random.sample(X, n)), v)
for i in range(100)]
print(measure, metric, p, n, np.std(stats), sep='\t')
self.trials = tmp_trials
3
Example 6
Project: stingray Source File: test_powerspectrum.py
def test_fractional_rms_in_frac_norm(self):
ps = Powerspectrum(lc=self.lc, norm="frac")
rms_ps, rms_err = ps.compute_rms(min_freq=ps.freq[1],
max_freq=ps.freq[-1])
rms_lc = np.std(self.lc.counts) / np.mean(self.lc.counts)
assert np.isclose(rms_ps, rms_lc, atol=0.01)
3
Example 7
def _standardize_col(col):
""" Standardizes a single column (subtracts mean and divides by std
dev).
"""
std = np.std(col)
mean = np.mean(col)
if abs(std) > 0.001:
return col.apply(lambda val: (val - mean)/std)
else:
return col
3
Example 8
def test_random_starts():
# not as strong a test as the direct case!
# using training error here, and a higher threshold.
# We observe the lifted solver reaches rather diff. solutions.
degree = 3
noisy_y = _lifted_predict(U[:degree], X)
noisy_y += 5. * rng.randn(noisy_y.shape[0])
common_settings = dict(degree=degree, n_components=n_components,
beta=0.01, tol=0.01)
scores = []
for k in range(5):
est = PolynomialNetworkRegressor(random_state=k, **common_settings)
y_pred = est.fit(X, noisy_y).predict(X)
scores.append(mean_squared_error(noisy_y, y_pred))
assert_less_equal(np.std(scores), 1e-4)
3
Example 9
Project: luminol Source File: derivative_detector.py
def _set_scores(self):
"""
Compute anomaly scores for the time series.
"""
anom_scores = {}
self._compute_derivatives()
derivatives_ema = utils.compute_ema(self.smoothing_factor, self.derivatives)
for i, (timestamp, value) in enumerate(self.time_series_items):
anom_scores[timestamp] = abs(self.derivatives[i] - derivatives_ema[i])
stdev = numpy.std(anom_scores.values())
if stdev:
for timestamp in anom_scores.keys():
anom_scores[timestamp] /= stdev
self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
3
Example 10
Project: scipy Source File: test_binned_statistic.py
def test_dd_std(self):
X = self.X
v = self.v
stat1, edges1, bc = binned_statistic_dd(X, v, 'std', bins=3)
stat2, edges2, bc = binned_statistic_dd(X, v, np.std, bins=3)
assert_allclose(stat1, stat2)
assert_allclose(edges1, edges2)
3
Example 11
def hurst(ts):
"""Returns the Hurst Exponent of the time series vector ts"""
# Create the range of lag values
lags = range(2, 100)
# Calculate the array of the variances of the lagged differences
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
# Use a linear fit to estimate the Hurst Exponent
poly = polyfit(log(lags), log(tau), 1)
# Return the Hurst exponent from the polyfit output
return poly[0]*2.0
3
Example 12
Project: qiime Source File: relatedness_library.py
def random_mntd(distmat, n, iters):
"""Calc mean,std of mntd of iters # of rand nxn mtx's drawn from distmat.
Notes:
Calculate the std of the means of minimums of the distances in the nxn mat
excluding the main diagonal since d(A,A)=0 (mtx is hollow).
The forumula from Webb 2002 seems to calculate the standard deviation of
the distances but based on tests of Phylocom and the Phylocom manual,
Phylocom computes the standard deviations of the means.
"""
means = []
indices = arange(distmat.shape[0]) # square so rows=cols
for i in range(iters):
shuffle(indices) # shuffling indices after its been shuffled is not
# mathematically different than shuffling fresh arange(n)
means.append(mntd(reduce_mtx(distmat, indices[:n])))
return mean(means), std(means)
3
Example 13
Project: statsmodels Source File: bandwidths.py
def _select_sigma(X):
"""
Returns the smaller of std(X, ddof=1) or normalized IQR(X) over axis 0.
References
----------
Silverman (1986) p.47
"""
# normalize = norm.ppf(.75) - norm.ppf(.25)
normalize = 1.349
# IQR = np.subtract.reduce(percentile(X, [75,25],
# axis=axis), axis=axis)/normalize
IQR = (sap(X, 75) - sap(X, 25))/normalize
return np.minimum(np.std(X, axis=0, ddof=1), IQR)
3
Example 14
def report(grid_scores, n_top=1) :
'''
Print out top models/parameters after a grid search for model params.
'''
top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
for i, score in enumerate(top_scores) :
if n_top>1:
print("Model with rank: {0}".format(i + 1))
print("Average (tuning) CV score : {0:.2f} (std: {1:.2f})".format(
score.mean_validation_score, np.std(score.cv_validation_scores)))
print("Optimal Model Hyperparameters: {0}".format(score.parameters))
print("")
3
Example 15
def __repr__(self):
"""Simple custom repr to summarize the main info"""
return "mean: {0:.5f}, std: {1:.5f}, params: {2}".format(
self.mean_validation_score,
np.std(self.cv_validation_scores),
self.parameters)
3
Example 16
@staticmethod
def scott(data, ddof=0):
"""
Scott's rule for the number of bins in a histogram.
"""
if np.std(data, ddof=ddof) == 0:
return sturges_rule(data)
h = 3.5*np.std(data, ddof=ddof)/cbrt(len(data))
return int((np.max(data) - np.min(data))/h)
3
Example 17
Project: kaggle-ndsb Source File: utils.py
def log_loss_std(y, t, eps=1e-15):
"""
cross entropy loss, summed over classes, mean over batches
"""
losses = log_losses(y, t, eps)
return np.std(losses)
3
Example 18
def StdDev(a):
# Not quite sure of the meaning for non-2d
if (a.ndim != 2):
print "Dimension must be 2"
exit(1)
return np.std(a, axis=0)
3
Example 19
def setUp(self):
self.time_series = test_ts_generator.generate_test_data(
8, 1000, test_sine, 100.0)
devariancing_node = \
normalization.DevariancingNode(devariance_method=numpy.std)
devariancing_node.train(self.time_series)
devariancing_node.stop_training()
self.devarianced_time_series = \
devariancing_node.execute(self.time_series)
3
Example 20
Project: pyTrade Source File: pyTrade.py
def bollingerBands(self, period, day, length):
s = self.sma(period, day, length)
std = self.forEachPeriod(lambda x: numpy.std([ii[4] for ii in x])*2, period, day, length)
stdBig = [a+d for a,d in zip(s, std)]
stdSmall = [a-d for a,d in zip(s,std)]
return (stdBig, s, stdSmall)
3
Example 21
def get_std_dev(fund_ts):
"""
@summary gets standard deviation of returns for a fund as a string
@param fund_ts: pandas fund time series
@param years: list of years to print out
@param ostream: stream to print to
"""
fund_ts=fund_ts.fillna(method='pad')
fund_ts=fund_ts.fillna(method='bfill')
ret=np.std(tsu.daily(fund_ts.values))*10000
return ("%+7.2f bps " % ret)
3
Example 22
def get_sharpe_ratio( rets, risk_free=0.00 ):
"""
@summary Returns the daily Sharpe ratio of the returns.
@param rets: 1d numpy array or fund list of daily returns (centered on 0)
@param risk_free: risk free returns, default is 0%
@return Annualized rate of return, not converted to percent
"""
f_dev = np.std( rets, axis=0 )
f_mean = np.mean( rets, axis=0 )
f_sharpe = (f_mean *252 - risk_free) / ( f_dev * np.sqrt(252) )
return f_sharpe
3
Example 23
Project: pyspace Source File: test_normalization.py
def test_devariancing(self):
# The object should be different!
self.assertNotEqual(id(self.time_series),
id(self.devarianced_time_series))
# Check that std has been set to 1
for channel_index in range(self.time_series.shape[1]):
self.assertAlmostEqual(
numpy.std(self.devarianced_time_series[:, channel_index]),
1.0)
3
Example 24
Project: qiime Source File: relatedness_library.py
def random_mpd(distmat, n, iters):
"""Calc mean,std of mean of iters # of rand nxn distmats drawn from distmat.
Notes:
Calculate the std of the means of the distances in the nxn mat excluding
the main diagonal and below since d(A,A)=0 and distmat is symmetric.
The forumula from Webb 2002 seems to calculate the standard deviation of
the distances but based on tests of Phylocom and the Phylocom manual,
Phylocom computes the standard deviations of the means."""
means = []
indices = arange(distmat.shape[0]) # square so rows=cols
for i in range(iters):
shuffle(indices) # shuffling indices after its been shuffled is not
# mathematically different than shuffling fresh arange(n)
means.append(mpd(reduce_mtx(distmat, indices[:n])))
return mean(means), std(means)
3
Example 25
Project: scipy Source File: test_binned_statistic.py
def test_dd_multi_values(self):
X = self.X
v = self.v
w = self.w
stat1v, edges1v, bc1v = binned_statistic_dd(X, v, np.std, bins=8)
stat1w, edges1w, bc1w = binned_statistic_dd(X, w, np.std, bins=8)
stat2, edges2, bc2 = binned_statistic_dd(X, [v, w], np.std, bins=8)
assert_allclose(stat2[0], stat1v)
assert_allclose(stat2[1], stat1w)
assert_allclose(edges1v, edges2)
assert_allclose(edges1w, edges2)
assert_allclose(bc1v, bc2)
3
Example 26
@set_property("fctype", "aggregate")
@not_apply_to_raw_numbers
def standard_deviation(x):
"""
Returns the standard deviation of x
:param x: the time series to calculate the feature of
:type x: pandas.Series
:return: the value of this feature
:return type: float
"""
return np.std(x)
3
Example 27
Project: orange Source File: orngPCA.py
def standardizeData(dataMatrix):
"""Performs standardization of data along rows. Throws error if constant
variable is present."""
scale = numpy.std(dataMatrix, axis=0)
if 0. in scale:
raise orange.KernelException, "Constant variable, cannot standardize!"
return scale, dataMatrix * 1. / scale
3
Example 28
def stdev(arr):
"""
Compute the standard deviation.
"""
if sys.version_info >= (3, 0):
import statistics
return statistics.pstdev(arr)
else:
# Dependency on NumPy
try:
import numpy
return numpy.std(arr, axis=0)
except ImportError:
return 0.
3
Example 29
Project: spc Source File: spc.py
def get_stats_x_bar_s_x(data, size):
n = size
assert n >= 2
assert n <= 10
Sbar = numpy.mean(numpy.std(data, 1, ddof=1))
Xbar = numpy.mean(data)
center = Xbar
lcl = center - A3[n]*Sbar
ucl = center + A3[n]*Sbar
return center, lcl, ucl
3
Example 30
@staticmethod
def scott(data):
sigma = np.std(data)
n = len(data)
h = 3.49 * sigma * n ** (-1. / 3.)
return (np.max(data) - np.min(data)) / h
3
Example 31
def report(grid_scores, n_top=3) :
top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
for i, score in enumerate(top_scores) :
print("Model with rank: {0}".format(i + 1))
print("Mean validation score: {0:.3f} (std: {1:.3f})".format(score.mean_validation_score,
np.std(score.cv_validation_scores)))
print("Parameters: {0}".format(score.parameters))
print("")
3
Example 32
Project: statsmodels Source File: _kernel_base.py
def _normal_reference(self):
"""
Returns Scott's normal reference rule of thumb bandwidth parameter.
Notes
-----
See p.13 in [2] for an example and discussion. The formula for the
bandwidth is
.. math:: h = 1.06n^{-1/(4+q)}
where ``n`` is the number of observations and ``q`` is the number of
variables.
"""
X = np.std(self.data, axis=0)
return 1.06 * X * self.nobs ** (- 1. / (4 + self.data.shape[1]))
3
Example 33
def report(grid_scores, n_top=2) :
'''
Print out top models/parameters after a grid search for model params.
'''
top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
for i, score in enumerate(top_scores) :
if n_top>1:
print("Model with rank: {0}".format(i + 1))
print("Average Cross-Validation score (while tuning): {0:.2f} (std: {1:.2f})".format(
score.mean_validation_score, np.std(score.cv_validation_scores)))
print("Model Parameters: {0}".format(score.parameters))
print("")
3
Example 34
Project: pyspace Source File: normalization.py
def _execute(self, data):
""" Perform a shift and normalization according
(whole_data - mean(specific_samples)) / std(specific_samples)
"""
if self.devariance:
# code copy from LocalStandardizationNode
std = numpy.std(data[self.subset],axis=0)
std = check_zero_division(self, std, tolerance=10**-15, data_ts=data)
return TimeSeries.replace_data(data,
(data-numpy.mean(data[self.subset], axis=0)) / std)
else:
return TimeSeries.replace_data(data, \
data-numpy.mean(data[self.subset], axis=0))
3
Example 35
def sharpe_ratio(rets, risk_free=0.00, period=TRADING_DAYS_PER_YEAR):
"""
summary Returns the daily Sharpe ratio of the returns.
param rets: 1d numpy array or fund list of daily returns (centered on 0)
param risk_free: risk free returns, default is 0%
return Sharpe Ratio, computed off daily returns
"""
dev = np.std(rets, axis=0)
mean = np.mean(rets, axis=0)
sharpe = (mean*period - risk_free) / (dev * np.sqrt(period))
return sharpe
3
Example 36
Project: stingray Source File: test_powerspectrum.py
def test_fractional_rms_in_leahy_norm(self):
"""
fractional rms should only be *approximately* equal the standard
deviation divided by the mean of the light curve. Therefore, we allow
for a larger tolerance in np.isclose()
"""
ps = Powerspectrum(lc=self.lc, norm="Leahy")
rms_ps, rms_err = ps.compute_rms(min_freq=ps.freq[0],
max_freq=ps.freq[-1])
rms_lc = np.std(self.lc.counts) / np.mean(self.lc.counts)
assert np.isclose(rms_ps, rms_lc, atol=0.01)
3
Example 37
Project: rscoin Source File: estthroughput.py
def process_recs(directory, fname):
data = sorted(get_times(file(join(directory, fname))))
recs = defaultdict(int)
for tend, td in data:
recs[int(tend)] += 1
recs = list(sorted(recs.iteritems()))
recs = [v for k, v in recs[1:-1]]
print recs
print "%10s\t% 6.4f\t% 6.4f" % (fname, mean(recs), std(recs))
return recs, mean(recs), std(recs)
3
Example 38
Project: polylearn Source File: test_factorization_machine.py
def test_random_starts():
noisy_y = _poly_predict(X, P, lams, kernel="anova", degree=2)
noisy_y += 5. * rng.randn(noisy_y.shape[0])
X_train, X_test = X[:10], X[10:]
y_train, y_test = noisy_y[:10], noisy_y[10:]
scores = []
# init_lambdas='ones' is important to reduce variance here
reg = FactorizationMachineRegressor(degree=2, n_components=n_components,
beta=5, fit_lower=None,
fit_linear=False, max_iter=2000,
init_lambdas='ones', tol=0.001)
for k in range(10):
reg.set_params(random_state=k)
y_pred = reg.fit(X_train, y_train).predict(X_test)
scores.append(mean_squared_error(y_test, y_pred))
assert_less_equal(np.std(scores), 0.001)
3
Example 39
Project: NucleoATAC Source File: test_var.py
def test_sd1(self):
"""Make sure variance calculation is close to what is obtained by simulation
"""
self.signaldist.simulateDist(5000)
sd1 = np.std(self.signaldist.scores)
sd2 = self.signaldist.analStd()
self.assertTrue(abs(sd1-sd2)<0.05*sd1)
3
Example 40
def standardize(self, axis=1):
"""
Divide by standard deviation either within or across records.
Parameters
----------
axis : int, optional, default = 0
Which axis to standardize along, within (1) or across (0) records
"""
if axis == 1:
return self.map(lambda x: x / std(x))
elif axis == 0:
stdval = self.std().toarray()
return self.map(lambda x: x / stdval)
else:
raise Exception('Axis must be 0 or 1')
3
Example 41
Project: brainiak Source File: tfa.py
def _get_max_sigma(self, R):
"""Calculate maximum sigma of scanner RAS coordinates
Parameters
----------
R : 2D array, with shape [n_voxel, n_dim]
The coordinate matrix of fMRI data from one subject
Returns
-------
max_sigma : float
The maximum sigma of scanner coordinates.
"""
max_sigma = 2.0 * math.pow(np.nanmax(np.std(R, axis=0)), 2)
return max_sigma
3
Example 42
Project: xarray Source File: test_variable.py
def test_reduce(self):
v = Variable(['x', 'y'], self.d, {'ignored': 'attributes'})
self.assertVariableIdentical(v.reduce(np.std, 'x'),
Variable(['y'], self.d.std(axis=0)))
self.assertVariableIdentical(v.reduce(np.std, axis=0),
v.reduce(np.std, dim='x'))
self.assertVariableIdentical(v.reduce(np.std, ['y', 'x']),
Variable([], self.d.std(axis=(0, 1))))
self.assertVariableIdentical(v.reduce(np.std),
Variable([], self.d.std()))
self.assertVariableIdentical(
v.reduce(np.mean, 'x').reduce(np.std, 'y'),
Variable([], self.d.mean(axis=0).std()))
self.assertVariableAllClose(v.mean('x'), v.reduce(np.mean, 'x'))
with self.assertRaisesRegexp(ValueError, 'cannot supply both'):
v.mean(dim='x', axis=0)
3
Example 43
def report(grid_scores, n_top=3):
scores = sorted(grid_scores, key=itemgetter(1), reverse=False)
for i, score in enumerate(scores[:n_top]):
print("Model with rank: {0}".format(i + 1))
print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
score.mean_validation_score,
np.std(score.cv_validation_scores)))
print("Parameters: {0}".format(score.parameters))
print("")
3
Example 44
def testStdDev(self):
for dt in tf.float16, tf.float32, tf.float64:
stddev = 3.0
sampler = self._Sampler(100000, 0.0, stddev, dt, use_gpu=True)
x = sampler()
print("std(x)", np.std(x), abs(np.std(x) / stddev - 0.85))
self.assertTrue(abs(np.std(x) / stddev - 0.85) < 0.04)
3
Example 45
Project: scipy Source File: _differentialevolution.py
@property
def convergence(self):
"""
The standard deviation of the population energies divided by their
mean.
"""
return (np.std(self.population_energies) /
np.abs(np.mean(self.population_energies) + _MACHEPS))
3
Example 46
def report(grid_scores, n_top=3):
top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
for i, score in enumerate(top_scores):
print("Model with rank: {0}".format(i + 1))
print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
score.mean_validation_score,
np.std(score.cv_validation_scores)))
print("Parameters: {0}".format(score.parameters))
print("")
3
Example 47
Project: python-oceans Source File: RPSstuff.py
def gstd(x, **kw):
"""
Just like std, except that it skips over bad points.
"""
xnew = ma.masked_invalid(x)
return np.std(xnew, **kw)
3
Example 48
Project: rqalpha Source File: risk_cal.py
def cal_volatility(self):
daily_returns = self.strategy_current_daily_returns
if len(daily_returns) <= 1:
return 0.
volatility = const.DAYS_CNT.TRADING_DAYS_A_YEAR ** 0.5 * np.std(daily_returns, ddof=1)
return volatility
3
Example 49
def _testH(self, Hlist):
Hstd = np.std(Hlist)
if Hstd<self.Hstd_th[0] or Hstd>self.Hstd_th[1]:
return False
else:
return True
3
Example 50
Project: blaze Source File: test_python_compute.py
def test_std():
amt = [row[1] for row in data]
assert np.allclose(compute(t.amount.std(), data), np.std(amt))
assert np.allclose(compute(t.amount.std(unbiased=True), data),
np.std(amt, ddof=1))
assert np.allclose(compute(t.amount.var(), data), np.var(amt))
assert np.allclose(compute(t.amount.var(unbiased=True), data),
np.var(amt, ddof=1))