Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pandas
- import numpy as numpy
- import matplotlib.pyplot as plt
- # punkt 4
- def initialize_weights():
- # print("----------------------initialize_weights----------------------------")
- matrix = numpy.random.rand(x.shape[1] + 1, 1)
- return matrix
- # punkt 5
- def insert_coeficient_to_weights_matrix(x):
- # print("-----------------------insert_coeficient_to_weights_matrix---------------------")
- return numpy.insert(x, 0, 1, axis=1)
- # punkt 6
- def predict(x, macierzWspolRegresji):
- # print("----------------------------------predict-----------------------------------------")
- predictions = numpy.dot(x, macierzWspolRegresji)
- return predictions
- # punkt 7a
- def calculate_residual(predictions, df):
- # print("-------------------------------calculate_residual---------------------------------------")
- residuals = numpy.subtract(predictions, df)
- return residuals
- #punkt 7b
- def multiply_residual_by_features_matrix(x, df):
- # print("-------------------------------multiply_residual_by_features_matrix----------------------------------------")
- gradient_vector = numpy.dot(numpy.transpose(x), calc_residuals(prediction, df))
- return gradient_vector
- #punkt 7
- def calculate_gradient(predictions, df, x):
- # print("-------------------------------calculate_gradient----------------------------------------")
- residuals = calculate_residual(predictions, df)
- gradient_vector = multiply_residual_by_features_matrix(x, df)
- n = 35
- gradient_vector = gradient_vector * 1/n
- return gradient_vector
- #punkt 8
- def update_weights(macierzWspolRegresji, gradient_vector):
- # print("-------------------------------update_weights----------------------------------------")
- alfa = 0.0001
- matrix = numpy.subtract(macierzWspolRegresji, (alfa * gradient_vector))
- return matrix
- def learn_and_fit(x, y):
- # print("-------------------------------learn and fit----------------------------------------")
- weights = initialize_weights()
- # print(weights)
- expanded_x = insert_coeficient_to_weights_matrix(x)
- # print(expanded_x)
- # punkt 9
- for i in range(140000):
- predictions = predict(expanded_x, weights)
- # print(predictions)
- gradient_vector = calculate_gradient(predictions, df, expanded_x)
- # print(gradient_vector)
- weights = update_weights(weights, gradient_vector)
- return weights
- # ---------------------------------------------------------------------------------------
- df = pandas.read_csv('Salary.csv')
- x = df.iloc[:,:-2].values
- y = df.iloc[:,-1].values.reshape(df.shape[0], 1)
- obliczoneWspolRegresji = learn_and_fit(x, y)
- x1 = obliczoneWspolRegresji.iloc[:,:-2].values
- y1 = obliczoneWspolRegresji.iloc[:,-1].values.reshape(df.shape[0], 1)
- # punkt 11
- plt.plot(range(35), y, color="green")
- plt.plot(range(35), y1*100, color="red")
- plt.grid(True);
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement