import pandas as pd
usecols = ["title", "datetime", "content"]
ofo = pd.read_csv("data/ofo.csv", usecols = usecols)
ofo.head()
title | datetime | content | |
---|---|---|---|
0 | 共享单车成新出行方式 摩拜、OFO“橙黄大战”一触即发 | 2016-11-18-08:55 | 央广网北京11月18日(记者杜希萌 朱敏)据中国之声《新闻纵横》报道,近段时间以来,共享单车... |
1 | ofo加快城市业务布局速度 已覆盖全国33座城市 | 2017-01-11-16:21 | 中新网1月11日电 共享单车行业领军企业ofo宣布启动“2017城市战略”,计划到1月2... |
2 | ofo建言南京共享单车城市治理 共建绿色出行志愿者联盟 | 2017-01-22-10:32 | 中新网1月22日电 1月19日,南京市城市治理委员会举办圆桌论坛,就共享单车城市发展及治理问... |
3 | 破局春节“最难一公里” ofo百万单车免费骑 | 2017-01-23-11:31 | 中新网1月23日电 2017春运开始,各地车站、机场等交通枢纽呈现出人流量集中的现象,尤其在... |
4 | 春节短途出行ofo小黄车受青睐 全国各地掀绿色出行潮 | 2017-02-03-16:24 | 中新网2月3日电 春节假期这几天,在全国各地街头上,到处是骑着“小黄车”悠闲前行的人们。无论... |
ofo.shape
(130, 3)
mobike = pd.read_csv("data/mobike.csv", usecols = usecols)
mobike.head()
title | datetime | content | |
---|---|---|---|
0 | 连自行车也不放过 滴滴宣布投资共享单车叫板摩拜 | 2016-09-26-14:14 | 【新民网·最新报道】近日火遍大街小巷的摩拜共享单车将迎来挑战。滴滴出行今天宣布,将以“数千万... |
1 | 透过“摩拜”网约自行车热闹,看出什么门道 | 2016-10-10-10:48 | “无桩公共自行车”摩拜单车最近越来越火,有力挺也有吐槽。解决“最后一公里”痛点、结合互联网的... |
2 | 上海闵行首例盗窃摩拜单车案犯罪嫌疑人被公诉 | 2016-11-03-15:12 | 【新民晚报·新民网】“我只是想占有这部自行车!”看到摩拜自行车大气美观,韩某便将车搬回了家。... |
3 | 摩拜单车现诈骗“二维码” 扫码后自行输入金额 | 2016-11-18-03:11 | 使用摩拜单车扫码取车,却发现除了钉在车上的红色底面的“扫码骑走”二维码,车上还新贴了其他两个... |
4 | 共享单车成新出行方式 摩拜、OFO“橙黄大战”一触即发 | 2016-11-18-08:55 | 央广网北京11月18日(记者杜希萌 朱敏)据中国之声《新闻纵横》报道,近段时间以来,共享单车... |
mobike.shape
(150, 3)
merged = pd.concat([ofo, mobike]).reset_index().drop('index', axis=1)
merged.head()
title | datetime | content | |
---|---|---|---|
0 | 共享单车成新出行方式 摩拜、OFO“橙黄大战”一触即发 | 2016-11-18-08:55 | 央广网北京11月18日(记者杜希萌 朱敏)据中国之声《新闻纵横》报道,近段时间以来,共享单车... |
1 | ofo加快城市业务布局速度 已覆盖全国33座城市 | 2017-01-11-16:21 | 中新网1月11日电 共享单车行业领军企业ofo宣布启动“2017城市战略”,计划到1月2... |
2 | ofo建言南京共享单车城市治理 共建绿色出行志愿者联盟 | 2017-01-22-10:32 | 中新网1月22日电 1月19日,南京市城市治理委员会举办圆桌论坛,就共享单车城市发展及治理问... |
3 | 破局春节“最难一公里” ofo百万单车免费骑 | 2017-01-23-11:31 | 中新网1月23日电 2017春运开始,各地车站、机场等交通枢纽呈现出人流量集中的现象,尤其在... |
4 | 春节短途出行ofo小黄车受青睐 全国各地掀绿色出行潮 | 2017-02-03-16:24 | 中新网2月3日电 春节假期这几天,在全国各地街头上,到处是骑着“小黄车”悠闲前行的人们。无论... |
merged.shape
(280, 3)
merged[merged.title.duplicated()].shape # 重复新闻
(12, 3)
merged = merged[~merged.title.duplicated()]
merged.shape
(268, 3)
def count_ofo(mystr):
mycount = mystr.count('OFO') + mystr.count('ofo')
return mycount
merged["count_ofo"] = merged.content.apply(count_ofo)
def count_mobike(mystr):
mycount = mystr.count('摩拜') + mystr.count('mobike')
return mycount
merged["count_mobike"] = merged.content.apply(count_mobike)
merged.head()
title | datetime | content | count_ofo | count_mobike | |
---|---|---|---|---|---|
0 | 共享单车成新出行方式 摩拜、OFO“橙黄大战”一触即发 | 2016-11-18-08:55 | 央广网北京11月18日(记者杜希萌 朱敏)据中国之声《新闻纵横》报道,近段时间以来,共享单车... | 6 | 5 |
1 | ofo加快城市业务布局速度 已覆盖全国33座城市 | 2017-01-11-16:21 | 中新网1月11日电 共享单车行业领军企业ofo宣布启动“2017城市战略”,计划到1月2... | 26 | 0 |
2 | ofo建言南京共享单车城市治理 共建绿色出行志愿者联盟 | 2017-01-22-10:32 | 中新网1月22日电 1月19日,南京市城市治理委员会举办圆桌论坛,就共享单车城市发展及治理问... | 11 | 0 |
3 | 破局春节“最难一公里” ofo百万单车免费骑 | 2017-01-23-11:31 | 中新网1月23日电 2017春运开始,各地车站、机场等交通枢纽呈现出人流量集中的现象,尤其在... | 13 | 0 |
4 | 春节短途出行ofo小黄车受青睐 全国各地掀绿色出行潮 | 2017-02-03-16:24 | 中新网2月3日电 春节假期这几天,在全国各地街头上,到处是骑着“小黄车”悠闲前行的人们。无论... | 9 | 0 |
merged[merged.count_mobike > merged.count_ofo].shape
(145, 5)
mobike = merged[merged.count_mobike > merged.count_ofo]
mobike = mobike.reset_index().drop(['index','count_ofo','count_mobike'], axis=1)
mobike.head()
title | datetime | content | |
---|---|---|---|
0 | 摩拜单车与ofo共享单车平台:押金已实现“秒退” | 2017-02-23-11:14 | 央广网北京2月23日消息(见习记者许睿)据中国之声《央广新闻》报道,根据媒体调查,共享单车押... |
1 | 完成D轮融资,ofo与摩拜谁主江湖? | 2017-03-02-10:35 | 完成D轮融资,ofo与摩拜谁主江湖互联网+相比滴滴定位出租车领域,共享单车实则是以城市短途出... |
2 | ofo欲接入WiFi万能钥匙 获得新入口 | 2017-03-10-13:21 | 摩拜和 ofo 的入口之争再次升级了。继摩拜单车宣布接入微信小程序以后,业内消息人士称,of... |
3 | 英媒称中国共享单车战火烧至新加坡:ofo免费骑行 | 2017-03-22-09:53 | 原标题:英媒称中国共享单车战火烧至新加坡:ofo免费骑行免押金参考消息网3月22日报道 英媒... |
4 | 共享单车如何“共管”?ofo创始人:平台勇于担责 | 2017-03-31-08:00 | 央广网北京3月31日消息(记者吴喆华)据中国之声《新闻纵横》报道,共享单车遍布大街小巷。方便... |
mobike.shape
(145, 3)
merged[merged.count_mobike <= merged.count_ofo].shape
(123, 5)
ofo = merged[merged.count_mobike <= merged.count_ofo]
ofo = ofo.reset_index().drop(['index','count_ofo','count_mobike'], axis=1)
ofo.head()
title | datetime | content | |
---|---|---|---|
0 | 共享单车成新出行方式 摩拜、OFO“橙黄大战”一触即发 | 2016-11-18-08:55 | 央广网北京11月18日(记者杜希萌 朱敏)据中国之声《新闻纵横》报道,近段时间以来,共享单车... |
1 | ofo加快城市业务布局速度 已覆盖全国33座城市 | 2017-01-11-16:21 | 中新网1月11日电 共享单车行业领军企业ofo宣布启动“2017城市战略”,计划到1月2... |
2 | ofo建言南京共享单车城市治理 共建绿色出行志愿者联盟 | 2017-01-22-10:32 | 中新网1月22日电 1月19日,南京市城市治理委员会举办圆桌论坛,就共享单车城市发展及治理问... |
3 | 破局春节“最难一公里” ofo百万单车免费骑 | 2017-01-23-11:31 | 中新网1月23日电 2017春运开始,各地车站、机场等交通枢纽呈现出人流量集中的现象,尤其在... |
4 | 春节短途出行ofo小黄车受青睐 全国各地掀绿色出行潮 | 2017-02-03-16:24 | 中新网2月3日电 春节假期这几天,在全国各地街头上,到处是骑着“小黄车”悠闲前行的人们。无论... |
ofo.shape
(123, 3)
from snownlp import SnowNLP
def get_sentiment(text):
return SnowNLP(text).sentiments
ofo["sentiment"] = ofo.content.apply(get_sentiment)
mobike["sentiment"] = mobike.content.apply(get_sentiment)
import numpy as np
pd.Series(np.where(ofo['sentiment'] > 0.5, True, False)).value_counts()
True 104 False 19 dtype: int64
pd.Series(np.where(mobike['sentiment'] > 0.5, True, False)).value_counts()
True 120 False 25 dtype: int64
# from bosonnlp import BosonNLP
# import json
# with open("boson_api_key.json") as f:
# secret = json.load(f)
# nlp = BosonNLP(secret["boson_api"])
# def get_sentiment_bosonnlp(text):
# return nlp.sentiment(text, model="news")[0][0]
# ofo["sentiment"] = ofo.content.apply(get_sentiment_bosonnlp)
# mobike["sentiment"] = mobike.content.apply(get_sentiment_bosonnlp)
import pickle
# with open('sentiment_bosonnlp.pickle', 'wb') as f:
# pickle.dump([ofo, mobike], f)
with open('data/sentiment_bosonnlp.pickle', 'rb') as f:
[ofo, mobike] = pickle.load(f)
pd.Series(np.where(ofo['sentiment'] > 0.5, True, False)).value_counts()
True 101 False 22 dtype: int64
pd.Series(np.where(mobike['sentiment'] > 0.5, True, False)).value_counts()
True 99 False 46 dtype: int64
%matplotlib inline
ofo.set_index('datetime', inplace=True)
mobike.set_index('datetime', inplace=True)
(ofo[['sentiment']] - 0.5).plot(kind='bar', figsize=(24, 6))
<matplotlib.axes._subplots.AxesSubplot at 0x11ff686a0>
(mobike[['sentiment']] - 0.5).plot(kind='bar', figsize=(24, 6))
<matplotlib.axes._subplots.AxesSubplot at 0x1208419b0>
import seaborn as sns
ofo["brand"] = "ofo"
mobike["brand"] = "mobike"
ofo.head()
title | content | sentiment | brand | |
---|---|---|---|---|
datetime | ||||
2016-11-18-08:55 | 共享单车成新出行方式 摩拜、OFO“橙黄大战”一触即发 | 央广网北京11月18日(记者杜希萌 朱敏)据中国之声《新闻纵横》报道,近段时间以来,共享单车... | 0.209684 | ofo |
2017-01-11-16:21 | ofo加快城市业务布局速度 已覆盖全国33座城市 | 中新网1月11日电 共享单车行业领军企业ofo宣布启动“2017城市战略”,计划到1月2... | 0.960631 | ofo |
2017-01-22-10:32 | ofo建言南京共享单车城市治理 共建绿色出行志愿者联盟 | 中新网1月22日电 1月19日,南京市城市治理委员会举办圆桌论坛,就共享单车城市发展及治理问... | 0.947963 | ofo |
2017-01-23-11:31 | 破局春节“最难一公里” ofo百万单车免费骑 | 中新网1月23日电 2017春运开始,各地车站、机场等交通枢纽呈现出人流量集中的现象,尤其在... | 0.950851 | ofo |
2017-02-03-16:24 | 春节短途出行ofo小黄车受青睐 全国各地掀绿色出行潮 | 中新网2月3日电 春节假期这几天,在全国各地街头上,到处是骑着“小黄车”悠闲前行的人们。无论... | 0.806527 | ofo |
df = pd.concat([ofo, mobike])
sns.boxplot(x='brand', y='sentiment', data=df)
<matplotlib.axes._subplots.AxesSubplot at 0x12f883b38>
ofo_positive = ofo[ofo.sentiment > 0.5]
ofo_negative = ofo[ofo.sentiment <= 0.5]
mobike_positive = mobike[mobike.sentiment > 0.5]
mobike_negative = mobike[mobike.sentiment <= 0.5]
ofo_positive.shape
(101, 4)
ofo_negative.shape
(22, 4)
mobike_positive.shape
(99, 4)
mobike_negative.shape
(46, 4)
import pyLDAvis
import pyLDAvis.sklearn
pyLDAvis.enable_notebook()
from helper import *
stopwords = get_custom_stopwords("stopwordsHIT.txt") # HIT停用词词典
max_df = 0.7 # 在超过这一比例的文档中出现的关键词(过于平凡),去除掉。
min_df = 2 # 在低于这一数量的文档中出现的关键词(过于独特),去除掉。
n_features = 1000 # 最大提取特征数量
n_top_words = 20 # 显示主题下关键词的时候,显示多少个
col_content = "content" # 说明其中的文本信息所在列名称
def lda_on_chinese_articles(df, n_topics):
return lda_on_chinese_articles_with_param(df, n_topics,
col_content = col_content,
stopwords = stopwords,
n_features = n_features,
max_df = max_df,
min_df = min_df,
n_top_words = n_top_words)
df = ofo_positive
n_topics = 4
lda, tf, vect = lda_on_chinese_articles(df = df, n_topics = n_topics)
pyLDAvis.sklearn.prepare(lda, tf, vect)
Building prefix dict from the default dictionary ... Loading model from cache /var/folders/yt/307v0c_945z1rh043jsdx1lh0000gn/T/jieba.cache Loading model cost 0.655 seconds. Prefix dict has been built succesfully. /Users/vija/miniconda3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py:300: UserWarning: Your stop_words may be inconsistent with your preprocessing. Tokenizing the stop words generated tokens ['exp', 'lex', '①①', '①②', '①③', '①④', '①⑤', '①⑥', '①⑦', '①⑧', '①⑨', '①a', '①b', '①c', '①d', '①e', '①f', '①g', '①h', '①i', '①o', '②①', '②②', '②③', '②④', '②⑤', '②⑥', '②⑦', '②⑧', '②⑩', '②a', '②b', '②d', '②e', '②f', '②g', '②h', '②i', '②j', '③①', '③⑩', '③a', '③b', '③c', '③d', '③e', '③f', '③g', '③h', '④a', '④b', '④c', '④d', '④e', '⑤a', '⑤b', '⑤d', '⑤e', '⑤f', 'li', 'ng', 'zxfitl'] not in stop_words. 'stop_words.' % sorted(inconsistent))
Topic #0: 数据 技术 北斗 导航 智能 定位 自行车 张巳丁 运营 curve 发展 采用 未来 北京 连接 进行 投放 平台 合作 系统 Topic #1: 信用 押金 芝麻 授权 支付宝 体系 信息 使用 平台 上海 用车 儿童 杭州 即可 app 失踪 解锁 完成 社会 成功 Topic #2: 骑行 自行车 发展 合作 企业 经济 平台 超过 戴威 市场 模式 国家 品牌 体验 创新 互联网 数据 目前 创始人 进行 Topic #3: 拥堵 下降 光华 北大 高峰 地铁 周边 mba 学院 缓解 明显 计划 数据 出现 同比 高德 机会 北京大学光华管理学院 第二季度 学生
/Users/vija/miniconda3/lib/python3.7/site-packages/pyLDAvis/_prepare.py:257: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default. To accept the future behavior, pass 'sort=False'. To retain the current behavior and silence the warning, pass 'sort=True'. return pd.concat([default_term_info] + list(topic_dfs))
df = ofo_negative
n_topics = 3
lda, tf, vect = lda_on_chinese_articles(df = df, n_topics = n_topics)
pyLDAvis.sklearn.prepare(lda, tf, vect)
/Users/vija/miniconda3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py:300: UserWarning: Your stop_words may be inconsistent with your preprocessing. Tokenizing the stop words generated tokens ['exp', 'lex', '①①', '①②', '①③', '①④', '①⑤', '①⑥', '①⑦', '①⑧', '①⑨', '①a', '①b', '①c', '①d', '①e', '①f', '①g', '①h', '①i', '①o', '②①', '②②', '②③', '②④', '②⑤', '②⑥', '②⑦', '②⑧', '②⑩', '②a', '②b', '②d', '②e', '②f', '②g', '②h', '②i', '②j', '③①', '③⑩', '③a', '③b', '③c', '③d', '③e', '③f', '③g', '③h', '④a', '④b', '④c', '④d', '④e', '⑤a', '⑤b', '⑤d', '⑤e', '⑤f', 'li', 'ng', 'zxfitl'] not in stop_words. 'stop_words.' % sorted(inconsistent))
Topic #0: 押金 摩拜 定位 企业 车辆 信用 智能 投放 市场 没有 认证 要求 上海 自行车 进行 脉脉 功能 黄车 相关 问题 Topic #1: 滴滴 方面 工资 天津 第三方 自行车 供应商 上海 员工 大家 调查 相关 领域 发生 校园 投资 网友 黄车 宣布 出行 Topic #2: 红包 黄车 密码 软件 活动 定位 问题 需要 智能 金额 运营 认证 手机 责任 情况 qq 押金 骑行 互联网 新加坡
/Users/vija/miniconda3/lib/python3.7/site-packages/pyLDAvis/_prepare.py:257: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default. To accept the future behavior, pass 'sort=False'. To retain the current behavior and silence the warning, pass 'sort=True'. return pd.concat([default_term_info] + list(topic_dfs))
df = mobike_positive
n_topics = 4
lda, tf, vect = lda_on_chinese_articles(df = df, n_topics = n_topics)
pyLDAvis.sklearn.prepare(lda, tf, vect)
/Users/vija/miniconda3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py:300: UserWarning: Your stop_words may be inconsistent with your preprocessing. Tokenizing the stop words generated tokens ['exp', 'lex', '①①', '①②', '①③', '①④', '①⑤', '①⑥', '①⑦', '①⑧', '①⑨', '①a', '①b', '①c', '①d', '①e', '①f', '①g', '①h', '①i', '①o', '②①', '②②', '②③', '②④', '②⑤', '②⑥', '②⑦', '②⑧', '②⑩', '②a', '②b', '②d', '②e', '②f', '②g', '②h', '②i', '②j', '③①', '③⑩', '③a', '③b', '③c', '③d', '③e', '③f', '③g', '③h', '④a', '④b', '④c', '④d', '④e', '⑤a', '⑤b', '⑤d', '⑤e', '⑤f', 'li', 'ng', 'zxfitl'] not in stop_words. 'stop_words.' % sorted(inconsistent))
Topic #0: 红包 行业 停车 自行车 车辆 微信 运营 平台 超过 发展 停放 使用 企业 合作 文明 投放 提供 市场 表示 管理 Topic #1: 中国 成为 创新 上海 胡玮炜 北京 发展 全国 超过 企业 期间 科技 深圳 广州 表示 世界 ofo 全球 显示 持续 Topic #2: 融资 模式 自行车 资本 创新 行业 投资 企业 上海 市场 发展 经济 中国 战略 全球 互联网 服务 分享 产品 本轮 Topic #3: 联网 全球 超过 提供 服务 技术 创新 平台 合作 实现 行业 运营 表示 定位 新加坡 应用 中国 管理 移动 投放
/Users/vija/miniconda3/lib/python3.7/site-packages/pyLDAvis/_prepare.py:257: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default. To accept the future behavior, pass 'sort=False'. To retain the current behavior and silence the warning, pass 'sort=True'. return pd.concat([default_term_info] + list(topic_dfs))
df = mobike_negative
n_topics = 3
lda, tf, vect = lda_on_chinese_articles(df = df, n_topics = n_topics)
pyLDAvis.sklearn.prepare(lda, tf, vect)
/Users/vija/miniconda3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py:300: UserWarning: Your stop_words may be inconsistent with your preprocessing. Tokenizing the stop words generated tokens ['exp', 'lex', '①①', '①②', '①③', '①④', '①⑤', '①⑥', '①⑦', '①⑧', '①⑨', '①a', '①b', '①c', '①d', '①e', '①f', '①g', '①h', '①i', '①o', '②①', '②②', '②③', '②④', '②⑤', '②⑥', '②⑦', '②⑧', '②⑩', '②a', '②b', '②d', '②e', '②f', '②g', '②h', '②i', '②j', '③①', '③⑩', '③a', '③b', '③c', '③d', '③e', '③f', '③g', '③h', '④a', '④b', '④c', '④d', '④e', '⑤a', '⑤b', '⑤d', '⑤e', '⑤f', 'li', 'ng', 'zxfitl'] not in stop_words. 'stop_words.' % sorted(inconsistent))
Topic #0: 共享 用户 自行车 记者 押金 问题 表示 发现 目前 公司 使用 企业 停放 没有 专利 北京 app 投放 城市 充值 Topic #1: 公司 管理 停放 物业公司 知乎 平台 共享 问题 ofo 方面 没有 贪腐 爆料 信息 用户 原告 存在 认为 责任 押金 Topic #2: 韩女士 记者 显示 没有 扣费 小时 分钟 骑行 账户 客服 客户端 反映 返还 工作人员 北京晨报 发现 被扣 确实 锁车 使用
/Users/vija/miniconda3/lib/python3.7/site-packages/pyLDAvis/_prepare.py:257: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default. To accept the future behavior, pass 'sort=False'. To retain the current behavior and silence the warning, pass 'sort=True'. return pd.concat([default_term_info] + list(topic_dfs))