Write a function called randomization that takes as input a positive integer n, and returns A, a random n x 1 Numpy array
Question:
Write a function called randomization that takes as input a positive integer n, and returns A, a random n x 1 numpy array.
Below is what I have but it’s not working.
import numpy as np
def randomization(n):
if n>0 and n==int:
A=np.random.random([n, 1])
print(A)
x=int(input("enter a positive number: "))
r=randomization(x)
print(r)
If I run this I get a message saying “local variable ‘A’ referenced before assignment”.
Answers:
First, n == int
will always be false, because n
is not the type int
. Use isinstance(n, int)
instead.
Because of that, A
is never assigned, but then you call print(A)
as if it were assigned.
In addition to what chepner said, np.random.rand expects dimensions as arguments and not a list. That is, you should use A=np.random.rand(n, 1). Note that this returns a uniformly distributed random vector.
Also, your function doesn’t return any value. use – return A at the end.
try using this. Please make sure your indentation is correct:
def operations(h,w):
"""
Takes two inputs, h and w, and makes two Numpy arrays A and B of size
h x w, and returns A, B, and s, the sum of A and B.
Arg:
h - an integer describing the height of A and B
w - an integer describing the width of A and B
Returns (in this order):
A - a randomly-generated h x w Numpy array.
B - a randomly-generated h x w Numpy array.
s - the sum of A and B.
"""
A = np.random.random([h,w])
B = np.random.random([h,w])
s = A + B
return A,B,s
A,B,s = operations(3,4)
assert(A.shape == B.shape == s.shape)*
I think what you are looking for is something like this. You must define your function, set A equal to a random matrix nx1 and then return the value A within you definition.
A = np.random.random([n,1])
return A
hope this helps
Q) Write a function called randomization that takes as input a positive integer n, and returns A, a random n x 1 numpy array.—-
Ans)
def randomization(n):
random_array=np.random.random([n,1])
return random_array
a=randomization(4)
print(a)
Write a function called randomization that takes as input a positive integer n, and returns A, a random n x 1 numpy array.
Below is what I have but it’s not working.
import numpy as np
def randomization(n):
if n>0 and n==int:
A=np.random.random([n, 1])
print(A)
x=int(input("enter a positive number: "))
r=randomization(x)
print(r)
If I run this I get a message saying “local variable ‘A’ referenced before assignment”.
First, n == int
will always be false, because n
is not the type int
. Use isinstance(n, int)
instead.
Because of that, A
is never assigned, but then you call print(A)
as if it were assigned.
In addition to what chepner said, np.random.rand expects dimensions as arguments and not a list. That is, you should use A=np.random.rand(n, 1). Note that this returns a uniformly distributed random vector.
Also, your function doesn’t return any value. use – return A at the end.
try using this. Please make sure your indentation is correct:
def operations(h,w):
"""
Takes two inputs, h and w, and makes two Numpy arrays A and B of size
h x w, and returns A, B, and s, the sum of A and B.
Arg:
h - an integer describing the height of A and B
w - an integer describing the width of A and B
Returns (in this order):
A - a randomly-generated h x w Numpy array.
B - a randomly-generated h x w Numpy array.
s - the sum of A and B.
"""
A = np.random.random([h,w])
B = np.random.random([h,w])
s = A + B
return A,B,s
A,B,s = operations(3,4)
assert(A.shape == B.shape == s.shape)*
I think what you are looking for is something like this. You must define your function, set A equal to a random matrix nx1 and then return the value A within you definition.
A = np.random.random([n,1])
return A
hope this helps
Q) Write a function called randomization that takes as input a positive integer n, and returns A, a random n x 1 numpy array.—-
Ans)
def randomization(n):
random_array=np.random.random([n,1])
return random_array
a=randomization(4)
print(a)