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 impo
linear_model.LinearRegression().fit(X,y) expects its arguments
X
: numpy array or sparse matrix of shape[n_samples,n_features]
y
: numpy array of shape[n_samples, n_targets]
Here you have 1 "feature" and 1 "target", hence the expected shape of the input would be (n_samples,1)
While this is the case for
x_values=dataframe[['Brain']]
y_values=dataframe[['Body']]
the shape for np.array(dataframe['Brain'],dtype=np.float64).reshape(1,-1)
is (n_samples,)
.
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]
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()