【问题标题】:DecisionTree - Python决策树 - Python
【发布时间】:2020-12-05 03:03:10
【问题描述】:

我是新手,任何事情都会有所帮助。数据量很大... 我不确定错误可能来自哪里。我什至不知道这是否是个好主意哈哈,我的 x 和 y 使用经度和纬度。

from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import pandas as pd
import numpy as np

df = pd.read_csv('aug.csv')
X = df.Lon
y = df.Lat

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)

clf = DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)```

ValueError: Expected 2D array, got 1D array instead:
array=[-73.9713 -74.0635 -73.9881 ... -74.1777 -73.9923 -73.9661].
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.

【问题讨论】:

  • 二维数组是数组中的数组。它是一个数组数组。例如 T = [[111, 222, 333, 444], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]。

标签: python arrays pandas numpy


【解决方案1】:

输入的 X 变量需要是一组特征。您在该 csv 中有一个列,因此它将其解释为一维数组。您收到的错误消息是正确的,因此将该行“X = df.Lon”更改为: "X = df.Lon.reshape(-1, 1)"

需要注意的一点:您所做的事情没有多大意义。这段代码试图做的是在给定 X (lon) 的情况下预测 Y (lat)。这些确实应该是自变量,因此从另一个预测一个可能不会产生任何有意义的结果。

【讨论】:

    猜你喜欢
    • 2016-03-12
    • 2021-08-19
    • 2020-02-26
    • 2015-09-21
    • 2021-08-23
    • 2016-11-09
    • 2017-08-10
    • 2018-10-20
    • 2018-09-06
    相关资源
    最近更新 更多