Vectoring verschachtelt, während Aussagen blockierenPython

Python-Programme
Anonymous
 Vectoring verschachtelt, während Aussagen blockieren

Post by Anonymous »

All,
Die folgende Funktion get_frequency_of_events < /code> erkennt die Häufigkeit aufeinanderfolgender Zahlen, z. B. < /p>

Code: Select all

import numpy as np
aa=np.array([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
get_frequency_of_events(aa)
this yields the following:
list of indices @ the beginning of each group [1, 3, 6, 10]
frequency of each group [2, 3, 4, 5]
another example,

Code: Select all

aa=np.array([1,1,1,np.nan,np.nan,1,1,np.nan])
idx, feq= get_frequency_of_events(aa)
< /code>
Liste der Indizes @ Zu Beginn jeder Gruppe [0, 5] < /p>
Frequenz jeder Gruppe [3, 2] < /p>
ist diese Funktion jedoch langsam, insbesondere bei der Iterierung von 3D -Daten. Wie kann ich eine solche Funktion vectorisieren, um eine schnellere Verarbeitung zu erreichen? < /P>
Hier ist die Funktion < /p>
def get_frequency_of_events(mydata):
"""
Author  :  Shaaban
Date    : Jan 22, 2025
Purpose : get the frequency of repeated consecutive numbers and their indices, this is important when finding the frequency of heatwaves and etc ... All we have to do is to build matrix of ones (or any other number), and NAN. One refers to the existence of the EVENT, and nan refers to the inexistence of the event. Then this function could give you a summary of the the frequency of the events and their associated indices.
tests :

aa=np.array([1,1,0,0,0,1,0,1,1,1,1,0,1,1])
get_frequency(aa)

aa=np.array([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
get_frequency(aa)

aa=np.array([1,1,1,1,0,0,1,1,1])
get_frequency(aa)

aa=np.arange(10)
get_frequency(aa)

aa=np.ones(10)
get_frequency(aa)

# CAUTION CAUTION CAUTION
#For heatwave numbers, etc , make your array consits of fixed number (any number) that is associated with an evens and Nan for days/hours/month not associated with events. The trick here is that no nan could ever be equal to another nan.

aa=np.array([1,1,1,np.nan,np.nan,1,1,np.nan])
idx, feq= get_frequency(aa)
"""

index_list=[]
events_frequency_list=[]

idx_last_num=len(mydata)-1

counter=0
ii=0
while(ii

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post