Skip to content

Instantly share code, notes, and snippets.

View yatakeke's full-sized avatar

Yatakeke a.k.a Agile Zamurai yatakeke

View GitHub Profile
sorted(iterable, key)
# - iterable: list, dict
# - key: function called to each element before comparison
score = {'kokugo': 33, 'sansuu': 85, 'eigo': 60}
# when you want to sort by key
score_sorted = sorted(score.items(), key=lambda x:x[0])
# when you want to sort by value
score_sorted = sorted(score.items(), key=lambda x:x[1], reverse=True)
@yatakeke
yatakeke / matplotlib_make_pie_chart.py
Last active August 1, 2020 07:32
matplotlib pie chart
import matplotlib.pyplot as plt
plt.pie(fraction, startangle, wedgeprops, autopct, explode, **)
# example: plt.pie(frac, startangle=90, wedgeprops={'linewidth': 3, 'edgecolor':"white"}, autopct="%1.1f%%", explode=[0.2, 0, 0, 0, 0]
# fraction: total of fraction should be 1
# startangle: startangle
# wedgeprops: edge props
# autopct: show percentage
# explode: array for explode
s = {1,2,3}
s.add(4)
print(s)
# {1,2,3,4}
s.remove(3)
print(s)
# {1,2,4}
s1 = {1,2,3}
# linecache module allows one to get any line fromm python source code file
import linecache
linecache.getline(filename, line_number, module_globals=None)
# get line line_number from file named filename
# ex. line = linecache.getline('./test.txt', 8)
# get 8th line from test.txt
import pandas as pd
# save single sheet for excel file
df.to_excel(fp, index=True, columns=True)
# save multiple sheets for excel file
with pd.ExcelWriter(fp) as writer:
df1.to_excel(writer, sheet_name='sheet1')
df2.to_excel(writer, sheet_name='sheet2')
import pandas as pd
pd.concat([df1, df2], axis=0, ignore_index=False)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import torch
# save parameters
torch.save(model.state_dict(), PATH)
# load parameters
model = ModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
@yatakeke
yatakeke / git.md
Last active February 19, 2020 11:23
gitの忘れやすいコマンド類を使った度に更新していく予定

git rm --cached 'file name' ファイルを管理対象から外す(-rでディレクトリごと)

git stash 一時的に変更を保存する

git stash apply stash@{num} 一時的に保存した変更を元に戻す

git checkout