[英]Find max r-value**2 in python
I have a (x,y) dataset, and I would like to calculate the r_value**2
for every 10 elements (so between element 0 and 9, between 1 and 10, ..., between n-10 and n). 我有一个(x,y)数据集,我想为每10个元素计算
r_value**2
(所以在元素0和9之间,在1和10之间,...,在n-10和n之间)。
Ideally the code should give out the r_value**2_max
and save all r
-values in a list. 理想情况下,代码应给出
r_value**2_max
并将所有r
保存在列表中。 I've made a loop, but don't know how to tell stats.linregress
to look between test_i
and test_i+10
and save all r-values**2
in a list. 我做了一个循环,但是不知道如何告诉
stats.linregress
在test_i
和test_i+10
之间test_i
并将所有r-values**2
保存在列表中。
So far, I have this: 到目前为止,我有这个:
import matplotlib.pyplot as plt
from scipy import stats
import numpy as np
import csv
path = '/storage/.../01_python_in/'
test = np.loadtxt(path + 'sample_data.txt', skiprows=0)
test_min = 0
test_max = len(test)
for test_i in range(test_min, test_max-10):
slope, intercept, r_value, p_value, std_err = stats.linregress(test[:, 0], test[:, 1])
print 'i:', test_i, 'r**2:', r_value**2
The way to manually implement this is to slice the first dimension of your array from test_i
to test_i + 10
, like this: 手动实现此方法的方法是将数组的第一维从
test_i
为test_i + 10
,如下所示:
linregress(test[test_i:test_i+window, 0], test[test_i:test_i+window, 1])
Actually, you don't have to split apart the x
and y
parts for linregress
: 实际上,您不必为
linregress
拆分x
和y
部分:
linregress(test[test_i:test_i+window])
You could also save the r_values
by building a list in your loop. 您还可以通过在循环中构建列表来保存
r_values
。 This, along with the above is shown here: 这里显示了上述内容:
window = 10
r_values = []
for test_i in range(len(test)-window):
slope, intercept, r_value, p_value, std_err = \
stats.linregress(test[test_i:test_i + window])
r_values.append(r_value)
print 'i:', test_i, 'r**2:', r_value**2
It's actually simple enough for a list comprehension: 实际上,对于列表理解来说足够简单:
r_values = [stats.linregress(test[i:i+w]).rvalue for i in range(len(test)-w)]
You can get the squares then with: 您可以使用以下方法获得平方:
r_values = np.asarray(r_values)
r_values2 = r_values**2
And the max i
with: 和最大
i
与:
max_i = np.argmax(r_values2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.