[Tutor] Fwd: How to roughly associate the values of two numpy arrays, or python lists if necessary

Shall, Sydney sydney.shall at kcl.ac.uk
Sun Sep 23 07:00:55 EDT 2018

```On 23/09/2018 10:42, Peter Otten wrote:
> 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]
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
>

Thanks to Oscar and to Pater for their help. They have set me on the
correct path.
The crucial advice to was to look at the randomisation procedures. I
have used a procedure similar to that suggested by Peter and it works well.

Cheers,

Sydney

_________

Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]
```