2017-11-15 7 views
1

다음은 this linear regression example이지만 다음과 같은 결과가 나옵니다. 문제는 플롯 축에 있으며 순서가 맞지 않습니다.(matplotlib.pyplot) 분산 형 플롯 축의 순서가 잘못되었습니다

예상 :

enter image description here

내 결과 : 축을 볼 수

enter image description here

줌 :

enter image description here

강령 :

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() 

brain_body.txt

파이썬에서 내 처음으로 내가 어떤 모듈의 설치에 문제라고 생각하지만 난 아무것도하지 않습니다.

도움이 될 것입니다.

+2

코드를 게시하시기 바랍니다. – James

+0

감사합니다, 깜빡했습니다 – Pablo

+0

'brain_body.txt'를 사용하는 경우에만 문제가 발생하면이 파일을 제공해야합니다. 문제가'brain_body.txt'의 사용에 의존하지 않는다면, 그것없이 [mcve]를 제공하십시오. – ImportanceOfBeingErnest

답변

3

값을 플롯하려는 경우 plt.scatter(x_values.values, y_values.values). 또한 데이터를 정렬하여 부드러운 선을 얻는 것이 좋습니다.

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

#read data 
dataframe = pd.read_fwf('data/brainbody.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.values, y_values.values) 

x = np.sort(x_values.values.flatten()) 
plt.plot(x, body_reg.predict(x[:,np.newaxis])) 

plt.show() 

enter image description here

관련 문제