【问题标题】:Sklearn ValueError: Expected 2D array, got 1D array instead:Sklearn ValueError: Expected 2D array, got 1D array instead:
【发布时间】:2021-05-05 20:27:56
【问题描述】:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataset = pd.read_csv('Admission_Predict_Ver1.1.csv')
print(dataset)

x1 = dataset.drop('SOP', axis='columns')
print(x1)  

x = dataset.iloc[:,1:-1].values 
y = dataset.iloc[:,-1].values

from sklearn.model_selection import train_test_split
x_tr,x_te,y_tr,y_te = train_test_split(x,y,test_size = 0.2, random_state = 0) 

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_tr,y_tr) 

y_pred = regressor.predict(x_te) 

import pickle

with open('model_pickle','wb') as f:
pickle.dump(y_pred,f) 

with open('model_pickle','rb') as f:
pickle.load(f) 

gre_score = float(input("Enter GRE Score: "))
toefl_score = float(input("Enter TOEFL Score: "))
university_rating = float(input("Enter University Rating: "))
cgpa = float(input("Enter CGPA: "))
research = float(input("Enter Research: ")) 

result = regressor.predict([gre_score,toefl_score,university_rating,cgpa,research]) 
print(result) 

我正在尝试在 Web 应用程序中部署机器学习模型之上,但在打印预测时出现低于值错误。我必须做些什么来解决它!

ValueError: Expected 2D array, got 1D array instead:
array=[337.   118.     4.     4.5    9.65].
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.

【问题讨论】:

    标签: machine-learning valueerror supervised-learning machine-learning-model


    【解决方案1】:

    使用这个:

    x = dataset.iloc[:,1:-1].values 
    y = dataset.iloc[:,-1].values
    
    # reshaping to 1D
    x = np.array(x).reshape(-1,1)
    y = np.array(y).reshape(-1,1)
    

    这会将您的数据重塑为一维数据。

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2021-11-01
      • 2020-11-22
      • 2020-12-05
      • 2018-12-23
      • 2020-10-10
      • 2018-08-25
      • 2018-03-20
      相关资源
      最近更新 更多