File size: 4,186 Bytes
1aa8890
 
 
6ddbd04
1aa8890
2832d10
6ddbd04
1aa8890
 
 
 
 
 
2832d10
1aa8890
 
2832d10
 
 
6ddbd04
1aa8890
 
2832d10
6ddbd04
1aa8890
 
 
 
 
 
d6dcf5b
1aa8890
2832d10
 
1aa8890
 
 
 
 
6ddbd04
1aa8890
6ddbd04
1aa8890
 
 
 
 
 
d6dcf5b
2832d10
 
 
d6dcf5b
 
2832d10
6ddbd04
 
 
 
 
 
 
 
 
d6dcf5b
 
 
 
2832d10
1aa8890
d6dcf5b
2832d10
 
1aa8890
d6dcf5b
2832d10
d6dcf5b
 
 
1aa8890
2832d10
d6dcf5b
 
 
 
8ff190b
1aa8890
d6dcf5b
1aa8890
6ddbd04
2832d10
1aa8890
6ddbd04
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense, Dropout

data = pd.read_csv('cars_raw.csv')

le = LabelEncoder()
data['Make'] = le.fit_transform(data['Make'])
data['Model'] = le.fit_transform(data['Model'])

data = data[data['Price'] != 'Not Priced']

data["Price"] = data["Price"].str.replace("$", "")
data["Price"] = data["Price"].str.replace(",", "").astype(float)

scaler = MinMaxScaler()
data['Price'] = scaler.fit_transform(data['Price'].values.reshape(-1, 1))

data = data.dropna()

for col in data.select_dtypes(include=['category', 'object']).columns:
    data[col] = le.fit_transform(data[col])

for col in data.select_dtypes(include=['number']).columns:
    data[col] = scaler.fit_transform(data[col].values.reshape(-1, 1))

# Удаление ненужных колонок
data = data.drop(columns=["Mileage", "SellerType", "VIN", "Stock#", "Drivetrain", "SellerName", "ConsumerReviews", "ExteriorStylingRating", "State", "Zipcode", "DealType"])
data_df = pd.DataFrame(data)

X = data.drop('Price', axis=1)
y = data['Price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(X_train.shape[1],)))
model.add(Dropout(0.3))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1))

model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_test, y_test))

# Оценка модели
test_loss = model.evaluate(X_test, y_test)
print(f'Test loss (MSE): {test_loss}')

import gradio as gr

def predict_car_price(Year, Make, Model, FuelType, Engine):
    car_data = {
        "Year": int(Year),
        "Make": Make,
        "Model": Model,
        "FuelType": FuelType,
        "Engine": Engine
    }

    car_data_df = pd.DataFrame([car_data])
    result_data = pd.read_csv('cars_raw.csv')
    result_data_df = pd.DataFrame(result_data)

    needs_df = result_data_df.drop(columns=["Mileage", "SellerType", "VIN", "Stock#", "Drivetrain", "SellerName", "ConsumerReviews", "ExteriorStylingRating", "State", "Zipcode", "DealType", "Used/New", "Price", "ConsumerRating", "SellerRating", "SellerReviews", "StreetName", "ComfortRating", "PerformanceRating", "ValueForMoneyRating", "ReliabilityRating", "ExteriorColor", "InteriorColor", "MinMPG", "MaxMPG", "Transmission", "InteriorDesignRating"])
    needs_df = pd.concat([needs_df, car_data_df], ignore_index=True)

    le = LabelEncoder()
    for col in needs_df.select_dtypes(include=['category', 'object']).columns:
        needs_df[col] = le.fit_transform(needs_df[col])

    scaler = MinMaxScaler()
    for col in needs_df.select_dtypes(include=['number']).columns:
        needs_df[col] = scaler.fit_transform(needs_df[col].values.reshape(-1, 1))

    result_df = needs_df.loc[[0]]

    prediction = model.predict(result_df)

    inverted_prediction = scaler.inverse_transform(
        prediction.reshape(-1, 1)
    )
    predicted_price = inverted_prediction[0][0]

    return predicted_price

iface = gr.Interface(
    fn=predict_car_price,
    inputs=[
        gr.Slider(label="Year", minimum=1990, maximum=2023, step=1),
        gr.Dropdown(label="Make", choices=["Acura", "Alfa Romeo", "Aston Martin", "Audi", "Bentley", "BMW", "Buick", "Cadillac", "Chevrolet", "Chrysler", "Dodge", "Ferrari", "Fiat", "Ford", "Genesis", "GMC", "Honda", "Hyundai", "Infiniti", "Jaguar", "Jeep", "Kia", "Lamborghini", "Land Rover", "Lexus", "Lincoln", "Lotus", "Maserati", "Mazda", "McLaren", "Mercedes-Benz", "Mini", "Mitsubishi", "Nissan", "Oldsmobile", "Peugeot", "Plymouth", "Pontiac", "Porsche", "Ram", "Rolls-Royce", "Saab", "Saturn", "Scion", "smart", "Subaru", "Suzuki", "Tesla", "Toyota", "Volkswagen", "Volvo"]),
        gr.Textbox(label="Model"),
        gr.Dropdown(label="Fuel Type", choices=["Gasoline", "Diesel", "Electric", "Hybrid"]),
        gr.Textbox(label="Engine")
    ],
    outputs="number"
)

iface.launch(share=True)