Write a function called getnparray. Your function should have three integers as…
- User
Write a function called getnparray. Your function should have three integers as parameters, a, b, and c. Your function should return a numpy array with a elements, each equal to a, with b rows and c columns. If its not possible to create such numpy array, return the integer -1.
- Assistant
import numpy as np
def getnparray(a, b, c): if b * c != a: return -1 else: return np.full((b, c), a)
- User
Write a function called getnparray. Your function should have three integers as parameters, a, b, and c. Your function should return a numpy array with a elements, each equal to a, with b rows and c columns. If its not possible to create such numpy array, return the integer -1.
For example:
Test Result print(getnparray(10,2,5)) [[10. 10. 10. 10. 10.] [10. 10. 10. 10. 10.]]
- Assistant
import numpy as np
def getnparray(a, b, c): if b * c != a: return -1 else: return np.full((b, c), a)
print(getnparray(10,2,5))
- User
Write a function called euclidean. It should have two numpy arrays as paramenters, x and y. Your function should return the Euclidean distance between them.
If x=[1, 2, 3, 4] and y= [5, 6, 7, 8] To calculate the Euclidean distance you should first calculate the element-wise subtraction x-y. Then, put each element of this result to the power of 2. Then, add each of the results. Then, calculate the square root of this sum.
If for whatever reason this cannot be calculate, return -1.
Hint:
The square root of a number a is the same as a to the power of 0.5
For example:
Test Result import numpy as np print(euclidean(np.array([1,2,3,4]), np.array([5,6,7,8]))) 8.0
- Assistant
import numpy as np
def euclidean(x, y): try: # Element-wise subtraction and square diff_sq = np.square(x - y)
code# Sum of squared differences sum_diff_sq = np.sum(diff_sq) # Square root of the sum euclidean_dist = np.sqrt(sum_diff_sq) return euclidean_dist except: return -1import numpy as np print(euclidean(np.array([1,2,3,4]), np.array([5,6,7,8])))
- User
Write a function called euclidean2. It should receive two 2-d numpy arrays as parameters (x and y). Your function should calculate the Euclidean distance between the rows in x and the rows in y, so that if x and y have 10 rows each then there are 10 distances. Your function should return a numpy array with these distances.
Hint: The numpy array method sum() can receive 1 parameter. This parameter should be 0 to sum over the rows, and 1 to sum over the columns.
- Assistant
import numpy as np
def euclidean2(x, y): try: # Calculate element-wise squared differences diff_sq = np.square(x - y)
code# Sum over columns to get squared differences for each row sum_diff_sq = np.sum(diff_sq, axis=1) # Calculate square root of sum for each row euclidean_dist = np.sqrt(sum_diff_sq) return euclidean_dist except: return -1x = np.array([[1,2,3,4], [5,6,7,8]]) y = np.array([[9,10,11,12], [13,14,15,16]])
print(euclidean2(x, y))
- User
Write a function called evenindex. It should receive one 1-d numpy array as parameter. Your function should return a numpy array containing the elements of the parameters that are in an even index.
- Assistant
import numpy as np
def evenindex(arr): return arr[::2]
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) print(evenindex(arr))