Week12_solution
Week 12 solution¶
Copyright By PowCoder代写 加微信 powcoder
pip install wordcloud
Collecting wordcloud
Using cached wordcloud-1.8.1-cp38-cp38-macosx_10_9_x86_64.whl
Requirement already satisfied: pillow in /Users/li3/opt/anaconda3/lib/python3.8/site-packages (from wordcloud) (8.2.0)
Requirement already satisfied: numpy>=1.6.1 in /Users/li3/opt/anaconda3/lib/python3.8/site-packages (from wordcloud) (1.20.1)
Requirement already satisfied: matplotlib in /Users/li3/opt/anaconda3/lib/python3.8/site-packages (from wordcloud) (3.3.4)
Requirement already satisfied: cycler>=0.10 in /Users/li3/opt/anaconda3/lib/python3.8/site-packages (from matplotlib->wordcloud) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/li3/opt/anaconda3/lib/python3.8/site-packages (from matplotlib->wordcloud) (1.3.1)
Requirement already satisfied: python-dateutil>=2.1 in /Users/li3/opt/anaconda3/lib/python3.8/site-packages (from matplotlib->wordcloud) (2.8.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /Users/li3/opt/anaconda3/lib/python3.8/site-packages (from matplotlib->wordcloud) (2.4.7)
Requirement already satisfied: six in /Users/li3/opt/anaconda3/lib/python3.8/site-packages (from cycler>=0.10->matplotlib->wordcloud) (1.15.0)
Installing collected packages: wordcloud
Successfully installed wordcloud-1.8.1
Note: you may need to restart the kernel to use updated packages.
# Python program to generate WordCloud
# importing all necessery modules
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import pandas as pd
import csv
# Reads ‘Youtube04-Eminem.csv’ file
dFile=open(‘Youtube04-Eminem.csv’)
dReader=csv.reader(dFile)
df = list(dReader)
comment_words = ‘ ‘ # for Task 2 (a)
positive_words = ‘ ‘ # for Task 2 (b)
negative_words = ‘ ‘ # for Task 2 (c)
stopwords = set(STOPWORDS)
#print(stopwords)
# iterate through the csv file
for val in df:
comment_words = comment_words + val[3].lower() + ‘ ‘
if str(val[4])==’1’: #for positive classes
positive_words = positive_words + val[3].lower() + ‘ ‘
elif str(val[4])==’0’: #for negative classes
negative_words = negative_words + val[3].lower() + ‘ ‘
#comment_words=”it pisses people off but please take a moment to check out my music”
wordcloud1 = WordCloud(width = 400, height = 200,
background_color =’white’,
stopwords = stopwords,
min_font_size = 10).generate(comment_words)
wordcloud2 = WordCloud(width = 400, height = 200,
background_color =’white’,
stopwords = stopwords,
min_font_size = 10).generate(positive_words)
wordcloud3 = WordCloud(width = 400, height = 200,
background_color =’white’,
stopwords = stopwords,
min_font_size = 10).generate(negative_words)
# plot the WordCloud image for task (a), (b) and (c)
plt.figure(figsize = (8, 8), facecolor = None)
plt.imshow(wordcloud1)
plt.axis(“off”)
#plt.tight_layout(pad = 0)
plt.show()
plt.figure(figsize = (8, 8), facecolor = None)
plt.imshow(wordcloud2)
plt.axis(“off”)
#plt.tight_layout(pad = 0)
plt.show()
plt.figure(figsize = (8, 8), facecolor = None)
plt.imshow(wordcloud3)
plt.axis(“off”)
#plt.tight_layout(pad = 0)
plt.show()
# k_main clustering example
from sklearn.cluster import KMeans
import numpy as np
# solution for (a)
X = np.array([[1, 2, 1], [1, 4, 2], [1, 0, 0],[10, 2, 0], [10, 4, 1], [10, 0, 5]])
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)
ll = kmeans.labels_
# cluster label list [1 1 1 0 0 2]
cc = kmeans.cluster_centers_
# clusters centers
# [[10. 3. 0.5]
# [ 1. 2. 1. ]
# [10. 0. 5. ]]
# solution for (b)
pr = kmeans.predict([[0, 0, 0], [12, 3, 5], [11, 0, 6], [11, 2, 0]])
# prediction results for the input list [1 2 2 0]
[1 1 1 0 0 2]
[[10. 3. 0.5]
[ 1. 2. 1. ]
[10. 0. 5. ]]
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com