Search code examples
pythonsklearn-pandas

Expected 2D array, got scalar array instead error


I am working on python 3.7. I get the error when I execute the code below. How can I solve it?

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

data = pd.read_csv("hw_25000.csv")
regression = LinearRegression()
boy= data.Height.values.reshape(-1,1)
kilo= data.Weight.values.reshape(-1,1)

regression.fit(boy,kilo)
regression.predict(70)

error:

ValueError: Expected 2D array, got scalar array instead: array=1. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

hw_25000.csv:

Index,Height,Weight
1, 65.78331, 112.9925
2, 71.51521, 136.4873
3, 69.39874, 153.0269
4, 68.2166, 142.3354

Solution

  • You can't predict on an int, it must be an array

    reg.predict(np.array(70).reshape(-1, 1))
    array([[141.94045785]])