mainArray=np.linspace(1,50,50)
##group1 is first 10 elements of mainArray
group1=np.array(1,2,3...10)
group2=np.array(11,12,13...20)
group3=np.array(21,22,23...30)
.
.
group5=np.array(41,42,43,44,45,46,47,48,49,50)
#i need to find standart deviation and mean value of these groups
#like np.mean(group1) and np.std(group1) for all groups
#then i have to calculate (group1-meanOfGroup1)/stdOfGroup1 for all groups
#and append it to one list or array.
我不知道如何通过循环解决这个问题,因为我的主要问题是我无法创建一个循环,将mainArray分组并应用np.mean和np.std。
答案 0 :(得分:1)
根据您的上一条评论,您可以执行以下操作:
# create a list whith values [1,2, ..., 100]
a = list(range(1, 101))
# Sum values by range of 3 values using list comprehension
final = [sum(a[k:k+3]) for k in range(0, len(a), 3)]
print(final)
输出:
[6,
15,
24,
33,
42,
51,
60,
...
267,
276,
285,
294,
100]
PS:最后一笔金额等于100
,因为在1 to 100
之间加上3个值的元素会将它们分组为:1->3, 4->6, ..., 94->96, 97->99, and the final element will be only one number which is 100
修改强>
使用list slicing
进行上次编辑的实现,如下所示。
例如:
a = np.linspace(1,50,50)
# unpacking groups
group1, group2, group3, group4, group5 = [a[k:k+10] for k in range(0, len(a), 10)]
# Applying: np.mean() to the groups
print(np.mean(group1))
print(np.mean(group2))
print(np.mean(group3))
print(np.mean(group4))
print(np.mean(group5))
输出:
5.5
15.5
25.5
35.5
45.5
答案 1 :(得分:0)
oc=np.linspace(1,100,100)
ocmean= [np.mean(oc[k:k+25]) for k in range(0,len(oc),25)]
ocstdev=[np.std(oc[k:k+25]) for k in range(0,len(oc),25)]
## this 2 loops works, i got ocmean and ocstdev.
d=[(oc[k:k+25]-ocmean[i])/ocstdev[i] for k in range(0,len(oc),25) for i in range(len(ocmean))]
## This "d" doesnt gives what i want. It gives a list with 16*25 elements.
答案 2 :(得分:0)
ocmean= [np.mean(oc[k:k+25]) for k in range(0,len(oc),25)]
ocstdev=[np.std(oc[k:k+25]) for k in range(0,len(oc),25)]
d=[]
i=0
k=0
while i <len(ocmean):
m=(oc[k:k+25]-ocmean[i])/ocstdev[i]
i=i+1
k=k+25
d.append(m)
像这样解决