本文整理汇总了Python中scipy.stats.linregress方法的典型用法代码示例。如果您正苦于以下问题:Python stats.linregress方法的具体用法?Python stats.linregress怎么用?Python stats.linregress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats
的用法示例。
在下文中一共展示了stats.linregress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: correlation_plot
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def correlation_plot(x, y,
save_path,
title,
xlabel, ylabel):
plt.scatter(x, y)
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
line_x = np.arange(x.min(), x.max())
line_y = slope*line_x + intercept
plt.plot(line_x, line_y,
label='$%.2fx + %.2f$, $R^2=%.2f$' % (slope, intercept, r_value**2))
plt.legend(loc='best')
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.tight_layout()
plt.savefig(save_path)
plt.clf() # clear figure
plt.close()
示例2: fit
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def fit(self, verbose=True):
"""
fit exponential trying find annualized increase
:param verbose: if True (default), print debug info of linear regression
:return:
"""
df = self.df
slope_b, intercept_b, r_b, p_b, std_err_b = stats.linregress(
df["date_count"], df["lnb"]
)
slope_e, intercept_e, r_e, p_e, std_err_e = stats.linregress(
df["date_count"], df["lne"]
)
if verbose:
print("B fit", slope_b, intercept_b, r_b, p_b, std_err_b)
print("E fit", slope_e, intercept_e, r_e, p_e, std_err_e)
self.slope_b = slope_b
self.intercept_b = intercept_b
self.slope_e = slope_e
self.intercept_e = intercept_e
示例3: generate_ols_regression_pixel_list
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def generate_ols_regression_pixel_list(candidate_pifs, reference_pifs):
''' Performs PCA analysis on the valid pixels and filters according
to the distance from the principle eigenvector.
:param list candidate_pifs: A list of candidate PIF data
:param list reference_pifs: A list of coincident reference PIF data
:returns: A LinearTransformation object (gain and offset)
'''
logging.info('Transformation: Calculating ordinary least squares '
'regression transformations')
gain, offset, r_value, p_value, std_err = linregress(
candidate_pifs, reference_pifs)
logging.debug(
'Fit statistics: r_value = {}, p_value = {}, std_err = {}'.format(
r_value, p_value, std_err))
logging.info("Transformation: gain {}, offset {}".format(gain, offset))
return LinearTransformation(gain, offset)
示例4: produce_the_kde_plot
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def produce_the_kde_plot(cycles, color, save_name):
ground_truth_and_suggested = [(eval_code.get_best_qed_from_smiles_bag(elem['ground_truth_product']),
eval_code.get_best_qed_from_smiles_bag(elem['suggested_product']))
for elem in cycles]
len_out = len(ground_truth_and_suggested)
ground_truth_and_suggested = [elem for elem in ground_truth_and_suggested if elem[1] != -np.inf]
len_filter = len(ground_truth_and_suggested)
num_discarding = len_out - len_filter
if num_discarding:
warnings.warn(f"Discarding {num_discarding} our of {len_out} as no successful reconstruction")
ground_truth_and_suggested = np.array(ground_truth_and_suggested)
ground_truth_product_qed = ground_truth_and_suggested[:, 0]
suggested_product_qed = ground_truth_and_suggested[:, 1]
g = sns.jointplot(x=ground_truth_product_qed, y=suggested_product_qed, kind="kde", color=color,
)
g.set_axis_labels("product's QED", "reconstructed product's QED", fontsize=16)
rsquare = lambda a, b: stats.pearsonr(ground_truth_product_qed, suggested_product_qed)[0] ** 2
g = g.annotate(rsquare, template="{stat}: {val:.2f}",
stat="$R^2$", loc="upper left", fontsize=12)
print(f"Rsquare: {stats.pearsonr(ground_truth_product_qed, suggested_product_qed)[0] ** 2}")
print(f"scipystats: {stats.linregress(ground_truth_product_qed, suggested_product_qed)}")
plt.tight_layout()
plt.savefig(f"{save_name}.pdf")
示例5: test_nist_norris
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def test_nist_norris(self):
x = [0.2, 337.4, 118.2, 884.6, 10.1, 226.5, 666.3, 996.3, 448.6, 777.0,
558.2, 0.4, 0.6, 775.5, 666.9, 338.0, 447.5, 11.6, 556.0, 228.1,
995.8, 887.6, 120.2, 0.3, 0.3, 556.8, 339.1, 887.2, 999.0, 779.0,
11.1, 118.3, 229.2, 669.1, 448.9, 0.5]
y = [0.1, 338.8, 118.1, 888.0, 9.2, 228.1, 668.5, 998.5, 449.1, 778.9,
559.2, 0.3, 0.1, 778.1, 668.8, 339.3, 448.9, 10.8, 557.7, 228.3,
998.0, 888.8, 119.6, 0.3, 0.6, 557.6, 339.3, 888.0, 998.5, 778.9,
10.2, 117.6, 228.9, 668.4, 449.2, 0.2]
# Expected values
exp_slope = 1.00211681802045
exp_intercept = -0.262323073774029
exp_rvalue = 0.999993745883712
actual = stats.linregress(x, y)
assert_almost_equal(actual.slope, exp_slope)
assert_almost_equal(actual.intercept, exp_intercept)
assert_almost_equal(actual.rvalue, exp_rvalue, decimal=5)
示例6: calc_beta
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def calc_beta(stock, benchmark, price_history):
"""
Calculate beta amounts for each security
"""
stock_prices = price_history[stock].pct_change().dropna()
bench_prices = price_history[benchmark].pct_change().dropna()
aligned_prices = bench_prices.align(stock_prices, join='inner')
bench_prices = aligned_prices[0]
stock_prices = aligned_prices[1]
bench_prices = np.array(bench_prices.values)
stock_prices = np.array(stock_prices.values)
bench_prices = np.reshape(bench_prices, len(bench_prices))
stock_prices = np.reshape(stock_prices, len(stock_prices))
if len(stock_prices) == 0:
return None
# market_beta, benchmark_beta = np.polyfit(bench_prices, stock_prices, 1)
slope, intercept, r_value, p_value, stderr = stats.linregress(bench_prices, stock_prices)
return slope
示例7: calc_beta_testing
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def calc_beta_testing(stock, benchmark, price_history):
"""
Calculate beta and alpha amounts for each security
"""
# stock_prices = price_history[stock].pct_change().dropna()
stock_prices = np.log(1+price_history[stock].pct_change().dropna())
# bench_prices = price_history[benchmark].pct_change().dropna()
bench_prices = np.log(1+price_history[benchmark].pct_change().dropna())
aligned_prices = bench_prices.align(stock_prices, join='inner')
bench_prices = aligned_prices[0]
stock_prices = aligned_prices[1]
bench_prices = np.array(bench_prices.values)
stock_prices = np.array(stock_prices.values)
bench_prices = np.reshape(bench_prices, len(bench_prices))
stock_prices = np.reshape(stock_prices, len(stock_prices))
if len(stock_prices) == 0:
return None
# market_beta, benchmark_beta = np.polyfit(bench_prices, stock_prices, 1)
slope, intercept, r_value, p_value, stderr = stats.linregress(bench_prices, stock_prices)
return slope, intercept
示例8: fit
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def fit(self, magnitude, time):
regression_slope = stats.linregress(time, magnitude)[0]
return {"LinearTrend": regression_slope}
示例9: _assert_linreg
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def _assert_linreg(self, nanx, nany, x, y):
'''nanx, nany: values for _nanlinreg. x, y: values for scipy linreg.
Asserts the results are the same'''
l_1 = linregress(np.asarray(x), np.asarray(y))
l_2 = _nanlinregress(np.asarray(nanx), np.asarray(nany))
if np.isnan(l_1.slope):
self.assertTrue(np.isnan(l_2.slope))
else:
self.assertEqual(l_1.slope, l_2.slope)
if np.isnan(l_1.intercept):
self.assertTrue(np.isnan(l_2.intercept))
else:
self.assertEqual(l_1.intercept, l_2.intercept)
示例10: _nanlinregress
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def _nanlinregress(x, y):
'''Calls scipy linregress only on finite numbers of x and y'''
finite = np.isfinite(x) & np.isfinite(y)
if not finite.any():
# empty arrays passed to linreg raise ValueError:
# force returning an object with nans:
return linregress([np.nan], [np.nan])
return linregress(x[finite], y[finite])
示例11: linregress_residuals
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def linregress_residuals(xdata,ydata):
"""
This function performs a linear regression and then gets the residuals
Args:
xdata (array-like): The x data
ydata (array-like): The y data
Returns:
residuals: the residuals of the regression
slope: the slope of regression line
intercept: intercept of the regression line
rvalue: correlation coeffficient
pvalue: two-sided p-value for a hypothesis test whose null hypothesis is that the slope is zero.
stderr: standard error of the estimated gradient
Author: SMM
"""
from scipy import stats
# Get the regression
(m,b,r,pvalue,stderr)=stats.linregress(xdata,ydata)
#print("In regress1, m is: "+str(m))
# get the residuals
residuals = np.copy(xdata)
for idx,x in enumerate(xdata):
yfit = m*x+b
residuals[idx] = yfit-ydata[idx]
return (residuals,m,b,r,pvalue,stderr)
示例12: remove_outlying_residuals
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def remove_outlying_residuals(xdata,ydata,residuals):
"""
This function removes data with outying residuals
Args:
xdata (array-like): The x data
ydata (array-like): The y data
residuals: The residuals
Returns:
new_x: data with outlier removed
new_y: data with outliers removed
is_outlier_vec: A vec of bools denoiting if they are outliers
m: the slope of the regression
b: the intercept of the regression
Author: SMM
"""
from scipy import stats
# get the outliers
is_outlier_vec = is_outlier(residuals, thresh=3.5)
# now remove the outliers from the dataset
new_x = []
new_y = []
for idx,x in enumerate(xdata):
if not is_outlier_vec[idx]:
new_x.append(xdata[idx])
new_y.append(ydata[idx])
NX = np.asarray(new_x)
NY = np.asarray(new_y)
# now get the new regression
(m,b,r,pvalue,stderr)=stats.linregress(NX,NY)
return (NX,NY, is_outlier_vec, m,b)
示例13: _completion_est
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def _completion_est(self):
if self._extent is None:
return 'unknown'
history = numpy.array(self._progress_history)
if not len(history):
return 'unknown'
history[:,1] -= self._extent
(_slope, intercept, _r, _p, _stderr) = linregress(history[:,1], history[:,0])
if not numpy.isfinite(intercept):
return 'unknown'
else:
total_seconds_remaining = intercept - time.time()
if total_seconds_remaining < 60:
return 'less than 1 minute'
# 1 minute or more remains
minutes = total_seconds_remaining / 60
if minutes < 60:
minutes = int(round(minutes))
return 'about {:d} {}'.format(minutes, 'minutes' if minutes > 1 else 'minute')
# 1 hour or more remains
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
days = int(days)
hours = int(round(hours))
minutes = int(round(minutes))
if days > 0:
return 'about {:d} {}, {:d} {}'.format(days, 'days' if days > 1 else 'day',
hours, 'hours' if hours > 1 else 'hour')
else:
return 'about {:d} {}, {:d} {}'.format(hours, 'hours' if hours > 1 else 'hour',
minutes, 'minutes' if minutes > 1 else 'minute')
示例14: linear_pred
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def linear_pred(y):
if len(y)==1:
return y
else:
x = np.array(range(0,len(y)))
slope, intercept, _, _, _ = stats.linregress(x,y)
return slope*len(y)+intercept
示例15: linear_pred_v2
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import linregress [as 别名]
def linear_pred_v2(tr_t, tr_y, ts_t):
ts_y = np.ones(len(ts_t))
if len(tr_t)==1:
ts_y = ts_y*tr_y
else:
slope, intercept, _, _, _ = stats.linregress(tr_t,tr_y)
ts_y = slope*ts_t+intercept
return ts_y