Task 1. Fitting regression models In this task, a linear, polynomial, and exponential regression model is created to det

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Task 1. Fitting regression models In this task, a linear, polynomial, and exponential regression model is created to det

Post by answerhappygod »

Task 1 Fitting Regression Models In This Task A Linear Polynomial And Exponential Regression Model Is Created To Det 1
Task 1 Fitting Regression Models In This Task A Linear Polynomial And Exponential Regression Model Is Created To Det 1 (61.47 KiB) Viewed 36 times
Task 1. Fitting regression models In this task, a linear, polynomial, and exponential regression model is created to determine the dependence of the stopping distance and speed of a car. The task uses data collected in the 1920s on stopping distances by cars at different speeds. The stopping distance consists of the braking distance and the distance corresponding to the response time. The original data contains a total of 50 samples and two variables: stopping distance in feet and speed in miles per hour. Convert the stopping distance to meters and the speed to km / h and place the data samples on the graph. In [ ]: # Load data cars into numpy-lists, variable X refers car's velocity and variable Y refers to stopping distance X, Y = np.loadtxt('data/cars.txt', delimiter=', ', unpack=True) # Convert variable X and Y to km/h and meters X = X*1.609344 Y = y*0.3048 # place data samples to graph plt.scatter (X, Y, color="black",s=10, marker='0') plt.title('Car\'s stopping distance with different velocities') plt.xlabel('Velocity (km/h)') plt.ylabel('Stopping distance (m)') plt.show() In machine learning, teaching data is used to teach the model and test data is used to evaluate the performance of the model being taught. In this case, teaching means determining the appropriate function parameters based on the samples of teaching data. Initially, the data is divided into teaching data and test data by the train_test_split() function of the scikit-learn library. The function takes the x-axis score, the y-axis score, and the absolute size of the test data as parameters. 70% of the samples, i.e. 35 samples, are used to teach the model and 30% of the samples, i.e. 15 samples, to test the model. Your task is to evaluate the performance of the linear, polynomial, and exponential regression model using the square of the correlation coefficient and the root mean square error, and to predict the stopping distance of the car from a given speed with different models.

Linear regression model Let's teach linear regression model with teaching data. This can be done with the numpy library functions np.polyfit () and np.poly1d(). Place samples of teaching data on the graph as blue dots and samples of test data as red dots. In [ ] # Divide data to teaching data and testing data X_teaching, X_test, Y_teaching, Y_test = train_test_split(X, Y, test_size=0.3, random_state=0) # Place samples of teaching data as blue dots on the graph and samples of test data as red dots plt.scatter (X_teaching, Y_teaching, color="blue", s=10, marker='0') plt.scatter (X_test, Y_test, color="red",s=10, marker='0') plt.title('Car\'s stopping distance relative to car\'s velocity on linear model') plt.xlabel('Velocity (km/h)') plt.ylabel('Stopping distance (m)') # Optimize Line's parameters k and b with Least squares methdod using polyfit-function from numpy Library # Function uses x-coordinates, y-coordinates and degree of polynomial function as parameters # Line is first degree polynomial function line_parameters = np.polyfit(x_teaching, Y_teaching, 1) # Create Linear model using numpy-Library's polyld function after finding optimal Line parameters linear_model= np.poly1d (line_parameters) # Draw Line on the graph in range [min (X_teaching), max(X_teaching)] X_line = np.linspace(min (X_teaching), max(X_teaching)) Y_line = linear_model(X_line) plt.plot(X_line, Y_line, c='black', linewidth=2) plt.show() def sign(number): www In this function we get sign of equation parameter for printing if number >= 0: return '+' return ' # Print the equation of the fitted Line print('The equation of the fitted line: y = {} x {} {}'.format(round(line_parameters [0],4), sign(line_parameters [1]), abs (round(1 The performance of the fitted linear model is then evaluated using test data. The square of the correlation coefficient and the root mean square error, which are calculated by the following equations, are used as performance measures.

Σ(Y; -f(X;))² SSE i=1 = 1 SST Σα - Y)² i=1 1 MSE = . Σα - f(x))2 n i=1 Implement performance measurement equations above using numpy library functions np.sum(), np.square(), np.mean(). For the mean square error, you also need the number of samples. After getting measurements check for the linear model that you get the values corresponding to the ready-made scikit-learn library functions sm.r2_score() and sm.mean_squared_error(). In [ ]: def mean_square_error(Y,Y_predict): In this function calculate mean square error when you know y-coordinate values for test data and predicted y-coordinate values sample_ammount = Y.size #-------- YOUR CODE HERE # Calculate mean square error (Tip: you need np. sum() and np.square() functions from numpy Library) # Function returns mean square error def square_of_correlation_coefficient (Y, Y_predict): In this function calculate square of the correlation coefficient when you know y-coordinate values for test data and predicted y-coordinate values # YOUR CODE HERE ----- # Calculate SSE (Tip: you need np.sum() and np.square from numpy Library) # Calculate SST (Tip: you need np.sum(), np.square () and np.mean() from numpy Library) # Calculate square of the correlation coefficient # Function returns square of the correlation coefficient print('Square of correlation coefficient for linear model using scikit-learn library\'s function: ', sm.r2_score(Y_test, linear_ print('Mean square error for linear model using scikit-learn library\'s function: , sm.mean_squared_error(Y_test, linear_model() print('\nSquare of correlation coefficient for linear model using function you implemented: ', square_of_correlation_coefficient print('Mean square error for linear model using function you implemented: ', mean_square_error(Y_test, linear_model(X_test))) SSR SST = 1-
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply