Error when using sklearn and linear regression: figures (1,16) and (1,1) are not aligned: 16 (dim 1)! = 1 (dim 0)

I learned to learn machine learning, and I came across a youtube siraj and his Udacity video and wanted to try and pick up a few things.

His video is in the link: https://www.youtube.com/watch?v=vOppzHpvTiQ&index=1&list=PL2-dafEMk2A7YdKv4XfKpfbTH5z6rEEj3

In his video, he had a txt file that he imported and read, but when I tried to recreate the txt file, it could not be read correctly. Instead, I tried to create a pandas framework with the same data and perform linear regression / prediction on it, but then I got the following error.

Found input variables with inconsistent number of samples: [1, 16] and something about passing 1d arrays, and I need to change them.

Then, when I tried to change them after this post: Sklearn: ValueError: found input variables with inconsistent number of samples: [1, 6]

I get this error ....

figures (1,16) and (1,1) are not aligned: 16 (dim 1)! = 1 (dim 0)

This is my code below. I know this is probably a syntax error, I am not familiar with this scklearn yet and I need help.

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

#DF = pd.read_fwf('BrainBodyWeight.txt')
DF = pd.DataFrame()
DF['Brain'] = [3.385, .480, 1.350, 465.00,36.330, 27.660, 14.830, 1.040, 4.190, 0.425, 0.101, 0.920, 1.000, 0.005, 0.060, 3.500 ]

DF['Body'] = [44.500, 15.5, 8.1, 423, 119.5, 115, 98.2, 5.5,58, 6.40, 4, 5.7,6.6, .140,1, 10.8]

try:
    x = DF['Brain']
    y = DF['Body']

    x = x.tolist()
    y = y.tolist()

    x = np.asarray(x)
    y = np.asarray(y)


    body_reg = linear_model.LinearRegression()
    body_reg.fit(x.reshape(-1,1),y.reshape(-1,1))
    plt.scatter(x,y)
    plt.plot(x,body_reg.predict(x))
    plt.show()
except Exception as e:
    print(e)

Can anyone explain why sklearn doesn't like my input?

+4
source share
2 answers

documentation LinearRegression.fit() x [n_samples,n_features]. , x, . , , (16), [n_samples,n_features], n_features.

x = DF['Brain']
x = x.tolist()
x = np.asarray(x)

# 16 samples, None feature
x.shape
(16,)

# 16 samples, 1 feature
x.reshape(-1,1).shape
(16,1)

LinearRegression.predict ( ), , .

plt.plot(x,body_reg.predict(x.reshape(-1,1)))

, , x .

numpy, DF['Brain'].values. → numpy. :

x = DF['Brain'].values.reshape(1,-1)
y = DF['Body'].values.reshape(1,-1)

body_reg = linear_model.LinearRegression()
body_reg.fit(x, y)
+1

pandas , , , . , , , numpy . Im , .

, np.reshape(X, (- 1,1) x.reshape(-1,1)

try:
    x = np.reshape(np.array(DF['Brain']),(-1,1))
    y = np.array(DF['Body'])
    body_reg = linear_model.LinearRegression()
    body_reg.fit(x,y)
    plt.scatter(x,y)
    plt.plot(x,body_reg.predict(x))
    plt.show()
except Exception as e:
    print(e)
+1

Source: https://habr.com/ru/post/1684584/


All Articles