五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

記錄一次特征詞分析

2023-03-05 16:24 作者:暗冥_紫  | 我要投稿

先介紹一些語(yǔ)句

%matplotlib inline 可以在Ipython編譯器里直接使用,功能是可以內(nèi)嵌繪圖,并且可以省略掉plt.show()這一步,是一個(gè)魔法函數(shù)。

data['comment_star'].value_counts()? # 將data的對(duì)應(yīng)name的列的元素排序統(tǒng)計(jì)。

data.log[3]? # 提取DataFrame對(duì)應(yīng)index的那一行裝換成Series并把列的name轉(zhuǎn)為index。

data.log[3, 'x']? # 在上面的基礎(chǔ)上提取index為‘x’的元素(注意是從Series提取)

Series.index() Series.values() 分別提取Series的值與索引 輸出<class 'numpy.ndarray'>

Series類的str屬性:針對(duì)字符串進(jìn)行處理

findall() 查找所有符合正則表達(dá)式的字符并以數(shù)組形式返回,get() 獲取指定位置的字符串


import pandas as pd

from pandas import Series,DataFrame

from matplotlib import pyplot as plt

import seaborn as sns

from wordcloud import WordCloud, STOPWORDS

from PIL import Image #導(dǎo)入模塊PIL(Python Imaging Library)圖像處理庫(kù)

import numpy as np #導(dǎo)入模塊numpy,多維數(shù)組

import matplotlib

%matplotlib inline


data = pd.read_csv('input.csv')

data

data.loc[data['comment_star'] == 'sml-str1','comment_star'] = 'sml-str10'

# 將data"comment_star"列的數(shù)據(jù)'sml-str1'轉(zhuǎn)為'sml-str10'


data['stars'] = data['comment_star'].str.findall(r'\d+').str.get(0)? # 提取數(shù)字?

data['stars']

data['stars'] = data['stars'].astype('float')/10? # 將分?jǐn)?shù)縮小10倍

sns.countplot(data=data, x='stars')

sns.countplot(data=data, x='stars')
data['shopID'].value_counts()
sns.boxplot(data=data, x='shopID', y='stars')

data.comment_time = pd.to_datetime(data.comment_time.str.findall(r'\d{4}-\d{2}-\d{2} .+').str.get(0))? # 切分時(shí)間方便之后分析

data['year'] = data.comment_time.dt.year

data['month'] = data.comment_time.dt.month

data['weekday'] = data.comment_time.dt.weekday

data['hour'] = data.comment_time.dt.hour

data
data['hour'].value_counts()

df=data.groupby(['hour', 'weekday']).count()? # 以指定的這兩個(gè)name進(jìn)行分組

df['cus_id'].unstack()

fig1, ax1=plt.subplots(figsize=(14,4))

print(fig1, type(ax1))

df['cus_id'].unstack().plot(ax=ax1, style='-.')

plt.show()

data['comment_len'] = data['cus_comment'].str.len()? # 查看評(píng)論長(zhǎng)度

fig2, ax2=plt.subplots()

sns.boxplot(x='stars',y='comment_len',data=data,ax=ax2)

ax2.set_ylim(0,600)? # 設(shè)置最大y值


data['cus_comment1'] = data['cus_comment'].str.replace(r'[^\u4e00-\u9fa5]','').str.replace('收起評(píng)論','')? # 使用Unicode 消去非中文字符


#中文分詞

import jieba


infile = open("stopwords.txt",encoding='utf-8')

stopwords_lst = infile.readlines()

STOPWORDS = [x.strip() for x in stopwords_lst]

stopwords = set(STOPWORDS)? # 設(shè)置停用詞

data['cus_comment1'].fillna(' ',inplace=True)? # 將None數(shù)據(jù)轉(zhuǎn)為空格

data['cus_comment1'] = data['cus_comment1'].apply(lambda x:' '.join(jieba.cut(x)))? # 分詞


data[['cus_id','stars','cus_comment1']].to_csv(path_or_buf='data-out.csv')? # 保存為csv



matplotlib.rcParams['font.sans-serif'] = ['KaiTi']#作圖的中文

matplotlib.rcParams['font.serif'] = ['KaiTi']#作圖的中文


def ciyun(shop_ID='all'):

? ? texts = data['cus_comment1']

? ? if shop_ID == 'all':

? ? ? ? text = ' '.join(texts)

? ? else:

? ? ? ? text = ' '.join(texts[data['shopID']==shop_ID])

? ? wc = WordCloud(font_path="simsun.ttc", background_color='white', max_words=100, ????????stopwords=stopwords, max_font_size=80, random_state=42, margin=3)? # 配置詞云參數(shù)

? ? wc.generate(text)? # 生成詞云

? ? plt.imshow(wc,interpolation="bilinear")? # 作圖

? ? plt.axis("off")? # 不顯示坐標(biāo)軸

? ? ? ??

ciyun(521698)


總結(jié):

主要使用pandas、matplolib、seaborn

本次程序?qū)崿F(xiàn)了分析總打分?jǐn)?shù)、商品打分?jǐn)?shù)、消費(fèi)時(shí)間,將分詞保存為csv,詞云展示評(píng)價(jià)

記錄一次特征詞分析的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
酉阳| 神木县| 洪江市| 台东县| 饶河县| 仁布县| 镇巴县| 尉氏县| 大荔县| 丹巴县| 高唐县| 许昌县| 叶城县| 象州县| 武城县| 揭西县| 从化市| 鄂州市| 囊谦县| 怀安县| 长春市| 瑞安市| 友谊县| 丹巴县| 澄城县| 大悟县| 辽源市| 萨嘎县| 富民县| 拜城县| 肃南| 河曲县| 吴堡县| 嘉峪关市| 宁夏| 扶沟县| 大理市| 陇南市| 德庆县| 安阳县| 邢台市|