Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Write a sample scikit-learn model saving and model inference pipeline on a popular…

  1. User

    Write a sample scikit-learn model saving and model inference pipeline on a popular dataset.

  2. Assistant

    Sure! Here's an example of how you can build a scikit-learn pipeline to train and save a model, and then use that saved model for inference on a popular dataset like the Iris dataset.

    import numpy as np
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    from sklearn.pipeline import Pipeline
    from sklearn.preprocessing import StandardScaler
    from sklearn.svm import SVC
    from sklearn.metrics import accuracy_score
    import joblib
    
    # Load the Iris dataset
    iris = load_iris()
    X, y = iris.data, iris.target
    
    # Split the dataset into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Define the pipeline
    pipeline = Pipeline([
        ('scaler', StandardScaler()),  # Apply standard scaling to the features
        ('svm', SVC())  # Use Support Vector Machine classifier
    ])
    
    # Train the model
    pipeline.fit(X_train, y_train)
    
    # Make predictions on the test set
    y_pred = pipeline.predict(X_test)
    
    # Calculate accuracy of the model
    accuracy = accuracy_score(y_test, y_pred)
    print(f"Accuracy: {accuracy}")
    
    # Save the model
    joblib.dump(pipeline, 'iris_model.pkl')
    
    # Load the saved model
    loaded_model = joblib.load('iris_model.pkl')
    
    # Perform inference on new data
    new_data = np.array([[5.1, 3.5, 1.4, 0.2], [6.2, 2.9, 4.3, 1.3], [7.3, 3.0, 6.3, 2.1]])  # Example new data
    predicted_classes = loaded_model.predict(new_data)
    print(f"Predicted classes: {predicted_classes}")
    

    In this example, we are using the Iris dataset and creating a pipeline that consists of a StandardScaler for scaling the features and a SVC (Support Vector Classifier) for classification. We then train the model using the training data, make predictions on the test set, and calculate the accuracy of the model.

    Afterwards, we save the trained model using joblib.dump to a file called 'iris_model.pkl'. Later, we load the saved model using joblib.load and perform inference on new data by passing it to the loaded model's predict method. The predicted classes are then printed.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.