Only need to write the code for the part "### YOUR CODE HERE" in Step 7 (do not change other codes)

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: 899604
Joined: Mon Aug 02, 2021 8:13 am

Only need to write the code for the part "### YOUR CODE HERE" in Step 7 (do not change other codes)

Post by answerhappygod »

Only need to write the code for the part "### YOUR
CODE HERE" in Step
7 (do not change other codes)
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 1
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 1 (18.94 KiB) Viewed 40 times
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 2
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 2 (26.63 KiB) Viewed 40 times
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 3
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 3 (19.83 KiB) Viewed 40 times
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 4
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 4 (32.1 KiB) Viewed 40 times
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 5
Only Need To Write The Code For The Part Your Code Here In Step 7 Do Not Change Other Codes 5 (13.07 KiB) Viewed 40 times
Step 1 Import the needed libraries. from math import sort from matplotlib import pyplot as plot from random import seed from random import randrange from csv import reader import numpy as np Step 2 Load a CSV file = def load_csv(filename, skip False): dataset = list) # Opens the file in read only mode = with open(filename, 'r') as file: csv_reader = reader(file) # Skip the header, if needed if skip: next(csv_reader, None) for row in csv_reader: dataset.append(row) return dataset
Step 3 Convert string column to float def string_column_to_float(dataset, column): for row in dataset: # The strip() function remove white space # then convert the data into a decimal number (float) # and overwrite the original data row[column] = float(row[column].strip) Step 4 Make Prediction def predict(X, b, W) : return X.dot(W) + b Step 5 Update the weights with the gradients and L2 penality = def update_weights(X, Y, b, W, no_of_training_examples, learning_rate, 12_penality): Y_pred predict(x, b, w) dW = ( - ( 2* ( X.T ).dot(Y Y_pred ) ) + ( 2 * 12_penality W) ) / no_of_training_examples db np. sum(Y Y_pred ) / no_of_training_examples = = 2 W = W b = b learning_rate * dw learning_rate * db return b, W
Step 6 Linear Regression with L2 (Ridge) Regularisation def ridge_regression (X, Y, iterations = 1000, learning_rate no_of_training_examples, no_of_features = X.shape 0.01, 12_penality = 1): W = np.zeros(no_of_features) b = 0 for in range(iterations): b, W = update_weights(X, Y, b, W, no_of_training_examples, learning_rate, 12_penality) return b, W Step 7 Split the data into training and test sets def train_test_split(dataset, split): ### YOUR CODE HERE return X_train, Y_train, X_test, Y_test
Step 8 Perform regression algorithm on dataset def evaluate_ridge_regression (dataset, split): # Spilt the data in training and test sets # And split further in X_train, Y_train, x_test, Y_test X_train, Y_train, X_test, Y_test train_test_split(dataset, split) = # Train the model = 0.001) b, W = ridge_regression (X_train, Y_train, iterations = 10000, 12_penality # Make a prediction with the model 3 yhat = predict(x_test, b, W) print("Predicted values np.round( yhat[:3], 2)) print("Real values Y_test[:3] ) print("Trained w ", round( W[@], 2)) print("Trained b ", round( b, 2)) 2 visualise (X_test, Y_test, yhat) Step 9 Visualise the results def visualise (X_test, Y_test, yhat): plot.scatter( x_test, Y_test, color = 'blue' ) plot.plot( X_test, yhat, color = 'orange' ) plot.title( 'Fertility Rate vs Worker Percentage' ) plot.xlabel('Fertility Rate') plot.ylabel('Worker Percentage') plot.show()
Step 10 Seed the random value seed(1) Step 11 Load and prepare data = filename = 'fertility_rate-worker_percent.csv' dataset = load_csv(filename, skip=True) for i in range(len (dataset[@])): string_column_to_float(dataset, i) Step 12 Evaluate regression algorithm on training dataset split = 0.66 evaluate_ridge_regression (dataset, split)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply