[Python] Multi-Label emotion classification

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    i'm working on an Multi-Label Emotion Classification problem to be solved by word2vec. this is my code that i've learned from a couple of tutorials. now the accuracy is very low. about 0.02 which is telling me something is wrong in my code. but i cannot find it. can anyone find any mistake? i tried this code for TF-IDF and BOW (obviously except word2vec part) and i got much better accuracy scores such as 0.28, but it seems this one is somehow wrong:

    #Pre-Processor Function
    pre_processor = TextPreProcessor(
    omit=['url', 'email', 'percent', 'money', 'phone', 'user',
    'time', 'url', 'date', 'number'],

    normalize=['url', 'email', 'percent', 'money', 'phone', 'user',
    'time', 'url', 'date', 'number'],

    segmenter="twitter",

    corrector="twitter",

    unpack_hashtags=True,
    unpack_contractions=True,

    tokenizer=SocialTokenizer(lowercase=True).tokenize,

    dicts=[emoticons]
    )

    #Averaging Words Vectors to Create Sentence Embedding
    class AverageEmbeddingVectorizer(object):
    def __init__(self, word2vec):
    self.word2vec = word2vec
    # if a text is empty we should return a vector of zeros
    # with the same dimensionality as all the other vectors
    self.dim =300 # because we use 100 embedding points

    def fit(self, X, y):
    return self

    def transform(self, X):
    return numpy.array([
    numpy.mean([self.word2vec[w] for w in words if w in self.word2vec]
    or [numpy.zeros(self.dim)], axis=0)
    for words in X
    ])

    #Loading data
    raw_train_tweets = pandas.read_excel('E:\\train.xlsx').iloc[:,1] #Loading all train tweets
    train_labels = pandas.read_excel('E:\\train.xlsx').iloc[:,2:13] #Loading corresponding train labels (11 emotions)

    raw_test_tweets = pandas.read_excel('E:\\test.xlsx').iloc[:300,1] #Loading 300 test tweets
    test_gold_labels = pandas.read_excel('E:\\test.xlsx').iloc[:300,2:13] #Loading corresponding test labels (11 emotions)
    print("please wait")

    #Pre-Processing
    train_tweets=[]
    test_tweets=[]
    for tweets in raw_train_tweets:
    train_tweets.append(pre_processor.pre_process_doc(tweets))
    train_tweets=pandas.Series(train_tweets)

    for tweets in raw_test_tweets:
    test_tweets.append(pre_processor.pre_process_doc(tweets))
    test_tweets=pandas.Series(test_tweets)
    all_tweets = train_tweets.append(test_tweets)
    all_tweets=(all_tweets.apply(lambda x:str(x).strip().split()))

    #Vectorizing
    vector_maker = Word2Vec(all_tweets, size=300, min_count=2, workers=6) #Vectorizer
    words_vectors=dict(zip(vector_maker.wv.index2word, vector_maker.wv.vectors))

    pipe1=Pipeline([("wordVectz",AverageEmbeddingVectorizer(words_vectors)),("multilabel",tree.DecisionTreeClassifier())])
    pipe1.fit(train_tweets,train_labels)

    predicted=pipe1.predict(test_tweets)
    print("Accuracy = ",accuracy_score(test_gold_labels,predicted))

    Continue reading...

Compartilhe esta Página