使用泡菜加速sklearn /机器学习的分类任务?

时间:2015-10-06 14:09:06

标签: python machine-learning scikit-learn classification text-classification

我已经训练了一个分类器,我通过泡菜加载。 我的主要疑问是,是否有任何东西可以加快分类任务。每个文本花费近1分钟(特征提取和分类),这是正常的吗?我应该继续进行多线程吗?

这里有一些代码片段可以看到整体流程:

for item in items:
    review = ''.join(item['review_body'])
    review_features = getReviewFeatures(review)
    normalized_predicted_rating = getPredictedRating(review_features)
    item_processed['rating'] = str(round(float(normalized_predicted_rating),1))

def getReviewFeatures(review, verbose=True):

    text_tokens = tokenize(review)

    polarity = getTextPolarity(review)

    subjectivity = getTextSubjectivity(review)

    taggs = getTaggs(text_tokens)

    bigrams = processBigram(taggs)
    freqBigram = countBigramFreq(bigrams)
    sort_bi = sortMostCommun(freqBigram)

    adjectives = getAdjectives(taggs)
    freqAdjectives = countFreqAdjectives(adjectives)
    sort_adjectives = sortMostCommun(freqAdjectives)

    word_features_adj = list(sort_adjectives)
    word_features = list(sort_bi)

    features={}
    for bigram,freq in word_features:
        features['contains(%s)' % unicode(bigram).encode('utf-8')] = True
        features["count({})".format(unicode(bigram).encode('utf-8'))] = freq

    for word,freq in word_features_adj:
        features['contains(%s)' % unicode(word).encode('utf-8')] = True
        features["count({})".format(unicode(word).encode('utf-8'))] = freq

    features["polarity"] = polarity
    features["subjectivity"] = subjectivity

    if verbose:
        print "Get review features..."    

    return features


def getPredictedRating(review_features, verbose=True):
    start_time = time.time()
    classifier = pickle.load(open("LinearSVC5.pickle", "rb" ))

    p_rating = classifier.classify(review_features) # in the form of "# star"
    predicted_rating = re.findall(r'\d+', p_rating)[0]
    predicted_rating = int(predicted_rating)

    best_rating = 5
    worst_rating = 1
    normalized_predicted_rating = 0
    normalized_predicted_rating = round(float(predicted_rating)*float(10.0)/((float(best_rating)-float(worst_rating))+float(worst_rating)))

    if verbose:
        print "Get predicted rating..."
        print "ML_RATING: ", normalized_predicted_rating
        print("---Took %s seconds to predict rating for the review---" % (time.time() - start_time)) 

    return normalized_predicted_rating

2 个答案:

答案 0 :(得分:1)

NLTK是一个很好的工具,也是自然语言处理的一个很好的起点,但如果速度很重要,它有时并不是很有用,正如作者暗示的那样:

  

NLTK被称为“使用Python进行计算语言学教学和工作的绝佳工具”,以及“使用自然语言进行游戏的神奇图书馆。”

因此,如果您的问题仅在于工具包分类器的速度,则必须使用其他资源,或者您必须自己编写分类器。

如果您想使用可能更快的分类器,

Scikit可能对您有所帮助。

答案 1 :(得分:1)

您似乎使用dictionary来构建要素向量。我强烈怀疑问题就在那里。

正确的方法是使用numpy ndarray,并在列上显示行和要素的示例。所以,像

import numpy as np
# let's suppose 6 different features = 6-dimensional vector
feats = np.array((1, 6))
# column 0 contains polarity, column 1 subjectivity, and so on..
feats[:, 0] = polarity
feats[:, 1] = subjectivity
# ....
classifier.classify(feats)

当然,您必须使用相同的数据结构并在培训期间遵守相同的惯例。