Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 684625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:50:14+00:00 2026-05-14T01:50:14+00:00

I’m trying to vectorize a simple function in numpy and getting inconsistent behavior. I

  • 0

I’m trying to vectorize a simple function in numpy and getting inconsistent behavior. I expect my code to return 0 for values < 0.5 and the unchanged value otherwise. Strangely, different runs of the script from the command line yield varying results: sometimes it works correctly, and sometimes I get all 0’s. It doesn’t matter which of the three lines I use for the case when d <= T. It does seem to be correlated with whether the first value to be returned is 0. Any ideas? Thanks.

import numpy as np

def my_func(d, T=0.5):
    if d > T:   return d
    #if d <= T:  return 0
    else:  return 0
    #return 0

N = 4
A = np.random.uniform(size=N**2)
A.shape = (N,N)
print A
f = np.vectorize(my_func)
print f(A)

$ python x.py
[[ 0.86913815  0.96833127  0.54539153  0.46184594]
 [ 0.46550903  0.24645558  0.26988519  0.0959257 ]
 [ 0.73356391  0.69363161  0.57222389  0.98214089]
 [ 0.15789303  0.06803493  0.01601389  0.04735725]]
[[ 0.86913815  0.96833127  0.54539153  0.        ]
 [ 0.          0.          0.          0.        ]
 [ 0.73356391  0.69363161  0.57222389  0.98214089]
 [ 0.          0.          0.          0.        ]]
$ python x.py
[[ 0.37127366  0.77935622  0.74392301  0.92626644]
 [ 0.61639086  0.32584431  0.12345342  0.17392298]
 [ 0.03679475  0.00536863  0.60936931  0.12761859]
 [ 0.49091897  0.21261635  0.37063752  0.23578082]]
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:50:15+00:00Added an answer on May 14, 2026 at 1:50 am

    If this really is the problem you want to solve, then there’s a much better solution:

    A[A<=0.5] = 0.0
    

    The problem with your code, however, is that if the condition passes, you are returning the integer 0, not the float 0.0. From the documentation:

    The data type of the output of vectorized is determined by calling the function with the first element of the input. This can be avoided by specifying the otypes argument.

    So when the very first entry is <0.5, it tries to create an integer, not float, array.
    You should change return 0 to

    return 0.0
    

    Alternately, if you don’t want to touch my_func, you can use

    f = np.vectorize(my_func, otypes=[np.float])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 358k
  • Answers 358k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, you are not down casting. You are just casting,… May 14, 2026 at 2:14 pm
  • Editorial Team
    Editorial Team added an answer CAS is generally much faster than locking, but it does… May 14, 2026 at 2:14 pm
  • Editorial Team
    Editorial Team added an answer If you look at the linker settings you can change… May 14, 2026 at 2:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.