Question

In: Computer Science

Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2,...

Write PYTHON CODE to answer the following question:

Consider the following data:

x = [0, 2, 4, 6, 9, 11, 12, 15, 17, 19]
y = [5, 6, 7, 6, 9, 8, 8, 10, 12, 12]

Using Python, use least-squares regression to fit a straight line to the given data. Along with the slope and intercept, compute the standard error of the estimate and the correlation coefficient.

Best fit equation y = ___ + ___ x

Standard error, Sy/x = ___

Correlation coefficient, r = ___

Solutions

Expert Solution

ANSWER:

I have provided the properly commented and indented code so you can easily copy the code as well as check for correct indentation.
I have provided the output image of the code so you can easily cross-check for the correct output of the code.
Have a nice and healthy day!!

CODE

# Using scipy.optimize.curve_fit for least-squares fit to straight line
# importing module
from scipy.optimize import curve_fit
# import numpy
import numpy as np

# defining data points
x = np.array([0, 2, 4, 6, 9, 11, 12, 15, 17, 19])
y = np.array([5, 6, 7, 6, 9, 8, 8, 10, 12, 12])

# defining straight line eqn function with data points x, m=slope, c = intercept
# Straight line eqn => y = mx + c
f = lambda x,m,c: m*x + c

# using curve_fit function to least-squares fit on defined straight line function
poptimized, pcov = curve_fit(f,x,y)

# fetching slope(m) and intercept from optimized parameters
m,c = poptimized

# displaying best fit eqn
print("Best fit equation y = {:.3f} + {:.3f}(x)".format(m,c))

# calculating error
# predicting y from coefficents
y_pred = f(x,m,c)
# calculating standard error between y and y_pred
# standard error = mean of absolute(y-y_pred)
std_err = np.abs(y-y_pred).mean()

# displaying result
print("Standard error, Sy/x = {:.3f}".format(std_err))

# Correlation coefficient, using corrcoef method numpy to find the same
r = np.corrcoef(y,y_pred)
# displaying result
print("Correlation coefficient, r =\n",r)

OUTPUT IMAGE


Related Solutions

Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Answer the following bootstrap question by showing the R code : A set of data X...
Answer the following bootstrap question by showing the R code : A set of data X contains the following numbers: 119.7 104.1 92.8 85.4 108.6 93.4 67.1 88.4 101.0 97.2 95.4 77.2 100.0 114.2 150.3 102.3 105.8 107.5 0.9 94.1 We generated n = 20 observations Xi = 10 Wi+100, where Wi has a contaminated normal distribution with proportion of contamination 20% and σc = 4. Suppose we are interested in testing: H0 : μ = 90 versus H1 :...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
Use the Data Below to answer the following question below 0 0 2 0 5 3...
Use the Data Below to answer the following question below 0 0 2 0 5 3 1 12 0 0 0 1 6 0 1 1 2 8 1 3 1 6 2 4 0 16 17 0 8 0 3 0 0 1 2 5 2 0 2 1 5 0 7 0 1 0 0 1 0 0 3 1 9 4 1 3 0 1 1 1 0 7 1 9 2 0 1 1 1 1...
Use the data set below to answer the question. x −2 −1 0 1 2 y...
Use the data set below to answer the question. x −2 −1 0 1 2 y 2 2 4 5 5 Find a 90% prediction interval for some value of y to be observed in the future when x = −1. (Round your answers to three decimal places.)
The statements in this question are based on the following data: X 2:6 2:6 3:2 3:0...
The statements in this question are based on the following data: X 2:6 2:6 3:2 3:0 2:4 3:7 3:7 PX D 21:2 Y 5:6 5:1 5:4 5:0 4:0 5:0 5:2 PY D 35:3The correlation coefficient .r/ was calculated as 0:327: Identify the incorrect statement. 1. There is a positive relationship between x and y. 2. N y D 5:043 3. The coefficient of determination is 0:5719: 4. The regression coefficient b1 is also positive. 5. Only 10.7% of the variation...
PLEASE USE PYTHON CODE Compute the zero of the function y(x) from the following data: x...
PLEASE USE PYTHON CODE Compute the zero of the function y(x) from the following data: x = 0.2, 0.4, 0.6, 0.8, 1.0 y = 1.150, 0.855, 0.377, -0.266, -1.049 Use inverse interpolation with the natural cubic spline
Use python programming to write this code and provide a screen short for the code. 2....
Use python programming to write this code and provide a screen short for the code. 2. Write a function that takes one argument (a string) and returns a string consisting of the single character from that string with the largest value. Your function should contain a for loop. You can assume that the input to your function will always be a valid string consisting of at least one character. You can assume that the string will consist only of lower-case...
Consider the IVP x' = t^2 +x^2, x(0) = 1. Complete the following table for the...
Consider the IVP x' = t^2 +x^2, x(0) = 1. Complete the following table for the numerical solutions of given IVP with step-size h = 0.05. t - x by Euler’s Method - x by Improved Euler’s Method 0 -    1 - 1 0.05 - …….    - ……... 0.1 -    ……. - ……..
Question Consider the function ?(?) = ??2 and x = 0, 0.25, 0.5, 1. Then use...
Question Consider the function ?(?) = ??2 and x = 0, 0.25, 0.5, 1. Then use the suitable Newton interpolating polynomial to approximate f(0.75). Also, compute an error bound for your approximation Dont use a sheet to solve thanks numerical methods
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT