机器学习-如何使用matplotlib绘制非线性模型



我有点迷失了如何继续实现这一点。通常对于线性模型,当我执行线性回归时,我只是简单地取我的训练数据(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中使用)。使用转换,我能够获取值。

比起一次绘制具有多个离散特征的非线性模型,我发现简单地根据我的观察/输出观察每一个特征对我的研究来说更好,更容易。

最新更新