Spaces:
Sleeping
Sleeping
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) |