我正在关注这个线性回归示例,但我的结果与应有的不同。问题出在情节轴上,它们没有顺序。
预期的:
我的结果:
缩放以查看轴:
编码:
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
#read data
dataframe = pd.read_fwf('brain_body.txt')
x_values = dataframe[['Brain']]
y_values = dataframe[['Body']]
#train model on data
body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)
#visualize results
plt.scatter(x_values, y_values)
plt.plot(x_values, body_reg.predict(x_values))
plt.show()
Run Code Online (Sandbox Code Playgroud)
脑体.txt
Brain Body
3.385 44.500
0.480 15.500
1.350 8.100
465.000 423.000
36.330 119.500
27.660 115.000
14.830 98.200
1.040 5.500
4.190 58.000
0.425 6.400
0.101 4.000
0.920 5.700
1.000 6.600
0.005 0.140
0.060 1.000
3.500 …Run Code Online (Sandbox Code Playgroud)