Extract output from scipy optimise - python-2.7

After minimising a two parameter function in Python using the Powell algorithm, I am left with the following output:
fun: -3.5839582049322310
maxcv: 0.0
message: 'Optimization terminated successfully.'
nfev: 98
status: 1
success: True
x: array([ 1.9116445888705806, 3.7094985019795996])
I wish to extract the two optimised parameters in x to then be carried through to another part of the script. Is there a way to automate the extraction of these numbers without having to manually enter them in the script? I have looked in the scipy documentation but have not found anything on how to access the output.

It would be helpful to see the code you use to calculate your result, now I can only guess what you did.
I assume your code looks like:
res = scipy.optimize.minimize(fun, f0, method='Powell')
This function returns an optimizeResult object. To get the actual result of your optimization you need to call
print(res.x)
The type of res.x is a numpy.ndarray. If you want to proceed only if your calculation was successful, then you can do something like
if res.success:
print('success, the results are')
print(res.x)

Related

Error while passing test data point to sci-kit learn <endpoint>.predict() function created inside AWS-Sagemaker

I created a scikit learn model endpoint inside AWS Sagemaker. I want to make predictions on my test set using this endpoint. My endpoint creation code looks like
predictor = sklearn.deploy(1, 'ml.m4.xlarge')
from sagemaker.predictor import csv_serializer
predictor.content_type = 'text/csv'
predictor.serializer = csv_serializer
predictor.deserializer = None
When I pass my test data point as a list predictor.predict(l) where l is the list, it throws an error
ValueError: Expected 2D array, got 1D array instead:
When I pass it as a numpy array of 2 dimensions(I checked dimensions using .ndim), it still throws the same error. When I tried to pass the data as a string separated by commas without any spaces, it still throws the same error.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
This line gets displayed every time the Valueerror is thrown but even after reshaping, the same error persists.
So I have tried the formats '2,3,4,5', ['2,3,4,5'], array[[2,3,4,5]],[2,3,4,5] but none of these work.
Can someone please convey what the right format is for the input to the predictor function of sci-kit learn?
Are you still having problems with your input dimensions? Users are able to implement their own input_fn in order to help with debugging as well. I suggest you try printing the deserialized input when it is passed to the inference call to see where you might be having trouble with the dimension size.

How to parse a matrix in Python?

I created a matrix of 0s using join(). Assignment works if I hotcode it. If I get as an input, it doesn't work.
theatre=[]
for i in range(5):
theatre.append(["0"]*5)
def print_screen(theatre):
for i in theatre:
print(" ".join(i))
print_screen(theatre)
theatre[int(raw_input("Enter row"))][int(raw_input("Enter col"))]=="x" ## this doesn't work
theatre[0][1]="x" ## This is working.
Oh Boy surely the problem you stated would exist in your code as it is NOT AT ALL A PROBLEM as far as python interpreter is considered.
See what you are doing
theatre[int(raw_input("Enter row"))][int(raw_input("Enter col"))]=="x" ## this doesn't work
is to compare if the value at a given point in the matrix is equal to x or not, so as far as your code goes python is comparing the given point and then just moving on to the next statement.
I would suggest that instead of asking here, it would be more helpful for you if you try to debug your code using print() statement or any debugger.

SegNet results of train set (test via test_segmentation.py)

I run SegNet on my own dataset (by Segnet tutorial). I see great results via test_segmentation.py.
my problem is that I want to see the real net results and not test_segmentation own colorisation (via classes).
for example, if I have trained net with 2 classes, so after the train I will see not only 2 colors (as we see with the classes), but we will see the real net color segmentation ([0.22,0.19,0.3....) lighter and darker as the net see it]
I hope that I explained myself well. thanks for helping.
You could use a python script to achieve what you want. Take a look at this script.
The command out = out['argmax'], extracts the raw output, so you can get a segmentation map with 'lighter and darker' values as you wanted.
When you say the 'real' net color segmentation I will assume that you mean the probability maps. Effectively the last layer will have one map for every class; and if you check the function predict in inference.py, they take the argmax; that is the channel (which represents the class) with the highest probability. If you want to get these maps, you just have to get the data without computing the argmax; something like:
predicted = net.blobs['prob'].data
I solve it. the solution is to range cmin and cmax from 0 to 1 in the scipy saving method. for example: scipy.misc.toimage(output, cmin=0.0, amax=1).save(/path/.../image.png)

How to use nimprof?

In one of my Nim projects I'm having performance issues. I'm now trying to use nimprof to see what's going on. I have an import nimprof in my main source file, and I'm compiling with --profiler:on. When I run the program I can see the messages:
writing profile_results.txt...
... done
However, profile_results.txt only contains this:
total executions of each stack trace:
Entry: 1/1 Calls: 2741/2741 = 1.0e+02% [sum: 2741; 2741/2741 = 1.0e+02%]
The run time was about 1 minute -- so I don't think it is just not enough time to sample anything. Is there any way to get something more meaningful out of nimprof?
You need to add the compiler flag --stackTrace:on, or there won't be any function names or line numbers to analyze.
1.0e+02% is just a silly way to say 100%. It says it took a lot of stack samples and they were all the same, which is not surprising.
What you need is to actually see the sample.
It should appear below the line above. It will show you what the problem is.
Just as an aside, it should show line numbers as well as function names,
and it shouldn't just sort the stacks by frequency.
The reason is there could easily be a guilty line of code that is on a large fraction of stacks, even though the stacks are otherwise different, so if the stacks are sorted, that line will not be aggregated.

Stata seems to be ignoring my starting values in maximum likelihood estimation

I am trying to estimate a maximum likelihood model and it is running into convergence problems in Stata. The actual model is quite complicated, but it converges with no troubles in R when it is supplied with appropriate starting values. I however cannot seem to get Stata to accept the starting values I provide.
I have included a simple example below estimating the mean of a poisson distribution. This is not the actual model I am trying to estimate, but it demonstrates my problem. I set the trace variable, which allows you to see the parameters as Stata searches the likelihood surface.
Although I use init to set a starting value of 0.5, the first iteration still shows that Stata is trying a coefficient of 4.
Why is this? How can I force the estimation procedure to use my starting values?
Thanks!
generate y = rpoisson(4)
capture program drop mypoisson
program define mypoisson
args lnf mu
quietly replace `lnf' = $ML_y1*ln(`mu') - `mu' - lnfactorial($ML_y1)
end
ml model lf mypoisson (mean:y=)
ml init 0.5, copy
ml maximize, iterations(2) trace
Output:
Iteration 0:
Parameter vector:
mean:
_cons
r1 4
Added: Stata doesn't ignore the initial value. If you look at the output of the ml maximize command, the first line in the listing will be titled
initial: log likelihood =
Following the equal sign is the value of the likelihood for the parameter value set in the init statement.
I don't know how the search(off) or search(norescale) solutions affect the subsequent likelihood calculations, so these solution might still be worthwhile.
Original "solutions":
To force a start at your initial value, add the search(off) option to ml maximize:
ml maximize, iterate(2) trace search(off)
You can also force a use of the initial value with search(norescale). See Jeff Pitblado's post at http://www.stata.com/statalist/archive/2006-07/msg00499.html.