【问题标题】:scikit-learn: How to classify data train and data test with a different features?scikit-learn:如何对具有不同特征的数据训练和数据测试进行分类?
【发布时间】:2017-06-07 02:43:03
【问题描述】:

我的数据训练:3 个特征(永久数据)

我的数据测试:它每次都会改变(2 个功能或 1 个功能),在我的示例代码中它现在是 2 个功能。

我想用不同的特征进行分类,因为它是不同的维度。我怎样才能做到这一点?在我的代码下面:

def classify(a):
    xtrain = np.loadtxt(open("el.csv","rb"), delimiter=",", usecols= (0,1,2), skiprows=1)
    print xtrain
    >>[[ -56.  -82. -110.]  
       [-110. -110. -110.]  
       [ -58. -110.  -79.]  
       [ -56. -110. -110.]  
       [ -57.  -83. -110.]  
       [ -63. -110. -110.]  
       [-110. -110. -110.]]

    ytrain = np.loadtxt(open("el.csv","rb"), delimiter=",", usecols= (3,), dtype=int, skiprows=1)   
    print ytrain
    >>[1 1 2 2 3 3 4]       

    xtest = np.asarray(a)
    xtest = xtest.reshape([1,-1])
    print xtest
    >>[['-83' '-56']]

    knn = neighbors.KNeighborsClassifier(n_neighbors=7, weights='distance') #Fuzzy K-Nearest Neighbor
    knn.fit(xtrain, ytrain)

    results = knn.predict(xtest)
    print results

错误是:

ValueError:X 和 Y 矩阵的维度不兼容:X.shape[1] == 2 而 Y.shape[1] == 3

【问题讨论】:

    标签: python numpy scikit-learn knn


    【解决方案1】:

    目前 sklearn 模型不处理测试集中的缺失值。您可以维护多个模型(针对不同特征进行训练),并为您想要预测的每种类型的数据使用适当的模型。 另一种选择是为不具备所有特征的实例填充缺失值。

    【讨论】:

      【解决方案2】:

      首先,让 as 生成一些训练和测试数据:

      import numpy as np
      xtrain = np.asarray([[ -56.,  -82., -110.],
                           [-110., -110., -110.],
                           [ -58., -110.,  -79.],
                           [ -56., -110., -110.],
                           [ -57.,  -83., -110.],
                           [ -63., -110., -110.],
                           [-110., -110., -110.]], dtype='float')
      ytrain = np.asarray([1, 1, 2, 2, 3, 3, 4], dtype='int')
      

      现在您必须使用整数键创建字典 knns。对应于键 n 的值是一个最近邻分类器,它使用 仅使用您的前 n 个特征进行训练训练数据。

      from sklearn.neighbors import KNeighborsClassifier
      knns = {}
      for n_feats in range(1, xtrain.shape[-1] + 1):
          knns[n_feats] = KNeighborsClassifier(n_neighbors=7, weights='distance')
          knns[n_feats].fit(xtrain[:, :n_feats], ytrain)
      

      classify 函数应该使用两个参数,即测试数据和分类器字典。通过这种方式,您可以确保分类是由使用与测试数据完全相同的特征(并丢弃其他特征)进行训练的分类器执行的:

      def classify(test_data, classifiers):
          """Classify test_data using classifiers[n], which is the classifier
          trained with the first n features of test_data
          """
          X = np.asarray(test_data, dtype='float')
          n_feats = X.shape[-1]
          return classifiers[n_feats].predict(X)
      

      Demo(注意测试数据必须是数字而不是字符串):

      In [107]: xtest1 = [[-83, -56]]
      
      In [108]: classify(xtest1, knns)
      Out[108]: array([3])
      
      In [109]: xtest2 = [[ -52],
           ...:           [-108],
           ...:           [ -71]]
           ...: 
      
      In [110]: classify(xtest2, knns)
      Out[110]: array([2, 1, 3])
      
      In [111]: xtest3 = [[-122,  -87,  -94],
           ...:           [-136,  -99, -107]]
           ...: 
      
      In [112]: classify(xtest3, knns)
      Out[112]: array([1, 1])
      

      【讨论】:

        猜你喜欢
        • 2020-06-17
        • 2017-04-05
        • 2019-06-18
        • 1970-01-01
        • 2014-05-01
        • 2021-03-30
        • 2020-02-08
        • 2017-05-02
        • 2019-03-18
        相关资源
        最近更新 更多