assignment 2

pdf

School

Stevens Institute Of Technology *

*We aren’t endorsed by this school

Course

MIS637

Subject

Economics

Date

Feb 20, 2024

Type

pdf

Pages

5

Uploaded by MajorEnergyOryx33

Report
In [1]: In [2]: Out[2]: Avg. Area Income Avg. Area House Age Avg. Area Number of Rooms Avg. Area Number of Bedrooms Area Population Price Addr 0 79545.45857 5.682861 7.009188 4.09 23086.80050 1.059034e+06 208 Michael Ferry A 674\nLaurabury, 370 1 79248.64245 6.002900 6.730821 3.09 40173.07217 1.505891e+06 188 Johnson Vie Suite 079\nL Kathleen, C 2 61287.06718 5.865890 8.512727 5.13 36882.15940 1.058988e+06 9127 Elizab Stravenue\nDanielto WI 0648 3 63345.24005 7.188236 5.586729 3.26 34310.24283 1.260617e+06 USS Barnett\nFPO 44 4 59982.19723 5.040555 7.839388 4.23 26354.10947 6.309435e+05 USNS Raymond\nF AE 09 ... ... ... ... ... ... ... 4995 60567.94414 7.830362 6.137356 3.46 22837.36103 1.060194e+06 USNS Williams\nF AP 30153-7 4996 78491.27543 6.999135 6.576763 4.02 25616.11549 1.482618e+06 PSC 9258, B 8489\nAPO 42991-3 4997 63390.68689 7.250591 4.805081 2.13 33266.14549 1.030730e+06 4215 Tracy Gar Suite 076\nJoshuala VA 0 4998 68001.33124 5.534388 7.130144 5.44 42625.62016 1.198657e+06 USS Wallace\nFPO 73 4999 65510.58180 5.992305 6.792336 4.07 46501.28380 1.298950e+06 37778 George Rid Apt. 509\nEast H NV 5000 rows × 7 columns import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import numpy as np from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_absolute_error, mean_squared_error df = pd.read_csv( 'USA_Housing.csv' ) df assignment_1 (1) - Jupyter Notebook http://localhost:8888/notebooks/Downloads/assignment_1%20(1).ipynb# 1 of 5 19-12-2023, 22:43
In [3]: In [4]: In [5]: In [6]: Out[4]: Index(['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms ', 'Avg. Area Number of Bedrooms', 'Area Population', 'Price', 'Address '], dtype='object') sns.pairplot(df) plt.show() df.columns features = df.drop([ 'Price' , 'Address' ], axis = 1 ) # Dropping 'Price' and 'Addr target = df[ 'Price' ] X_train, X_test, y_train, y_test = train_test_split(features, target, test_siz assignment_1 (1) - Jupyter Notebook http://localhost:8888/notebooks/Downloads/assignment_1%20(1).ipynb# 2 of 5 19-12-2023, 22:43
In [7]: In [8]: X_train shape: (4000, 5) X_test shape: (1000, 5) y_train shape: (4000,) y_test shape: (1000,) Coefficients: [2.16522058e+01 1.64666481e+05 1.19624012e+05 2.44037761e+03 1.52703134e+01] Intercept: -2635072.900916779 Mean Squared Error: 10089009299.49942 print ( "X_train shape:" , X_train.shape) print ( "X_test shape:" , X_test.shape) print ( "y_train shape:" , y_train.shape) print ( "y_test shape:" , y_test.shape) model = LinearRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) print ( 'Coefficients:' , model.coef_) print ( 'Intercept:' , model.intercept_) mse = mean_squared_error(y_test, y_pred) print ( 'Mean Squared Error:' , mse) assignment_1 (1) - Jupyter Notebook http://localhost:8888/notebooks/Downloads/assignment_1%20(1).ipynb# 3 of 5 19-12-2023, 22:43
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
In [9]: In [10]: Mean Error (ME): 349.89660601592436 Mean Squared Error (MSE): 10089009299.49942 Root Mean Squared Error (RMSE): 100444.0605486428 # Plot the histogram of residuals residuals = y_test - y_pred plt.figure(figsize = ( 8 , 6 )) sns.histplot(residuals, bins = 20 , kde = True , color = 'skyblue' ) plt.title( 'Residual Distribution Histogram' ) plt.xlabel( 'Residuals' ) plt.ylabel( 'Frequency' ) plt.show() mean_error = np.mean(y_test - y_pred) mean_squared_error = mean_squared_error(y_test, y_pred) root_mean_squared_error = np.sqrt(mse) print ( "Mean Error (ME):" , mean_error) print ( "Mean Squared Error (MSE):" , mean_squared_error) print ( "Root Mean Squared Error (RMSE):" , root_mean_squared_error) assignment_1 (1) - Jupyter Notebook http://localhost:8888/notebooks/Downloads/assignment_1%20(1).ipynb# 4 of 5 19-12-2023, 22:43
assignment_1 (1) - Jupyter Notebook http://localhost:8888/notebooks/Downloads/assignment_1%20(1).ipynb# 5 of 5 19-12-2023, 22:43