Learn Before
Code
3D regression surface
import numpy as np import plotly.express as px import plotly.graph_objects as go from sklearn.svm import SVR mesh_size = .02 margin = 0 df = px.data.iris() X = df[['sepal_width', 'sepal_length']] y = df['petal_width'] # Condition the model on sepal width and length, predict the petal width model = SVR(C=1.) model.fit(X, y) # Create a mesh grid on which we will run our model x_min, x_max = X.sepal_width.min() - margin, X.sepal_width.max() + margin y_min, y_max = X.sepal_length.min() - margin, X.sepal_length.max() + margin xrange = np.arange(x_min, x_max, mesh_size) yrange = np.arange(y_min, y_max, mesh_size) xx, yy = np.meshgrid(xrange, yrange) # Run model pred = model.predict(np.c_[xx.ravel(), yy.ravel()]) pred = pred.reshape(xx.shape) # Generate the plot fig = px.scatter_3d(df, x='sepal_width', y='sepal_length', z='petal_width') fig.update_traces(marker=dict(size=5)) fig.add_traces(go.Surface(x=xrange, y=yrange, z=pred, name='pred_surface')) fig.show()

0
1
Updated 2021-05-28
Tags
Python Programming Language
Data Science