We will use the
sklearn.datasets package to use the diabetes dataset to make and deploy a simple Linear Regression Model using dstack, then pull it to make a linear regression plot using
We will first import scikit-learn, numpy and matplotlib for plotting and of course the
pull methods from dstack
import matplotlib.pyplot as pltimport numpy as npfrom sklearn import datasets, linear_modelfrom sklearn.metrics import mean_squared_error, r2_scorefrom sklearn.linear_model import LinearRegressionimport sklearnimport dstack as ds
Let's load our diabetes dataset now from scikit-learn, and split it.
# Loading diabetes databsediabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)# Use only one featurediabetes_X = diabetes_X[:, np.newaxis, 2]# Split the data into training/testing setsdiabetes_X_train = diabetes_X[:-20]diabetes_X_test = diabetes_X[-20:]# Split the targets into training/testing setsdiabetes_y_train = diabetes_y[:-20]diabetes_y_test = diabetes_y[-20:]
Finally we fit the model with the
LinearRegression() object in scikit-learn
# Create linear regression objectregr = LinearRegression()# Fitting the linear modelregr.fit(diabetes_X_train, diabetes_y_train)
Now that our model is fit and ready, we push it to dstack as a stack using the
# Push the frameds.push("simpleLinearReg", regr, "My first linear model")
Now that you have it pushed on a Stack, you can share it with anyone so they can pull the model and use it, or you can pull it and re-use it anytime you like as well! Let's see how we can pull the model from the Stack and use it to display a plot.
# Pull from the Stackmy_model = ds.pull("simpleLinearReg")
That's it! You have pulled your model. It's that easy.
6. Using the Model to Make Predictions or Plots
Now we can use the model to make predictions and use Matplotlib for plotting.
# Make predictions using the testing setdiabetes_y_pred = my_model.predict(diabetes_X_test)# Plot outputsplt.scatter(diabetes_X_test, diabetes_y_test, color='black')plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth=3)plt.xticks(())plt.yticks(())plt.show()
You should see the following output.
You can also push this plot onto dstack and create a report with the model, plot as well as the dataset! Read the tutorial on the plotting libraries to try this out.
This error probably means you haven't called
.fit() method on the Linear Regression model.
AttributeError: 'LinearRegression' object has no attribute 'coef_'