<jupyter_start><jupyter_text>This notebooks walks you through the steps done to predict King-County housing prices using linear regression with the Vectice MLflow integration  Install and import the required packages<jupyter_code>!pip install -q vectice[github]
!pip install -q fsspec
!pip install -q gcsfs 
!pip install -q MLflow

import logging
from math import sqrt
from vectice import Vectice
from vectice.models import JobType
import os
import mlflow
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from vectice.models.model import ModelType<jupyter_output><empty_output><jupyter_text>Connect to your vectice project<jupyter_code># Project token from Vectice UI
PROJECT_TOKEN = "<PASSWORD>"
# The API Endpoint and Vectice API 
os.environ['VECTICE_API_ENDPOINT'] ='https://beta.vectice.com'
os.environ['VECTICE_API_TOKEN'] = '<PASSWORD>'
# Your Google Cloud Storage json
# Can be downloaded from the tutorial page (readerKey.json)
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'Path to the GCS service account'<jupyter_output><empty_output><jupyter_text>Getting the data from GCS<jupyter_code># Get data from GCS Storage bucket
df = pd.read_csv("gs://vectice_tutorial/kc_house_data.csv") 
df.head()<jupyter_output><empty_output><jupyter_text>Creating the inputs of our run<jupyter_code>def create_inputs():
    return [
        # With this line we declare a reference to code existing in GitHub as the code at the origin of the outputs
        Vectice.create_code_version_with_github_uri("https://github.com/vectice/vectice-examples", "Notebooks/MLflow/KC_housing_prices/KC_housing_prices-Jupyter.ipynb"),
        ## create_dataset_version creates an input or output for the run, you link an existing dataset in the vectice backend via "with_parent_name()"
        Vectice.create_dataset_version().with_parent_name("kc_house_data"),
        Vectice.create_dataset_version().with_parent_name("kc_house_data_cleaned")
    ]<jupyter_output><empty_output><jupyter_text>Data preparation<jupyter_code>def prepare_data():
    """Read and prepare data."""
    df = pd.read_csv("gs://vectice_tutorial/kc_house_data.csv") 

    df = df.drop(["id", "date"], axis=1)

    X = df.drop("price", axis=1)
    y = df["price"]

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test<jupyter_output><empty_output><jupyter_text>Modeling<jupyter_code>vectice = Vectice(project_token=PROJECT_TOKEN)
X_train, X_test, y_train, y_test = prepare_data()

"""Vectice MLflow adapter sync-up usage."""
# Set autolog to silent to reduce output
mlflow.autolog(silent=True)

## Setting up the MLflow tracking uri
mlflow.set_tracking_uri("https://mlflow-beta.vectice.com")
MLFLOW_EXPERIMENT_NAME = "LINEAR REGRESSION"

mlflow.set_experiment(MLFLOW_EXPERIMENT_NAME)
# Use MLflow as usual
with mlflow.start_run() as run:
    reg = LinearRegression().fit(X_train, y_train)

    y_pred = reg.predict(X_test)

    MAE = mean_absolute_error(y_pred, y_test)
    print("Mean Absolute Error:", MAE)
    RMSE = sqrt(mean_squared_error(y_pred, y_test))
    print("Root Mean Square Error:", RMSE)

    mlflow.log_param("algorithm", "linear regression")
    mlflow.log_metric("MAE", MAE)
    mlflow.log_metric("RMSE", RMSE)
    run_data = mlflow.get_run(run_id=run.info.run_id)

Vectice.save_after_run(project_token=PROJECT_TOKEN, run=run_data, lib="MLflow", inputs=create_inputs())<jupyter_output><empty_output><jupyter_text>we can have the list of models in the project by using vectice.list_models()<jupyter_code>vectice.list_models().list<jupyter_output><empty_output><jupyter_text>We can update an existing model by using vectice.update_model()<jupyter_code>vectice.update_model(parent_name="LINEAR REGRESSION", model_type=ModelType.REGRESSION)
# If you have a mlflow run that is still running and you need to end it, then run this cell.
mlflow.end_run()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09845322857844341, 'fraction_numerical': 0.0014731156395777069, 'mean_word_length': 6.1599297012302285, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '340319', 'license': 'MIT', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1388, 'n_words': 332, 'path': 'Notebooks/MLflow/KC_housing_prices/KC_housing_prices-Jupyter.ipynb', 'repo_name': 'vectice/vectice-examples', 'size': '4075'}