Question

In: Computer Science

Write a function which takes in X and Y arrays, a column number, and a threshold....

Write a function which takes in X and Y arrays, a column number, and a threshold. The function should return arrays X0 and Y0 containing all rows where the value in the specified column falls strictly below the threshold, as well as arrays X1 and Y1 containing all rows where the value in the specified column is above or equal to the threshold.(use numpy)

def split_on_feature(X_test, Y_test, column, thresh):
## TYPE ANSWER HERE

Solutions

Expert Solution

Used a non-standard library (Numpy), please install, if not installed. It allows for faster numerical computations in Python.

Code Snippet (Python): Please make sure that the unpacking of the arguments (return statement) is correctly done.

import numpy as np

def split_on_feature(x_test,y_test,column,thresh):
  x_0 = x_test[np.where(x_test[:,column] < thresh)]
  y_0 = y_test[np.where(y_test[:,column] < thresh)]
  x_1 =  x_test[np.where(x_test[:,column] >= thresh)]
  y_1 = y_test[np.where(y_test[:,column] >= thresh)]

  
  return x_0,y_0,x_1,y_1

Sample code with use of the above function :

import numpy as np
A = 1
B = 100
N = 4
x_test = (A + np.random.random((N,N)) * (B - A)).astype(int)
y_test = (A + np.random.random((N,N)) * (B - A)).astype(int)

print("X_test: ")
print(x_test)
print()
print("Y_test: ")
print(y_test)

def split_on_feature(x_test,y_test,column,thresh):
  x_0 = x_test[np.where(x_test[:,column] < thresh)]
  y_0 = y_test[np.where(y_test[:,column] < thresh)]
  x_1 =  x_test[np.where(x_test[:,column] >= thresh)]
  y_1 = y_test[np.where(y_test[:,column] >= thresh)]

  
  return x_0,y_0,x_1,y_1

threshold = int(np.random.random()*100)
x_0,y_0,x_1,y_1 = split_on_feature(x_test,y_test,3,threshold)
print("Threshold : " + str(threshold))
print()
print("X_0: ")
print(x_0)
print()
print("Y_0 : ")
print(y_0)
print()
print("X_1: ")
print(x_1)
print()
print("Y_1: ")
print(y_1)


Output:


Related Solutions

Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the product of two prime numbers ? and ? and returns the tuple ( ?, ? ) where ?≤? . Example: ????????(22)=(2,11) Example: ????????(3605282209)=(59447,60647) This problem has a time-out limit of 1 second and a memory limit of 1MB. The number ? in all test-cases will satisfy 4≤?≤800000000000000 For example: Test Result print(factorMe(22)) (2, 11) print(factorMe(3605282209)) (59447, 60647)
Write a function fun(x) that takes as input a positive number x and solves the following...
Write a function fun(x) that takes as input a positive number x and solves the following equation for y and returns y. The equation is 10^4y=x+3. Could you help me with this by using python? I can use loop or if statement for this question.
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a LISP function COUNTX which takes an atom and a list and returns the number...
Write a LISP function COUNTX which takes an atom and a list and returns the number of top-level occurrences of the atom in the list. For example: (COUNTX ‘A ‘(A (A B) B A B A (B A)) Returns the value 3, the other two A’s are not at the top level
Write a function script DirCos.m that takes a vector (any row or column array) as the...
Write a function script DirCos.m that takes a vector (any row or column array) as the argument and returns the direction cosines for that vector. This is for a MatLab script
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ with explanations
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
1.1 Write a python in Jupiter notebook function called trng that takes three numbers x, y,...
1.1 Write a python in Jupiter notebook function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangle otherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT