【问题标题】:Python Supervised Machine LearningPython 监督机器学习
【发布时间】:2016-05-28 02:02:42
【问题描述】:

我试图了解如何使用scikit 进行有监督的机器学习,所以我编造了一些属于两个不同集合的数据:集合 A 和集合 B。集合 A 中有 18 个元素,集合中有 18 个元素B. 每个元素都有三个变量。见下文:

#SetA
Variable1A = [ 3,4,4,5,4,5,5,6,7,7,5,4,5,6,4,9,3,4]
Variable2A = [ 5,4,4,3,4,5,4,5,4,3,4,5,3,4,3,4,4,3]
Variable3A = [ 7,8,4,5,6,7,3,3,3,4,4,9,7,6,8,6,7,8]


#SetB
Variable1B = [ 7,8,11,12,7,9,8,7,8,11,15,9,7,6,9,9,7,11]
Variable2B = [ 1,2,3,3,4,2,4,1,0,1,2,1,3,4,3,1,2,3]
Variable3B = [ 12,18,14,15,16,17,13,13,13,14,14,19,17,16,18,16,17,18]

我将如何使用scikit 来使用监督机器学习,以便在我引入新的 setA 和 setB 数据时,它可以尝试识别哪些新数据属于 setA 或 setB。

对数据集的道歉很小并且是“编造的”。我只想在其他数据集上使用 scikit 应用相同的方法。

【问题讨论】:

  • 我相信sklearn 可以与numpy 一起使用,因此您可能应该从它开始。否则你有没有看过examples中的任何一个?
  • @TheSchwa 那是无监督机器学习。我需要监督机器学习
  • 你错了。那是有监督的。如果不给它标签,你就不能使用 KNN 算法。无监督类似于聚类(例如 K 均值)。大多数常见算法(KNN、SVM、朴素贝叶斯)都是有监督的。

标签: python machine-learning supervised-learning


【解决方案1】:

您的问题相当广泛,所以这只是一个简短的概述。您不想以这种方式格式化数据,而是希望将这两个集合放在一个列表/数组中,另一列表示每行所属的集合。像这样的:

data = [
    [3, 5, 7, 0]
    [4, 4, 8, 0],  # these rows have 0 as the last element to represent group A
    ...
    [7, 1, 12, 1],
    [8, 2, 18, 1], # these have 1 as the last element to represent group A
    ...
]

另一种方法是仅将前三列放入data 并将其命名为X,然后有一个单独的数组y 仅包含[0, 0, 0, ..., 1, 1, 1, ...](表示每行的组成员身份)。您要避免的是将有关点所在组的信息存储在变量的 name 中;相反,您希望将“设置 A 或设置 B”信息存储在变量的 中(因为这里它存储在 datay 的最后一列中的值中) ,

无论你做什么,你几乎肯定会想要使用 numpy 数组或 pandas 数据结构来保存你的数据,而不是列表。

有许多教程和示例可用于介绍如何使用 scikit-learn,以及可能比您编写的数据集更有用的示例数据集。 “监督机器学习”是一个广义术语,包含许多不同的方法来确定数据点在哪个组中,因此您必须尝试不同的分类算法。所有这些信息都可以通过谷歌搜索和/或浏览 scikit 文档找到。

【讨论】:

    【解决方案2】:

    我认为这是一个很好的问题,如果您觉得它不够清楚,请不要担心。监督学习可用于将实例(数据行)分类为多个类别(或者在您的情况下仅为 2 个集合)。您在上面的示例中缺少的是一个变量,它说明了第 1 行属于哪个集合。

    import numpy as np # numpy will help us to concatenate the columns into a 2-dimensional array
    # so instead of hiving 3 separate arrays, we have 1 array with 3 columns and 18 rows 
    
    Variable1A = [ 3,4,4,5,4,5,5,6,7,7,5,4,5,6,4,9,3,4]
    Variable2A = [ 5,4,4,3,4,5,4,5,4,3,4,5,3,4,3,4,4,3]
    Variable3A = [ 7,8,4,5,6,7,3,3,3,4,4,9,7,6,8,6,7,8]
    
    #our target variable for A
    
    target_variable_A=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
    
    Variable1B = [ 7,8,11,12,7,9,8,7,8,11,15,9,7,6,9,9,7,11]
    Variable2B = [ 1,2,3,3,4,2,4,1,0,1,2,1,3,4,3,1,2,3]
    Variable3B = [ 12,18,14,15,16,17,13,13,13,14,14,19,17,16,18,16,17,18]
    
    # target variable for B
    target_variable_B=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    
    #lets create a dataset C with only 4 rows that we need to predict if belongs to "1" which is data set A or "0" which is  dataset B
    
    Variable1C = [ 7,4,4,12]
    Variable2C = [ 1,4,4,3]
    Variable3C = [ 12,8,4,15]
    
    #make the objects 2-dimenionsal arrays (so 1 array with X rows and 3 columns-variables)
    Dataset_A=np.column_stack((Variable1A,Variable2A,Variable3A))
    Dataset_B=np.column_stack((Variable1B,Variable2B,Variable3B))
    Dataset_C=np.column_stack((Variable1C,Variable2C,Variable3C))
    
    print(" dataset A rows ", Dataset_A.shape[0]," dataset A columns ", Dataset_A.shape[1] )
    print(" dataset B rows ", Dataset_B.shape[0]," dataset B columns ", Dataset_B.shape[1] )
    print(" dataset C rows ", Dataset_C.shape[0]," dataset C columns ", Dataset_C.shape[1] )
    
    ##########Prints ##########
    #(' dataset A rows ', 18L, ' dataset A columns ', 3L)
    #(' dataset B rows ', 18L, ' dataset B columns ', 3L)
    #(' dataset C rows ', 4L, ' dataset C columns ', 3L)
    
    # since now we have an identification that tells us if it belongs to A or B (e.g. 1 or 0) we can append the new sets together
    Dataset_AB=np.concatenate((Dataset_A,Dataset_B),axis=0) # this creates a set with 36 rows and 3 columns
    target_variable_AB=np.concatenate((target_variable_A,target_variable_B),axis=0)
    
    print(" dataset AB rows ", Dataset_AB.shape[0]," dataset Ab columns ", Dataset_AB.shape[1] )
    print(" target Variable rows ", target_variable_AB.shape[0])
    
    ##########Prints ##########
    #(' dataset AB rows ', 36L, ' dataset Ab columns ', 3L)
    #(' target Variable rows ', 36L)
    
    #now we will select the most common supervised scikit model - Logistic Regression
    from sklearn.linear_model import LogisticRegression
    model=LogisticRegression() # we create an instance of the model
    
    model.fit(Dataset_AB,target_variable_AB) # the model learns to distinguish between A and B (1 or 0)
    
    #now we make predictions for the new dataset C
    
    predictions_for_C=model.predict(Dataset_C)
    print(predictions_for_C)
    # this will print
    #[0 1 1 0]
    # so first case belongs to set A , second to B, third to B and fourth to A
    

    【讨论】:

      【解决方案3】:

      监督学习意味着您为训练模型提供的数据被标记,即用于训练的每个样本的结果都是事先已知的。

      在您提供的问题中,基本上有 2 个集合:集合 A 和集合 B,因此您将不得不使用像逻辑回归模型这样的二元分类器。

      集合 A 和 B 的第一个标签元素为 1 或 0,反之亦然,这取决于它们属于哪个集合,也就是说,如果元素 e 属于集合 A,则将其标记为 1,否则将其标记为 0。

      然后在 python 中从 scikitlearn 导入 Logistic Regression 分类器。

      接下来是合并两个集合,例如集合 A,然后是集合 B,反之亦然,并以相同的顺序合并您已经提供的标签。

      您可以使用 pandas 或 numpy 来堆叠这些设置并准备标记的数据集。

      现在你有了一个标记良好的数据集。

      您现在可以使用数据集(包含集合 A 和集合 B 元素)和标签集从逻辑回归分类器调用拟合函数。

      之后,使用您要测试的数据调用预测函数,您将获得预测的类别,即 0 或 1。

      如果您想要这些集合,您可以使用字典将键映射为 1 和 0,其值为 'set A' 和 'set B' 。 这样您就可以从中获取集合。

      import pandas as pd
      import numpy as np 
      from sklearn.linear_model import LogisticRegression as lr
      
      #set A
      
      firstA=[3,4,4,5,4,5,5,6,7,7,5,4,5,6,4,9,3,4]
      secondA=[5,4,4,3,4,5,4,5,4,3,4,5,3,4,3,4,4,3]
      thirdA=[7,8,4,5,6,7,3,3,3,4,4,9,7,6,8,6,7,8]
      
      #set B
      
      firstB=[7,8,11,12,7,9,8,7,8,11,15,9,7,6,9,9,7,11]
      secondB=[1,2,3,3,4,2,4,1,0,1,2,1,3,4,3,1,2,3]
      thirdB=[12,18,14,15,16,17,13,13,13,14,14,19,17,16,18,16,17,18]
      
      #stacking up and building the dataset
      
      Aset=[firstA,secondA,thirdA]
      Bset=[firstB,secondB,thirdB]
      totalset=[Aset,Bset]
      
      
      data=pd.DataFrame(columns["0","1","2","3","4","5","6",
      "7","8","9","10","11","12","13","14","15","16","17"])
      c=0
      for i in range(0,2):
          for j in range(0,3):
              data.loc[c]=totalset[i][j]
              c=c+1 
      label=np.array([0,0,0,1,1,1])
      df2=pd.DataFrame(columns=["0","1","2","3","4","5"])
      df2=label
      
      
      #Training and testing the model
      
      model=lr()
      model.fit(df,df2)
      k=model.predict([[17,18,14,15,16,17,13,
      13,13,41,14,19,17,16,18,16,17,28]])
      
      #mapping(chosen set A element's with label 0 and set B with 1)
      
      dic={0:"set A",1:"set B"}
      print(dic[int(k)])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-21
        • 2017-08-21
        • 2014-04-20
        • 2013-03-24
        • 2018-10-17
        • 2021-10-17
        • 2018-09-25
        • 2013-03-09
        相关资源
        最近更新 更多