gpt4 book ai didi

python - 值错误 : A ELE probability distribution must have at least one bin

转载 作者:行者123 更新时间:2023-12-02 03:23:06 25 4
gpt4 key购买 nike

我正在尝试使用朴素贝叶斯分类器对推文的情绪进行分类。所以当我运行下面的代码时,我收到了这个错误,

ValueError:ELE 概率分布必须至少有一个 bin。

代码如下

import re,nltk

# start process_tweet
def processTweet(tweet):
# process the tweets

# Convert to lower case
tweet = tweet.lower()
# Convert www.* or https?://* to URL
tweet = re.sub('((www\.[^\s]+)|(https?://[^\s]+))', 'URL', tweet)
# Convert @username to AT_USER
tweet = re.sub('@[^\s]+', 'AT_USER', tweet)
# Remove additional white spaces
tweet = re.sub('[\s]+', ' ', tweet)
# Replace #word with word
tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
# trim
tweet = tweet.strip('\'"')
return tweet


# end
# Read the tweets one by one and process it
fp = open('/home/ashish/PyCharm_proj/twitter_sentiment/data/sampleData.txt', 'r')

line = fp.readline()
print "Processed tweets\n"
while line:
processedTweet = processTweet(line)
print processedTweet
line = fp.readline()
# end loop

#start getfeatureVector
def getFeatureVector(tweet):
featureVector = []
#split tweet into words
words = tweet.split()
for w in words:
#replace two or more with two occurrences
w = replaceTwoOrMore(w)
#strip punctuation
w = w.strip('\'"?,.')
#check if the word stats with an alphabet
val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*$", w)
#ignore if it is a stop word
if(w in stopWords or val is None):
continue
else:
featureVector.append(w.lower())
return featureVector
#end

#fp.close()
# initialize stopWords
stopWords = []

inpTweets=fp
featureList=[]
#Read the tweets one by one and process it
tweets = []
for row in inpTweets:
sentiment = row[0]
tweet = row[1]
processedTweet = processTweet(tweet)
featureVector = getFeatureVector(processedTweet, stopWords)
featureList.extend(featureVector)
tweets.append((featureVector, sentiment));
#end loop

#start extract_features
def extract_features(tweet):
tweet_words = set(tweet)
features = {}
for word in featureList:
features['contains(%s)' % word] = (word in tweet_words)
#print "Features are: "+features
return features

#end

#print "Feature List is:"+"\n"+featureList

# Remove featureList duplicates
featureList = list(set(featureList))
training_set = nltk.classify.util.apply_features(extract_features, tweets)
# start replaceTwoOrMore
def replaceTwoOrMore(s):
# look for 2 or more repetitions of character and replace with the character itself
pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
return pattern.sub(r"\1\1", s)


# end

# start getStopWordList
def getStopWordList(stopWordListFileName):
# read the stopwords file and build a list
stopWords = []
stopWords.append('AT_USER')
stopWords.append('URL')

fp = open(stopWordListFileName, 'r')
line = fp.readline()
while line:
word = line.strip()
stopWords.append(word)
line = fp.readline()
fp.close()
return stopWords


# end

# start getfeatureVector
def getFeatureVector(tweet):
featureVector = []
# split tweet into words
words = tweet.split()
for w in words:
# replace two or more with two occurrences
w = replaceTwoOrMore(w)
# strip punctuation
w = w.strip('\'"?,.')
# check if the word stats with an alphabet
val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*$", w)
# ignore if it is a stop word
if (w in stopWords or val is None):
continue
else:
featureVector.append(w.lower())
return featureVector


# Train the classifier
NBClassifier = nltk.NaiveBayesClassifier.train(training_set)

# Test the classifier
testTweet = 'Congrats @ashish, The classifier works'
processedTestTweet = processTweet(testTweet)
print NBClassifier.classify(extract_features(getFeatureVector(processedTestTweet)))


# end

# Read the tweets one by one and process it
fp = open('/home/ashish/PyCharm_proj/twitter_sentiment/data/sampleData.txt', 'r')

line = fp.readline()

stopWords = getStopWordList('/home/ashish/PyCharm_proj/twitter_sentiment/data/feature_list/stopwords.txt')
print "\n Feature vectors are:\n "
while line:
processedTweet = processTweet(line)
featureVector = getFeatureVector(processedTweet)
print featureVector
line = fp.readline()
# end loop
fp.close()

我怎么解决这个问题。
谢谢

最佳答案

您必须首先为训练数据创建字典格式。如果您查看 .train() 的文档你会发现很多细节。

关于python - 值错误 : A ELE probability distribution must have at least one bin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31937797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com