gpt4 book ai didi

python - 具有二维坐标+分类值列表的决策树分类器

转载 作者:行者123 更新时间:2023-11-30 09:06:56 25 4
gpt4 key购买 nike

我有一个带有特征和标签的 numpy 数组。

每个数据点由 10 个 2d 坐标 (x,y) 和一个指示方向(“左”或“右”)的字符串组成。

示例:

[array([[-19.24181754,  -0.6933614 ],
[-17.39631579, -0.84320702],
[-14.57501754, 12.99707368],
[ -8.6202386 , 4.90138246],
[ 0.82478596, 20.01929825],
[ 4.79946667, -10.70312982],
[ 7.10694035, 17.47812632],
[ 11.06254737, 14.17312982],
[ 17.04467368, 0.19169825],
[ 18.94181053, 6.92687018]])
'left']]

标签是 12 个不同的字符串('4-4-2'、'5-3-2'、...)。我想用这些数据尝试不同的算法来比较它们的性能。第一个算法是决策树分类器。

我发现两个潜在的问题:

  1. 10 个点的数组作为特征
  2. 分类数据

对于第二点,one-hot-coding 应该可以解决问题。有没有一种非常简单的方法可以在 numpy 数组中执行此操作?

对于第一点,我不确定这是否是一个问题,因为我到目前为止还没有尝试过。

编辑:

我的编码:

#Separate features and labels
X = test[:, [0, 4]]
Y = test[:,10]

zeros = np.zeros((len(X), 2), dtype=int)

X = np.append(X, zeros, axis=1)

for datapoint in X:
if(datapoint[1] == 'left'):
datapoint[2] = 1
else:
datapoint[3] = 1

X = np.delete(X, 1, 1)

#Divide into test and training data: 80% training, 20% test
X_train, X_test, y_train, y_test = train_test_split(X,Y, test_size=0.2, random_state=100)

#Initialize classifier
clf_gini = DecisionTreeClassifier(criterion = "gini", random_state = 100,
max_depth=3, min_samples_leaf=5)

#Train classifier
clf_gini.fit(X_train, y_train)

错误:

  "This module will be removed in 0.20.", DeprecationWarning)
Backend TkAgg is interactive backend. Turning interactive mode on.
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.2\helpers\pydev\pydevd.py", line 1668, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.2\helpers\pydev\pydevd.py", line 1662, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.2\helpers\pydev\pydevd.py", line 1072, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/D071947/PycharmProjects/Formation/DecisionTree.py", line 41, in <module>
clf_gini.fit(X_train, y_train)
File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\sklearn\tree\tree.py", line 790, in fit
X_idx_sorted=X_idx_sorted)
File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\sklearn\tree\tree.py", line 116, in fit
X = check_array(X, dtype=DTYPE, accept_sparse="csc")
File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array
array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: setting an array element with a sequence.

我猜这与二维坐标数组有关?

最佳答案

问题确实是嵌套的坐标数组。正如 Cardelling 所建议的,将 10 个 2D 坐标表示为 20 个功能。

为了展平列表,我使用了numpy.ravel()

关于python - 具有二维坐标+分类值列表的决策树分类器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49673029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com