此问题已在此处找到答案:
valueerror:应为2d数组,改为1d数组:(6个答案)
两天前关门了。
请帮帮我。我无法解决我遇到的一个错误。我是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), Transport
print("\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_pred
y_pred = model = model.predict([int(a1),int(b1),int(c1),int(d1)])
# for loop to predict customizable output
if 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 dtypes
ValueError: 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
1条答案
按热度按时间y4ekin9u1#
错误消息非常详细,并告诉您,您提供了一个1d数组,其中需要一个2d数组:
应为2d数组,改为1d数组
堆栈跟踪指向此行:
它还告诉您如何解决此问题:
使用数组重塑数据。如果数据具有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)。
由于您试图预测单个样本,因此应使用后者:
注意,我删除了双重赋值
y_pred = model = ...
这是没有用的。附加说明
与此特定错误无关,但可能不是您想要的:您只在性别特征上拟合模型。请参见以下几行:
当您在单个功能上拟合模型,然后想要预测具有四个功能的示例时,这将破坏您的代码。你也应该解决这个问题。解决方案可能如下所示:
看看我用的
OrdinalEncoder
对于自LabelEncoder
仅用于对目标进行编码y
(与文档进行比较)。