File size: 2,754 Bytes
5512da3
 
 
 
 
 
 
 
 
 
 
 
 
 
aa8b168
 
5512da3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab80adf
 
 
 
aa8b168
5512da3
 
 
 
 
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
# import argparse
import os
import pandas as pd
import time
import mlflow
from mlflow.models.signature import infer_signature
from sklearn.model_selection import train_test_split 

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Définir l'URI de suivi MLflow
mlflow.set_tracking_uri("http://0.0.0.0:5000")

if __name__ == "__main__":


    os.environ["APP_URI"] = "https://zacbl-getaround.hf.space"

    # Set your variables for your environment
    EXPERIMENT_NAME="Car_Price_Prediction"

    print("training model...")
    
    # Time execution
    start_time = time.time()

    # Call mlflow autolog
    mlflow.sklearn.autolog() 

    with mlflow.start_run() as run:

        # Import dataset
        df = pd.read_csv("https://full-stack-assets.s3.eu-west-3.amazonaws.com/Deployment/get_around_pricing_project.csv", sep=";")


        # Split the data into train and test :
        X = df.drop(columns = 'rental_price_per_day', axis = 1)
        y = df['rental_price_per_day']

        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)

        # Define the numerical and categorical columns :
        num_cols = X.select_dtypes(include = ['int64', 'float64']).columns
        cat_cols = X.select_dtypes(include = ['object', 'bool']).columns
        # Train / test split 
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)


        # Define the pipeline :
        num_pipeline = Pipeline([
            ('scaler', StandardScaler())
        ])

        full_pipeline = ColumnTransformer([
            ('num', num_pipeline, num_cols),
            ('cat', OneHotEncoder(), cat_cols)
        ])

        # Make a pipeline containing the full pipeline and the model :
        model = Pipeline([
            ('preprocessing', full_pipeline),
            ('model', LinearRegression())
        ])

        # Fit the model :
        model.fit(X_train, y_train)

        # Predict the price :
        predictions = model.predict(X_test)

        # Calculate the mean squared error :
        mean_squared_error(y_test, predictions)
        # Print r2 score :
        model.score(X_test, y_test)

        # Enregistrer le run_id dans un fichier
        run_id = run.info.run_id
        with open("run_id.txt", "w") as f:
            f.write(run_id)

    print("...Done!")
    print("Saving model...")
    mlflow.sklearn.log_model(model, "model", signature=infer_signature(X_train, predictions))
    print("...Model saved!")
    print(f"---Total training time: {time.time()-start_time}")