【问题标题】:Expected 2D array, got 1D array instead, when using decision tree使用决策树时,预期 2D 数组,得到 1D 数组
【发布时间】:2021-07-13 14:14:31
【问题描述】:

我有一个数据集(19 个属性和 1700 个实例),我正在训练逻辑回归、决策树、随机森林。逻辑回归工作正常,但是当我尝试决策树时出现错误:

Expected 2D array, got 1D array instead:
array=[1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.
 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0.
data = data.interpolate()
print(data.isnull().sum())

data['Year_upd'].fillna('0', inplace=True)
data['Month_upd'].fillna('0', inplace=True)
data['Day_upd'].fillna('0', inplace=True)
data['Hour_upd'].fillna('0', inplace=True)

print(data.isnull().sum())

x = data.drop(labels='Type', axis='columns')
y = data.iloc [:, 13]
x.head()

x_train, x_test, y_train, y_test = train_test_split(x, y ,test_size = 0.2,)
model = LogisticRegression(max_iter = 4000)
model.fit (x_train, y_train)

model3= tree.DecisionTreeClassifier()
model3.fit(x_train,y_train)

我尝试使用以下代码解决问题

model3= tree.DecisionTreeClassifier()
model3.fit(x_train.values.reshape(-1, 1),y_train)

我仍然得到:标签数=1424 与样本数=29904 不匹配

【问题讨论】:

    标签: python machine-learning scikit-learn decision-tree


    【解决方案1】:

    x_train 应该是形状为(n_instances, n_features) 的二维特征矩阵。我建议在拆分和测试之前将表转换为 NumPy 数组,如下所示:

    x = data.drop(labels='Type', axis='columns').to_numpy()
    y = data.iloc[:, 13].to_numpy()
    
    # ...
    model3= tree.DecisionTreeClassifier()
    model3.fit(x_train, y_train)
    

    【讨论】:

    • 嗨,我按照你的建议做了,但我仍然遇到同样的错误。
    • 标签数=1424 与样本数=29904 不匹配
    • x_trainy_train的形状是什么?
    • y_train.shape (1424,) (x_train.shape) (1424, 21)
    • 那么它应该可以工作了。你还在扁平化阵列还是什么?显然,如果 reshape(-1, 1) 仍然存在,您需要删除它。
    猜你喜欢
    • 2018-09-16
    • 2020-12-18
    • 2018-12-11
    • 2019-06-06
    • 1970-01-01
    • 2021-05-14
    • 2021-12-28
    • 2020-11-02
    相关资源
    最近更新 更多