使用matplotlib绘制sklearn LinearRegression输出 [英] Plot sklearn LinearRegression output with matplotlib

查看:474
本文介绍了使用matplotlib绘制sklearn LinearRegression输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在导入文件后,当我使用numpy分隔x_values和y_values时:

After importing the file when I separate the x_values and y_values using numpy as:

import pandas as pd
from sklearn import linear_model
from  matplotlib import pyplot 
import numpy as np

#read data
dataframe = pd.read_csv('challenge_dataset.txt')
dataframe.columns=['Brain','Body']
x_values=np.array(dataframe['Brain'],dtype=np.float64).reshape(1,-1)
y_values=np.array(dataframe['Body'],dtype=np.float64).reshape(1,-1)

#train model on data
body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)
prediction=body_reg.predict(x_values)

print(prediction)
#visualize results
pyplot.scatter(x_values, y_values)
pyplot.plot(x_values,prediction)
pyplot.show()

我得到如下图的图,它没有显示出最适合的线条,而且当我打印预测"的值时,它也显示了与"y_values"相同的值.

I get the plot as following image, which doesn't show up the line of best fit and also when I print the value of 'prediction' it shows up values same as 'y_values'.

相反,当我使用以下代码时.我得到回归线.

Contrary when I use the following code. I get the regression line.

#read data
dataframe = pd.read_csv('challenge_dataset.txt')
dataframe.columns=['Brain','Body']
x_values=dataframe[['Brain']]
y_values=dataframe[['Body']]

为什么会这样?

谢谢.

推荐答案

linear_model.LinearRegression().fit(X,y) expects its arguments

X:形状为[n_samples,n_features]
的numpy数组或稀疏矩阵 y:形状为[n_samples, n_targets]

X : numpy array or sparse matrix of shape [n_samples,n_features]
y : numpy array of shape [n_samples, n_targets]

这里您有1个功能"和1个目标",因此输入的预期形状为(n_samples,1)

Here you have 1 "feature" and 1 "target", hence the expected shape of the input would be (n_samples,1)

x_values=dataframe[['Brain']]
y_values=dataframe[['Body']]

np.array(dataframe['Brain'],dtype=np.float64).reshape(1,-1)的形状是(n_samples,).

从数据框列中获得所需形状的另一种方法是将它们广播到具有新轴的2D阵列中

Another option to optain the desired shape from the dataframe columns would be to broadcast them to a 2D array with a new axis

x_values=dataframe['Brain'].values[:,np.newaxis]
y_values=dataframe['Body'].values[:,np.newaxis]

请注意,为了显示漂亮的一行,您可能需要对x值进行排序.

Note that in order to show a nice line, you would probably want to sort the x values.

import pandas as pd
from sklearn import linear_model
from  matplotlib import pyplot 
import numpy as np

#read data
x = np.random.rand(25,2)
x[:,1] = 2*x[:,0]+np.random.rand(25)
dataframe = pd.DataFrame(x,columns=['Brain','Body'])


x_values=dataframe['Brain'].values[:,np.newaxis]
y_values=dataframe['Body'].values[:,np.newaxis]

body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)
prediction=body_reg.predict(np.sort(x_values, axis=0))

pyplot.scatter(x_values, y_values)
pyplot.plot(np.sort(x_values, axis=0),prediction)
pyplot.show()

这篇关于使用matplotlib绘制sklearn LinearRegression输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆