關(guān)于數(shù)據(jù)的清洗,實際上在上一一篇文章關(guān)于抓取數(shù)據(jù)的過程中已經(jīng)做了一部分,后面我又做了一下用戶數(shù)據(jù)的抓取
歌曲評論:
包括后臺返回的空用戶信息、重復(fù)數(shù)據(jù)的去重等。除此之外,還要進(jìn)行一些清洗:用戶年齡錯誤、用戶城市編碼轉(zhuǎn)換等。
關(guān)于數(shù)據(jù)的去重,評論部分可以以sommentId為數(shù)據(jù)庫索引,利用數(shù)據(jù)庫來自動去重;用戶信息部分以用戶ID為數(shù)據(jù)庫索引實現(xiàn)自動去重。
API返回的用戶年齡一般是時間戳的形式(以毫秒計)、有時候也會返回一個負(fù)值或者一個大于當(dāng)前時間的值,暫時沒有找到這兩種值代表的含義,故而一律按0來處理。
API返回的用戶信息中,城市分為province和city兩個字段,本此分析中只保存了city字段。實際上字段值是一個城市code碼,具體對照在這里下載。
利用Python的數(shù)據(jù)處理庫pandas進(jìn)行數(shù)據(jù)處理,利用可視化庫pyecharts進(jìn)行數(shù)據(jù)可視化。
分別查看下面分析結(jié)果。
import pandas as pd import pymysql from pyecharts import Bar,Pie,Line,Scatter,Map TABLE_COMMENTS = '****' TABLE_USERS = '****' DATABASE = '****' conn = pymysql.connect(host='localhost', user='****', passwd='****', db=DATABASE, charset='utf8mb4') sql_users = 'SELECT id,gender,age,city FROM '+TABLE_USERS sql_comments = 'SELECT id,time FROM '+TABLE_COMMENTS comments = pd.read_sql(sql_comments, con=conn) users = pd.read_sql(sql_users, con=conn) # 評論時間(按天)分布分析 comments_day = comments['time'].dt.date data = comments_day.id.groupby(comments_day['time']).count() line = Line('評論時間(按天)分布') line.use_theme('dark') line.add( '', data.index.values, data.values, is_fill=True, ) line.render(r'./評論時間(按天)分布.html') # 評論時間(按小時)分布分析 comments_hour = comments['time'].dt.hour data = comments_hour.id.groupby(comments_hour['time']).count() line = Line('評論時間(按小時)分布') line.use_theme('dark') line.add( '', data.index.values, data.values, is_fill=True, ) line.render(r'./評論時間(按小時)分布.html') # 評論時間(按周)分布分析 comments_week = comments['time'].dt.dayofweek data = comments_week.id.groupby(comments_week['time']).count() line = Line('評論時間(按周)分布') line.use_theme('dark') line.add( '', data.index.values, data.values, is_fill=True, ) line.render(r'./評論時間(按周)分布.html') # 用戶年齡分布分析 age = users[users['age']>0] # 清洗掉年齡小于1的數(shù)據(jù) age = age.id.groupby(age['age']).count() # 以年齡值對數(shù)據(jù)分組 Bar = Bar('用戶年齡分布') Bar.use_theme('dark') Bar.add( '', age.index.values, age.values, is_fill=True, ) Bar.render(r'./用戶年齡分布圖.html') # 生成渲染的html文件 # 用戶地區(qū)分布分析 # 城市code編碼轉(zhuǎn)換 def city_group(cityCode): city_map = { '11': '北京', '12': '天津', '31': '上海', '50': '重慶', '5e': '重慶', '81': '香港', '82': '澳門', '13': '河北', '14': '山西', '15': '內(nèi)蒙古', '21': '遼寧', '22': '吉林', '23': '黑龍江', '32': '江蘇', '33': '浙江', '34': '安徽', '35': '福建', '36': '江西', '37': '山東', '41': '河南', '42': '湖北', '43': '湖南', '44': '廣東', '45': '廣西', '46': '海南', '51': '四川', '52': '貴州', '53': '云南', '54': '西藏', '61': '陜西', '62': '甘肅', '63': '青海', '64': '寧夏', '65': '新疆', '71': '臺灣', '10': '其他', } return city_map[cityCode[:2]] city = users['city'].apply(city_group) city = city.id.groupby(city['city']).count() map_ = Map('用戶地區(qū)分布圖') map_.add( '', city.index.values, city.values, maptype='china', is_visualmap=True, visual_text_color='#000', is_label_show=True, ) map_.render(r'./用戶地區(qū)分布圖.html')
可視化結(jié)果
評論時間按周分布圖可以看出,評論數(shù)在一周當(dāng)中前面較少,后面逐漸增多,這可以解釋為往后接近周末,大家有更多時間來聽聽歌、刷刷歌評,而一旦周末過完,評論量馬上下降(周日到周一的下降過渡),大家又回歸到工作當(dāng)中。
評論時間按小時分布圖可以看出,評論數(shù)在一天當(dāng)中有兩個小高峰:11點-13點和22點-0點。這可以解釋為用戶在中午午飯時間和晚上下班(課)在家時間有更多的時間來聽歌刷評論,符合用戶的日常。至于為什么早上沒有出現(xiàn)一個小高峰,大概是早上大家都在搶時間上班(學(xué)),沒有多少時間去刷評論。
https://blog.csdn.net/u011371360
用戶年齡分布圖可以看出,用戶大多集中在14-30歲之間,以20歲左右居多,除去虛假年齡之外,這個年齡分布也符合網(wǎng)易云用戶的年齡段。圖中可以看出28歲有個高峰,猜測可能是包含了一些異常數(shù)據(jù),有興趣的化可以做進(jìn)一步分析。
用戶地區(qū)分布圖可以看出,用戶涵蓋了全國各大省份,因為中間數(shù)據(jù)(坑)的缺失,并沒有展現(xiàn)出哪個省份特別突出的情況。對別的歌評(完全數(shù)據(jù))的可視化分析,可以看出明顯的地區(qū)分布差異。
此次分析只是對某一首歌曲評論時間、用戶年齡/地區(qū)分布進(jìn)行的,實際上抓取到的信息不僅僅在于此,可以做進(jìn)一步分析(比如利用評論內(nèi)容進(jìn)行文本內(nèi)容分析等),這部分,未來會進(jìn)一步分析。當(dāng)然也可以根據(jù)自己情況對不同歌曲進(jìn)行分析。
情感分析采用Python的文本分析庫snownlp,代碼如下:
import numpy as np import pymysql from snownlp import SnowNLP from pyecharts import Bar TABLE_COMMENTS = '****' DATABASE = '****' SONGNAME = '****' def getText(): conn = pymysql.connect(host='localhost', user='root', passwd='root', db=DATABASE, charset='utf8') sql = 'SELECT id,content FROM '+TABLE_COMMENTS text = pd.read_sql(sql%(SONGNAME), con=conn) return text def getSemi(text): text['content'] = text['content'].apply(lambda x:round(SnowNLP(x).sentiments, 2)) semiscore = text.id.groupby(text['content']).count() bar = Bar('評論情感得分') bar.use_theme('dark') bar.add( '', y_axis = semiscore.values, x_axis = semiscore.index.values, is_fill=True, ) bar.render(r'情感得分分析.html') text['content'] = text['content'].apply(lambda x:1 if x>0.5 else -1) semilabel = text.id.groupby(text['content']).count() bar = Bar('評論情感標(biāo)簽') bar.use_theme('dark') bar.add( '', y_axis = semilabel.values, x_axis = semilabel.index.values, is_fill=True, ) bar.render(r'情感標(biāo)簽分析.html')
結(jié)果:
詞云生成采用jieba分詞庫分詞,wordcloud生成詞云,代碼如下:
from wordcloud import WordCloud import matplotlib.pyplot as plt plt.style.use('ggplot') plt.rcParams['axes.unicode_minus'] = False def getWordcloud(text): text = ''.join(str(s) for s in text['content'] if s) word_list = jieba.cut(text, cut_all=False) stopwords = [line.strip() for line in open(r'./StopWords.txt', 'r').readlines()] # 導(dǎo)入停用詞 clean_list = [seg for seg in word_list if seg not in stopwords] #去除停用詞 clean_text = ''.join(clean_list) # 生成詞云 cloud = WordCloud( font_path = r'C:/Windows/Fonts/msyh.ttc', background_color = 'white', max_words = 800, max_font_size = 64 ) word_cloud = cloud.generate(clean_text) # 繪制詞云 plt.figure(figsize=(12, 12)) plt.imshow(word_cloud) plt.axis('off') plt.show() if __name__ == '__main__': text = getText() getSemi(text) getWordcloud(text)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
標(biāo)簽:海南 南平 普洱 林芝 漯河 盤錦 寧夏 大同
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《用Python實現(xiàn)網(wǎng)易云音樂的數(shù)據(jù)進(jìn)行數(shù)據(jù)清洗和可視化分析》,本文關(guān)鍵詞 用,Python,實現(xiàn),網(wǎng)易,云,音,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。