5

I'm following this linear regression example but my result differs from what should be. The problem is in the plot axis, they are not in order.

expected:

enter image description here

my result:

enter image description here

zoom to see the axis:

enter image description here

The Code:

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

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    10.800
    2.000    12.300
    1.700     6.300
 2547.000  4603.000
    0.023     0.300
  187.100   419.000
  521.000   655.000
    0.785     3.500
   10.000   115.000
    3.300    25.600
    0.200     5.000
    1.410    17.500
  529.000   680.000
  207.000   406.000
   85.000   325.000
    0.750    12.300
   62.000  1320.000
 6654.000  5712.000
    3.500     3.900
    6.800   179.000
   35.000    56.000
    4.050    17.000
    0.120     1.000
    0.023     0.400
    0.010     0.250
    1.400    12.500
  250.000   490.000
    2.500    12.100
   55.500   175.000
  100.000   157.000
   52.160   440.000
   10.550   179.500
    0.550     2.400
   60.000    81.000
    3.600    21.000
    4.288    39.200
    0.280     1.900
    0.075     1.200
    0.122     3.000
    0.048     0.330
  192.000   180.000
    3.000    25.000
  160.000   169.000
    0.900     2.600
    1.620    11.400
    0.104     2.500
    4.235    50.400

It is my first time in python and I think there is a problem with the installation of some module but I don't have a clue.

6
  • 2
    Please post your code. Commented Nov 15, 2017 at 14:26
  • If the problem only occurs if you use brain_body.txt you need to supply this file. If the problem does not depend on the use of brain_body.txt, then provide a minimal reproducible example without it. Commented Nov 15, 2017 at 15:07
  • 1
    I put a link to the file but I know the problem is not in the code because it works in teacher's computer (OSX) but not in mine (windows). I think it should be some module but I don't know how to check them. I get an exit code 0, no stack trace. Commented Nov 15, 2017 at 16:11
  • Why is this closed as a duplicate? It has an important difference. The numbers on the Y axis are not in order - the same problem I'm having today. The allegedly duplicate question does not show this oddity. Commented Oct 31, 2018 at 18:29
  • 1
    I had the exact same problem with strange values, not in order, along the Y axis. But I'm not using Pandas or doing line fits. Just plotting x,y values. After several hours of chasing wild geese, I finally discovered the culprit: The values read from a text file were never converted to floats, i.e., x = float(linefromfile.split[0]). D'oh! Once I put in the float() converstions, plots came out fine. Another reason to avoid weakly typed languages! Commented Oct 31, 2018 at 22:04

1 Answer 1

5

You want to plot the values, plt.scatter(x_values.values, y_values.values). It would also make sense to sort the data, to get a smooth line.

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.