0% found this document useful (0 votes)
93 views4 pages

5 Exercise One Hot Encoding - Ipynb - Colaboratory

The document reads in car price data from a CSV file and performs some data cleaning and preprocessing. It separates the car models into dummy variables, drops unnecessary columns, splits the data into features and target, and fits a linear regression model to predict sell price based on mileage and age. The model scores with 94% accuracy. It then uses the model to predict prices for specific cars based on their mileage and age.

Uploaded by

duryodhan sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views4 pages

5 Exercise One Hot Encoding - Ipynb - Colaboratory

The document reads in car price data from a CSV file and performs some data cleaning and preprocessing. It separates the car models into dummy variables, drops unnecessary columns, splits the data into features and target, and fits a linear regression model to predict sell price based on mileage and age. The model scores with 94% accuracy. It then uses the model to predict prices for specific cars based on their mileage and age.

Uploaded by

duryodhan sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

import 

pandas as pd
df = pd.read_csv("carprices.csv")
df

Car Model Mileage Sell Price($) Age(yrs)

0 BMW X5 69000 18000 6

1 BMW X5 35000 34000 3

2 BMW X5 57000 26100 5

3 BMW X5 22500 40000 2

4 BMW X5 46000 31500 4

5 Audi A5 59000 29400 5

6 Audi A5 52000 32000 5

7 Audi A5 72000 19300 6

8 Audi A5 91000 12000 8

9 Mercedez Benz C class 67000 22000 6

10 Mercedez Benz C class 83000 20000 7

11 Mercedez Benz C class 79000 21000 7

12 Mercedez Benz C class 59000 33000 5

dummies = pd.get_dummies(df['Car Model'])
dummies
Audi A5 BMW X5 Mercedez Benz C class

0 0 1 0
merged = pd.concat([df,dummies],axis='columns')

merged
1 0 1 0

2 0 1 0
Car Model Mileage Sell Price($) Age(yrs) Audi A5 BMW X5 Mercedez
3 0 1 0
0 BMW X5 69000 18000 6 0 1
4 0 1 0
1 BMW X5 35000 34000 3 0 1
5 1 0 0
2 BMW X5 57000 26100 5 0 1
6 1 0 0
3 BMW X5 22500 40000 2 0 1
7 1 0 0
4 BMW X5 46000 31500 4 0 1
8 1 0 0
5 Audi A5 59000 29400 5 1 0
9 0 0 1
6 Audi A5 52000 32000 5 1 0
10 0 0 1
7 Audi A5 72000 19300 6 1 0
11 0 0 1
8 Audi A5 91000 12000 8 1 0
12 0 0 1
9 Mercedez Benz C class 67000 22000 6 0 0

10 Mercedez Benz C class 83000 20000 7 0 0

11 Mercedez Benz C class 79000 21000 7 0 0

12 Mercedez Benz C class 59000 33000 5 0 0

final = merged.drop(["Car Model","Mercedez Benz C class"],axis='columns')

final

Mileage Sell Price($) Age(yrs) Audi A5 BMW X5

0 69000 18000 6 0 1
X = final.drop('Sell Price($)',axis='columns')

1 35000 34000 3 0 1
X

2 57000 26100 5 0 1
Mileage Age(yrs) Audi A5 BMW X5
3 22500 40000 2 0 1
0 69000 6 0 1
4 46000 31500 4 0 1
1 35000 3 0 1
5 59000 29400 5 1 0
2 57000 5 0 1
6 52000 32000 5 1 0
3 22500 2 0 1
7 72000 19300 6 1 0
4 46000 4 0 1
8 91000 12000 8 1 0
5 59000 5 1 0
9 67000 22000 6 0 0
6 52000 5 1 0
10 83000 20000 7 0 0
7 72000 6 1 0
11 79000 21000 7 0 0
8 91000 8 1 0
12 59000 33000 5 0 0
9 67000 6 0 0

10 83000 7 0 0

11 79000 7 0 0

12 59000 5 0 0

y = final['Sell Price($)']

0 18000

1 34000

2 26100

3 40000

4 31500

5 29400

6 32000

7 19300

8 12000

9 22000

10 20000

11 21000

12 33000

Name: Sell Price($), dtype: int64

from sklearn.linear_model import LinearRegression

model = LinearRegression()

model.fit(X,y)

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

model.score(X,y)

0.94170509372810818

Price of mercedez benz that is 4 yr old with mileage 45000

model.predict([[45000,4,0,0]])

array([ 36991.31721061])

Price of BMW X5 that is 7 yr old with mileage 86000

model.predict([[86000,7,0,1]])

array([ 11080.74313219])

Colab paid products


-
Cancel contracts here

You might also like