LoginSignup
423
506

More than 3 years have passed since last update.

Python 割と使うライブラリ集

Last updated at Posted at 2018-12-08

Python-Logo-PNG-Image.png

背景

Pythonでよく使うライブラリ集です。
簡単なソースと学習に役立つサイトのリンクを記載しています。
GitHubで公開されているものに関してはライブラリ名にリンクを貼っています。

基本的には趣味レベルのみでの利用なので専門的情報が欲しい方は戻るボタン推奨。

標準ライブラリ

公式ドキュメントにありますのでこちらをご参照ください。
https://docs.python.jp/3/library/index.html

数学系

NumPy

ベクトルや行列計算を行うためのライブラリ
内積、フーリエ変換。行列計算などプログラミング知識が少なくても簡単に使用できるのが特徴。
少ないコード量で効率よく高速に数値計算
学習サイト:https://qiita.com/jyori112/items/a15658d1dd17c421e1e2

連立方程式
# -*- coding: utf-8 -*-
import numpy as np

A = np.array([[1.,3.] ,[4.,2.]]) # 行列Aの生成
B = np.array([1.,1.])   # 行列Bの生成

X = np.linalg.solve(A, B)
print( "X=\n" + str(X) )

scipy

信号処理や統計などの科学計算用のライブラリ
scipyではnumpyで行える配列や行列の演算を行うことができ、加えてさらに信号処理や統計といった計算ができる
NumPy ⊂ SciPy
な、関係。Scipyだけでいい気がするが世間ではNumPyが主流なのでNumPyで事が足りる機能であるならばNumPyを使うべきなのでしょう
学習/参考サイト:https://algorithm.joho.info/programming/python/scipy-integrate-quad/

定積分
# -*- coding:utf-8 -*-
from scipy import integrate

def f(x):
    return 3*x + 1

# 定積分(積分区間[0, 10])
ix, err = integrate.quad(f, 0, 10)
print('計算結果:', ix)
print('誤差:', err)

matplotlib

グラフを描画するためのライブラリ
学習サイト:https://qiita.com/skotaro/items/08dc0b8c5704c94eafb9

グラフ作成
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)

plt.plot(X, C)
plt.plot(X, S)

plt.show()

pandas

Rに類似のデータフレームを提供するライブラリ
pandasは、Pythonにおいて、データ解析を支援する機能を提供するライブラリである。
特に、数表および時系列データを操作するためのデータ構造と演算を提供する。
Pythonで実用的なデータ分析をする際の、高水準な基盤となるべく開発されている。
Pandasは扱うデータが幅広い
学習/参考サイト:https://qiita.com/ysdyt/items/9ccca82fc5b504e7913a

スクレイピング/クローリング

BeautifulSoup4

Pythonの定番HTMLパースライブラリ
スクレイピングする際は必須のライブラリ。

学習/参考サイト:https://python.civic-apps.com/beautifulsoup4-selector/

html
# -*- coding: utf-8 -*-

import urllib.request
from bs4 import BeautifulSoup

url = "https://qiita.com/"
f = urllib.request.urlopen(url)
html = f.read().decode('utf-8')

soup = BeautifulSoup(html, "html.parser")

lxml

lxml はXMLやHTML文書を非常に素早く解析するために書かれた非常に広範囲なライブラリ
学習/参考サイト:https://python.keicode.com/advanced/xml-lxml-1.php

selenium

selenium ライブラリは、同名の Selenium というブラウザ操作用のツールがあるのですが、
その Python バインディング、いわゆる「 Selenium の Python 版」

学習/参考サイト:https://www.inet-solutions.jp/technology/python-selenium/

クローリング
import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.google.com/')
time.sleep(5)
search_box = driver.find_element_by_name("q")
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)
driver.quit()

画像認識

pix2code

UI 画像からそれを生成するコードを自動生成するライブラリ

学習/参考サイト:https://ledge.ai/pix2code-on-floydhub/

face_recognition

画像内の人の顔を認識するためのライブラリ
学習/参考サイト:http://shikouno.hatenablog.com/entry/20171001/1506862903

その他

python-fire

Python のユーティリティクラスをコマンドラインからすぐに呼び出せるようにできるライブラリ
Google が公開している。

学習/参考サイト:https://blog.amedama.jp/entry/2017/03/07/223319
学習/参考サイト:https://qiita.com/koga1020/items/0c51e365612689615c91

# -*- coding: utf-8 -*-

import fire

def add(x, y):
  return x + y

def multiply(x, y):
  return x * y

if __name__ == '__main__':
  fire.Fire()

tqdm

走らせた処理の進捗状況をプログレスバーとして表示するためのライブラリ
説明文をつけたり、進捗バーを自分のタイミングで進めることもできます。
学習/参考サイト:https://blog.amedama.jp/entry/2018/07/23/080000

進捗率表示
# -*- coding: utf-8 -*-

from tqdm import tqdm
import time

for _ in tqdm(range(100)):
        time.sleep(0.1)

pycrypto

Pythonの暗号化/復号するためのライブラリ
学習/参考サイト:https://kamatari.github.io/2016/04/23/what-is-pycrypto/

暗号化して復号
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES

secret_key = 'this is secret:)'
message = 'Awesome python!!'

crypto = AES.new(secret_key)
cipher_data = crypto.encrypt(message)
print(cipher_data)

original_message = crypto.decrypt(cipher_data)
print(original_message)

紹介

PYthon関連書籍で個人的に「退屈なことはPythonにやらせよう」がとても好き
普段自宅でやってる作業のいろいろが自動化するヒントがたくさん詰まっている本。
値段はちょっと高いがPythonの文法から実践方法まで学べるのは割とお得だと思う。

bookfan_bk-487311778x.jpg

423
506
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
423
506