Task 1. Fitting regression models In this task, a linear, polynomial, and exponential regression model is created to det
Posted: Mon Jun 06, 2022 1:24 pm
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-