'In predicting a z_fit value for each x_fit and y_fit pair by invoking the model's predict() method, the resulting 3D plot shows no dispersion at all
I'm trying to fit a linear regression model by passing it a set of features (col x, y) from a CSV file, to predict label (z). I then need to use the model to plot a regression line for the data. x_fit and y_fit were given to me and I fed them to the model to get z_fit. These should then be used to plot a 3d scatter plot with a line of best fit. The issue I'm having is that 4 plots (at the bottom of the code section) are producing lines that have no dispersion at all. I'm not sure what is occurring but can someone take a look at my code and tell me if anything looks off?
# Import LinearRegression Class
from sklearn.linear_model import LinearRegression
# Read in data from csv and store it in variable
data = pd.read_csv('MultipleLinearRegression.csv')
X = data[['x', 'y']].values.reshape(-1,2) # values.reshape converts dataframe (two brackets) to numpy.ndarray
Y = data['z'] # pandas.core.series.Series
# Instantiate an object of the LinearRegression class
reg = LinearRegression()
# fit my multivariate linear regression model
model = reg.fit(X, Y)
# Plot Curve Fit
x_fit = np.linspace(0,21,1000) # range of porosity values
y_fit = x_fit # range of VR values
# Create dataframe with of x_fit and y_fit
preds = pd.DataFrame({'xdata': x_fit, 'ydata': y_fit})
# Pass to the model's predict() method to return z-fit
z_fit = model.predict(preds)
# Assign col values to separate variables
df = pd.DataFrame(x_fit, columns = ['x']) # 1000 non-null
df1 = pd.DataFrame(y_fit, columns = ['y']) # 1000 non-null
df2 = pd.DataFrame(z_fit, columns = ['z']) # 1000 non-null
# Assign col values to separate variables
x = df['x'].values
y = df1['y'].values
z = df2['z'].values
# Use figure method of matplotlibs plyplot library and set the figure dimensions
fig = plt.figure(figsize=[15, 15])
# Create axis 1 and set parameters
ax1 = fig.add_subplot(2, 2, 1, projection='3d')
ax1.view_init(elev=0, azim=100)
ax1.scatter3D(x, y, z, c=z, cmap = 'jet')
ax1.set_xlabel('x', fontsize = 11)
ax1.set_ylabel('y', fontsize = 11)
ax1.set_zlabel('z', fontsize = 11)
# Create axis 2 and set parameters
ax2 = fig.add_subplot(2, 2, 2, projection='3d')
ax2.view_init(elev=36, azim=0)
ax2.scatter3D(x, y, z, c=z, cmap = 'jet')
ax2.set_xlabel('x', fontsize = 11)
ax2.set_ylabel('y', fontsize = 11)
ax2.set_zlabel('z', fontsize = 11)
# Create axis 3 and set parameters
ax3 = fig.add_subplot(2, 2, 3, projection='3d')
ax3.view_init(elev=33, azim=40)
ax3.scatter3D(x, y, z, c=z, cmap = 'jet')
ax3.set_xlabel('x', fontsize = 11)
ax3.set_ylabel('y', fontsize = 11)
ax3.set_zlabel('z', fontsize = 11)
# Create axis 4 and set parameters
ax4 = fig.add_subplot(2, 2, 4, projection='3d')
ax4.view_init(elev=8, azim=8)
ax4.scatter3D(x, y, z, c=z, cmap = 'jet')
ax4.set_xlabel('x', fontsize = 11)
ax4.set_ylabel('y', fontsize = 11)
ax4.set_zlabel('z', fontsize = 11)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
