0

I have a list of 43 objects and then each object includes 75 points. Each of those 75 points shows a specific time of the day and I want to get the standard deviation of that exact time from each of those 43 objects. I read that I should use a nested for loop but it shows a matrix of zeros. Can anyone help me?

y1 = [
    a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
    a11, a12, a13, a14, a15, a16, a17, a18, a19, a20,
    a21, a22, a23, a24, a25, a26, a27, a28, a29, a30,
    a31, a32, a33, a34, a35, a36, a37, a38, a39, a40,
    a41, a42, a43
]

#an example of what 'a' is
a1 = np.array(df1['Level'][33:108])
a2 = np.array(df1['Mlevel'][105:180])

#get the standard deviation
SD = []
for i in range(43):
    for j in range(75):
        SD.append(np.std(y1[i[j]]))

#plot the standard deviation with mean
for i in range(43):
    axs[0].plot(x1, mean_y1 + SD, color='lightblue')
    axs[0].plot(x1, mean_y1 - SD, color='lightblue')

So basically what I want is to repeat the loop below for j = 0 to 75 but it does not work.

c0 = []
for i in range(43):
    c0.append((y1[i][0]))
print(np.std(c0))

So in case anyone is interested I figured it out and the code below works:

#create a list of all the elements (c)
c = []    
for j in range(75):
     for i in range(43): 
         c.append((y1[i][j]))
     
     
#print(c) 

#Get the standard deviation of every 43 points    
start = 0       # First to consider
stop = 3225     # the length of the list c
interval = 43   # chunk size

SD = []
for i in range(start, stop, interval):
    SD.append(np.std(c[i:(i + interval)]))
    
print(SD)
6
  • 1
    can you provide the whole traceback? Commented May 20, 2021 at 1:55
  • you are accessing [i[j]], but i is a int the right is: SD.append(np.std(y1[i][j])) Commented May 20, 2021 at 1:57
  • I changed it but get the same zeros and the new traceback is this: File "C:/Users/mr179/Desktop/Research/PhD/codes/Averages/SD_WL.py", line 93, in <module> axs[0].plot(x1, mean_y1+SD, color='lightblue') ValueError: operands could not be broadcast together with shapes (75,) (3225,) Commented May 20, 2021 at 2:04
  • Isn't np.std doing the second part of the for loop for you? Can't know what's in the arrays without a data sample. What does y1[0] look like? Commented May 20, 2021 at 2:26
  • @fzzylogic y1[0] would be a1 which itself contains 75 float type numbers. what I am looking for is to get the standard deviation of first element of a1 to a43 and repeat that until the last element (75) Commented May 20, 2021 at 22:30

2 Answers 2

1

You are subscripting

SD.append(np.std(y1[i[j]])) 

but i[j] does not make sense, because i is a number 0,1,2,..., you should rather type

SD.append(np.std(y1[i][j]))

in order to access an element of a list in a list

Sign up to request clarification or add additional context in comments.

6 Comments

I tried that first and I get the same zeros, the error is different though : ValueError: operands could not be broadcast together with shapes (75,) (3225,)
Then you might be interested in writing SD.append(np.std(y1[i])) as the std would be calculated from the whole list hidden under y1[i]
That would mean to omit the inner for loop - iterating only over i
yeah but that isn't what I want , I want the standard deviation of each element from the objects in y1. and not the standard deviation of each object.
That means you need to create the list with all elements and then compute the std. Not sure if I understand your objective good enough though.
|
0

If you have a list consisting of elements that are all 75-element arrays, you can convert the list into a proper array and vectorize the standard deviation operation:

y1 = np.array(y1)
sd = np.std(y1, axis=0)

Use axis=1 if you want standard deviation across all the times in each day, and axis=None for the standard deviation of all the measurements across all 43 days.

You can likely simplify the plots by computing the mean in the same way:

my1 = y1.mean(0)
...

    axs[0].plot(x1, my1 + sd, color='lightblue')
    axs[0].plot(x1, my1 - sd, color='lightblue')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.