Open9

日々の積み上げ(Python)

schnellschnell

12/16

vscodeでのデバッグ

置換変数

  • [公式]
  • ${varname}で置換される変数。ハードコーディング不要なのでとても良い

辞書に初期値を設定して初期化する

defaultdictを使う。
存在しないkeyに対しても初期値の設定ができるので便利

from collections import defaultdict as ddict

# char_count = ddict(lambda: int()) i,e ddict(lambda: 0)の意味
char_count = ddict(int)

for c in "Done is Better Than Perfect":
    char_count[c.lower()] += 1

for k, v in char_count.items():
     print(k, v)

# d 1
# o 1
# n 2
# ...

クラス継承関係の確認

issubclass, mro関数が使える。

class Animal():
    pass

class Dog(Animal):
    pass

issubclass(Dog, Animal)
# True

Dog.mro()
# [__main__.Dog, __main__.Animal, object]
schnellschnell

12/18

主成分分析

量的観測変数から量的潜在変数を見つけるのが目的
例えば生徒の5科目の模試の得点の特徴をうまく表す新たな潜在変数を探すようなこと
https://factor-analyzer.readthedocs.io/en/latest/index.html

pip install factor_analyzer

因子分析用ライブラリ


pandasでxlsxファイルを読み込む

sheet_nameパラメタに開くシート名を指定

import pandas as pd
DATA1 = pd.read_excel("LatStr_PCA.xlsx", sheet_name = "科目", index_col = 0)
schnellschnell

12/20

パスの操作(pathlib)

Path

基本のクラス

from pathlib import Path

Path.mro()
# [pathlib.Path, pathlib.PurePath, object]

# カレントディレクトリのファイルが表示される
p = Path(".")
print([file for file in p.iterdir() if file.is_file()])

# "/"演算子を使ってパスの連結が可能
p/"test"/"foo"/"bar.txt"
# PosixPath('test/foo/bar.txt')

PurePath

実際にはファイルシステムにはアクセスしないでパスの処理を行う

jsonの操作

schnellschnell

12/21

クラスの演算子に関しての特殊メソッド

https://blog.codecamp.jp/python-class-code

class Point():
    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y

    def __add__(self, __o: object):
        return Point(self.x+__o.x, self.y+__o.y)

    def __sub__(self, __o: object):
        return Point(self.x+__o.x, self.y+__o.y)

    def __mul__(self, __o: object):
        return Point(self.x*__o.x, self.y*__o.y)

    def __truediv__(self, __o: object):
        return Point(self.x/__o.x, self.y/__o.y)

    def __floordiv__(self, __o: object):
        return Point(self.x//__o.x, self.y//__o.y)

    def __iadd__(self, __o: object):
        self.x += __o.x
        self.y += __o.y
        return Point(self.x, self.y)

    def __isub__(self, __o: object):
        self.x -= __o.x
        self.y -= __o.y
        return Point(self.x, self.y)

    def __imul__(self, __o: object):
        self.x *= __o.x
        self.y *= __o.y
        return Point(self.x, self.y)

    def __itruediv__(self, __o: object):
        self.x /= __o.x
        self.y /= __o.y
        return Point(self.x, self.y)

主成分分析と因子分析

主成分分析は観測変数に入る誤差の分散を同じと仮定
因子分析は観測変数に入る誤差の分散を異なるものと仮定する

schnellschnell

12/22

Google Driveからwget

wget --content-disposition  "https://drive.google.com/uc?export=download&id=<ファイルid>"

# ↓コピペよう
wget --content-disposition  https://drive.google.com/uc?export=download&id=
schnellschnell

12/23

リモートで削除済みのブランチをローカルからも削除する

# マージ済みブランチ表示
git branch --merged
# マージしていないブランチを表示
git branch --no-merged
# リモートリポジトリで削除済みのブランチを削除
git fetch --prune
# ローカルのマージ済みブランチを指定して削除
git branch -d <ブランチ名>
schnellschnell

12/28

pythonで集合演算

set型(mutable), frozenset(imutable)を使う

# 波括弧で生成可能
s = {1,2,3,3,4,4,5,6,6,6,7}
print(s)
# {1, 2, 3, 4, 5, 6, 7}

s1 = {1,2,3}
s2 = {2,3,4}

# 和集合
print(s1|s2)
# {1, 2, 3, 4}
print(s1.union(s2))

# 積集合
print(s1&s2)
print(s1.intersection(s2))
# {2, 3}

# 差集合
print(s1-s2)
print(s1.difference(s2))
# {1}
schnellschnell

12/29

Pandadでのデータ読み込み

tsvファイルの読み込み(pandas.read_csv)

popular-names.txtをダウンロードして以下を実行

import pandas as pd

df = pd.read_csv("popular-names.txt", delimiter="\t", header=None, names=["name", "sex", "total", "year"])
schnellschnell

12/31

makefileの自動変数公式

参考
$@ : ターゲットファイル名
$% : ターゲットがアーカイブメンバだったときのターゲットメンバ名
$< : 最初の依存するファイルの名前
$? : ターゲットより新しいすべての依存するファイル名
$^ : すべての依存するファイルの名前
$+ : Makefileと同じ順番の依存するファイルの名前
$* : サフィックスを除いたターゲットの名前

main: main.c test.c
	gcc -o $@ main.c

と記述すれば$@はターゲット名であるmainと展開される。

アセンブリ

https://www.mztn.org/lxasm64/amd04.html

 .intel_syntax noprefix
 .globl _main
 _main:
         mov rax, 3    # RAXレジスタに3を代入する
         add rax, 4    # RAXレジスタに4を足す
         ret           # 呼び出し元に戻る