Sklearn ValueError:预期的二维数组,而是得到一维数组:

问题描述

 do_foo<Bar>({11,12}); 

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

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) 

解决方法

使用这个:

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)

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...