-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (65 loc) · 2.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
import pickle
import numpy as np
model = pickle.load(open('regression_model.pkl', 'rb'))
def process_fuel_type(fuel: str):
"""
Processfuel type input and return a one-hot encoded representation.
"""
fuel_types = {
'Diesel': [1, 0, 0, 0, 0, 0],
'Electric': [0, 1, 0, 0, 0, 0],
'Electric/Diesel': [0, 0, 1, 0, 0, 0],
'Electric/Gasoline': [0, 0, 0, 1, 0, 0],
'Gasoline': [0, 0, 0, 0, 1, 0],
'LPG': [0, 0, 0, 0, 0, 1],
}
return fuel_types.get(fuel, [0, 0, 0, 0, 0, 0])
def process_offer_type(offer_type: str):
"""
Process offer type input and returns one-hot encoded values.
"""
offer_types = {
'Used': [1, 0, 0],
'Pre_registered': [0, 1, 0],
'Employee_car': [0, 0, 1],
}
return offer_types.get(offer_type, [0, 0, 0])
def process_gear_type(gear: str):
"""
Processtransmission gear type and return one-hot encoded values.
"""
gears = {
'Manual': [1, 0],
'Semi_Automatic': [0, 1],
}
return gears.get(gear, [0, 0])
def preprocess_input_data(data):
year = int(data['Year'])
hp = float(data['Horse_power'])
mileage = int(data['mileage'])
fuel = data['Fuel_Type']
offer_type = data['offerType']
gear = data['Transmission']
age = 2024 - year
fuel_encoding = process_fuel_type(fuel)
offer_type_encoding = process_offer_type(offer_type)
gear_encoding = process_gear_type(gear)
features = [
mileage, hp, age,
*fuel_encoding,
*gear_encoding,
*offer_type_encoding
]
return features
def predict_car_value(features):
prediction = model.predict([features])
return round(prediction[0], 2)
def lambda_handler(event, context):
features = preprocess_input_data(event)
predicted_value = predict_car_value(features)
return predicted_value
# if predicted_value > 0:
# return predicted_value
# else:
# return "The car value is below 0"