OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Error when using scipy brute: object of type 'slice' has no len()

  • Thread starter Thread starter elscan
  • Start date Start date
E

elscan

Guest
I am posting this question-answer since this detail was subtle and I only happened on the solution because of a particular way somebody defined their slice object in a Stack Overflow answer that might not come up if someone searches for this particular error.

My original code:

Code:
from scipy.optimize import brute

f = lambda x : x
ranges = ( slice(-10,10,1) )
result = brute( f, ranges, full_output=True, finish=optimize.fmin )
print( result[1] )

The error, as in the title of the question:

Code:
TypeError: object of type 'slice' has no len()

The new code simply contains one more comma in the tuple defining ranges

Code:
from scipy.optimize import brute

f = lambda x : x
ranges = ( slice(-10,10,1), )
result = brute( f, ranges, full_output=True, finish=optimize.fmin )
print( result[1] )

The output:

Code:
-6.33825300114115e+29

And the answer to why the output was much less than -10 is answered in a different Stack Overflow post.
<p>I am posting this question-answer since this detail was subtle and I only happened on the solution because of a particular way somebody defined their slice object in <a href="https://stackoverflow.com/questions/39236863/restrict-scipy-optimize-minimize-to-integer-values">a Stack Overflow answer</a> that might not come up if someone searches for this particular error.</p>
<p>My original code:</p>
<pre class="lang-py prettyprint-override"><code>from scipy.optimize import brute

f = lambda x : x
ranges = ( slice(-10,10,1) )
result = brute( f, ranges, full_output=True, finish=optimize.fmin )
print( result[1] )
</code></pre>
<p>The error, as in the title of the question:</p>
<pre><code>TypeError: object of type 'slice' has no len()
</code></pre>
<p>The new code simply contains one more comma in the tuple defining ranges</p>
<pre class="lang-py prettyprint-override"><code>from scipy.optimize import brute

f = lambda x : x
ranges = ( slice(-10,10,1), )
result = brute( f, ranges, full_output=True, finish=optimize.fmin )
print( result[1] )
</code></pre>
<p>The output:</p>
<pre><code>-6.33825300114115e+29
</code></pre>
<p>And the answer to why the output was much less than -10 is answered in a <a href="https://stackoverflow.com/questions...get-the-minimizer-outside-the-specified-range">different Stack Overflow post</a>.</p>
 
Top