using rand MethodError: no method matching isless(::Array{Float64,1}, ::Float64) - if-statement

If I run the following code,
A = 0.2
if rand(1)<0.85
println(A)
end
the error is
MethodError: no method matching isless(::Array{Float64,1}, ::Float64)
Closest candidates are:
isless(!Matched::Missing, ::Any) at missing.jl:87
isless(!Matched::Float64, ::Float64) at float.jl:465
isless(!Matched::AbstractFloat, ::AbstractFloat) at operators.jl:165
I know that this is a simple code. But no idea why Julia is throwing this error. Please help.

You want if rand()<0.85. rand(1) generates a 1 element Vector of Float64. rand() generates a Float64.

Related

Python method not running

I have a method play_lottery() that I am trying to run in my script.
The method is called at the base of the script but nothing appears to be executing.
I have put the print 10 line in debugging and I cannot even see execution reaching this point.
Any ideas what is going on here?
This is Python 2.7.10 running on OSX.
def play_lottery():
print 10
num = 0
range = 150000000
while num < range:
yield
print num
num += 1
play_lottery()
Calling your generator function creates a generator object. You never actually request anything from the generator though, so it isn't run.
You need to request values from the generator:
import itertools
lot_gen = play_lottery()
# Take the first 3
vals = list(itertools.islice(lot_gen, 3))
This will at least cause your generator to run.
And you don't need to use islice necessarily. You just need to request values from the generator by some means.
What you have created is the python generator since yield keyword is used in side function. Generators in python are memory friendly. Generators won't execute unless you call next method on generator until StopIteration
Here you code looks fine. All you have to do is either of the following. First take the generator into variable
play_lottery_gen = play_lottery()
Now you can retrieve the next value using next method
play_lottery_gen.next()
Or, you can also built in method next to retrieve the next value
next(play_lottery_gen)
You can convert generator into the list by simply passing it into the list factory function. But it is memory inefficient
list(play_lottery_gen)

RcppShark Random Forest example throws exception about the random number generator

I followed this example:
https://www.2021.ai/randsharkmachinelearning/
when running this command on R:
sharkFit <- SharkRFTrain(X, Y, nTrees = 100)
I get:
Error in SharkRFTrain(X, Y, nTrees = 100) :
Should not call this. Fix the random numbers generator if you need this. 478
This exception is thrown from this "cpp" line:
trainer.train(model, trainData);
I suspect the reason is written here:
https://github.com/aydindemircioglu/RcppShark#notes
The random number generator was replaced, as R packages must use the random generator from R and not the C/C++ internal one. Thereffore, a direct comparison of results of algorithms that depend on (pseudo) random numbers cannot be done.
Is there a way to mitigate this?
tried both R versions: 3.4.1, 3.3.2
I have investigated this and was able to narrow it down to some degree:
The error message is produced here: https://github.com/aydindemircioglu/RcppShark/blob/master/src/shark/Rng/Runif.h#L71
The calling code is here: https://github.com/aydindemircioglu/RcppShark/blob/master/src/src/Algorithms/RFTrainer.cpp#L178
Since I was not able to fix it, I have opened an issue here: https://github.com/aydindemircioglu/RcppShark/issues/1

Assertion sv_count !=0 failed - Function train_auto, SVM type - EPS_SVR

The question is related to the OpenCV library, version 2.4.13.2.
I am using n dimensional feature vectors from images for training and performing regression. The output values range between 0 and 255.
The function CvSVM::train works without an error, but requires a manual setting of parameters. So, I would prefer using the function CvSVM::train_auto to perform cross validation and determine the best parameters for the situation.
But I am facing the error:
OpenCV Error: Assertion failed (sv_count != 0) in CvSVM::do_train.
On changing the type to NU_SVR, it works well. The problem is only with type EPS_SVR.
I would appreciate any help I could receive to fix this.
EDIT: I was able to pinpoint the problem to line Number 1786 in the file-
opencv-master\sources\modules\ml\src\svm.cpp
FOR_IN_GRID(p, p_grid)
Upon commenting it, the code runs without errors. I am unaware of the reasons possible.
Facing the same bug. Found out that this bug was caused by svm.setP(x) and svm.setTermCriteria((cv2.TERM_CRITERIA_EPS, y)) where x and y values more than 0.1 (10^-1).

OverflowError in a for loop

I'm working on problem 3 of Project Euler using Python, but I can't seem to solve the problem without running into the following error: "OverflowError: range() result has too many items"
I'm wondering if there's a way to increase the allowed range? My code looks as follows:
target = 600851475143
largest_prime_factor = 1
#find largest prime factor of target
for possible_factor in range(2,(target/2)+1):
if target % possible_factor == 0:
is_prime = True
for i in range(2,(possible_factor/2)+1):
if possible_factor % i == 0:
is_prime = False
break
if is_prime:
largest_prime_factor = possible_factor
print largest_prime_factor
If you run into limitations of your computer or language while trying to solve a puzzle problem, or if it takes too long, it is an indication that probably there exists a better way (read: algorithm) to solve the problem. In your case, you do not need to loop to target / 2 + 1 (though that is a good educated upper bound). You only need to go as far as ceil(sqrt(target)).
And, as a sidenote, you can overcome this limitation by using xrange, which will create a generator, instead of range for Python 2, which creates a list. In Python 3, range will return a sequence type instead of a list by default.
Thanks to #Fernando for the clarification in the comments.

vision.internal.disparityParser in MATLAB

I am working with the computer Vision toolbox in MATLAB 2014b
there is a function for Semi-global Matching (SGM )
I am trying to generate a disparity map of a stereo images. However, the disparity range needs to be quite large for some experiments.
Here is the function call:
Dmap = disparity(I1 I2, 'BlockSize', 15, 'DisparityRange', [-2466, 2466]);
The problem is the DisparityRange is limited to be in the range of [-2464, 2464]. Thus, I am getting an error msg like this one bellow.
Error using disparity
The value of 'DisparityRange' is invalid. Expected DisparityRange to be an array with all of the values >
-2466.
Error in vision.internal.disparityParser (line 38)
parser.parse(varargin{:});
Error in disparity>parseOptionalInputs (line 264)
r = vision.internal.disparityParser(imageSize, getDefaultParameters(),...
Error in disparity>parseInputs (line 244)
r = parseOptionalInputs(imageSize, varargin{:});
Error in disparity (line 137)
r = parseInputs(I1, I2, varargin{:});
My questions:
1. I could not find the function (vision.internal.disparityParser). Where is should be located.
2. I would like to modify the code to work for rainges beyond the specified limit. Is that possible?
3. For anyone who worked with the C++ version of the SGM function (OpenCV), does the same problem exist (i.e., the disparity range limits).
Thank you!
:)
I could only answer the first question. The function vision.internal.disparityParser is located at $MATLAB/toolbox/vision/vision/+vision/+internal/disparityParser.m .