Question

In: Computer Science

Write a function hitRate(A, B) to return the percentage of elements in array A that match...

Write a function hitRate(A, B) to return the percentage of elements in array A that match the elements with the same index in array B. A and B are 1darrays or 2darrays of the same shape. Sample: if X = np.array([1, 3, 5, 8]); Y = np.array([2, 1, 3, 8]), then hitRate(X, Y) returns 0.25. in python program

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for Question . Be safe .

Note : check attached image for output and indentation . code compiled and tested in spyder python 3.

Program Plan :

  1. inside hit rate function
  2. first check numpy array is 2d or 1d using arrayName.ndim
  3. if array is 2d than run two loop and one outer loop and inner loop .
  4. when we traverse each element in array increment length so we after loop we have number of element in 2d array .
  5. inside inner loop if element of A and B same increment count
  6. now we have total element in 2d array and number of elements same in array (for same index)
  7. now calculate percentage = number of elements same*100/total elements

If Array is 1 dimensional :

  1. get length of array in length variable
  2. now run a loop from first element to last element of 1d array
  3. if A[i]==B[i] same increment count
  4. after loop ends
  5. calculate percentage = number of element same *100 /length of 1d array

Program :

import numpy #import numpy array

#function takes two numpy array A and B
#both are 2d or 1d
def hitRate(A,B):
    dim=A.ndim #get number of dimensional
    count=0   #count number of element same
    length=0 #total element
    if dim==2: #if array is 2D
        #run loop for first to last column
        for i in range(len(A)):
            for j in range(len(A[i])): #run a loop from 0 to last column element
                length+=1 #increment number of elements
                if A[i][j]==B[i][j]: #if both element are same
                    count+=1 #increment count
    elif dim==1:#if array is 1D
        length=len(A)#get length of array
        #from 1st to last element
        for i in range(len(A)):
            if A[i]==B[i]: #if element same
                count+=1 #increment count
    #calculate percentage
    percentage=count*100/length
    return percentage #return
      
      
      
if __name__=="__main__":
    arr = numpy.array([[1, 3, 5,8],[3,4,5,8]])
    arr1 =numpy.array([[3,4,5,2],[1, 3, 5,8]])
    print("Hit percentage : ",hitRate(arr, arr1))
  
    arr2 = numpy.array([1, 3, 5,8])
    arr3 =numpy.array([3,4,5,8])
    print("Hit Percentage : ",hitRate(arr2, arr3))
           

Output :

Program :

Please up vote ,comment if any query .


Related Solutions

Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT