我对如何继续实现这一目标有点迷失。通常,对于线性模型,当我执行线性回归时,我只需获取训练数据 (x) 和输出数据 (y) 并使用 matplotlib 绘制它们。现在我有 3 个特征和我的输出/观察结果 (y)。谁能指导我如何使用 matplotlib 绘制此类模型的图形?我的目标是拟合多项式模型并使用 matplotlib 绘制多项式图表。
%matplotlib inline
import sframe as frame
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
# Initalize SFrame
sales = frame.SFrame('kc_house_data.gl/')
# Separate data into test and training data
train_data,test_data = sales.random_split(.8,seed=0)
# Organize data into training and testing data
train_x = train_data[['sqft_living', 'bedrooms', 'bathrooms']].to_dataframe().values
train_y = train_data[['price']].to_dataframe().values
test_x = test_data[['sqft_living', 'bedrooms', 'bathrooms']].to_dataframe().values
test_y = test_data[['price']].to_dataframe().values
# Create a model using sklearn with multiple features
regr = linear_model.LinearRegression(fit_intercept=True, n_jobs=2)
# test predictions
regr.predict(train_x)
# Prepare to plot the data
注意:
train_x 变量包含我的 3 个特征,train_y 包含输出数据。我使用 SFrame 来包含数据。 SFrame 能够将自身转换为数据帧(在 Pandas 中使用)。使用转换我能够获取这些值。
最佳答案
我发现,相对于我的观察/输出简单地观察每个特征,而不是一次性绘制具有多个离散特征的非线性模型,对我的研究来说更好、更容易。
关于matplotlib - 如何使用 matplotlib 绘制非线性模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086751/