Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please don't copy from previous chegg answers. I am not paying to get answers that you copied from other chegg experts. We will use the

Please don't copy from previous chegg answers. I am not paying to get answers that you copied from other chegg experts.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

We will use the scipy module for the first and matplotlib (see lab 3) for the second. You should be able to install both terminal using pip; e.g.: If you are struggling, you may consider using Anaconda - an all-in-one python install designed for data science. Anaconda includes many common packages, including the ones used here. Fitting Data The curve fitting functionality we are interested in is in scipy's optimize module. We can fit data to a function of our choice as follows: from scipy import optimize params, params_cov =optimize.curve_fit(func, xdata, ydata) optimize.curve_fit returns two collections - the parameters that make func best fit our data, and the covariance of those parameters (which can be ignored for this assignment). An example: from scipy import optimize from matplotlib import pyplot as plt \# linear fitting function \# the first parameter of a fitting funciton must be x \# subsequent parameters are automatically adjusted by curve_fit def lin(x,m,b) : return mx+b \# generate data on y=1x+0, then add some "noise" xdata=[i for i in range(10) ] y data =[i for i in range(10) ] y[1]=2 y[8]=7 \# Find parameters for lin that best fit xdata and ydata params, params_cov = optimize.curve_fit(lin, xdata, ydata) \# unpack the calculated parameters m = params[0] b= params[1] \# create an empty list for the line of best fit y_fit = [] for x in xdata: y_fit.append(lin(x, m,b)) \# plot the raw data and the line of best fit plt.figure() plt.scatter(xdata, ydata) plt.plot(xdata, yfit) plt.show() See the included figure example_data_fitting.png below: Task 1: Fitting py Create three fitting functions: - const - tries to fit with f(x)=c0 - 1 in - tries to fit with f(x)=c1x+c0 - quad - tries to fit with f(x)=c2x2+c1x+c0 Quantifying a Fit We may ask ourselves "how good is that fit?" To answer that, we need to define the error of our fit relative to the provided data. There are many ways to do this, but we will use the "root mean square" method - we will calculate the square root of the mean of the squares of the error at all points. The sum of the squares of the errors (se) is: se=i=0N1(ydata[i]yfit[i])2 The mean (average) square error (mse) is: mse=N1se And the square root of the mean square error (RMS) is: mse Putting it all together: RMS=Ni=0N1(ydata[i]yfit[i])2 For the data above, if our fit line was y=1x+0, then the RMS can be calculated as follows: se=(00)2(12)2(22)2(33)2++(87)2+(99)2=2meansqerr=1012=0.2RMS=0.20.4472 Task 2: Fitting py Write a function named fit_data that takes three inputs: - fitting function - some xdata - some ydata and returns 3 outputs (by returning a 3 -tuple): - parameters of best fit - the root mean square error of that fit - a list containing the y-data of that fit The numbers you get back will likely be very long floats. If we truncated them to integers, your function's return might look something like: \[ \begin{array}{l} \gg>\text { from Fitting import } * \\ \gg>x=[i \text { for i in range(5)] } \\ \gg>y=[\text { for i in range(5)] } \\ \gg>\text { fit_data(lin, } x, y) \\ (\operatorname{array}([1,0]), 0,[0,1,2,3,4]) \end{array} \] Notes: - Do not actually truncate your return values. We did it here for demonstration purposes. - array denotes a special class used in scipy - you do not need to "get rid of it", and it should not impact the rest of this assignment if you treat it like a typical list. Algorithm Timing We will look at sorting algorithms more in-depth in the coming weeks. For now, it is sufficient to know that these algorithms should take a (possibly) unsorted list as input, and modify that list until it is sorted. These algorithms do not need to return anything in Python - when a collection is passed as an argument to a function. any modifications to that collection are persistent outside of the function. Task 3 - TimingPlot.py Generate a series of x (number of items to sort) and y (time to sort) data using the above algorithm. Use randomly sorted lists. For instance, to generate a list of 100 random integers, you might write: import random n=100 L=[ random. randint(,n) for i in range (n)] - Fit that data ( n vs time) with the linear and quadratic functions. Choose n such that you can clearly see the expected shape of the curve. - Generate a figure with three curves: - a scatter plot of the raw data - a line of best fit with l in - a line of best fit with quad - Follow best practices when presenting data: - Scale your data so the axes numbers are between 1 and 1000 - Include axis labels with units - Label each curve clearly, either with a textbox on the figure or a legend - Save your figure as "bestfit.png". We are purposefully giving you flexibility here - how should you generate the lists as n grows? How should you time the functions? You solved a very similar problem in lab; try to apply those techniques here. Note that we will manually grade the final plot, and that you produced the data appropriately. Bad-faith attempts (like arbitrarily generating numbers for your times) will not receive any credit on this assignment. Include all code you use to generate data and the figures. Submission At a minimum, submit the following files on GradeScope under "Assignment 1 - Programming (1/2)" category: - Fitting.py - const () lin() - quad() - fit_data() - TimingPlot.py - bubble_sort() - whatever functions you use to generate timing data

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions

Question

explain the need for human resource strategies in organisations

Answered: 1 week ago

Question

describe the stages involved in human resource planning

Answered: 1 week ago