微信好友特征数据分析及可视化
一、研究背景及現(xiàn)狀
在我國互聯(lián)網(wǎng)的發(fā)展過程中,PC互聯(lián)網(wǎng)已日趨飽和,移動互聯(lián)網(wǎng)卻呈現(xiàn)井噴式發(fā)展。數(shù)據(jù)顯示,截止2013年底,中國手機(jī)網(wǎng)民超過5億,占比達(dá)81%。伴隨著移動終端價格的下降及wifi的廣泛鋪設(shè),移動網(wǎng)民呈現(xiàn)爆發(fā)趨勢。
微信已經(jīng)成為連接線上與線下、虛擬與現(xiàn)實、消費與產(chǎn)業(yè)的重要工具,它提高了O2O類營銷用戶的轉(zhuǎn)化率。過去開發(fā)軟件,程序員常要考慮不同開發(fā)環(huán)境的語言、設(shè)備的適配性和成本。現(xiàn)在,開發(fā)者可以在一個“類操作底層”去開發(fā)應(yīng)用,打破了過去受限的開發(fā)環(huán)境。
二、研究意義及目的
隨著寬帶無線接入技術(shù)和移動終端技術(shù)的飛速發(fā)展,人們迫切希望能夠隨時隨地乃至在移動過程中都能方便地從互聯(lián)網(wǎng)獲取信息和服務(wù),移動互聯(lián)網(wǎng)應(yīng)運而生并迅猛發(fā)展。然而,移動互聯(lián)網(wǎng)在移動終端、接入網(wǎng)絡(luò)、應(yīng)用服務(wù)、安全與隱私保護(hù)等方面還面臨著一系列的挑戰(zhàn)。其基礎(chǔ)理論與關(guān)鍵技術(shù)的研究,對于國家信息產(chǎn)業(yè)整體發(fā)展具有重要的現(xiàn)實意義。
三、研究內(nèi)容及數(shù)據(jù)獲取
配置完python運行環(huán)境之后,啟動程序自動彈出網(wǎng)頁版微信登錄二維碼
普通用戶使用手機(jī)掃描二維碼登錄微信網(wǎng)頁版,并在手機(jī)上確認(rèn)后,wxpy自動從用戶的網(wǎng)頁版微信獲取好友列表,包括好友昵稱、所在地區(qū)、個性簽名、性別等信息。
四、python程序設(shè)計
#微信好友特征數(shù)據(jù)分析及可視化 # 1.導(dǎo)包操作 from wxpy import * import re import jieba import numpy as np from scipy.misc import imread from wordcloud import WordCloud import matplotlib.pyplot as plt from pylab import mpl import pandas as pdmpl.rcParams['font.sans-serif'] = ['SimHei'] from pyecharts.charts import Map from pyecharts import options as opts# 2.登錄操作 bot = Bot() # 列舉登錄賬號的所有好友 all_friends = bot.friends() print(all_friends)# 獲取登錄賬號所關(guān)注的所有的公眾號 all_maps = bot.mps() print("所有好友列表", all_maps)# 獲取當(dāng)前登錄賬號的群聊列表 all_groups = bot.groups() print("所有群聊列表", all_groups)# 根據(jù)好友的備注名稱搜索好友 #myfriend = bot.friends().search('許寬')[0] #print("搜索好友:", myfriend)# 搜索好友并發(fā)送信息 # bot.friends().search('許寬')[0].send('你好呀')# 向文件傳輸助手發(fā)送信息 bot.file_helper.send("hello")# 3.顯示男女比例 sex_dict = {'male': 0, 'female': 0, "no_set": 0} for friend in all_friends:print(friend, friend.sex)if friend.sex == 1:sex_dict['male'] += 1elif friend.sex == 2:sex_dict['female'] += 1elif friend.sex == 0:sex_dict['no_set'] += 1 print(sex_dict)# 4使用matplotlib可視化 slices = [sex_dict["male"], sex_dict["female"], sex_dict["no_set"]] activities = ["male", "female", "no_set"] cols = ["r", "m", "g"] # startangle:開始繪圖的角度,逆時針旋轉(zhuǎn) # shadow:陰影 # %1.1f%%:格式化字符串,整數(shù)部分最小1位,小數(shù)點后保留一位,%%:轉(zhuǎn)義字符 plt.pie(slices, labels=activities, colors=cols, startangle=90, shadow=True, autopct='%1.1f%%') plt.title("微信好友比例圖") plt.savefig("WeChat_sex.png")# 統(tǒng)計登錄賬號好友的各省人數(shù) province_dict = {'河北': 0, '山東': 0, '遼寧': 0, '廣西': 0, '吉林': 0,'甘肅': 0, '青海': 0, '河南': 0, '江蘇': 0, '湖北': 0,'湖南': 0, '江西': 0, '浙江': 0, '廣東': 0, '云南': 0,'福建': 0, '臺灣': 0, '海南': 0, '山西': 0, '四川': 0,'陜西': 0, '貴州': 0, '安徽': 0, '北京': 0, '天津': 0,'重慶': 0, '上海': 0, '香港': 0, '澳門': 0, '新疆': 0,'內(nèi)蒙古': 0, '西藏': 0, '黑龍江': 0, '寧夏': 0} # 統(tǒng)計省份 for friend in all_friends:# print(friend.province)if friend.province in province_dict.keys():province_dict[friend.province] += 1print("province_dict") print(province_dict)# 為了方便數(shù)據(jù)呈現(xiàn),生成JSON Array格式數(shù)據(jù) data = [] for key, value in province_dict.items():data.append({'name': key, 'value': value}) # 在data列表末尾添加一個字典元素 print(data)data_process = pd.DataFrame(data) # 創(chuàng)建數(shù)據(jù)框 data_process.columns = ['city', 'popu'] print(data_process)map = Map().add("微信好友城市分布圖", [list(z) for z in zip(data_process['city'], data_process['popu'])],"china").set_global_opts(title_opts=opts.TitleOpts(title="Map-VisualMap(連續(xù)型)"), visualmap_opts=opts.VisualMapOpts(max_=10)) map.render('map.html')# with...as...語句結(jié)束時,自動調(diào)用f.close() # a表示:在文件末尾追加 def write_txt_file(path, txt): # 寫文件with open(path, 'a', encoding='gbk') as f:return f.write(txt)# 每次運行程序前,需要刪除上一次的文件 # 默認(rèn)字符編碼為GBK def read_txt_file(path):with open(path, 'r', encoding='gbk') as f:return f.read()# 統(tǒng)計登錄賬號好友個性簽名 for friend in all_friends:print(friend, friend.signature)# 對數(shù)據(jù)進(jìn)行清洗,將標(biāo)點符號等對詞頻率統(tǒng)計造成影響的因素剔除# [...]:匹配中括號任意一個字符# r:防止轉(zhuǎn)義pattern = re.compile(r'[一-龥]+') # 將正則字符串編譯成正則表達(dá)式對象,以后在后期的匹配中復(fù)用# 對某一個簽名進(jìn)行匹配,只匹配中文漢字,結(jié)果是列表filterdata = re.findall(pattern, friend.signature)print(filterdata)write_txt_file('signatures.txt', ''.join(filterdata))# 讀取文件并輸出。 content = read_txt_file('signatures.txt') print(content) # 輸出內(nèi)容,僅漢字# 輸出分詞結(jié)果,結(jié)果為列表 segment = jieba.lcut(content) # 精確模式:不存在冗余數(shù)據(jù),適合文本分析 print(segment)# 生成數(shù)據(jù)框且有一列元素 word_df = pd.DataFrame({'segment': segment}) # 字典類型 print(word_df)# index_col=False:第一行不做為索引 # seq=" ":分隔符 # names=['stopword']:列名 # "stopwords.txt":停止詞庫 stopwords = pd.read_csv("stopwords.txt", index_col=False, sep=" ", names=['stopword'], encoding='gbk') print(stopwords)# 查看過濾停止詞后的數(shù)據(jù)框 word_df = word_df[~word_df.segment.isin(stopwords.stopword)] print("過濾后:") print(word_df)# 查看分詞的詞頻 # python中的groupby可以看作是基于行或者是基于index的聚合操作 # agg函數(shù)提供基于列的聚合操作,一般與groupby連用 # np.size:numpy庫中統(tǒng)計一列中不同值的個數(shù) words_stat = word_df.groupby(by=['segment'])['segment'].agg({"計數(shù)": np.size}) # 警告信息 print(words_stat)# 根據(jù)計數(shù)這一列降序排列 words_stat = words_stat.reset_index().sort_values(by=["計數(shù)"], ascending=False) print(words_stat)# 讀入背景圖片 color_mask = imread("black_mask.png")# 設(shè)置詞云屬性 wordcloud = WordCloud(font_path="Hiragino.ttf", # 設(shè)置字體background_color="pink", # 設(shè)置顏色max_words=100, # 詞云顯示的最大詞數(shù)mask=color_mask, # 設(shè)置背景圖片max_font_size=100 # 字體最大值) # 生成詞云字典,獲取詞云最高的前一百詞 word_frequence = {x[0]: x[1] for x in words_stat.head(100).values} print(word_frequence)# 繪制詞云圖 wordcloud.generate_from_frequencies(word_frequence) wordcloud.to_file("wordcloud.png")# 對圖像進(jìn)行處理 plt.imshow(wordcloud) plt.axis("off") # 隱藏坐標(biāo)軸 plt.show()五、數(shù)據(jù)分析及可視化
聲明
祖國領(lǐng)土主權(quán)神圣不可侵犯!
部分地區(qū)未標(biāo)注,敬請諒解!
總結(jié)
以上是生活随笔為你收集整理的微信好友特征数据分析及可视化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【归并排序】-求逆序数算法
- 下一篇: 【建议收藏】二叉树的序列化与反序列化