我已经创建了一些代码,该代码实际上创建了100个大小递增的列表,因此第一个列表仅包含一个元素,第二个列表具有两个元素,等等。我设法创建了一些代码,该代码返回每个列表的最小值以及索引这个值。我需要做的是能够将所有这些值编译为100个元素的数组。有人可以看看我的代码,并帮助我解决该问题吗?
我尝试使用循环,但是无法产生任何结果
这是我正在使用的代码
def optimisation(c,n):
import numpy as np
import itertools
optimal=n*[0] #create empty set for optimal values for each segment
optimal[0]=0 #set error value for first partition equal to zero
for j in range(2,n): #finds the minimum error for each partition
samples=[]
for i in range(1,j): #finds the minimal error for each line segment
samples.append(find_error(i,j))
samples=np.trim_zeros(samples)
#print(samples)
val=n*[0]
idx=n*[0]
(val, idx) = min((val, idx) for (idx, val) in enumerate(samples))
print(val,idx)
for j in range(1,n):
for i in range(1,j):
optimal[j]= val+c+min(optimal[i-1])
只要给出命令
,此代码就会产生类似以下的输出optimisation(5,100)
这将为左侧显示的每个列表生成最小值列表,并在右侧显示相应的索引。我不会显示包含所有元素的完整列表,因为它的大小会很大。
0.05234944 0
0.23141890982 0
0.0606494102718 2
0.125266172852 0
0.468722738436 4
0.161172451696 0
0.0368827349571 6
0.406437961967 6
0.058411270794 0
0.17900671743 2
0.295569771275 5
0.811505725221 10
0.664073724598 1
1.09909164122 12
0.855491327741 0
1.10540230556 1
0.422713454883 1
1.10272876541 13
1.29587621875 8
1.51194852411 1
2.26497408277 14
3.0111216055 1
1.50627139464 10
3.16516961248 3
2.20094742717 12
3.14639332131 3
2.29403930871 17
2.56656811255 0
5.58629931099 0
5.17459137087 14
3.94986666265 26
6.17020493729 1
6.24984116656 3
6.70767705495 5
6.5236306679 1
7.0887021377 0
7.82672887258 26
5.8351667607 19
5.95734934174 9
11.7341734401 0
14.8294023759 20
8.36809353684 38
12.4300969144 5
15.0063530073 19
13.5851074804 4
20.2610057741 20
17.8737428506 8
17.9420111426 41
17.7309372978 32
24.1566446052 8
26.1398912466 25
23.5973369461 1
26.6403811411 38
34.0977995904 8
32.7782334418 51
24.3663099693 15
39.1511730112 23
43.0494833023 49
39.8814013457 25
47.3142093575 47
47.7307536374 30
49.7080368308 18
52.13139424 18
61.0036863302 5
67.7867571815 60
74.4165905127 54
62.7673033574 30
78.4737938102 5
82.0223279841 29
84.0406948927 19
98.4941295494 28
92.6064227071 65
120.043135174 2
119.294931784 21
129.513913827 14
129.86828615 5
155.771315864 35
157.91467315 17
159.894102894 35
174.519517561 1
181.339901926 70
199.118265967 0
175.772059217 31
220.748358524 82
221.246388848 5
220.620796722 64
256.067301454 30
256.591536797 19
277.194513836 39
294.652427163 3
335.961156216 12
315.188196294 34
329.288057775 29
375.147283979 39
378.195190729 25
407.371302973 22
393.743613132 47
416.971899107 93
我正在尝试将所有这些值编译为一个数组,以便其格式为[0.05234944,0.23141890982,...,416.971899107],并为索引创建一个单独的值,以便其格式为[0,0 ,...,93]
答案 0 :(得分:0)
由于您没有在帖子中添加它,所以我制作了一个find_error
函数的模型
find_error = lambda i, j: random.random()
除了创建随机值外,它实际上什么也没做。当然,您将需要使用自己的功能。
另外,我不知道你打算怎么做
for j in range(1, n):
for i in range(1, j):
optimal[j] = val + c + min(optimal[i - 1])
这将导致错误,因为min
需要一个序列,但是optimal[i - 1]
始终是整数。
我添加了两个列表val_list
和idx_list
val_list = []
idx_list = []
它将存储您的值并最终打印出来。
import itertools
import numpy as np
import random
find_error = lambda i, j: random.random()
def optimisation(c, n):
val_list = []
idx_list = []
optimal = n * [0] # create empty set for optimal values for each segment
optimal[0] = 0 # set error value for first partition equal to zero
for j in range(2, n): # finds the minimum error for each partition
samples = []
for i in range(1, j): # finds the minimal error for each line segment
samples.append(find_error(i, j))
samples = np.trim_zeros(samples)
#print(samples)
val = n * [0]
idx = n * [0]
(val, idx) = min((val, idx) for (idx, val) in enumerate(samples))
#print(val, idx)
val_list.append(val)
idx_list.append(idx)
print(val_list)
print(idx_list)
#for j in range(1, n):
# for i in range(1, j):
# optimal[j] = val + c + min(optimal[i - 1])
optimisation(5, 100)
哪些印刷品:
[0.6748920956425049, 0.20157630160777396, 0.541618597753885, 0.2768964675507879, 0.2976946308913999, 0.3766115048894233, 0.13759194561337484, 0.14987103061621476, 0.014534443068388692, 0.0031480380328120505, 0.034201372897887716, 0.062040806572080553, 0.12730306740011677, 0.24416492868734152, 0.16290838010569786, 0.0018081174520545584, 0.08755932761277996, 0.019842603354791377, 0.020590886194172042, 0.0036695714888764774, 0.12112028957966736, 0.011358619347358823, 0.0066146447764062755, 0.001394609349268272, 0.029586896510528815, 0.002756814742788438, 0.0583562604851291, 0.04284009994467686, 0.016023085954640637, 0.022566876451069584, 0.033772524651475844, 0.005337819133243049, 0.008672534870379667, 0.07821234490854523, 0.039383235142540496, 0.06648877424948574, 0.02217050629348072, 0.0011878402013764111, 0.0076300194701606205, 0.05055673273571437, 0.005344908265560777, 0.007028301085901245, 0.04165421018218529, 0.049295508008472266, 0.05093904397035376, 0.0034540854779047114, 0.02459850682010234, 0.0025335204612035866, 0.05614152881910395, 0.006084903262635, 0.0009059035342229294, 0.01200570503260534, 0.02220929842526087, 0.015366700312140713, 0.00958513758114643, 0.07156333927540381, 0.04216894275052541, 0.008754644639532905, 0.0002495576990957371, 0.008797367341674467, 7.564443573293556e-05, 0.024383979420044333, 0.02788410790756546, 0.021374008158427937, 0.0036953803742704183, 0.013104935266165296, 0.002671114966841137, 0.009159016248525886, 0.005064956945909693, 0.003681503103729278, 0.0009758561877718508, 0.04040161906291373, 0.0004948154900136226, 0.04624146511169147, 0.006594572334752913, 0.003143832522642165, 0.007757875588931151, 0.0030850898821545014, 0.027203566823009617, 0.01721978081614073, 0.0029359570317655237, 0.01059502437128057, 0.006236364658167459, 0.004946834269317746, 0.0032451574275564887, 0.016990675372012154, 0.004935140406089111, 0.0006326108410349418, 0.0036785177995684037, 0.0017698129491926506, 7.475397610190448e-05, 0.0032981939769008983, 0.014765189588278771, 0.012874838822510615, 0.0024783407934703128, 0.006636389782331609, 0.003974376946173419, 0.025291230241184626]
[0, 1, 2, 3, 2, 3, 4, 5, 6, 6, 0, 9, 10, 1, 11, 4, 11, 10, 8, 15, 10, 5, 9, 22, 10, 20, 20, 22, 5, 14, 24, 15, 5, 20, 3, 17, 29, 15, 24, 0, 13, 27, 40, 37, 37, 34, 21, 15, 6, 19, 8, 1, 32, 6, 43, 52, 56, 45, 49, 57, 13, 3, 22, 24, 54, 24, 34, 35, 5, 62, 41, 31, 59, 34, 71, 49, 47, 7, 42, 61, 16, 20, 58, 60, 40, 85, 57, 40, 19, 71, 31, 83, 73, 35, 5, 91, 42, 29]