-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlutilities.py
More file actions
606 lines (540 loc) · 22.4 KB
/
mlutilities.py
File metadata and controls
606 lines (540 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# Functions for use in CS445, Introduction to Machine Learning
# Spring, 2018
# by Chuck Anderson, for CS445
# http://www.cs.colostate.edu/~anderson
# You may use, but please credit the source.
#
# makeIndicatorVars(T)
# trainValidateTestKFolds(trainf, evaluatef, X, T, parameterSets, nFolds=5,
# shuffle=False, verbose=False)
#
# draw(Vs, W, inputNames=None, outputNames=None, gray=False)
#
# scg(x, f, gradf, *fargs, **params)
# steepest(x, f, gradf, *fargs, **params)
import matplotlib.pyplot as plt
import matplotlib.patches as pltpatch # for Arc
import matplotlib.collections as pltcoll
from copy import copy
import numpy as np
import pandas as pd
import sys
from math import sqrt, ceil
floatPrecision = sys.float_info.epsilon
def makeIndicatorVars(T):
''' Assumes argument is N x 1, N samples each being integer class label '''
return (T == np.unique(T)).astype(int)
def partition(X,T,trainFraction,shuffle=False,classification=False):
"""Usage: Xtrain,Train,Xvalidate,Tvalidate,Xtest,Ttest = partition(X,T,(0.6,0.2,0.2),shuffle=False,classification=True)
X is nSamples x nFeatures.
fractions can have just two values, for partitioning into train and test only
If classification=True, T is target class as integer. Data partitioned
according to class proportions.
"""
# Skip the validation step
validateFraction = 0
testFraction = 1 - trainFraction # fractions[1]
# else:
# validateFraction = fractions[1]
# testFraction = fractions[2]
rowIndices = np.arange(X.shape[0])
if shuffle:
np.random.shuffle(rowIndices)
if not classification:
# regression, so do not partition according to targets.
n = X.shape[0]
nTrain = round(trainFraction * n)
nValidate = round(validateFraction * n)
nTest = round(testFraction * n)
if nTrain + nValidate + nTest > n:
nTest = n - nTrain - nValidate
Xtrain = X[rowIndices[:nTrain],:]
Ttrain = T[rowIndices[:nTrain],:]
if nValidate > 0:
Xvalidate = X[rowIndices[nTrain:nTrain+nValidate],:]
Tvalidate = T[rowIndices[nTrain:nTrain:nValidate],:]
Xtest = X[rowIndices[nTrain+nValidate:nTrain+nValidate+nTest],:]
Ttest = T[rowIndices[nTrain+nValidate:nTrain+nValidate+nTest],:]
else:
# classifying, so partition data according to target class
classes = np.unique(T)
trainIndices = []
validateIndices = []
testIndices = []
for c in classes:
# row indices for class c
cRows = np.where(T[rowIndices,:] == c)[0]
# collect row indices for class c for each partition
n = len(cRows)
nTrain = round(trainFraction * n)
nValidate = round(validateFraction * n)
nTest = round(testFraction * n)
if nTrain + nValidate + nTest > n:
nTest = n - nTrain - nValidate
trainIndices += rowIndices[cRows[:nTrain]].tolist()
if nValidate > 0:
validateIndices += rowIndices[cRows[nTrain:nTrain+nValidate]].tolist()
testIndices += rowIndices[cRows[nTrain+nValidate:nTrain+nValidate+nTest]].tolist()
Xtrain = X[trainIndices,:]
Ttrain = T[trainIndices,:]
if nValidate > 0:
Xvalidate = X[validateIndices,:]
Tvalidate = T[validateIndices,:]
Xtest = X[testIndices,:]
Ttest = T[testIndices,:]
if nValidate > 0:
return Xtrain,Ttrain,Xvalidate,Tvalidate,Xtest,Ttest
else:
return Xtrain,Ttrain,Xtest,Ttest
def trainValidateTestKFolds(trainf, evaluatef, X, T, parameterSets, nFolds=5,
shuffle=False, verbose=False):
# Randomly arrange row indices
rowIndices = np.arange(X.shape[0])
if shuffle:
np.random.shuffle(rowIndices)
isNewBetterThanOld = lambda new,old: new < old
# Calculate number of samples in each of the nFolds folds
nSamples = X.shape[0]
nEach = int(nSamples / nFolds)
if nEach == 0:
raise ValueError("trainValidateTestKFolds: Number of samples in each fold is 0.")
# Calculate the starting and stopping row index for each fold.
# Store in startsStops as list of (start,stop) pairs
starts = np.arange(0,nEach*nFolds, nEach)
stops = starts + nEach
stops[-1] = nSamples
startsStops = list(zip(starts, stops))
# Repeat with testFold taking each single fold, one at a time
results = []
for testFold in range(nFolds):
# Leaving the testFold out, for each validate fold, train on remaining
# folds and evaluate on validate fold. Collect theseRepeat with validate
# Construct Xtest and Ttest
a,b = startsStops[testFold]
rows = rowIndices[a:b]
Xtest = X[rows, :]
Ttest = T[rows, :]
bestParms = None
for parms in parameterSets:
# trainEvaluationSum = 0
validateEvaluationSum = 0
for validateFold in range(nFolds):
if testFold == validateFold:
continue
# Construct Xtrain and Ttrain
trainFolds = np.setdiff1d(range(nFolds), [testFold, validateFold])
rows = []
for tf in trainFolds:
a,b = startsStops[tf]
rows += rowIndices[a:b].tolist()
Xtrain = X[rows, :]
Ttrain = T[rows, :]
# Construct Xvalidate and Tvalidate
a,b = startsStops[validateFold]
rows = rowIndices[a:b]
Xvalidate = X[rows, :]
Tvalidate = T[rows, :]
model = trainf(Xtrain, Ttrain, parms)
# trainEvaluationSum += evaluatef(model,Xtrain,Train)
validateEvaluationSum += evaluatef(model, Xvalidate, Tvalidate)
validateEvaluation = validateEvaluationSum / (nFolds-1)
if verbose:
if hasattr(model, 'bestIteration') and model.bestIteration is not None:
print('{} Val {:.3f} Best Iter {:d}'.format(parms, validateEvaluation, model.bestIteration))
else:
print('{} Val {:.3f}'.format(parms, validateEvaluation))
if bestParms is None or isNewBetterThanOld(validateEvaluation, bestValidationEvaluation):
bestParms = parms
bestValidationEvaluation = validateEvaluation
if verbose:
print('New best')
# trainEvaluation = trainEvaluationSum / (nFolds-1)
newXtrain = np.vstack((Xtrain, Xvalidate))
newTtrain = np.vstack((Ttrain, Tvalidate))
model = trainf(newXtrain, newTtrain, bestParms)
trainEvaluation = evaluatef(model, newXtrain, newTtrain)
testEvaluation = evaluatef(model, Xtest, Ttest)
# resultThisTestFold = [bestParms, trainEvaluation,
# bestValidationEvaluation, testEvaluation]
resultThisTestFold = [nFolds, testFold+1, bestParms, trainEvaluation, bestValidationEvaluation, testEvaluation]
results.append(resultThisTestFold)
print(pd.DataFrame(results,
columns=('nFolds',
'Test Fold',
'Best Parameters',
'Train Accuracy',
'Validation Accuracy',
'Test Accuracy')))
# print('{}/{}'.format(testFold+1, nFolds), end=' ', flush=True)
return pd.DataFrame(results,
columns=('nFolds',
'Test Fold',
'Best Parameters',
'Train Accuracy',
'Validation Accuracy',
'Test Accuracy'))
def confusionMatrix(actual, predicted, classes):
nc = len(classes)
confmat = np.zeros((nc, nc))
for ri in range(nc):
trues = (actual==classes[ri]).squeeze()
predictedThisClass = predicted[trues]
keep = trues
predictedThisClassAboveThreshold = predictedThisClass
# print 'confusionMatrix: sum(trues) is ', np.sum(trues),'for classes[ri]',classes[ri]
for ci in range(nc):
confmat[ri,ci] = np.sum(predictedThisClassAboveThreshold == classes[ci]) / float(np.sum(keep))
# confmat[ri,nc] = np.sum(keep)
# confmat[ri,nc+1] = np.sum(trues)
printConfusionMatrix(confmat,classes)
return confmat
def printConfusionMatrix(confmat,classes):
print(' ',end='')
for i in classes:
print('%5d' % (i), end='')
print('\n ',end='')
print('{:s}'.format('------'*len(classes)))
for i,t in enumerate(classes):
print('{:2d} |'.format(t), end='')
for i1,t1 in enumerate(classes):
if confmat[i,i1] == 0:
print(' 0 ',end='')
else:
print('{:5.1f}'.format(100*confmat[i,i1]), end='')
print()
######################################################################
def draw(Vs, W, inputNames=None, outputNames=None, gray=False):
def isOdd(x):
return x % 2 != 0
W = Vs + [W]
nLayers = len(W)
# calculate xlim and ylim for whole network plot
# Assume 4 characters fit between each wire
# -0.5 is to leave 0.5 spacing before first wire
xlim = max(map(len, inputNames))/4.0 if inputNames else 1
ylim = 0
for li in range(nLayers):
ni, no = W[li].shape # no means number outputs this layer
if not isOdd(li):
ylim += ni + 0.5
else:
xlim += ni + 0.5
ni, no = W[nLayers-1].shape
if isOdd(nLayers):
xlim += no + 0.5
else:
ylim += no + 0.5
# Add space for output names
if outputNames:
if isOdd(nLayers):
ylim += 0.25
else:
xlim += round(max(map(len,outputNames))/4.0)
ax = plt.gca()
character_width_factor = 0.07
padding = 2
if inputNames:
x0 = max([1, max(map(len, inputNames)) * (character_width_factor * 3.5)])
else:
x0 = 1
y0 = 0 # to allow for constant input to first layer
# First Layer
if inputNames:
y = 0.55
for n in inputNames:
y += 1
ax.text(x0 - (character_width_factor * padding), y, n, horizontalalignment="right", fontsize=20)
patches = []
for li in range(nLayers):
thisW = W[li]
maxW = np.max(np.abs(thisW))
ni, no = thisW.shape
if not isOdd(li):
# Even layer index. Vertical layer. Origin is upper left.
# Constant input
ax.text(x0-0.2, y0+0.5, '1', fontsize=20)
for i in range(ni):
ax.plot((x0, x0+no-0.5), (y0+i+0.5, y0+i+0.5), color='gray')
# output lines
for i in range(no):
ax.plot((x0+1+i-0.5, x0+1+i-0.5), (y0, y0+ni+1), color='gray')
# cell "bodies"
xs = x0 + np.arange(no) + 0.5
ys = np.array([y0+ni+0.5]*no)
for x, y in zip(xs, ys):
patches.append(pltpatch.RegularPolygon((x, y-0.4), 3, 0.3, 0, color ='#555555'))
# weights
if gray:
colors = np.array(["black", "gray"])[(thisW.flat >= 0)+0]
else:
colors = np.array(["red", "green"])[(thisW.flat >= 0)+0]
xs = np.arange(no) + x0+0.5
ys = np.arange(ni) + y0 + 0.5
coords = np.meshgrid(xs, ys)
for x, y, w, c in zip(coords[0].flat, coords[1].flat,
np.abs(thisW/maxW).flat, colors):
patches.append(pltpatch.Rectangle((x-w/2, y-w/2), w, w, color=c))
y0 += ni + 1
x0 += -1 # shift for next layer's constant input
else:
# Odd layer index. Horizontal layer. Origin is upper left.
# Constant input
ax.text(x0+0.5, y0-0.2, '1', fontsize=20)
# input lines
for i in range(ni):
ax.plot((x0+i+0.5, x0+i+0.5), (y0, y0+no-0.5), color='gray')
# output lines
for i in range(no):
ax.plot((x0, x0+ni+1), (y0+i+0.5, y0+i+0.5), color='gray')
# cell "bodies"
xs = np.array([x0 + ni + 0.5] * no)
ys = y0 + 0.5 + np.arange(no)
for x, y in zip(xs, ys):
patches.append(pltpatch.RegularPolygon((x-0.4, y), 3, 0.3, -np.pi/2, color ='#555555'))
# weights
if gray:
colors = np.array(["black", "gray"])[(thisW.flat >= 0)+0]
else:
colors = np.array(["red", "green"])[(thisW.flat >= 0)+0]
xs = np.arange(ni)+x0 + 0.5
ys = np.arange(no)+y0 + 0.5
coords = np.meshgrid(xs, ys)
for x, y, w, c in zip(coords[0].flat, coords[1].flat,
np.abs(thisW/maxW).flat, colors):
patches.append(pltpatch.Rectangle((x-w/2, y-w/2), w, w, color=c))
x0 += ni + 1
y0 -= 1 # shift to allow for next layer's constant input
collection = pltcoll.PatchCollection(patches, match_original=True)
ax.add_collection(collection)
# Last layer output labels
if outputNames:
if isOdd(nLayers):
x = x0+1.5
for n in outputNames:
x += 1
ax.text(x, y0+0.5, n, fontsize=20)
else:
y = y0+0.6
for n in outputNames:
y += 1
ax.text(x0+0.2, y, n, fontsize=20)
ax.axis([0, xlim, ylim, 0])
ax.axis('off')
######################################################################
### Scaled Conjugate Gradient algorithm from
### "A Scaled Conjugate Gradient Algorithm for Fast Supervised Learning"
### by Martin F. Moller
### Neural Networks, vol. 6, pp. 525-533, 1993
###
### Adapted by Chuck Anderson from the Matlab implementation by Nabney
### as part of the netlab library.
###
### Call as scg() to see example use.
def scg(x, f, gradf, *fargs, **params):
"""scg:
Example:
def parabola(x,xmin,s):
d = x - xmin
return np.dot( np.dot(d.T, s), d)
def parabolaGrad(x,xmin,s):
d = x - xmin
return 2 * np.dot(s, d)
center = np.array([5,5])
S = np.array([[5,4],[4,5]])
firstx = np.array([-1.0,2.0])
r = scg(firstx, parabola, parabolaGrad, center, S,
xPrecision=0.001, nIterations=1000)
print('Optimal: point',r[0],'f',r[1])"""
evalFunc = params.pop("evalFunc",lambda x: "Eval "+str(x))
nIterations = params.pop("nIterations",1000)
xPrecision = params.pop("xPrecision",0)
fPrecision = params.pop("fPrecision",0)
xtracep = params.pop("xtracep",False)
ftracep = params.pop("ftracep",False)
verbose = params.pop("verbose",False)
iterationVariable = params.pop("iterationVariable",None)
### from Nabney's netlab matlab library
nvars = len(x)
sigma0 = 1.0e-6
fold = f(x, *fargs)
fnow = fold
gradnew = gradf(x, *fargs)
gradold = copy(gradnew)
d = -gradnew # Initial search direction.
success = True # Force calculation of directional derivs.
nsuccess = 0 # nsuccess counts number of successes.
beta = 1.0e-6 # Initial scale parameter. Lambda in Moeller.
betamin = 1.0e-15 # Lower bound on scale.
betamax = 1.0e20 # Upper bound on scale.
j = 1 # j counts number of iterations.
if xtracep:
xtrace = np.zeros((nIterations+1,len(x)))
xtrace[0,:] = x
else:
xtrace = None
if ftracep:
ftrace = np.zeros(nIterations+1)
ftrace[0] = fold
else:
ftrace = None
### Main optimization loop.
while j <= nIterations:
# Calculate first and second directional derivatives.
if success:
mu = np.dot(d, gradnew)
if np.isnan(mu): print("mu is NaN")
if mu >= 0:
d = -gradnew
mu = np.dot(d, gradnew)
kappa = np.dot(d, d)
if kappa < floatPrecision or sqrt(kappa) < floatPrecision:
print( kappa)
return {'x':x, 'f':fnow, 'nIterations':j, 'xtrace':xtrace[:j,:] if xtracep else None,
'ftrace':ftrace[:j] if ftracep else None,
'reason':"limit on machine precision"}
sigma = sigma0/sqrt(kappa)
xplus = x + sigma * d
gplus = gradf(xplus, *fargs)
theta = np.dot(d, gplus - gradnew)/sigma
## Increase effective curvature and evaluate step size alpha.
delta = theta + beta * kappa
if np.isnan(delta): print("delta is NaN")
if delta <= 0:
delta = beta * kappa
beta = beta - theta/kappa
alpha = -mu/delta
## Calculate the comparison ratio.
xnew = x + alpha * d
fnew = f(xnew, *fargs)
Delta = 2 * (fnew - fold) / (alpha*mu)
# if np.isnan(Delta):
# pdb.set_trace()
if not np.isnan(Delta) and Delta >= 0:
success = True
nsuccess += 1
x = xnew
fnow = fnew
else:
success = False
fnow = fold
if verbose and j % max(1,ceil(nIterations/10)) == 0:
print("SCG: Iteration",j,"fValue",evalFunc(fnow),"Scale",beta)
if xtracep:
xtrace[j,:] = x
if ftracep:
ftrace[j] = fnow
if success:
## Test for termination
if max(abs(alpha*d)) < xPrecision:
return {'x':x, 'f':fnow, 'nIterations':j, 'xtrace':xtrace[:j,:] if xtracep else None,
'ftrace':ftrace[:j] if ftracep else None,
'reason':"limit on x Precision"}
elif abs(fnew-fold) < fPrecision:
return {'x':x, 'f':fnow, 'nIterations':j, 'xtrace':xtrace[:j,:] if xtracep else None,
'ftrace':ftrace[:j] if ftracep else None,
'reason':"limit on f Precision"}
else:
## Update variables for new position
fold = fnew
gradold = gradnew
gradnew = gradf(x, *fargs)
## If the gradient is zero then we are done.
if np.dot(gradnew, gradnew) == 0:
return {'x':x, 'f':fnow, 'nIterations':j, 'xtrace':xtrace[:j,:] if xtracep else None, 'ftrace':ftrace[:j],
'reason':"zero gradient"}
## Adjust beta according to comparison ratio.
if np.isnan(Delta) or Delta < 0.25:
beta = min(4.0*beta, betamax)
elif Delta > 0.75:
beta = max(0.5*beta, betamin)
## Update search direction using Polak-Ribiere formula, or re-start
## in direction of negative gradient after nparams steps.
if nsuccess == nvars:
d = -gradnew
nsuccess = 0
elif success:
gamma = np.dot(gradold - gradnew, gradnew/mu)
d = gamma * d - gradnew
j += 1
if iterationVariable is not None:
iterationVariable.value = j
## If we get here, then we haven't terminated in the given number of
## iterations.
return {'x':x, 'f':fnow, 'nIterations':j, 'xtrace':xtrace[:j,:] if xtracep else None, 'ftrace':ftrace[:j],
'reason':"did not converge"}
######################################################################
### steepest
def steepest(x, f, gradf, *fargs, **params):
"""steepest:
Example:
def parabola(x,xmin,s):
d = x - xmin
return np.dot( np.dot(d.T, s), d)
def parabolaGrad(x,xmin,s):
d = x - xmin
return 2 * np.dot(s, d)
center = np.array([5,5])
S = np.array([[5,4],[4,5]])
firstx = np.array([-1.0,2.0])
r = steepest(firstx, parabola, parabolaGrad, center, S,
stepsize=0.01,xPrecision=0.001, nIterations=1000)
print('Optimal: point',r[0],'f',r[1])"""
stepsize= params.pop("stepsize",0.1)
evalFunc = params.pop("evalFunc",lambda x: "Eval "+str(x))
nIterations = params.pop("nIterations",1000)
xPrecision = params.pop("xPrecision", 1.e-8) # 1.e-8 is a default value
fPrecision = params.pop("fPrecision", 1.e-8)
xtracep = params.pop("xtracep",False)
ftracep = params.pop("ftracep",False)
xtracep = True
ftracep = True
i = 1
if xtracep:
xtrace = np.zeros((nIterations+1,len(x)))
xtrace[0,:] = x
else:
xtrace = None
oldf = f(x,*fargs)
if ftracep:
ftrace = np.zeros(nIterations+1)
ftrace[0] = f(x,*fargs)
else:
ftrace = None
while i <= nIterations:
g = gradf(x,*fargs)
newx = x - stepsize * g
newf = f(newx,*fargs)
if (i % (nIterations/10)) == 0:
print("Steepest: Iteration",i,"Error",evalFunc(newf))
if xtracep:
xtrace[i,:] = newx
if ftracep:
ftrace[i] = newf
if np.any(newx == np.nan) or newf == np.nan:
raise ValueError("Error: Steepest descent produced newx that is NaN. Stepsize may be too large.")
if np.any(newx==np.inf) or newf==np.inf:
raise ValueError("Error: Steepest descent produced newx that is NaN. Stepsize may be too large.")
if max(abs(newx - x)) < xPrecision:
return {'x':newx, 'f':newf, 'nIterations':i, 'xtrace':xtrace[:i,:], 'ftrace':ftrace[:i],
'reason':"limit on x precision"}
if abs(newf - oldf) < fPrecision:
return {'x':newx, 'f':newf, 'nIterations':i, 'xtrace':xtrace[:i,:], 'ftrace':ftrace[:i],
'reason':"limit on f precision"}
x = newx
oldf = newf
i += 1
return {'x':newx, 'f':newf, 'nIterations':i, 'xtrace':xtrace[:i,:], 'ftrace':ftrace[:i], 'reason':"did not converge"}
if __name__ == "__main__":
def parabola(x,xmin,s):
d = x - xmin
return np.dot( np.dot(d.T, s), d)
def parabolaGrad(x,xmin,s):
d = x - xmin
return 2 * np.dot(s, d)
center = np.array([5,5])
S = np.array([[5,4],[4,5]])
firstx = np.array([-1.0,2.0])
r = scg(firstx, parabola, parabolaGrad, center, S,
xPrecision=0.001, nIterations=1000)
print('Stopped after',r['nIterations'],'iterations. Reason for stopping:',r['reason'])
print('Optimal: point =',r['x'],'f =',r['f'])