国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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ó)家法律
马尔康县| 泰和县| 绥芬河市| 奉贤区| 苍梧县| 新和县| 沁水县| 塔河县| 淄博市| 淮阳县| 东莞市| 夹江县| 赤水市| 白玉县| 武隆县| 始兴县| 恩施市| 义马市| 通山县| 夏津县| 浠水县| 临沭县| 吴江市| 怀柔区| 临泽县| 田东县| 高邑县| 富源县| 湘阴县| 哈尔滨市| 全州县| 喀什市| 兴安盟| 叶城县| 慈利县| 开原市| 红河县| 浙江省| 台南县| 庆云县| 兰西县|