Iterating through lists with periodic boundary condition and computing distances between elementsHow to iterate through two lists in parallel?Iterating over every two elements in a listProcessing a very very big data set in python - memory errorHistogram for data with periodic boundary conditionsnp.ndarray with Periodic Boundary conditionsEfficiently select random matrix indices with given probabilitiesEfficiently get permutation of 3 numpy arrays of differing sizes and typesDistance computation between (M,N) and (N,) arrayswindowed selection from a list in python
Category-theoretic treatment of diffs, patches and merging?
Tesco's Burger Relish Best Before End date number
How do I explain that I don't want to maintain old projects?
What are the effects of abstaining from eating a certain flavor?
What factors could lead to bishops establishing monastic armies?
How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant
Did William Shakespeare hide things in his writings?
What is the highest level of accuracy in motion control a Victorian society could achieve?
Does the Wild Magic sorcerer's Tides of Chaos feature grant advantage on all attacks, or just the first one?
Why do people prefer metropolitan areas, considering monsters and villains?
Array or vector? Two dimensional array or matrix?
What was the nature of the known bugs in the Space Shuttle software?
Where are the Wazirs?
What does "frozen" mean (e.g. for catcodes)?
The Apéry's constant and the Airy function
What are some bad ways to subvert tropes?
How do ballistic trajectories work in a ring world?
Why are co-factors 4 and 8 so popular when co-factor is more than one?
How many Jimmys can fit?
How to say "is going" in Russian in "this game is going to perish"
Who goes first? Person disembarking bus or the bicycle?
Four ships at the ocean with the same distance
What do you call a situation where you have choices but no good choice?
Computer name naming convention for security
Iterating through lists with periodic boundary condition and computing distances between elements
How to iterate through two lists in parallel?Iterating over every two elements in a listProcessing a very very big data set in python - memory errorHistogram for data with periodic boundary conditionsnp.ndarray with Periodic Boundary conditionsEfficiently select random matrix indices with given probabilitiesEfficiently get permutation of 3 numpy arrays of differing sizes and typesDistance computation between (M,N) and (N,) arrayswindowed selection from a list in python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to write a python function that takes an index of a list as an input n1
, then in an interval of size w
, selects [n1-w,n1+w]
selects another index n2 with probability that decays as a function of the separation between n1
and n2
and returns the index n2
and the element of the list corresponding to n2
.
In my current implementation ("Attempt 1"), first I compute all the probabilities using, then select an index with the weights.
This works fine (I think) given a list [0,1,2,3,4,5,6,7,8,9]
and if my n1
is somewhere in the middle say 5
and w=2
, then an element is being chosen from the interval
with probability weights
[3 4 5 6 7][0.16666667 0.33333333 0. 0.33333333 0.16666667]
.
The problem I am dealing with is if I am selecting an element from the ends, for example, choosing n2choice(8)
gives
[6 7 8 9]#indices interval
[0.2 0.4 0. 0.4]#weights
Instead I would like an interval
[6 7 8 9 0]# indices interval
[0.16666667 0.33333333 0. 0.33333333 0.16666667]#weights
Or similarly, at the other end, n2choice(1)
gives [0 1 2 3]#interval
[0.2 0.4 0. 0.4]#weights
Instead I would like to get
[9 0 1 2 3]# indices interval
[0.16666667 0.33333333 0. 0.33333333 0.16666667]#weights
Essentially, I would like to convert the current "absorbing" boundary conditions I have implemented to "periodic boundaries" wherein the 1D list is in fact treated as a 1D ring.
To do so i used i tried using the %
operator (see attempt 2). However this throws an "IndexError: list index out of range". I could use some help troubleshooting this code, and also suggestion on a fast, pythonic way to do this. I would like to be able to do this for arrays of arbitrary length, and with arbitrary w, instead of this simple test case presented here.
#Attempt 1
import numpy as np
import random
a = [0,1,2,3,4,5,6,7,8,9]
w =2
mylist = np.array(a)
indices_list = np.arange(mylist.size)
def n2choice(n1):
prob_wts = [0 if i+(n1-w) == n1 else 1/(abs(i+(n1-w)-n1))**(1.) for i in np.arange(mylist[max(n1-w,0):n1+w+1].size)]
prob_wts = np.array(prob_wts)/sum(prob_wts)
#for i in np.arange(mylist[max(n1-w,0):n1+w+1].size):
#print(i, i+(n1-w))
print(prob_wts)
print(sum(prob_wts))
print(indices_list[max(n1-w,0):n1+w+1])
n2 = random.choices(indices_list[max(n1-w,0):n1+w+1], weights=prob_wts,k=1)
print(prob_wts[max(n1-w,0):n1+w+1])
n2_c = (n2[0], mylist[n2[0]])
return np.array(n2_c)
n2choice(5)
#Attempt 2
import numpy as np
import random
import itertools
a = [0,1,2,3,4,5,6,7,8,9]
w =2
mylist = np.array(a)
indices_list = np.arange(mylist.size)
def n2choice(n1):
prob_wts = [0 if i+(n1-w) == n1 else 1/(abs(i+(n1-w)-n1))**(1.) for i in np.arange(mylist[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)].size)]
prob_wts = np.array(prob_wts)/sum(prob_wts)
#for i in np.arange(mylist[max(n1-w,0):n1+w+1].size):
#print(i, i+(n1-w))
print(prob_wts)
print(sum(prob_wts))
print(indices_list[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)])
n2 = random.choices(indices_list[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)], weights=prob_wts,k=1)
#print(prob_wts[max(n1-w,0):n1+w+1])
n2_c = (n2[0], mylist[n2[0]])
return np.array(n2_c)
n2choice(1)
python python-3.x numpy
add a comment |
I am trying to write a python function that takes an index of a list as an input n1
, then in an interval of size w
, selects [n1-w,n1+w]
selects another index n2 with probability that decays as a function of the separation between n1
and n2
and returns the index n2
and the element of the list corresponding to n2
.
In my current implementation ("Attempt 1"), first I compute all the probabilities using, then select an index with the weights.
This works fine (I think) given a list [0,1,2,3,4,5,6,7,8,9]
and if my n1
is somewhere in the middle say 5
and w=2
, then an element is being chosen from the interval
with probability weights
[3 4 5 6 7][0.16666667 0.33333333 0. 0.33333333 0.16666667]
.
The problem I am dealing with is if I am selecting an element from the ends, for example, choosing n2choice(8)
gives
[6 7 8 9]#indices interval
[0.2 0.4 0. 0.4]#weights
Instead I would like an interval
[6 7 8 9 0]# indices interval
[0.16666667 0.33333333 0. 0.33333333 0.16666667]#weights
Or similarly, at the other end, n2choice(1)
gives [0 1 2 3]#interval
[0.2 0.4 0. 0.4]#weights
Instead I would like to get
[9 0 1 2 3]# indices interval
[0.16666667 0.33333333 0. 0.33333333 0.16666667]#weights
Essentially, I would like to convert the current "absorbing" boundary conditions I have implemented to "periodic boundaries" wherein the 1D list is in fact treated as a 1D ring.
To do so i used i tried using the %
operator (see attempt 2). However this throws an "IndexError: list index out of range". I could use some help troubleshooting this code, and also suggestion on a fast, pythonic way to do this. I would like to be able to do this for arrays of arbitrary length, and with arbitrary w, instead of this simple test case presented here.
#Attempt 1
import numpy as np
import random
a = [0,1,2,3,4,5,6,7,8,9]
w =2
mylist = np.array(a)
indices_list = np.arange(mylist.size)
def n2choice(n1):
prob_wts = [0 if i+(n1-w) == n1 else 1/(abs(i+(n1-w)-n1))**(1.) for i in np.arange(mylist[max(n1-w,0):n1+w+1].size)]
prob_wts = np.array(prob_wts)/sum(prob_wts)
#for i in np.arange(mylist[max(n1-w,0):n1+w+1].size):
#print(i, i+(n1-w))
print(prob_wts)
print(sum(prob_wts))
print(indices_list[max(n1-w,0):n1+w+1])
n2 = random.choices(indices_list[max(n1-w,0):n1+w+1], weights=prob_wts,k=1)
print(prob_wts[max(n1-w,0):n1+w+1])
n2_c = (n2[0], mylist[n2[0]])
return np.array(n2_c)
n2choice(5)
#Attempt 2
import numpy as np
import random
import itertools
a = [0,1,2,3,4,5,6,7,8,9]
w =2
mylist = np.array(a)
indices_list = np.arange(mylist.size)
def n2choice(n1):
prob_wts = [0 if i+(n1-w) == n1 else 1/(abs(i+(n1-w)-n1))**(1.) for i in np.arange(mylist[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)].size)]
prob_wts = np.array(prob_wts)/sum(prob_wts)
#for i in np.arange(mylist[max(n1-w,0):n1+w+1].size):
#print(i, i+(n1-w))
print(prob_wts)
print(sum(prob_wts))
print(indices_list[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)])
n2 = random.choices(indices_list[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)], weights=prob_wts,k=1)
#print(prob_wts[max(n1-w,0):n1+w+1])
n2_c = (n2[0], mylist[n2[0]])
return np.array(n2_c)
n2choice(1)
python python-3.x numpy
add a comment |
I am trying to write a python function that takes an index of a list as an input n1
, then in an interval of size w
, selects [n1-w,n1+w]
selects another index n2 with probability that decays as a function of the separation between n1
and n2
and returns the index n2
and the element of the list corresponding to n2
.
In my current implementation ("Attempt 1"), first I compute all the probabilities using, then select an index with the weights.
This works fine (I think) given a list [0,1,2,3,4,5,6,7,8,9]
and if my n1
is somewhere in the middle say 5
and w=2
, then an element is being chosen from the interval
with probability weights
[3 4 5 6 7][0.16666667 0.33333333 0. 0.33333333 0.16666667]
.
The problem I am dealing with is if I am selecting an element from the ends, for example, choosing n2choice(8)
gives
[6 7 8 9]#indices interval
[0.2 0.4 0. 0.4]#weights
Instead I would like an interval
[6 7 8 9 0]# indices interval
[0.16666667 0.33333333 0. 0.33333333 0.16666667]#weights
Or similarly, at the other end, n2choice(1)
gives [0 1 2 3]#interval
[0.2 0.4 0. 0.4]#weights
Instead I would like to get
[9 0 1 2 3]# indices interval
[0.16666667 0.33333333 0. 0.33333333 0.16666667]#weights
Essentially, I would like to convert the current "absorbing" boundary conditions I have implemented to "periodic boundaries" wherein the 1D list is in fact treated as a 1D ring.
To do so i used i tried using the %
operator (see attempt 2). However this throws an "IndexError: list index out of range". I could use some help troubleshooting this code, and also suggestion on a fast, pythonic way to do this. I would like to be able to do this for arrays of arbitrary length, and with arbitrary w, instead of this simple test case presented here.
#Attempt 1
import numpy as np
import random
a = [0,1,2,3,4,5,6,7,8,9]
w =2
mylist = np.array(a)
indices_list = np.arange(mylist.size)
def n2choice(n1):
prob_wts = [0 if i+(n1-w) == n1 else 1/(abs(i+(n1-w)-n1))**(1.) for i in np.arange(mylist[max(n1-w,0):n1+w+1].size)]
prob_wts = np.array(prob_wts)/sum(prob_wts)
#for i in np.arange(mylist[max(n1-w,0):n1+w+1].size):
#print(i, i+(n1-w))
print(prob_wts)
print(sum(prob_wts))
print(indices_list[max(n1-w,0):n1+w+1])
n2 = random.choices(indices_list[max(n1-w,0):n1+w+1], weights=prob_wts,k=1)
print(prob_wts[max(n1-w,0):n1+w+1])
n2_c = (n2[0], mylist[n2[0]])
return np.array(n2_c)
n2choice(5)
#Attempt 2
import numpy as np
import random
import itertools
a = [0,1,2,3,4,5,6,7,8,9]
w =2
mylist = np.array(a)
indices_list = np.arange(mylist.size)
def n2choice(n1):
prob_wts = [0 if i+(n1-w) == n1 else 1/(abs(i+(n1-w)-n1))**(1.) for i in np.arange(mylist[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)].size)]
prob_wts = np.array(prob_wts)/sum(prob_wts)
#for i in np.arange(mylist[max(n1-w,0):n1+w+1].size):
#print(i, i+(n1-w))
print(prob_wts)
print(sum(prob_wts))
print(indices_list[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)])
n2 = random.choices(indices_list[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)], weights=prob_wts,k=1)
#print(prob_wts[max(n1-w,0):n1+w+1])
n2_c = (n2[0], mylist[n2[0]])
return np.array(n2_c)
n2choice(1)
python python-3.x numpy
I am trying to write a python function that takes an index of a list as an input n1
, then in an interval of size w
, selects [n1-w,n1+w]
selects another index n2 with probability that decays as a function of the separation between n1
and n2
and returns the index n2
and the element of the list corresponding to n2
.
In my current implementation ("Attempt 1"), first I compute all the probabilities using, then select an index with the weights.
This works fine (I think) given a list [0,1,2,3,4,5,6,7,8,9]
and if my n1
is somewhere in the middle say 5
and w=2
, then an element is being chosen from the interval
with probability weights
[3 4 5 6 7][0.16666667 0.33333333 0. 0.33333333 0.16666667]
.
The problem I am dealing with is if I am selecting an element from the ends, for example, choosing n2choice(8)
gives
[6 7 8 9]#indices interval
[0.2 0.4 0. 0.4]#weights
Instead I would like an interval
[6 7 8 9 0]# indices interval
[0.16666667 0.33333333 0. 0.33333333 0.16666667]#weights
Or similarly, at the other end, n2choice(1)
gives [0 1 2 3]#interval
[0.2 0.4 0. 0.4]#weights
Instead I would like to get
[9 0 1 2 3]# indices interval
[0.16666667 0.33333333 0. 0.33333333 0.16666667]#weights
Essentially, I would like to convert the current "absorbing" boundary conditions I have implemented to "periodic boundaries" wherein the 1D list is in fact treated as a 1D ring.
To do so i used i tried using the %
operator (see attempt 2). However this throws an "IndexError: list index out of range". I could use some help troubleshooting this code, and also suggestion on a fast, pythonic way to do this. I would like to be able to do this for arrays of arbitrary length, and with arbitrary w, instead of this simple test case presented here.
#Attempt 1
import numpy as np
import random
a = [0,1,2,3,4,5,6,7,8,9]
w =2
mylist = np.array(a)
indices_list = np.arange(mylist.size)
def n2choice(n1):
prob_wts = [0 if i+(n1-w) == n1 else 1/(abs(i+(n1-w)-n1))**(1.) for i in np.arange(mylist[max(n1-w,0):n1+w+1].size)]
prob_wts = np.array(prob_wts)/sum(prob_wts)
#for i in np.arange(mylist[max(n1-w,0):n1+w+1].size):
#print(i, i+(n1-w))
print(prob_wts)
print(sum(prob_wts))
print(indices_list[max(n1-w,0):n1+w+1])
n2 = random.choices(indices_list[max(n1-w,0):n1+w+1], weights=prob_wts,k=1)
print(prob_wts[max(n1-w,0):n1+w+1])
n2_c = (n2[0], mylist[n2[0]])
return np.array(n2_c)
n2choice(5)
#Attempt 2
import numpy as np
import random
import itertools
a = [0,1,2,3,4,5,6,7,8,9]
w =2
mylist = np.array(a)
indices_list = np.arange(mylist.size)
def n2choice(n1):
prob_wts = [0 if i+(n1-w) == n1 else 1/(abs(i+(n1-w)-n1))**(1.) for i in np.arange(mylist[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)].size)]
prob_wts = np.array(prob_wts)/sum(prob_wts)
#for i in np.arange(mylist[max(n1-w,0):n1+w+1].size):
#print(i, i+(n1-w))
print(prob_wts)
print(sum(prob_wts))
print(indices_list[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)])
n2 = random.choices(indices_list[(n1 - w)%len(mylist):(n1+w+1)%len(mylist)], weights=prob_wts,k=1)
#print(prob_wts[max(n1-w,0):n1+w+1])
n2_c = (n2[0], mylist[n2[0]])
return np.array(n2_c)
n2choice(1)
python python-3.x numpy
python python-3.x numpy
asked Mar 25 at 21:30
jcpjcp
154 bronze badges
154 bronze badges
add a comment |
add a comment |
0
active
oldest
votes
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: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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');
);
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%2fstackoverflow.com%2fquestions%2f55346696%2fiterating-through-lists-with-periodic-boundary-condition-and-computing-distances%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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%2fstackoverflow.com%2fquestions%2f55346696%2fiterating-through-lists-with-periodic-boundary-condition-and-computing-distances%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');
);
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');
);
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');
);
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