溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python中微信好友特征數(shù)據(jù)分析及可視化的示例分析

發(fā)布時(shí)間:2021-08-03 12:29:52 來(lái)源:億速云 閱讀:136 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下python中微信好友特征數(shù)據(jù)分析及可視化的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

一、背景及研究現(xiàn)狀

在我國(guó)互聯(lián)網(wǎng)的發(fā)展過(guò)程中,PC互聯(lián)網(wǎng)已日趨飽和,移動(dòng)互聯(lián)網(wǎng)卻呈現(xiàn)井噴式發(fā)展。數(shù)據(jù)顯示,截止2013年底,中國(guó)手機(jī)網(wǎng)民超過(guò)5億,占比達(dá)81%。伴隨著移動(dòng)終端價(jià)格的下降及wifi的廣泛鋪設(shè),移動(dòng)網(wǎng)民呈現(xiàn)爆發(fā)趨勢(shì)。

微信已經(jīng)成為連接線上與線下、虛擬與現(xiàn)實(shí)、消費(fèi)與產(chǎn)業(yè)的重要工具,它提高了O2O類營(yíng)銷用戶的轉(zhuǎn)化率。過(guò)去開發(fā)軟件,程序員常要考慮不同開發(fā)環(huán)境的語(yǔ)言、設(shè)備的適配性和成本?,F(xiàn)在,開發(fā)者可以在一個(gè)“類操作底層”去開發(fā)應(yīng)用,打破了過(guò)去受限的開發(fā)環(huán)境。

二、研究意義及目的

隨著寬帶無(wú)線接入技術(shù)和移動(dòng)終端技術(shù)的飛速發(fā)展,人們迫切希望能夠隨時(shí)隨地乃至在移動(dòng)過(guò)程中都能方便地從互聯(lián)網(wǎng)獲取信息和服務(wù),移動(dòng)互聯(lián)網(wǎng)應(yīng)運(yùn)而生并迅猛發(fā)展。然而,移動(dòng)互聯(lián)網(wǎng)在移動(dòng)終端、接入網(wǎng)絡(luò)、應(yīng)用服務(wù)、安全與隱私保護(hù)等方面還面臨著一系列的挑戰(zhàn)。其基礎(chǔ)理論與關(guān)鍵技術(shù)的研究,對(duì)于國(guó)家信息產(chǎn)業(yè)整體發(fā)展具有重要的現(xiàn)實(shí)意義。

三、研究?jī)?nèi)容及數(shù)據(jù)獲取

普通用戶使用手機(jī)掃描二維碼并在手機(jī)上確認(rèn)后,wxpy自動(dòng)從用戶的網(wǎng)頁(yè)版微信獲取好友列表,包括好友昵稱、所在地區(qū)、個(gè)性簽名、性別等信息。

四、python程序設(shè)計(jì)

#微信好友特征數(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 pd

mpl.rcParams['font.sans-serif'] = ['SimHei']
from pyecharts.charts import Map
from pyecharts import options as opts

# 2.登錄操作
bot = Bot()
# 列舉登錄賬號(hào)的所有好友
all_friends = bot.friends()
print(all_friends)

# 獲取登錄賬號(hào)所關(guān)注的所有的公眾號(hào)
all_maps = bot.mps()
print("所有好友列表", all_maps)

# 獲取當(dāng)前登錄賬號(hào)的群聊列表
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'] += 1
  elif friend.sex == 2:
    sex_dict['female'] += 1
  elif 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:開始繪圖的角度,逆時(shí)針旋轉(zhuǎn)
# shadow:陰影
# %1.1f%%:格式化字符串,整數(shù)部分最小1位,小數(shù)點(diǎn)后保留一位,%%:轉(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)計(jì)登錄賬號(hào)好友的各省人數(shù)
province_dict = {'河北': 0, '山東': 0, '遼寧': 0, '廣西': 0, '吉林': 0,
         '甘肅': 0, '青海': 0, '河南': 0, '江蘇': 0, '湖北': 0,
         '湖南': 0, '江西': 0, '浙江': 0, '廣東': 0, '云南': 0,
         '福建': 0, '臺(tái)灣': 0, '海南': 0, '山西': 0, '四川': 0,
         '陜西': 0, '貴州': 0, '安徽': 0, '北京': 0, '天津': 0,
         '重慶': 0, '上海': 0, '香港': 0, '澳門': 0, '新疆': 0,
         '內(nèi)蒙古': 0, '西藏': 0, '黑龍江': 0, '寧夏': 0}
# 統(tǒng)計(jì)省份
for friend in all_friends:
  # print(friend.province)
  if friend.province in province_dict.keys():
    province_dict[friend.province] += 1

print("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列表末尾添加一個(gè)字典元素
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...語(yǔ)句結(jié)束時(shí),自動(dòng)調(diào)用f.close()
# a表示:在文件末尾追加
def write_txt_file(path, txt): # 寫文件
  with open(path, 'a', encoding='gbk') as f:
    return f.write(txt)


# 每次運(yùn)行程序前,需要?jiǎng)h除上一次的文件
# 默認(rèn)字符編碼為GBK
def read_txt_file(path):
  with open(path, 'r', encoding='gbk') as f:
    return f.read()


# 統(tǒng)計(jì)登錄賬號(hào)好友個(gè)性簽名
for friend in all_friends:
  print(friend, friend.signature)
  # 對(duì)數(shù)據(jù)進(jìn)行清洗,將標(biāo)點(diǎn)符號(hào)等對(duì)詞頻率統(tǒng)計(jì)造成影響的因素剔除
  # [...]:匹配中括號(hào)任意一個(gè)字符
  # r:防止轉(zhuǎn)義
  pattern = re.compile(r'[一-龥](méi)+') # 將正則字符串編譯成正則表達(dá)式對(duì)象,以后在后期的匹配中復(fù)用
  # 對(duì)某一個(gè)簽名進(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":停止詞庫(kù)
stopwords = pd.read_csv("stopwords.txt", index_col=False, sep=" ", names=['stopword'], encoding='gbk')
print(stopwords)

# 查看過(guò)濾停止詞后的數(shù)據(jù)框
word_df = word_df[~word_df.segment.isin(stopwords.stopword)]
print("過(guò)濾后:")
print(word_df)

# 查看分詞的詞頻
# python中的groupby可以看作是基于行或者是基于index的聚合操作
# agg函數(shù)提供基于列的聚合操作,一般與groupby連用
# np.size:numpy庫(kù)中統(tǒng)計(jì)一列中不同值的個(gè)數(shù)
words_stat = word_df.groupby(by=['segment'])['segment'].agg({"計(jì)數(shù)": np.size}) # 警告信息
print(words_stat)

# 根據(jù)計(jì)數(shù)這一列降序排列
words_stat = words_stat.reset_index().sort_values(by=["計(jì)數(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")

# 對(duì)圖像進(jìn)行處理
plt.imshow(wordcloud)
plt.axis("off") # 隱藏坐標(biāo)軸
plt.show()

五、數(shù)據(jù)分析及可視化

微信好友男女性別比例

python中微信好友特征數(shù)據(jù)分析及可視化的示例分析

微信好友所在省份比例

python中微信好友特征數(shù)據(jù)分析及可視化的示例分析

聲明
祖國(guó)領(lǐng)土主權(quán)神圣不可侵犯!
部分地區(qū)未標(biāo)注,敬請(qǐng)諒解!

微信好友個(gè)性簽名詞云圖

python中微信好友特征數(shù)據(jù)分析及可視化的示例分析

看完了這篇文章,相信你對(duì)“python中微信好友特征數(shù)據(jù)分析及可視化的示例分析”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI