ひよこ、通勤中。

通勤中の電車の中でひよこは何を思うのか。

自分流jupyter notebookの1セル目

import collections
import pathlib

# データ処理周り
import numpy as np
import pandas as pd
from IPython.core.display import display

# 可視化ライブラリ
import matplotlib.pyplot as plt
from matplotlib import cm # colormap
import seaborn as sns
%matplotlib inline

# pandasのwarningが邪魔なので
import warnings
warnings.filterwarnings('ignore')

# options
# pd.set_option('display.max_columns', 50)
plt.style.use('ggplot')

いつものHML分析

d = access_features['count'].sort_values().reset_index()
d.columns = ['base_index', 'count']
d = d.reset_index() 

from sklearn.cluster import KMeans
from matplotlib import cm
kmeans = KMeans(n_clusters=3)
kmeans.fit(d[['count']])
d.loc[:, 'cluster'] = kmeans.labels_

fig, ax = plt.subplots()
colors = cm.Accent.colors
for i in range(3):
    target = d[d['cluster'] == i]
    ax.scatter(x=target['index'], y=target['count'], c=colors[i]

f:id:cocodrips:20180119160746p:plain

pandasで日時周り

dftimeカラムがあることを想定

文字列 -> datetime型

time2017-01-01とかの文字列だったとき

pd.to_datatime(df['time'])

でだいたいよしなにparseしてくれる

unixtime -> datetime型

timeがunixtimeだったとき

pd.to_datatime(df['time'], unit='s')

一部情報を取り出す(日付、月、時間等)

datetime = pd.to_datatime(df['time'])
datetime.dt.hour

数百字のテキストを分類/クラスタリングしてみる with Keras

参考文献メモ。

分類

短文ならLSTM, 長文ならRNNが良いという噂。

クラスタリングするなら

色々

NLPクラスタリングにAutoEncoderを使う

Recursive AutoEncoder

LSTMを理解する

KerasでLSTM AutoEncoder

postgresqlでcsvファイルから一括upsert

全部1行ずつupsertしてたら100万件で数時間かかったので、 他の解決策を考える。

1時テーブルを作成しupdate + insert

PostgreSQL CSV 取り込み upsert | odekakeshimasyo.me

  1. copy from => tmp table
  2. tmp tableからUpdate
  3. tmp tableからUpdateした分を削除してInsert

PostgreSQLメモ

Activeで何クエリが走ってるか

select pid, query from pg_stat_activity where state = 'active';

クエリジョブのkill

select pg_cancel_backend(pid);