在Python的sklearn中遇到”ValueError: Expected 2D array, got 1D array instead”错误 [duplicate]

请帮帮我。我无法解决一个错误。我是Python机器学习的新手。对于任何建议我将不胜感激。

下面是我的代码,我编写它是为了预测一家公司的员工根据他们的性别、学历和驾照情况可能喜欢的交通方式:

Gender = preprocessing.LabelEncoder().fit_transform(df.loc[:,'Gender'])Engineer = preprocessing.LabelEncoder().fit_transform(df.loc[:,'Engineer'])MBA = preprocessing.LabelEncoder().fit_transform(df.loc[:,'MBA'])License = preprocessing.LabelEncoder().fit_transform(df.loc[:,'license'])Transport = preprocessing.LabelEncoder().fit_transform(df.loc[:,'Transport'])x,y = Gender.reshape(-1,1), Transportprint("\n\nGender:", Gender, "\n\nEngineer:", Engineer, "\n\nMBA:", MBA, "\n\nLicense:", license, "\n\nTransport:", Transport)model = GaussianNB().fit(x,y)a1 = input("\n\n Choose Gender : Male:1 or Female:0 = ")b1 = input("\n\n Are you an Engineer? : Yes:1 or No:0 = ")c1 = input("\n\n Have you done MBA? : Yes:1 or No:0 = ")d1 = input("\n\n Do you have license? : Yes:1 or No:0 = ")#store the output in y_predy_pred = model = model.predict([int(a1),int(b1),int(c1),int(d1)])#for loop to predict customizable outputif y_pred == [1]:    print("\n\n You prefer Public Transport")else:    print("\n\n You prefer Private Transport")

这是我在最后阶段遇到的错误:

ValueError                                Traceback (most recent call last)<ipython-input-104-a14f86182731> in <module>      6 #store the output in y_pred      7 ----> 8 y_pred = model = model.predict([int(a1),int(b1),int(c1),int(d1)])      9      10 #for loop to predict customizable output~\Anaconda3\lib\site-packages\sklearn\naive_bayes.py in predict(self, X)     63             Predicted target values for X     64         """---> 65         jll = self._joint_log_likelihood(X)     66         return self.classes_[np.argmax(jll, axis=1)]     67 ~\Anaconda3\lib\site-packages\sklearn\naive_bayes.py in _joint_log_likelihood(self, X)    428         check_is_fitted(self, "classes_")    429 --> 430         X = check_array(X)    431         joint_log_likelihood = []    432         for i in range(np.size(self.classes_)):~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)    519                     "Reshape your data either using array.reshape(-1, 1) if "    520                     "your data has a single feature or array.reshape(1, -1) "--> 521                     "if it contains a single sample.".format(array))    522     523         # in the future np.flexible dtypes will be handled like object dtypesValueError: Expected 2D array, got 1D array instead:array=[1 1 0 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.

下面是我的数据集结构:

<class 'pandas.core.frame.DataFrame'>    Int64Index: 444 entries, 28 to 39    Data columns (total 8 columns):    Gender       444 non-null object    Engineer     444 non-null int64    MBA          444 non-null int64    Work Exp     444 non-null int64    Salary       444 non-null float64    Distance     444 non-null float64    license      444 non-null int64    Transport    444 non-null object    dtypes: float64(2), int64(4), object(2)    memory usage: 31.2+ KB

回答:

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注