[Tutor] Fwd: How to roughly associate the values of two numpy arrays, or python lists if necessary
Peter Otten
__peter__ at web.de
Sun Sep 23 05:42:00 EDT 2018
Shall, Sydney via Tutor wrote:
> What I want is the following.
>
> I have:
> > property_a = [1, 6, 2, 4]
> > property_b = [62, 73, 31 102]
>
> Result should approximately be:
> > property_b = [31, 102, 62, 73]
>
> That is both lists change in value in exactly the same order.
>
> Now, this is easy to achieve. I could simply sort both lists is
> ascending order and I would then have an exact alignment of values is
> ascending order. The correlation would be a perfect linear relationship,
> I suppose.
>
> But my actual scientific problem requires that the correlation should be
> only approximate and I do not know how close to to a perfect correlation
> it should be. So, I need to introduce some lack of good correlation when
> I set up the correlation. How to do that is my problem.
>
> I hope this helps to clarify what my problem is.
Maybe you could sort the already-sorted property_b again, with some random
offset:
>>> import itertools
>>> def wiggled(items, sigma):
... counter = itertools.count()
... def key(item): return random.gauss(next(counter), sigma)
... return sorted(items, key=key)
...
>>> wiggled(range(20), 3)
[0, 5, 2, 4, 1, 6, 7, 8, 3, 9, 11, 10, 13, 14, 16, 12, 18, 17, 19, 15]
>>> wiggled([31, 102, 62, 73], .8)
[102, 31, 62, 73]
>>> wiggled([31, 102, 62, 73], .8)
[31, 102, 62, 73]
>>> wiggled([31, 102, 62, 73], .8)
[31, 102, 62, 73]
>>> wiggled([31, 102, 62, 73], .8)
[31, 62, 102, 73]
More information about the Tutor
mailing list