|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created on Tue Mar 13 10:27:11 2018 |
| 5 | +
|
| 6 | +@author: bh387886 |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +import pandas as pd |
| 11 | +import numpy as np |
| 12 | +import matplotlib.pyplot as plt |
| 13 | +import sklearn |
| 14 | +from sklearn.linear_model import LinearRegression |
| 15 | + |
| 16 | +headers = ['cylinders','displacement','horsepower','weight' |
| 17 | + ,'acceleration','model year','origin'] |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +raw = open('auto-mpg.data.txt') |
| 22 | + |
| 23 | +mpg = [] #Dependent variable : mpg |
| 24 | +features = [] #Independent variables: rest of them except car name |
| 25 | + |
| 26 | + |
| 27 | +#Preparing the data |
| 28 | +for line in raw: |
| 29 | + i=0 |
| 30 | + var = [] |
| 31 | + for y in line.split(): |
| 32 | + if (i==0): |
| 33 | + mpg.append([float(y)]) |
| 34 | + if (i<8 and i>0): |
| 35 | + if(y=='?'): # Handaling missing values for horsepower |
| 36 | + y = 0 # (Setting them to 0) |
| 37 | + if(i==4): |
| 38 | + var.append(float(y.replace('.',''))) |
| 39 | + else: |
| 40 | + var.append(float(y)) |
| 41 | + i+=1 |
| 42 | + features.append(var) |
| 43 | + |
| 44 | + |
| 45 | +df_x = pd.DataFrame.from_records(features, columns = headers) |
| 46 | +df_y = pd.DataFrame.from_records(mpg, columns = ['mpg']) |
| 47 | + |
| 48 | +#replacing 0(missing) horsepower with average horsepower |
| 49 | +avg_bhp = (np.average(df_x['horsepower'])*len(df_x))/(len(df_x)-6) |
| 50 | +df_x['horsepower'] = df_x['horsepower'].replace(0,avg_bhp) |
| 51 | + |
| 52 | +lm = LinearRegression() |
| 53 | +lm1 = LinearRegression() |
| 54 | + |
| 55 | +X_train, X_test, Y_train, Y_test = sklearn.model_selection.train_test_split( |
| 56 | + df_x,df_y,test_size=0.2,random_state = 5) |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +######################## USING ALL INDEPENDENT VARS ########################### |
| 61 | + |
| 62 | +lm.fit(X_train, Y_train) |
| 63 | + |
| 64 | +pred_test = lm.predict(X_test) |
| 65 | + |
| 66 | +print('coefficients :(all vars)') |
| 67 | +print(lm.coef_) |
| 68 | + |
| 69 | +print('Intercept :(all vars)') |
| 70 | +print(lm.intercept_) |
| 71 | + |
| 72 | +print('Accuracy Score(all vars): %s' % lm.score(X_test,Y_test)) |
| 73 | + |
| 74 | +residues = (Y_test - pred_test) |
| 75 | + |
| 76 | +#print('Residues: ') |
| 77 | +#print((residues)) |
| 78 | + |
| 79 | +############################################################################### |
| 80 | + |
| 81 | +print('####################################################') |
| 82 | + |
| 83 | +##################### USING ALL ONE OF THE VAR(horse power) ################### |
| 84 | + |
| 85 | +df_new1 = df_x['horsepower'] |
| 86 | + |
| 87 | +df_new = df_new1 [:, np.newaxis] |
| 88 | + |
| 89 | +X_train1, X_test1, Y_train1, Y_test1 = sklearn.model_selection.train_test_split( |
| 90 | + df_new,df_y,test_size=0.2,random_state = 5) |
| 91 | + |
| 92 | + |
| 93 | +lm1.fit(X_train1, Y_train1) |
| 94 | + |
| 95 | +pred_test1 = lm1.predict(X_test1) |
| 96 | + |
| 97 | +print('coefficients :(single var)') |
| 98 | +print(lm1.coef_) |
| 99 | + |
| 100 | +print('Intercept :(single var)') |
| 101 | +print(lm1.intercept_) |
| 102 | + |
| 103 | +print('Accuracy Score(single vars): %s' % lm1.score(X_test1,Y_test1)) |
| 104 | + |
| 105 | +residues = (Y_test1 - pred_test1) |
| 106 | + |
| 107 | +#print('Residues: ') |
| 108 | +#print((residues)) |
| 109 | + |
| 110 | + |
| 111 | +plt.scatter(X_test1, Y_test1, color='black') |
| 112 | +plt.plot(X_test1, pred_test1, color='blue', linewidth=3) |
| 113 | + |
| 114 | +############################################################################### |
| 115 | + |
0 commit comments