Error using sklearn and linear regression shapes 1 16 and 1 1 not aligned 16 dim 1 1 dim 0

0 votes

I wanted to learn about machine learning and I stumbled upon youtube siraj and his Udacity videos and wanted to try and pick up a few things.

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

In his video, he had a txt file he imported and read, but when I tried to recreate the the txt file it couldn't be read correctly. Instead, I tried to create a pandas dataframe with the same data and perform the linear regression/predict on it, but then I got the below error.

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

Then when I tried to reshape them following this post: Sklearn : ValueError: Found input variables with inconsistent numbers of samples: [1, 6]

I get this error....

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

This is my code down below. I know it's probably a syntax error, I'm just not familiar with this scklearn yet and would like some 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????

Apr 11 in Machine Learning by Dev
• 6,000 points
796 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Machine Learning

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

ERROR: PyAudio-0.2.11-cp39-cp39-win32.whl is not a supported wheel on this platform.

Hi@akhtar, You should download the version that is ...READ MORE

answered Sep 7, 2020 in Machine Learning by MD
• 95,380 points
3,647 views
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,138 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP