【问题标题】:How to assign labels/score to data using machine learning如何使用机器学习为数据分配标签/分数
【发布时间】:2020-11-11 10:38:22
【问题描述】:

我有一个由许多行组成的数据框,其中包括推文。我想使用机器学习技术(监督或无监督)对它们进行分类。 由于数据集未标记,我想选择几行(50%)手动标记(+1 pos,-1 neg,0 中性),然后使用机器学习将标签分配给其他行。 为了做到这一点,我做了如下:

原始数据集

Date                   ID        Tweet                         
01/20/2020           4141    The cat is on the table               
01/20/2020           4142    The sky is blue                       
01/20/2020           53      What a wonderful day                  
...
05/12/2020           532     In this extraordinary circumstance we are together   
05/13/2020           12      It was a very bad decision            
05/22/2020           565     I know you are the best              
  1. 将数据集分成 50% 的训练和 50% 的测试。我手动标记了 50% 的数据如下:

    Date                   ID        Tweet                          PosNegNeu
     01/20/2020           4141    The cat is on the table               0
     01/20/2020           4142    The weather is bad today              -1
     01/20/2020           53      What a wonderful day                  1
     ...
     05/12/2020           532     In this extraordinary circumstance we are together   1
     05/13/2020           12      It was a very bad decision            -1
     05/22/2020           565     I know you are the best               1
    

然后我提取词的频率(去除停用词后):

               Frequency
 bad               2
 circumstance      1
 best              1
 day               1
 today             1
 wonderful         1

....

我想尝试为其他数据分配标签:

  • 频率表中的单词,例如说“如果一条推文包含例如比assign -1差;如果一条推文包含精彩的assign 1(即我应该创建一个字符串列表和一条规则);
  • 基于句子相似度(例如使用 Levenshtein 距离)。

我知道有几种方法可以做到这一点,甚至更好,但我在为我的数据分类/分配标签时遇到了一些问题,我无法手动完成。

我的预期输出,例如使用以下测试数据集

Date                   ID        Tweet                                   
06/12/2020           43       My cat 'Sylvester' is on the table            
07/02/2020           75       Laura's pen is black                                                
07/02/2020           763      It is such a wonderful day                                    
...
11/06/2020           1415    No matter what you need to do                  
05/15/2020           64      I disagree with you: I think it is a very bad decision           
12/27/2020           565     I know you can improve                         

应该是这样的

Date                   ID        Tweet                                   PosNegNeu
06/12/2020           43       My cat 'Sylvester' is on the table            0
07/02/2020           75       Laura's pen is black                          0                       
07/02/2020           763      It is such a wonderful day                    1                
...
11/06/2020           1415    No matter what you need to do                  0  
05/15/2020           64      I disagree with you: I think it is a very bad decision  -1          
12/27/2020           565     I know you can improve                         0   

可能更好的方法应该是考虑 n-gram 而不是单个单词或构建语料库/词汇表来分配分数,然后是情绪。任何建议将不胜感激,因为这是我关于机器学习的第一次练习。我认为也可以应用 k-means 聚类,试图获得更多相似的句子。 如果你能给我一个完整的例子(我的数据很好,但其他数据也很好),我将不胜感激。

【问题讨论】:

  • 我建议您使用自己的标记数据微调预训练模型,然后使用该模型预测其余推文的类别。
  • @luca-di-mauro 温和提醒您获得赏金。看起来你忘记分配赏金了。我以前的用户名是villisSO。上周我把它改成了特立独行的。我认为这不应该成为在我回答你的问题时奖励我赏金的威慑。
  • 非常感谢您留下我。我以为我已经分配了!非常感谢你特立独行!

标签: python pandas machine-learning sentiment-analysis


【解决方案1】:

IIUC,您已标记了一定百分比的数据,并且需要标记剩余的数据。我建议阅读有关半监督机器学习的文章。

半监督学习是一种机器学习方法,它在训练期间将少量标记数据与大量未标记数据相结合。半监督学习介于无监督学习(没有标记的训练数据)和监督学习(只有标记的训练数据)之间

Sklearn 提供了种类繁多的算法来帮助解决这个问题。请检查this

如果您需要更深入地了解此主题,我强烈建议您也查看此article

这是一个使用 iris 数据集的示例 -

import numpy as np
from sklearn import datasets
from sklearn.semi_supervised import LabelPropagation

#Init
label_prop_model = LabelPropagation()
iris = datasets.load_iris()

#Randomly create unlabelled samples
rng = np.random.RandomState(42)
random_unlabeled_points = rng.rand(len(iris.target)) < 0.3
labels = np.copy(iris.target)
labels[random_unlabeled_points] = -1

#propogate labels over remaining unlabelled data
label_prop_model.fit(iris.data, labels)

【讨论】:

    【解决方案2】:

    我将在此上下文中提出要分析极性的句子或推文。这可以使用textblob 库来完成。它可以安装为pip install -U textblob。一旦找到文本数据极性,就可以将其分配为数据框中的单独列。随后,可以使用句子极性进行进一步分析。

    初始代码

    from textblob import TextBlob
    df['sentiment'] = df['Tweet'].apply(lambda Tweet: TextBlob(Tweet).sentiment)
    print(df)
    

    中间结果

        Date     ...                                  sentiment
    0  1/1/2020  ...                                 (0.0, 0.0)
    1  2/1/2020  ...                                 (0.0, 0.0)
    2  3/2/2020  ...                                 (0.0, 0.1)
    3  4/2/2020  ...  (-0.6999999999999998, 0.6666666666666666)
    4  5/2/2020  ...                                 (0.5, 0.6)
    
    [5 rows x 4 columns]
    

    从情感列(在上面的输出中),我们可以看到情感列分为两类——极性和主观性。

    Polarity 是 [-1.0 到 1.0] 范围内的浮点值,其中 0 表示中性,+1 表示非常积极的情绪,-1 代表一种非常消极的情绪。

    主观性是 [0.0 到 1.0] 范围内的浮点值,其中 0.0 很客观,1.0很主观。主观句 表达一些个人的感受、观点、信仰、意见, 指控、欲望、信仰、怀疑和猜测,其中 客观的句子是事实。

    注意,情感列是一个元组。所以我们可以把它分成两列,比如df1=pd.DataFrame(df['sentiment'].tolist(), index= df.index)。现在,我们可以创建一个新的数据框,我将在其中添加拆分列,如图所示;

    df_new = df
    df_new['polarity'] = df1['polarity']
    df_new.polarity = df1.polarity.astype(float)
    df_new['subjectivity'] = df1['subjectivity']
    df_new.subjectivity = df1.polarity.astype(float)
    

    最后,根据之前找到的句子极性,我们现在可以在数据框中添加一个标签,这将指示推文是正面的、负面的还是中性的。

    import numpy as np
    conditionList = [
        df_new['polarity'] == 0,
        df_new['polarity'] > 0,
        df_new['polarity'] < 0]
    choiceList = ['neutral', 'positive', 'negative']
    df_new['label'] = np.select(conditionList, choiceList, default='no_label')
    print(df_new)
    

    最后,结果会是这样的;

    最终结果

    [5 rows x 6 columns]
           Date  ID                 Tweet  ... polarity  subjectivity     label
    0  1/1/2020   1  the weather is sunny  ...      0.0           0.0   neutral
    1  2/1/2020   2       tom likes harry  ...      0.0           0.0   neutral
    2  3/2/2020   3       the sky is blue  ...      0.0           0.0   neutral
    3  4/2/2020   4    the weather is bad  ...     -0.7          -0.7  negative
    4  5/2/2020   5         i love apples  ...      0.5           0.5  positive
    
    [5 rows x 7 columns]
    

    数据

    import pandas as pd
    
    # create a dictionary
    data = {"Date":["1/1/2020","2/1/2020","3/2/2020","4/2/2020","5/2/2020"],
        "ID":[1,2,3,4,5],
        "Tweet":["the weather is sunny",
                 "tom likes harry", "the sky is blue",
                 "the weather is bad","i love apples"]}
    # convert data to dataframe
    df = pd.DataFrame(data)
    

    完整代码

    # create some dummy data
    import pandas as pd
    import numpy as np
    
    # create a dictionary
    data = {"Date":["1/1/2020","2/1/2020","3/2/2020","4/2/2020","5/2/2020"],
            "ID":[1,2,3,4,5],
            "Tweet":["the weather is sunny",
                     "tom likes harry", "the sky is blue",
                     "the weather is bad","i love apples"]}
    # convert data to dataframe
    df = pd.DataFrame(data)
    
    from textblob import TextBlob
    df['sentiment'] = df['Tweet'].apply(lambda Tweet: TextBlob(Tweet).sentiment)
    print(df)
    
    # split the sentiment column into two
    df1=pd.DataFrame(df['sentiment'].tolist(), index= df.index)
    
    # append cols to original dataframe
    df_new = df
    df_new['polarity'] = df1['polarity']
    df_new.polarity = df1.polarity.astype(float)
    df_new['subjectivity'] = df1['subjectivity']
    df_new.subjectivity = df1.polarity.astype(float)
    print(df_new)
    
    # add label to dataframe based on condition
    conditionList = [
        df_new['polarity'] == 0,
        df_new['polarity'] > 0,
        df_new['polarity'] < 0]
    choiceList = ['neutral', 'positive', 'negative']
    df_new['label'] = np.select(conditionList, choiceList, default='no_label')
    print(df_new)
    

    【讨论】:

    • 非常感谢您的回答 villisSO。我会尽快分配赏金给你
    • @LucaDiMauro 很高兴它对你有用。我会期待赏金的。干杯。
    猜你喜欢
    • 2017-07-21
    • 2018-06-10
    • 2020-03-09
    • 2021-06-02
    • 2019-03-06
    • 2017-05-16
    • 2018-03-09
    • 2017-08-18
    • 2019-01-11
    相关资源
    最近更新 更多