Question: Create a data file with the name results.txt that contains a copy of the two columns under the titles xn and tn in the table

  1.  Create a data file with the name results.txt that contains aCreate a data file with the name results.txt that contains a copy of the two columns under the titles xn and tn in the table below. (Do not copy these titles and the bottom row of the table to the file.) Then add some code to the file linearRegression.py to call the two functions there to complete the following tasks:

    First fit a linear function to the data stored in file olympic_women.txt, and then print the coefficients to python IDLE shell. (Use print(*). Then plot the data as dots and the linear function you obtained in problem a as a line. Then predict the womens winning time at the 2012 and 2016 Olympic games and print them to python IDLE shell.

import numpy as np

import matplotlib.pyplot as plt

#This function fits a linear function to the data. The data

#is stored in matrix variable f where the 1st column is

#attribute values and the 2nd column is target values. The

#function returns the co-efficients

def linear(f):

s = np.size(f[:,0]) #number of sample observations

x = f[:,0] #attribute values: a row vector

t = f[:,1] #target values: a row vector

u = np.ones((1,s)) #a row vector of all 1s

u = np.vstack((u,x))#the transpose of the data matrix shown in the class

v = np.linalg.inv(u@u.T) #v = (uu')^-1

w = v@u@t #w = vut

return(w)

#This function plots the linear function with coefficient vector w

#against the attribue values. It also plots the data as the dots.

#The data is stored in matrix variable f where the 1st column is

#attribute values and the 2nd column is target values.

def plotit(w, f):

s = np.size(f[:,0])#These five lines are the same as in linear(f)

x = f[:,0]

t = f[:,1]

u = np.ones((1,s))

u = np.vstack((u,x))

t1 = w@u

plt.plot(x,t,'o', x,t1)

plt.xlabel('years')

plt.ylabel('winning times')

plt.xlim(1920,2020)

plt.show()

777788 3333333333333333444|3 00-8 68020006001855420722 21 90 64 81 48 94 60 89 48 30 94 98 64 53 53 36 40 8887 3 222222222222222222|2 24238-2 891972 82 826 82 87 n-1 2 3 4 5 6 7 8 9 0123456789 777788 3333333333333333444|3 00-8 68020006001855420722 21 90 64 81 48 94 60 89 48 30 94 98 64 53 53 36 40 8887 3 222222222222222222|2 24238-2 891972 82 826 82 87 n-1 2 3 4 5 6 7 8 9 0123456789

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To solve this problem follow these steps Step 1 Create resultstxt Extract the columns xn and tn from ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!