Drkcore

14 08 2013 R Tweet

twitterのリプライ頻度を可視化する

python+pandasで全部やればいいんだけど、ggplotが使いたかったので。

とりあえず全件取ってきた状態で

import csv

tweets = csv.reader(open("tweets.csv"))

print "Date\tMessage"
for tweet in tweets:
    if "[[user_id]]" in tweet[7]:
        print "{}\t{}".format(tweet[5].split()[0], tweet[7])

これである程度綺麗になったtsvが出力されるのであとはRStudioでいじる。data.tsvっていう名前で保存した。

library(plyr)
library(ggplot2)
library(scales)
setwd("/Users//kzfm/lang/rcode/tw")
tweets <- read.delim("data.tsv", sep="\t", stringsAsFactors=FALSE, header=TRUE)
tweet.counts <- ddply(tweets, .(Date), nrow)
date.range <- seq.Date(from=as.Date("2012-10-20"), to=as.Date("2013-8-12"), by="day")
date.strings <- strftime(date.range, "%Y-%m-%d")
dates <- data.frame(date.strings)
all.data <- merge(dates, tweet.counts, by.x=c("date.strings"), by.y=c("Date"), all=TRUE)
names(all.data) <- c("Date", "Counts")
all.data$Counts[is.na(all.data$Counts)] <- 0
all.data$Date <- as.Date(all.data$Date)

ggplot(all.data, aes(x=Date, y=Counts)) + 
geom_line() + 
scale_x_date(breaks="2 week", labels=date_format("%Y-%m-%d")) +
theme(axis.text.x=element_text(angle=-90))

入門機械学習の1章のやりかたが参考になった。

ProductName 入門 機械学習
Drew Conway
オライリージャパン / 3360円 ( 2012-12-22 )


About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021