Error in Python script “Expected 2D array, got 1D array instead:”?

后端 未结 9 753
执念已碎
执念已碎 2020-11-30 01:29

I\'m following this tutorial to make this ML prediction:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style

style.use(\"ggplot\         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 02:06

    I faced the same issue except that the data type of the instance I wanted to predict was a panda.Series object.

    Well I just needed to predict one input instance. I took it from a slice of my data.

    df = pd.DataFrame(list(BiogasPlant.objects.all()))
    test = df.iloc[-1:]       # sliced it here
    

    In this case, you'll need to convert it into a 1-D array and then reshape it.

     test2d = test.values.reshape(1,-1)
    

    From the docs, values will convert Series into a numpy array.

提交回复
热议问题