What is a more efficient way to change a value in a numpy array?

  • Context: Comp Sci 
  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Array Value
Click For Summary

Discussion Overview

The discussion revolves around improving a function designed to change values in a numpy array based on certain conditions. Participants explore issues with the initial implementation and suggest ways to enhance the function's efficiency and correctness.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about an error message encountered in their function and seeks assistance.
  • Another participant suggests that understanding the error message could provide insights into the problem.
  • A different participant notes that the working code provided does not encapsulate the logic within a function and encourages the original poster to return a value from the function.
  • It is pointed out that the function's parameter should be a numpy array, but an ordinary Python list could also be used without significant issues.
  • One participant identifies two main issues with the original function: redefining the input array within the function and using a numpy array in a range function, which expects an integer.
  • Another participant elaborates on the previous points by suggesting a revised approach to defining the function, including changing the parameter name for clarity and using the length of the array for iteration.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct the function's implementation, but there is no consensus on a single solution or approach to achieve the desired functionality.

Contextual Notes

Limitations include the initial misunderstanding of the error messages and the specific requirements for the function's parameters. The discussion does not resolve the overall approach to modifying the numpy array efficiently.

ver_mathstats
Messages
258
Reaction score
21
Homework Statement
Write a Python code to create the function array_change(a, new_val) that has two arguments:
a that is a NumPy array whose entries are numbers, and
new_val which is a number. The function returns a new array after changing every occurrence of entries in
a, that are in absolute value strictly less than 1, to new_val.
Relevant Equations
Python
Code:
import numpy as np

def array_change(a,new_val):
    a=np.array([])
    for i in range(a):
        if abs(i)<1:
            a[i]=new_val

e=np.arange(-2, 2, 0.2).reshape(4,5)
print(array_change(e,0))

I am not sure where I am going wrong exactly but I keep getting an error message.
I came up with a code that gives me the results I am looking for but it is not a function.

Code:
e=np.arange(-2, 2, 0.2).reshape(4,5)
c=abs(e)<1
e[c]=0
print(e)

Any help is appreciated. Thank you.
 
Physics news on Phys.org
What does the error message say? That might give you a hint. That is what they are for.
 
You have some code that works. Can you figure a way to put that into a function? Remember that a function usually returns a value.
 
ver_mathstats said:
I am not sure where I am going wrong exactly but I keep getting an error message.
If your code produces an error message, it's always a good idea to tell anyone helping what that error message says. A typical Python error message will give the type of error (e.g., Syntax Error, or Name Error) and will show you what line the error occurs on.
 
  • Like
Likes   Reactions: FactChecker
Aside from the program requirements (one parameter of the function must be a Numpy array), I don't see any other real reason why an ordinary Python list type can't be used.
 
I see at least two things wrong:

(1) a is the input array, which comes from outside the function. You should not be re-defining a within the function.

(2) The statement "for i in range(a)" requires that a be an integer. But a is a numpy array, not an integer. So this will fail right away.

Try fixing these two things and thinking a bit more about what you are trying to do. Then come back and tell us how you are doing.
 
To elaborate on the points that @phyzguy made, instead of doing this:
Python:
def array_change(a,new_val):
    a=np.array([])
      for i in range(a):
try this:
Python:
def array_change(arr, new_val):  
    arr_size = len(arr)
    for i in range(arr_size):
Note that I changed the name of the array parameter. Single letter variable names are discouraged in most programming languages, except possibly for loop control variables such as i, j, k, and so on.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
7
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K