avram48 commited on
Commit
d6dcf5b
·
verified ·
1 Parent(s): 10de213

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -6
app.py CHANGED
@@ -5,7 +5,6 @@ from sklearn.preprocessing import LabelEncoder, MinMaxScaler
5
  from sklearn.model_selection import train_test_split
6
  from keras.models import Sequential
7
  from keras.layers import Dense, Dropout
8
- import gradio as gr
9
 
10
  data = pd.read_csv('cars_raw.csv')
11
 
@@ -29,6 +28,7 @@ for col in data.select_dtypes(include=['category', 'object']).columns:
29
  for col in data.select_dtypes(include=['number']).columns:
30
  data[col] = scaler.fit_transform(data[col].values.reshape(-1, 1))
31
 
 
32
  data = data.drop(columns=["Mileage", "SellerType", "VIN", "Stock#", "Drivetrain", "SellerName", "ConsumerReviews", "ExteriorStylingRating", "State", "Zipcode", "DealType"])
33
  data_df = pd.DataFrame(data)
34
 
@@ -46,9 +46,12 @@ model.add(Dense(1))
46
  model.compile(optimizer='adam', loss='mean_squared_error')
47
  model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_test, y_test))
48
 
 
49
  test_loss = model.evaluate(X_test, y_test)
50
  print(f'Test loss (MSE): {test_loss}')
51
 
 
 
52
  def predict_car_price(Year, Make, Model, FuelType, Engine):
53
  car_data = {
54
  "Year": int(Year),
@@ -59,21 +62,30 @@ def predict_car_price(Year, Make, Model, FuelType, Engine):
59
  }
60
 
61
  car_data_df = pd.DataFrame([car_data])
62
- needs_df = data.drop(columns=["Price"]).copy()
 
 
 
63
  needs_df = pd.concat([needs_df, car_data_df], ignore_index=True)
64
 
 
65
  for col in needs_df.select_dtypes(include=['category', 'object']).columns:
66
  needs_df[col] = le.fit_transform(needs_df[col])
67
 
 
68
  for col in needs_df.select_dtypes(include=['number']).columns:
69
- needs_df[col] = scaler.transform(needs_df[col].values.reshape(-1, 1))
 
 
70
 
71
- result_df = needs_df.iloc[-1].values.reshape(1, -1)
72
  prediction = model.predict(result_df)
73
- inverted_prediction = scaler.inverse_transform(prediction.reshape(-1, 1))
 
 
 
74
  predicted_price = inverted_prediction[0][0]
75
 
76
- return round(predicted_price, 2)
77
 
78
  iface = gr.Interface(
79
  fn=predict_car_price,
 
5
  from sklearn.model_selection import train_test_split
6
  from keras.models import Sequential
7
  from keras.layers import Dense, Dropout
 
8
 
9
  data = pd.read_csv('cars_raw.csv')
10
 
 
28
  for col in data.select_dtypes(include=['number']).columns:
29
  data[col] = scaler.fit_transform(data[col].values.reshape(-1, 1))
30
 
31
+ # Удаление ненужных колонок
32
  data = data.drop(columns=["Mileage", "SellerType", "VIN", "Stock#", "Drivetrain", "SellerName", "ConsumerReviews", "ExteriorStylingRating", "State", "Zipcode", "DealType"])
33
  data_df = pd.DataFrame(data)
34
 
 
46
  model.compile(optimizer='adam', loss='mean_squared_error')
47
  model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_test, y_test))
48
 
49
+ # Оценка модели
50
  test_loss = model.evaluate(X_test, y_test)
51
  print(f'Test loss (MSE): {test_loss}')
52
 
53
+ import gradio as gr
54
+
55
  def predict_car_price(Year, Make, Model, FuelType, Engine):
56
  car_data = {
57
  "Year": int(Year),
 
62
  }
63
 
64
  car_data_df = pd.DataFrame([car_data])
65
+ result_data = pd.read_csv('cars_raw.csv')
66
+ result_data_df = pd.DataFrame(result_data)
67
+
68
+ 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"])
69
  needs_df = pd.concat([needs_df, car_data_df], ignore_index=True)
70
 
71
+ le = LabelEncoder()
72
  for col in needs_df.select_dtypes(include=['category', 'object']).columns:
73
  needs_df[col] = le.fit_transform(needs_df[col])
74
 
75
+ scaler = MinMaxScaler()
76
  for col in needs_df.select_dtypes(include=['number']).columns:
77
+ needs_df[col] = scaler.fit_transform(needs_df[col].values.reshape(-1, 1))
78
+
79
+ result_df = needs_df.loc[[0]]
80
 
 
81
  prediction = model.predict(result_df)
82
+
83
+ inverted_prediction = scaler.inverse_transform(
84
+ prediction.reshape(-1, 1)
85
+ )
86
  predicted_price = inverted_prediction[0][0]
87
 
88
+ return predicted_price
89
 
90
  iface = gr.Interface(
91
  fn=predict_car_price,