Question

How to rewrite the following functions using only numpy array operations (universal functions, aggregations, boolean indexing)...

How to rewrite the following functions using only numpy array operations (universal functions, aggregations, boolean indexing) without using loops

def mean_squared_error (v , p ):

n = len ( v [0])

result = 0

for i in range ( n ):

result += ( v [0][ i ] - p [0])**2 + ( v [1][ i ] - p [1])**2

result = result / n

return result

I tried doing it like this but nothing printed out

def mean_squared_error2 (v, p ):

v=np.array(v)
n = v.shape[1]
result = np.sum((v[0,:]- p[0])**2 + (v[1,:] - p[1])**2)
print(result)
return result/n

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
How to rewrite the following functions using only numpy array operations (universal functions, aggregations, boolean indexing)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3 Compute Euclidean distance using Numpy Arrays • The Euclidean distance d is given by the...

    3 Compute Euclidean distance using Numpy Arrays • The Euclidean distance d is given by the following equation: N d(a,b) = (a - b)2 Complete the following Euclidean distance function with two parameters of Numpy arrays • Hint: you may use np.sqrt and np.sum to compute the two Numpy arrays [73]: def euclidean_distance(a,b): return 0 Test your Euclidean distance function using two Numpy arrays [74]: A = np.array(range(100)) B - np array(range(1, 101)) print (euclidean_distance (A,B)) 0 [ ]:

  • I'm being asked to compute the probablity density at (0,0) using the above KDE function. i finish...

    I'm being asked to compute the probablity density at (0,0) using the above KDE function. i finished the previous related part. Not entirely sure of the syntax needed to enter the equation using NumPy. Code for previous related part for reference: Exercise 3 Based on the KDE function: Compute the probablity density at [e,e], i.e., f((0, 0)). This should return a scalar value. (2 points) : def compute (data, h): ##CODE HERE## return x i We turn our attention to...

  • I'm trying to model the velocity over time of a given time interval in python, but...

    I'm trying to model the velocity over time of a given time interval in python, but my code won't work. I need to solve the problem using the Euler method. import numpy as np from matplotlib import pyplot as plt def car(x0, v0, Cd, p, A, m, facc, T, dt): n = 0 t = np.arange(0, T, dt) x = np.array([x0]) v = np.array([v0]) while x[n] > 0: x = np.append(x, x[n]+v[n]*dt) v = np.append(v, v[n]+(facc/m-(1/(2*m))*Cd*A*p*v[n])*dt) t = np.append(t, t[n]+dt)...

  • Rewrite the following loops without using the enhanced for loop construct. Here, values is an array...

    Rewrite the following loops without using the enhanced for loop construct. Here, values is an array of floating-point numbers. a. for (double x : values) { total = total + x; } b. for (double x : values) { if (x == target) { return true; } } c. int i = 0; for (double x : values) { values[i] = 2 * x; i++; }

  • 1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n,...

    1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n, p): """Return a list of the run lengths in n tosses of a coin whose heads probability is p. Arguments: n--Number of tosses (a positive int), p--The probability of observing heads for any given toss (float between 0 and 1). """ For example, if the simulated sequence of coin tosses is HTTHHTHHHTTHHTTTTH, then the list of run lengths the function returns will be [1,...

  • # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sV...

    # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sVal + str(tree.getRootVal()) sVal = sVal + printexp(tree.getRightChild())+')' return sVal #### Functions and classes to help with Homework 6 #### You should not make any changes to the functions and classes in this file, #### but you should understand what they do and how they work. import sys ### This function will be re-defined in hw6.py. However, it is needed for ExpTree, ###...

  • 2. Consider the following functions. For each of them, determine how many times is ‘hey’ printed...

    2. Consider the following functions. For each of them, determine how many times is ‘hey’ printed in terms of the input n. You should first write down a recurrence and then solve it using the recursion tree method. That means you should write down the first few levels of the recursion tree, specify the pattern, and then solve. (a) def fun(n) { if (n > 1) { print( ‘hi’ ‘hi’ ‘hi’ ) fun(n/4) fun(n/4) fun(n/4) }} (b) def fun(n) {...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the...

    PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the map referred to is pictured in the screenshot. Need help with these 4 tasks: Function 4: def is_map(map) : given a map (see screenshot), does it meet the following criteria? 1) it is a list of lists of values of type int; 2) it has at least one row and one column; 3) it’s rectangular (all sub-lists are same length); 4) only non-negative ints...

  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT