Calculating difference statistics over a moving window Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Calculating and displaying number statisticsMoving average calculationMoving window with complete boundary in PythonOptimization for calculating statisticsNormalizing data over a distributionClass Design: Calculating statistics from weighted valuesCalculating exponential moving averages with multiples threadsStatistics mode functionModule for statisticsStatistics and calculations
Who's this lady in the war room?
The Nth Gryphon Number
How do you cope with tons of web fonts when copying and pasting from web pages?
How can I introduce the names of fantasy creatures to the reader?
Who's the Giant Batman in the back of this dark knights metal Batman picture?
Should man-made satellites feature an intelligent inverted "cow catcher"?
Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?
Is a copyright notice with a non-existent name be invalid?
Bash script to execute command with file from directory and condition
JImage - Set generated image quality
Is it possible to intall libXft.so.2 on WSL?
Centre cell vertically in tabularx across multiple multiline rows
How to achieve cat-like agility?
Table formatting with tabularx?
Maximum rotation made by a symmetric positive definite matrix?
Is there night in Alpha Complex?
3D Masyu - A Die
Understanding piped commands in GNU/Linux
What should one know about term logic before studying propositional and predicate logic?
Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?
As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?
The test team as an enemy of development? And how can this be avoided?
Which types of prepositional phrase is "toward its employees" in Philosophy guiding the organization's policies towards its employees is not bad?
Changing order of draw operation in PGFPlots
Calculating difference statistics over a moving window
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Calculating and displaying number statisticsMoving average calculationMoving window with complete boundary in PythonOptimization for calculating statisticsNormalizing data over a distributionClass Design: Calculating statistics from weighted valuesCalculating exponential moving averages with multiples threadsStatistics mode functionModule for statisticsStatistics and calculations
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I'd like to calculate some statistics of given data (frequencies of difference among elements on various distances in percent multiplied by 10) using moving window within that data. Is it possible to speed up the code below? I noticed that some calculations are repeating. But I was not able to exclude them without additional slowness.
def get_dist_stat(pdata, pwin_length):
''' pdata - given data array
pwin_length - the lenght of window
the function returns stat table where
row represents the distance between elements
col represents the difference for that distance in percent multiplied by 10 (assume that maximum difference can be 20 percent)
'''
l_data = len(pdata)
l_win = pwin_length
print("l_data=", l_data)
print("l_win=", l_win)
# stat table
stat_table = np.zeros((l_win-1, 20*10), dtype = int)
# loop over all data
for k in range(l_data - l_win + 1):
win = pdata[k : k + l_win]
print('-' * 10)
print("k=", k, " kend=", k + l_win )
print("win=", win)
# loop over window
for i in range(1 , l_win):
b=win[i:]
a=win[:-i]
diff=(abs((b-a)/a*100 ) * 10).astype(int)
print("i=",i)
print("b=", b)
print("a=", a)
print("diff=",diff)
# storing found differences into stat table
apercents, acount = np.unique(diff, return_counts = True)
l_apercents = len(apercents)
for j in range(l_apercents):
stat_table[i-1, apercents[j]] += acount[j]
return stat_table
adata=np.array([1.1,1.2,1.3,1.4,1.5])
print("adata=", adata)
astat_table=get_dist_stat(adata,3)
print(astat_table)
And that is its output
adata= [1.1 1.2 1.3 1.4 1.5]
l_data= 5
l_win= 3
----------
k= 0 kend= 3
win= [1.1 1.2 1.3]
i= 1
b= [1.2 1.3]
a= [1.1 1.2]
diff= [90 83]
i= 2
b= [1.3]
a= [1.1]
diff= [181]
----------
k= 1 kend= 4
win= [1.2 1.3 1.4]
i= 1
b= [1.3 1.4]
a= [1.2 1.3]
diff= [83 76]
i= 2
b= [1.4]
a= [1.2]
diff= [166]
----------
k= 2 kend= 5
win= [1.3 1.4 1.5]
i= 1
b= [1.4 1.5]
a= [1.3 1.4]
diff= [76 71]
i= 2
b= [1.5]
a= [1.3]
diff= [153]
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
python performance numpy statistics
$endgroup$
bumped to the homepage by Community♦ 3 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
$begingroup$
I'd like to calculate some statistics of given data (frequencies of difference among elements on various distances in percent multiplied by 10) using moving window within that data. Is it possible to speed up the code below? I noticed that some calculations are repeating. But I was not able to exclude them without additional slowness.
def get_dist_stat(pdata, pwin_length):
''' pdata - given data array
pwin_length - the lenght of window
the function returns stat table where
row represents the distance between elements
col represents the difference for that distance in percent multiplied by 10 (assume that maximum difference can be 20 percent)
'''
l_data = len(pdata)
l_win = pwin_length
print("l_data=", l_data)
print("l_win=", l_win)
# stat table
stat_table = np.zeros((l_win-1, 20*10), dtype = int)
# loop over all data
for k in range(l_data - l_win + 1):
win = pdata[k : k + l_win]
print('-' * 10)
print("k=", k, " kend=", k + l_win )
print("win=", win)
# loop over window
for i in range(1 , l_win):
b=win[i:]
a=win[:-i]
diff=(abs((b-a)/a*100 ) * 10).astype(int)
print("i=",i)
print("b=", b)
print("a=", a)
print("diff=",diff)
# storing found differences into stat table
apercents, acount = np.unique(diff, return_counts = True)
l_apercents = len(apercents)
for j in range(l_apercents):
stat_table[i-1, apercents[j]] += acount[j]
return stat_table
adata=np.array([1.1,1.2,1.3,1.4,1.5])
print("adata=", adata)
astat_table=get_dist_stat(adata,3)
print(astat_table)
And that is its output
adata= [1.1 1.2 1.3 1.4 1.5]
l_data= 5
l_win= 3
----------
k= 0 kend= 3
win= [1.1 1.2 1.3]
i= 1
b= [1.2 1.3]
a= [1.1 1.2]
diff= [90 83]
i= 2
b= [1.3]
a= [1.1]
diff= [181]
----------
k= 1 kend= 4
win= [1.2 1.3 1.4]
i= 1
b= [1.3 1.4]
a= [1.2 1.3]
diff= [83 76]
i= 2
b= [1.4]
a= [1.2]
diff= [166]
----------
k= 2 kend= 5
win= [1.3 1.4 1.5]
i= 1
b= [1.4 1.5]
a= [1.3 1.4]
diff= [76 71]
i= 2
b= [1.5]
a= [1.3]
diff= [153]
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
python performance numpy statistics
$endgroup$
bumped to the homepage by Community♦ 3 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
$begingroup$
I'd like to calculate some statistics of given data (frequencies of difference among elements on various distances in percent multiplied by 10) using moving window within that data. Is it possible to speed up the code below? I noticed that some calculations are repeating. But I was not able to exclude them without additional slowness.
def get_dist_stat(pdata, pwin_length):
''' pdata - given data array
pwin_length - the lenght of window
the function returns stat table where
row represents the distance between elements
col represents the difference for that distance in percent multiplied by 10 (assume that maximum difference can be 20 percent)
'''
l_data = len(pdata)
l_win = pwin_length
print("l_data=", l_data)
print("l_win=", l_win)
# stat table
stat_table = np.zeros((l_win-1, 20*10), dtype = int)
# loop over all data
for k in range(l_data - l_win + 1):
win = pdata[k : k + l_win]
print('-' * 10)
print("k=", k, " kend=", k + l_win )
print("win=", win)
# loop over window
for i in range(1 , l_win):
b=win[i:]
a=win[:-i]
diff=(abs((b-a)/a*100 ) * 10).astype(int)
print("i=",i)
print("b=", b)
print("a=", a)
print("diff=",diff)
# storing found differences into stat table
apercents, acount = np.unique(diff, return_counts = True)
l_apercents = len(apercents)
for j in range(l_apercents):
stat_table[i-1, apercents[j]] += acount[j]
return stat_table
adata=np.array([1.1,1.2,1.3,1.4,1.5])
print("adata=", adata)
astat_table=get_dist_stat(adata,3)
print(astat_table)
And that is its output
adata= [1.1 1.2 1.3 1.4 1.5]
l_data= 5
l_win= 3
----------
k= 0 kend= 3
win= [1.1 1.2 1.3]
i= 1
b= [1.2 1.3]
a= [1.1 1.2]
diff= [90 83]
i= 2
b= [1.3]
a= [1.1]
diff= [181]
----------
k= 1 kend= 4
win= [1.2 1.3 1.4]
i= 1
b= [1.3 1.4]
a= [1.2 1.3]
diff= [83 76]
i= 2
b= [1.4]
a= [1.2]
diff= [166]
----------
k= 2 kend= 5
win= [1.3 1.4 1.5]
i= 1
b= [1.4 1.5]
a= [1.3 1.4]
diff= [76 71]
i= 2
b= [1.5]
a= [1.3]
diff= [153]
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
python performance numpy statistics
$endgroup$
I'd like to calculate some statistics of given data (frequencies of difference among elements on various distances in percent multiplied by 10) using moving window within that data. Is it possible to speed up the code below? I noticed that some calculations are repeating. But I was not able to exclude them without additional slowness.
def get_dist_stat(pdata, pwin_length):
''' pdata - given data array
pwin_length - the lenght of window
the function returns stat table where
row represents the distance between elements
col represents the difference for that distance in percent multiplied by 10 (assume that maximum difference can be 20 percent)
'''
l_data = len(pdata)
l_win = pwin_length
print("l_data=", l_data)
print("l_win=", l_win)
# stat table
stat_table = np.zeros((l_win-1, 20*10), dtype = int)
# loop over all data
for k in range(l_data - l_win + 1):
win = pdata[k : k + l_win]
print('-' * 10)
print("k=", k, " kend=", k + l_win )
print("win=", win)
# loop over window
for i in range(1 , l_win):
b=win[i:]
a=win[:-i]
diff=(abs((b-a)/a*100 ) * 10).astype(int)
print("i=",i)
print("b=", b)
print("a=", a)
print("diff=",diff)
# storing found differences into stat table
apercents, acount = np.unique(diff, return_counts = True)
l_apercents = len(apercents)
for j in range(l_apercents):
stat_table[i-1, apercents[j]] += acount[j]
return stat_table
adata=np.array([1.1,1.2,1.3,1.4,1.5])
print("adata=", adata)
astat_table=get_dist_stat(adata,3)
print(astat_table)
And that is its output
adata= [1.1 1.2 1.3 1.4 1.5]
l_data= 5
l_win= 3
----------
k= 0 kend= 3
win= [1.1 1.2 1.3]
i= 1
b= [1.2 1.3]
a= [1.1 1.2]
diff= [90 83]
i= 2
b= [1.3]
a= [1.1]
diff= [181]
----------
k= 1 kend= 4
win= [1.2 1.3 1.4]
i= 1
b= [1.3 1.4]
a= [1.2 1.3]
diff= [83 76]
i= 2
b= [1.4]
a= [1.2]
diff= [166]
----------
k= 2 kend= 5
win= [1.3 1.4 1.5]
i= 1
b= [1.4 1.5]
a= [1.3 1.4]
diff= [76 71]
i= 2
b= [1.5]
a= [1.3]
diff= [153]
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
python performance numpy statistics
python performance numpy statistics
edited Mar 7 '18 at 21:12
Prokhozhii
asked Mar 7 '18 at 12:43
ProkhozhiiProkhozhii
163
163
bumped to the homepage by Community♦ 3 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 3 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.
In fact it's much faster to do all the calculations ahead of time into one big ndarray
, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a
and b
lists.
How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.
precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
# First pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# compute diff relative to elements [first_point_index] and [first_point_index+interval]
# line will be similar to precomputed_results[...] = ...
# Second pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# use slicing on precomputed_results
$endgroup$
$begingroup$
It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
$endgroup$
– Prokhozhii
Jul 26 '18 at 10:13
$begingroup$
@Prokhozhii Um, yes we will: wheninterval
is1
(which is certainly inrange(1, 7)
, andfirst_point_index
is8
(which is certainly inrange(1, 10-1)
thenfirst_point_index + interval
is9
. I didn't write all the code, but the comments indicate which elements are getting compared.
$endgroup$
– Snowbody
Jul 27 '18 at 12:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189045%2fcalculating-difference-statistics-over-a-moving-window%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.
In fact it's much faster to do all the calculations ahead of time into one big ndarray
, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a
and b
lists.
How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.
precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
# First pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# compute diff relative to elements [first_point_index] and [first_point_index+interval]
# line will be similar to precomputed_results[...] = ...
# Second pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# use slicing on precomputed_results
$endgroup$
$begingroup$
It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
$endgroup$
– Prokhozhii
Jul 26 '18 at 10:13
$begingroup$
@Prokhozhii Um, yes we will: wheninterval
is1
(which is certainly inrange(1, 7)
, andfirst_point_index
is8
(which is certainly inrange(1, 10-1)
thenfirst_point_index + interval
is9
. I didn't write all the code, but the comments indicate which elements are getting compared.
$endgroup$
– Snowbody
Jul 27 '18 at 12:57
add a comment |
$begingroup$
You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.
In fact it's much faster to do all the calculations ahead of time into one big ndarray
, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a
and b
lists.
How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.
precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
# First pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# compute diff relative to elements [first_point_index] and [first_point_index+interval]
# line will be similar to precomputed_results[...] = ...
# Second pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# use slicing on precomputed_results
$endgroup$
$begingroup$
It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
$endgroup$
– Prokhozhii
Jul 26 '18 at 10:13
$begingroup$
@Prokhozhii Um, yes we will: wheninterval
is1
(which is certainly inrange(1, 7)
, andfirst_point_index
is8
(which is certainly inrange(1, 10-1)
thenfirst_point_index + interval
is9
. I didn't write all the code, but the comments indicate which elements are getting compared.
$endgroup$
– Snowbody
Jul 27 '18 at 12:57
add a comment |
$begingroup$
You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.
In fact it's much faster to do all the calculations ahead of time into one big ndarray
, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a
and b
lists.
How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.
precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
# First pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# compute diff relative to elements [first_point_index] and [first_point_index+interval]
# line will be similar to precomputed_results[...] = ...
# Second pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# use slicing on precomputed_results
$endgroup$
You've already made the key observation here, which is that most of the work is redone. Each time you pick a window, most of the calculations are the same as the previous window.
In fact it's much faster to do all the calculations ahead of time into one big ndarray
, and then for each window, pick out the calculations that are relevant. So we don't need the temporary a
and b
lists.
How many dimensions do we need? Just starting point and length. It's going to be a triangular array, so we'll waste some space.
precomputed_results = np.zeros(l_win+1, l_data), dtype = int)
# First pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# compute diff relative to elements [first_point_index] and [first_point_index+interval]
# line will be similar to precomputed_results[...] = ...
# Second pass
for interval in range(1, l_win):
for first_point_index in range(l_data-interval):
# use slicing on precomputed_results
answered Mar 9 '18 at 3:21
SnowbodySnowbody
7,7871545
7,7871545
$begingroup$
It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
$endgroup$
– Prokhozhii
Jul 26 '18 at 10:13
$begingroup$
@Prokhozhii Um, yes we will: wheninterval
is1
(which is certainly inrange(1, 7)
, andfirst_point_index
is8
(which is certainly inrange(1, 10-1)
thenfirst_point_index + interval
is9
. I didn't write all the code, but the comments indicate which elements are getting compared.
$endgroup$
– Snowbody
Jul 27 '18 at 12:57
add a comment |
$begingroup$
It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
$endgroup$
– Prokhozhii
Jul 26 '18 at 10:13
$begingroup$
@Prokhozhii Um, yes we will: wheninterval
is1
(which is certainly inrange(1, 7)
, andfirst_point_index
is8
(which is certainly inrange(1, 10-1)
thenfirst_point_index + interval
is9
. I didn't write all the code, but the comments indicate which elements are getting compared.
$endgroup$
– Snowbody
Jul 27 '18 at 12:57
$begingroup$
It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
$endgroup$
– Prokhozhii
Jul 26 '18 at 10:13
$begingroup$
It looks like the window is not moving in your code. For example, if we have l_data = 10 and l_win = 7, we won't get difference between elements 8 and 9.
$endgroup$
– Prokhozhii
Jul 26 '18 at 10:13
$begingroup$
@Prokhozhii Um, yes we will: when
interval
is 1
(which is certainly in range(1, 7)
, and first_point_index
is 8
(which is certainly in range(1, 10-1)
then first_point_index + interval
is 9
. I didn't write all the code, but the comments indicate which elements are getting compared.$endgroup$
– Snowbody
Jul 27 '18 at 12:57
$begingroup$
@Prokhozhii Um, yes we will: when
interval
is 1
(which is certainly in range(1, 7)
, and first_point_index
is 8
(which is certainly in range(1, 10-1)
then first_point_index + interval
is 9
. I didn't write all the code, but the comments indicate which elements are getting compared.$endgroup$
– Snowbody
Jul 27 '18 at 12:57
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189045%2fcalculating-difference-statistics-over-a-moving-window%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown