This commit is contained in:
Krzysztof Raczyński 2024-05-21 19:47:48 +02:00
parent 234be6c972
commit 75a3a6e6c7
4 changed files with 87 additions and 0 deletions

12
mlflow/MLProject Normal file
View File

@ -0,0 +1,12 @@
name: Car Price Prediction
conda_env: conda.yaml
entry_points:
main:
parameters:
epochs: {type: int, default: 20}
batch_size: {type: int, default: 32}
command: "python mlflow_model.py {epochs} {batch_size}"
predict:
command: "python mlflow_predict.py"

13
mlflow/conda.yaml Normal file
View File

@ -0,0 +1,13 @@
name: car_price_env
channels:
- default
dependencies:
- python=3.8
- pip:
- pip
- pandas
- numpy
- scikit-learn
- tensorflow
- mlflow
- h5py

39
mlflow/mlflow_model.py Normal file
View File

@ -0,0 +1,39 @@
import mlflow
import mlflow.keras
import pandas as pd
import numpy as np
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from sklearn.preprocessing import MinMaxScaler
import sys
# Parameters from the command line
epochs = int(sys.argv[1])
batch_size = int(sys.argv[2])
mlflow.start_run()
train_data = pd.read_csv('./data/car_prices_train.csv')
train_data.dropna(inplace=True)
y_train = train_data['sellingprice'].astype(np.float32)
X_train = train_data[['year', 'condition', 'transmission']]
scaler_x = MinMaxScaler()
X_train['condition'] = scaler_x.fit_transform(X_train[['condition']])
scaler_y = MinMaxScaler()
y_train = scaler_y.fit_transform(y_train.values.reshape(-1, 1))
X_train = pd.get_dummies(X_train, columns=['transmission'])
model = Sequential([Dense(64, activation='relu'), Dense(32, activation='relu'), Dense(1)])
model.compile(optimizer='adam', loss='mean_squared_error')
# Training the model with MLflow tracking
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size)
mlflow.keras.log_model(model, "model")
mlflow.log_param("epochs", epochs)
mlflow.log_param("batch_size", batch_size)
mlflow.end_run()

23
mlflow/mlflow_predict.py Normal file
View File

@ -0,0 +1,23 @@
import mlflow.keras
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
model = mlflow.keras.load_model("model")
test_data = pd.read_csv('./data/car_prices_test.csv')
test_data.dropna(inplace=True)
y_test = test_data['sellingprice'].astype(np.float32)
X_test = test_data[['year', 'condition', 'transmission']]
scaler_y = MinMaxScaler()
scaler_y.fit(y_test.values.reshape(-1, 1))
scaler_X = MinMaxScaler()
X_test['condition'] = scaler_X.fit_transform(X_test[['condition']])
X_test = pd.get_dummies(X_test, columns=['transmission'])
y_pred_scaled = model.predict(X_test)
y_pred = scaler_y.inverse_transform(y_pred_scaled)
y_pred_df = pd.DataFrame(y_pred, columns=['PredictedSellingPrice'])
y_pred_df.to_csv('predicted_selling_prices.csv', index=False)