{"id":10006797,"date":"2022-11-03T21:50:29","date_gmt":"2022-11-03T13:50:29","guid":{"rendered":"https:\/\/ityet.com\/?p=10006797"},"modified":"2022-11-03T21:50:29","modified_gmt":"2022-11-03T13:50:29","slug":"%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e7%82%ab%e9%85%b7%e7%9a%84%e8%af%8d%e4%ba%91%ef%bc%88%e5%b7%b2%e9%aa%8c%e8%af%81%ef%bc%89","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006797","title":{"rendered":"\u5982\u4f55\u751f\u6210\u70ab\u9177\u7684\u8bcd\u4e91\uff08\u5df2\u9a8c\u8bc1\uff09"},"content":{"rendered":"<h2>wordcloud\u662f\u4ec0\u4e48\uff1f<\/h2>\n<p>wordcloud\u662fpython\u7684\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u79f0\u4e3a\u8bcd\u4e91\u4e5f\u53eb\u505a\u6587\u5b57\u4e91\uff0c\u662f\u6839\u636e\u6587\u672c\u4e2d\u7684\u8bcd\u9891\uff0c\u5bf9\u5185\u5bb9\u8fdb\u884c\u53ef\u89c6\u5316\u7684\u6c47\u603b\uff0c\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u7528\u6237\u753b\u50cf\u3002<\/p>\n<h2>\u5b89\u88c5wordcloud<\/h2>\n<p>\u8fd9\u91cc\u7528conda\u65b0\u5efa\u4e00\u4e2a\u73af\u5883\u6765\u6f14\u793a<\/p>\n<pre><code>conda create --name wordcloud python=3.9.12\nconda activate wordcloud<\/code><\/pre>\n<p>\u9700\u8981\u5b89\u88c54\u4e2a\u5305<\/p>\n<pre><code>conda install -y -n wordcloud numpy\nconda install -y -n wordcloud pillow\nconda install -y -n wordcloud matplotlib\nconda install -y -n wordcloud wordcloud\n<\/code><\/pre>\n<h2>\u4f7f\u7528 wordcloud<\/h2>\n<p>wordcloud\u5e93\u628a\u8bcd\u4e91\u5f53\u4f5c\u4e00\u4e2a WordCloud \u5bf9\u8c61\uff0c\u6240\u4ee5\u4f7f\u7528wordcloud\u53ef\u4ee5\u5927\u81f4\u5206\u4e3a\u4e09\u4e2a\u6b65\u9aa4<\/p>\n<pre><code>from wordcloud import WordCloud\n\nwc= WordCloud() # 1.\u521b\u5efa\u5bf9\u8c61\uff1a    \nwc.generate(&quot;Choose a life of action, not one of oste-ntation.&quot;) # 2.\u52a0\u8f7d\u6587\u672c\nwc.to_file(&quot;.\/word.png&quot;) # 3.\u8f93\u51fa\u6587\u4ef6<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042732673\" alt=\"word\" title=\"word\" loading=\"lazy\"><\/p>\n<p><strong>wordcloud \u57fa\u7840\u5b9e\u6218<\/strong><\/p>\n<p>\u6307\u5b9a\u4e2d\u6587\u5b57\u4f53\uff0c\u4e0d\u7136\u4f1a\u4e71\u7801<\/p>\n<pre><code>from PIL import Image\nfrom wordcloud import WordCloud, STOPWORDS\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nwc = WordCloud(background_color=&quot;white&quot;,# \u8bbe\u7f6e\u80cc\u666f\u989c\u8272\n           max_words=2000, # \u8bcd\u4e91\u663e\u793a\u7684\u6700\u5927\u8bcd\u6570\n           height=400, # \u56fe\u7247\u9ad8\u5ea6\n           width=800, # \u56fe\u7247\u5bbd\u5ea6\n           max_font_size=50, #\u6700\u5927\u5b57\u4f53     \n           stopwords=STOPWORDS, # \u8bbe\u7f6e\u505c\u7528\u8bcd\n           font_path='C:\/Windows\/Fonts\/simhei.ttf', # \u517c\u5bb9\u4e2d\u6587\u5b57\u4f53\uff0c\u4e0d\u7136\u4e2d\u6587\u4f1a\u663e\u793a\u4e71\u7801\n           )\n# \u751f\u6210\u8bcd\u4e91 \nwc.generate('\u8bd7\u8bcd\uff0c\u662f\u6307\u4ee5\u53e4\u4f53\u8bd7\u3001\u5f20\u4e09\u3002 \u4ea6\u662f\u6c49\u5b57\u6587\u5316\u5708\u7684\u7279\u8272\u4e4b\u4e00\u3002 \u674e\u56db\u3002 \u8bd7\u8bcd\u662f\u9610\u8ff0\u5fc3\u7075\u7684\u6587\u5b66\u827a\u672f\uff0c\u800c\u8bd7\u4eba\u3001\u8d75\u6587\uff0c\u5e76\u6309\u7167\u4e25\u683c\u97f5\u5f8b\u8981\u6c42\uff0c\u7528\u51dd\u7ec3\u7684\u8bed\u8a00\u3001\u7ef5\u5bc6\u7684\u7ae0\u6cd5\u3001\u738b\u4e94\u3002') # \u6b64\u5904\u7684text\u4fbf\u662f\u5206\u597d\u8bcd\u768419\u5927\u6587\u672c\n\n# \u751f\u6210\u7684\u8bcd\u4e91\u56fe\u50cf\u4fdd\u5b58\u5230\u672c\u5730\nwc.to_file(&quot;.\/result2.png&quot;)\n\n# \u663e\u793a\u56fe\u50cf\nplt.imshow(wc, interpolation='bilinear')\n# interpolation='bilinear' \u8868\u793a\u63d2\u503c\u65b9\u6cd5\u4e3a\u53cc\u7ebf\u6027\u63d2\u503c\nplt.axis(&quot;off&quot;)# \u5173\u6389\u56fe\u50cf\u7684\u5750\u6807\nplt.show()<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042732674\" alt=\"result2\" title=\"result2\" loading=\"lazy\"><\/p>\n<p><strong>\u9644\u4e0awordcloud\u5185\u7684\u914d\u7f6e\u53c2\u6570<\/strong><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042732675\" alt=\"img\" title=\"img\" loading=\"lazy\"><\/p>\n<h2><strong>wordcloud \u9ad8\u7ea7\u5b9e\u6218<\/strong><\/h2>\n<p>\u5728\u672c\u8282\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c06\u8bcd\u4e91\u7ed8\u5236\u5728\u9884\u8bbe\u5b9a\u597d\u7684\u80cc\u666f\u56fe\u7247\u4e0a\u3002\u4f8b\u5982\uff1a\u4ee5\u6211\u4eec\u6700\u719f\u6089\u7684\u4e2d\u56fd\u5730\u56fe\u4e3a\u80cc\u666f\uff0c\u6211\u4eec\u5c06\u5728\u6b64\u57fa\u7840\u4e0a\u7ed8\u5236\u8bcd\u4e91\uff0c\u53ea\u9700\u4fee\u6539wordcloud\u91cc\u7684\u201cmask\u201d\u5c5e\u6027\u503c\uff0c\u8be6\u7ec6\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre><code>from PIL import Image\nfrom wordcloud import WordCloud, STOPWORDS\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nwc = WordCloud(background_color=&quot;white&quot;,# \u8bbe\u7f6e\u80cc\u666f\u989c\u8272\n           max_words=2000, # \u8bcd\u4e91\u663e\u793a\u7684\u6700\u5927\u8bcd\u6570\n           max_font_size=60, #\u6700\u5927\u5b57\u4f53     \n           stopwords=STOPWORDS, # \u8bbe\u7f6e\u505c\u7528\u8bcd\n            mask=np.array(Image.open(&quot;.\/china.jpg&quot;)), # \u9009\u62e9\u80cc\u666f\u56fe\u7247\u4e3a\u4e2d\u56fd\u5730\u56fe\n           font_path='C:\/Windows\/Fonts\/simhei.ttf', # \u517c\u5bb9\u4e2d\u6587\u5b57\u4f53\uff0c\u4e0d\u7136\u4e2d\u6587\u4f1a\u663e\u793a\u4e71\u7801\n           )\n# \u751f\u6210\u8bcd\u4e91 \nwc.generate('\u5c24\u97eb\u7d20\u3002\u798f\u654f\u535a\u3002\u5386\u7075\u79c0\u3002\u4f46\u7d2b\u4e91\u3002\u53f8\u9a6c\u6e3a\u3002\u9ece\u6893\u73a5\u3002\u6bdb\u534a\u857e\u3002\u592a\u53d4\u777f\u8303\u3002\u8912\u534e\u5a49\u3002\u52b3\u5b50\u660e\u3002\u4ef5\u82b7\u5bb9\u3002\u7d22\u542c\u67ab\u3002\u8489\u6155\u51dd\u3002\u90a2\u606c\u6b23\u3002\u5c55\u5411\u9732\u3002\u4e94\u6c5f\u96ea\u3002\u9756\u5141\u3002\u909b\u5b5f\u3002\u5929\u68a6\u5c71\u3002\u7e41\u5b64\u4e39\u3002\u8c08\u6e05\u60a6\u3002\u64cd\u4e50\u5fd7\u3002\u7532\u5c0f\u857e\u3002\u6ee1\u8fb0\u3002\u94f6\u957f\u3002\u521d\u6625\u96ea\u3002\u772d\u82e5\u4e91\u3002\u5eb9\u5609\u6021\u3002\u595a\u9189\u5de7\u3002\u95fe\u4e18\u4ee4\u6021\u3002\u51ed\u9999\u5929\u3002\u53e4\u98de\u7ae0\u3002\u538d\u8587\u3002\u5723\u83b9\u7407\u3002\u7a0b\u601d\u3002\u529f\u77e5\u3002\u90dc\u9633\u9701\u3002\u626c\u6fb9\u3002\u6208\u7eff\u8776\u3002\u859b\u5409\u6708\u3002\u51b7\u660e\u8f69\u3002\u5ec9\u5409\u661f\u3002\u4f5f\u4ee5\u8f69\u3002\u52c7\u4ee3\u5929\u3002\u82cf\u5e73\u7075') # \u6b64\u5904\u7684text\u4fbf\u662f\u5206\u597d\u8bcd\u7684\u5973\u6392\u8bc4\u8bba\u6587\u672c\n\n# \u751f\u6210\u7684\u8bcd\u4e91\u56fe\u50cf\u4fdd\u5b58\u5230\u672c\u5730\nwc.to_file(&quot;.\/result3.png&quot;)\n\n# \u663e\u793a\u56fe\u50cf\nplt.imshow(wc, interpolation='bilinear')\n# interpolation='bilinear' \u8868\u793a\u63d2\u503c\u65b9\u6cd5\u4e3a\u53cc\u7ebf\u6027\u63d2\u503c\nplt.axis(&quot;off&quot;)# \u5173\u6389\u56fe\u50cf\u7684\u5750\u6807\nplt.show()<\/code><\/pre>\n<p>\u51c6\u5907\u7684\u5730\u56fe\u662f<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042732676\" alt=\"china\" title=\"china\" loading=\"lazy\"><\/p>\n<p>\u6548\u679c\u5982\u4e0b<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042732677\" alt=\"result3\" title=\"result3\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042732671\">https:\/\/segmentfault.com\/a\/1190000042732671<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>wordcloud\u662f\u4ec0\u4e48\uff1f wordcloud\u662fpython\u7684\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u79f0\u4e3a\u8bcd\u4e91\u4e5f\u53eb\u505a\u6587\u5b57\u4e91\uff0c\u662f\u6839\u636e\u6587\u672c\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006797","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006797"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006797\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}