首页 > 解决方案 > Python中的多个名称类型错误wordcloud

问题描述

我很混乱。我正在尝试为我的数据集制作一个 wordcloud,但我不断收到我似乎无法弄清楚的错误......数据集不是文本,它是一个 csv 文件。

我在此代码之上有一个完整的代码,用于清理数据并添加情绪分析列等。但这是我代码的 wordcloud 部分:

#create wordcloud to see what kind of words apear in the reviews
from wordcloud import WordCloud
import matplotlib.pyplot as plt

def show_wordcloud(data, title = None):
    wordcloud = WordCloud(
        background_color='white',
        max_words=200,
        max_font_size=40,
        scale=3,
        random_state=42
    ).generate(str(data))


fig = plt.figure(1, figsize = (20,20))
plt.axis('off')
plt.title('Review words')
if title:
        fig.suptitle(title, fontsize = 20)
        fig.subplots_adjust(top = 2.3)

plt.imshow(wordcloud)
plt.show()

#print wordcloud
show_wordcloud(reviews_df['review'])

我的 Python 终端的错误是:

>>> from wordcloud import WordCloud
import matplotlib.pyplot as plt
def show_wordcloud(data, title = None):
    wordcloud = WordCloud(
        background_color='white',
        max_words=200,
        max_font_size=40,
        scale=3,
        random_state=42
    ).generate(str(data))

fig = plt.figure(1, figsize = (20,20))
plt.axis('off')
plt.title('Review words')
if title:
        fig.suptitle(title, fontsize = 20)
        fig.subplots_adjust(top = 2.3)

plt.imshow(wordcloud)
plt.show()
#print wordcloud

show_wordcloud(reviews_df['review'])
>>> import matplotlib.pyplot as plt
>>> def show_wordcloud(data, title = None):
...     wordcloud = WordCloud(
...         background_color='white',
...         max_words=200,
...         max_font_size=40,
...         scale=3,
...         random_state=42
...     ).generate(str(data))
... 
>>> fig = plt.figure(1, figsize = (20,20))
>>> plt.axis('off')
(0.0, 1.0, 0.0, 1.0)
>>> plt.title('Review words')
Text(0.5, 1.0, 'Review words')
>>> if title:
...         fig.suptitle(title, fontsize = 20)
...         fig.subplots_adjust(top = 2.3)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
**NameError: name 'title' is not defined**
>>> plt.imshow(wordcloud)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
**NameError: name 'wordcloud' is not defined**
>>> plt.show()
>>> #print wordcloud
>>> 
>>> show_wordcloud(reviews_df['review'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
**NameError: name 'reviews_df' is not defined**
>>> 

我究竟做错了什么??有人可以帮忙吗?

标签: pythonmatplotlibmachine-learningsvmword-cloud

解决方案


推荐阅读