#! /usr/bin/env python3 # def homes_model ( ): #*****************************************************************************80 # ## homes_model() seeks the best linear model for home sale prices. # # Modified: # # 31 January 2022 # from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt import numpy as np print ( "home_model:" ) print ( " Find best linear model for home sales." ) print ( " bill = c0 + c1 * area + c2 * rooms + c3 * beds + c4 * baths" ) print ( " + c5 * age + c6 * lot + c7 * tax" ) # # Get the data. # filename = 'homes_data.txt' data = np.loadtxt ( filename ) n, d = np.shape ( data ) print ( " Data from " + filename + " involves n =", n, "items with dimension d =", d ) # # Analyze the data # print ( " Data statistics:" ) print ( " Min = ", np.min ( data, axis = 0 ) ) print ( " Max = ", np.max ( data, axis = 0 ) ) print ( " Range = ", np.max ( data, axis = 0 ) - np.min ( data, axis = 0 ) ) print ( " Mean = ", np.mean ( data, axis = 0 ) ) print ( " Variance = ", np.var ( data, axis = 0 ) ) # # Display a histogram of the asking price data, in data[:,0] # plt.clf ( ) plt.hist ( data[:,0], bins = 15 ) plt.grid ( True ) plt.title ( 'Asking price histogram' ) filename = 'homes_histogram.png' plt.savefig ( filename ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) plt.clf ( ) # # Set up the linear system X * c = y # y = selling = data[1] # X = data[2,3,4,5,6,7,8] # X = np.c_ [ np.ones ( n ), data[:,2:9] ] y = data[:,0] # # Find c using numpy linalg.lstsq() # c4, _, _, _ = np.linalg.lstsq ( X, y, rcond = None ) print ( " C = ", c4 ) r4 = np.dot ( X, c4 ) - y mse4 = np.sum ( r4**2 ) / n print ( " MSE = ", mse4 ) # # Make a predictions: # area = 2000 rooms = 9 beds = 3 baths = 2 age = 50 lot = 0.45 taxes = 2500 y4 = c4[0] + c4[1] * area + c4[2] * rooms + c4[3] * beds + c4[4] * baths\ + c4[5] * age + c4[6] * lot + c4[7] * taxes y_actual = 100000 print ( "" ) print ( " For area = ", area, "rooms = ", rooms, "beds = ", beds ) print ( " baths = ", baths, "age = ", age, "lot = ", lot ) print ( " taxes = ", taxes ) print ( " the model predicts a selling price $", y4 ) print ( " The actual selling price was $", y_actual ) return if ( __name__ == "__main__" ): homes_model ( )