barilog

エンジニアリング見習い雑記

オフラインでもssh接続を維持するために

awsなどのサーバーにssh接続した上で時間のかかる処理をしている場合,

基本的にパソコンをwifi環境から外してしまうとssh接続が途切れる.

 

10時間ぐらいかかるプログラムを回していて終電にぶちあたってしまったときなどは大変だ.

 

そういうときの対策をリサーチしてみた.

 

基本的に

 

moshを使用する
ターミナルの ServerAliveIntervalという設定を長めに設定する(?まだ試していない)

などの対策があるようだ.

 

以下参考

helen.hatenablog.com

qiita.com

ubuntuでpipでインストールしたライブラリを使いたいのにパスが通っていない,,

という場合のの対処法
結論から言えば
以下のように
site-packagesのパスを調べて
sys.pathに追加してあげればOK!!

#ライブラリのパスを設定
import sys
sys.path.append("/home/ubuntu/anaconda3/lib/python3.6/site-packages")


site-packagesのパスは使っている環境によって異なるのでみなさんも自分で調べてみてね.
調べるためには
【Python】site-packagesのパスを確認する方法【pip install】

のやり方を参考にすると良いと思う

mecabによるtokenize中に起こるUnicodeDecodeErrorの解決

import MeCab
#mecab = MeCab.Tagger(' -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd')
mecab = MeCab.Tagger('mecabrc -u expert.dic')

import re

from bs4 import BeautifulSoup

from urllib.request import urlopen

# ストップワードを定義
def make_stopwords():
#英語のストップワード
from nltk.corpus import stopwords
nltk_stopwords = stopwords.words('english')
#日本語のストップワード
slothlib_path = 'http://svn.sourceforge.jp/svnroot/slothlib/CSharp/Version1/SlothLib/NLP/Filter/StopWord/word/Japanese.txt'
slothlib_file = urlopen(slothlib_path)
slothlib_stopwords = [line.decode("utf-8").strip() for line in slothlib_file]
slothlib_stopwords = [ss for ss in slothlib_stopwords if not ss=='']

#その他の独自のストップワード
additional_stopwords = ['rt', '%', 'の', 'ん', '0']
#連結
stopwords = nltk_stopwords + slothlib_stopwords + additional_stopwords

return stopwords


def clean_text(text):
replaced_text = text
#replaced_text = '\n'.join(s.strip() for s in text.splitlines()[2:] if s != '') # skip header by [2:]
replaced_text = replaced_text.lower()
replaced_text = re.sub(r'[【】]', ' ', replaced_text) # 【】の除去
replaced_text = re.sub(r'[()()]', ' ', replaced_text) # ()の除去
replaced_text = re.sub(r'[[]\[\]]', ' ', replaced_text) # []の除去
replaced_text = re.sub(r'[「」]', ' ', replaced_text) # 「」の除去
replaced_text = re.sub(r'[@@]\w+', '', replaced_text) # メンションの除去
replaced_text = re.sub(r'^RT', '', replaced_text) # 文頭にRTがあれば除去
replaced_text = re.sub(r'\d+\.?\d*|\.\d+', '0', replaced_text) # 数値があれば全て0に変換
# replaced_text = re.sub(r'https?:\/\/.*?[\r\n ]', '', replaced_text) # URLの除去
replaced_text = re.sub(r"(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+\$,%#]+)", "" , replaced_text) #URLの除去
replaced_text = re.sub(r' ', ' ', replaced_text) # 全角空白の除去
return replaced_text


def clean_html_tags(html_text):
soup = BeautifulSoup(html_text, 'html.parser')
cleaned_text = soup.get_text()
cleaned_text = ''.join(cleaned_text.splitlines())
return cleaned_text


def clean_html_and_js_tags(html_text):
soup = BeautifulSoup(html_text, 'html.parser')
[x.extract() for x in soup.findAll(['script', 'style'])]
cleaned_text = soup.get_text()
cleaned_text = ''.join(cleaned_text.splitlines())
return cleaned_text


def clean_url(html_text):
"""
\S+ matches all non-whitespace characters (the end of the url)
:param html_text:
:return:
"""
clean_text = re.sub(r'http\S+', '', html_text)
return clean_text


def clean_code(html_text):
"""Qiitaのコードを取り除きます
:param html_text:
:return:
"""
soup = BeautifulSoup(html_text, 'html.parser')
[x.extract() for x in soup.findAll(class_="code-frame")]
cleaned_text = soup.get_text()
cleaned_text = ''.join(cleaned_text.splitlines())
return cleaned_text

def extract_hashtag(tweet):
out = tweet
no_hashtag_text = ''
hashtag_list =

pattern = r'[##]([\w一-龠ぁ-んァ-ヴーa-z]+)'

for hashtag in ((re.findall(pattern , tweet))):
#ハッシュタグ
hashtag_list.append(hashtag)
r = r'[##]%s' % hashtag
out = re.sub(r, '', out)
no_hashtag_text = out
return no_hashtag_text, hashtag_list

 


def tokenize(text):
'''
とりあえず形態素解析して名詞だけ取り出す感じにしてる
'''
node = mecab.parseToNode(text)
while node:
if node.feature.split(',')[0] == '名詞':
yield node.surface.lower()
node = node.next


def get_words(contents):
'''
記事群のdictについて、形態素解析してリストにして返す
'''
ret =
for k, content in contents.items():


#ハッシュタグの削除
content = extract_hashtag(content)[0]

#不要な文字列の削除
content = clean_text(content)

ret.append(get_words_main(content))
return ret


def get_words_main(content):
'''
一つの記事を形態素解析して返す
ストップワード以外の単語
'''

return [token for token in tokenize(content) if token not in stop_words]


#ライブラリのパスを設定
import sys
sys.path.append("/home/ubuntu/anaconda3/lib/python3.6/site-packages")


df = TweetItem.df.loc[:,['tweet_id','text']]
stop_words = make_stopwords()
words = get_words({row[1]['tweet_id'] : row[1]['text'] for row in df.iterrows()})

#wordsをpickleに保存
import pickle
with open('words.pkl', 'wb') as f:
pickle.dump(words, f)

from gensim import corpora

# words はさっきの単語リスト
dictionary = corpora.Dictionary(words)
#print(dictionary.token2id)

#dictionary.filter_extremes(no_below=20, no_above=0.3)
# no_berow: 使われてる文章がno_berow個以下の単語無視
# no_above: 使われてる文章の割合がno_above以上の場合無視

#辞書をファイルに保存します。
dictionary.save_as_text('tweetdic.txt')
#作った辞書ファイルをロードして辞書オブジェクト作るとき dictionary = corpora.Dictionary.load_from_text('livedoordic.txt')

corpus = [dictionary.doc2bow(text) for text in words]
with open('corpus.pkl', 'wb') as f:
pickle.dump(corpus, f)

 

上記のコード(tweet文書を読み込んで,corpusというBOWを出力する)を動かした結果,


<ipython-input-45-d0315e671431> in tokenize(text)
106 while node:
107 if node.feature.split(',')[0] == '名詞':
--> 108 yield node.surface.lower()
109 node = node.next
110

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 2: invalid start byte

 

というエラーが起きた.

 

これの原因はすぐ見つかった.

 

MeCabのバージョン0.996で、この方法によりPython3からMeCabのparseToNode関数を使うと、最初のsurface(表層形)が表示されないというバグがある。

 

 

 

www.trifields.jp

 

こちらを参考にした.

 

 

このバグの最も簡単な回避策は、parseToNode関数を呼び出す前に、一度、parse関数を呼び出すことで回避することができる。
実際に見てみよう。

先ほどのPython3のコードに「t.parse(”)」という一文を追加する。

 とあるので,

mecab = MeCab.Tagger(~~)

の直後に

mecab.parse("")

を挿入して対策完了

 

 

エラーが解消された.

 

 

PandasのDataFrameの行を反復処理する方法は?

 

[Python] PandasのDataFrameの行を反復処理する方法は? rows | CODE Q&A [日本語]

 

こちらのサイトを参考にした.

1.行をSeriesとして取り出す方法

DataFrame.iterrows()を使う

for row in df.iterrows():
    print("c1 :",row["c1"],"c2 :",row["c2"])

 

2.行をタプルとして取り出す方法(iterrowsより高速)

DataFrame.itertuples()を使う

 

for row in df.itertuples():
    print("c1 :",row.c1,"c2 :",row.c2)

 

 

3.apply()メソッドに反復処理してもらう方法(一般にfor文使う場合よりも高速らしい)

DataFrame.apply()を使う

 

例1

def valuation_formula(x, y):
    return x * y * 0.5

df['price'] = df.apply(lambda row: valuation_formula(row['x'], row['y']), axis=1)

例2

def valuation_formula(x):
    return x ** 0.5

df = df.apply(valuation_formula, axis=1) #行全体に関数を適用

 

4.インデックスを指定する方法

for ind in df.index:
     print(df['c1'][ind], df['c2'][ind])

 

 

 

 

 

 

 

DataFrameに関数を適応したいとき

sinhrks.hatenablog.com

 

こちらを参照した.

基本的に各列,各行に関数を適応したいときは

DataFrame.apply

というようにDataFrameのメソッドのひとつであるapplyメソッドを使えば良い

 

・各列に対して関数を適応したいとき

DataFrame.apply(関数)

 

・各行に対して関数を適応したいとき

dataframe.apply(関数, axis=1)

 

 

 

map関数を使うのは賢明ではありません.

map(method, DataFrame)

で得られるものはデータフレームではなく,mapオブジェクトです.

 

a = map(sample_method, sample_df)

print(type(a))

# 結果 map

python処理を早くするために

1.単純なfor文の代わりにmapまたはリスト内包表記で代用しよう

2.SQLクエリを何度も実行するのはやめよう

3.オブジェクトへの参照回数はなるべく減らそう

4.データフレームの読み書きにはcsvではなくpickleを使おう

 

まず1について.

utgwkk.hateblo.jp

 

これによると関数を呼び出す際はひとまずmapをつかっとくのが無難っぽい.

 

次に4について

yutori-datascience.hatenablog.com

 

これによるとこれはcsvの読み込みの早さはdask ≒ pickle >>read_csv

daskは使っと事ないからわからんので私のような雑魚はpickleをつかおう.

ただし,pickleだとメモリに全部一気に展開がされてしまうので,大きなデータサイズのcsvを読む場合だと他の処理如何でメモリリークが起こりそう.一行ずつ処理したり,並列で処理できるようにするのが理想だよね..