具有2d坐标列表+分类值的决策树分类器

时间:2018-04-05 12:58:57

标签: python machine-learning decision-tree

我有一个带有功能和标签的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. 分类数据
  3. 对于第二点,单热编码应解决问题。在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.
    

    我想这与2d坐标数组有关?

1 个答案:

答案 0 :(得分:1)

问题确实是嵌套的坐标数组。像cardelling建议代表10个2d坐标作为20个功能工作。

要展平我使用numpy.ravel()

的列表