====== NumPy, SciPy, Matplotlib, CuPy, Pandas のインストール ====== ====== NumPy, SciPy, Matplotlib, CuPy のインストール ====== ===== Linux ===== $ pip3 install numpy scipy matplotlib cupy ===== Windows ===== > pip install numpy scipy matplotlib cupy ※CuPy をインストールするには [[windows:visualstudio|Visual Studio 2019]] と [[windows:windows10_install_cuda|CUDA Tootkit]] のインストールが必要である。\\ ====== NumPy ====== {{:python:numpy_logo.svg?200|NumPy Logo}} 本家: [[https://numpy.org/|NumPy — NumPy]]\\ ソースコード: [[https://github.com/numpy/numpy|GitHub - numpy/numpy: The fundamental package for scientific computing with Python.]]\\ $ git clone https://github.com/numpy/numpy.git リファレンス: [[https://docs.scipy.org/doc/numpy/reference/|NumPy Reference]]\\ \\ **NumPy** は、プログラミング言語Pythonにおいて数値計算を効率的に行うための拡張モジュールである。効率的な数値計算を行うための型付きの多次元配列(例えばベクトルや行列などを表現できる)のサポートをPythonに加えるとともに、それらを操作するための大規模な高水準の数学関数ライブラリを提供する。\\ [[https://ja.wikipedia.org/wiki/NumPy|NumPy - Wikipedia]] より\\ ====== SciPy ====== [[https://www.scipy.org/|SciPy.org — SciPy.org]]\\ \\ **SciPy** は、プログラミング数学、科学、工学のための数値解析ソフトウェアである。無料かつオープンソースで、Windows・Linux・macOSを含むオペレーティングシステムで動作する。ScientificPythonとは無関係である。\\ [[https://ja.wikipedia.org/wiki/SciPy|SciPy - Wikipedia]] より\\ ====== Matplotlib ====== [[https://matplotlib.org/|Matplotlib: Python plotting — Matplotlib 3.1.1 documentation]]\\ \\ **Matplotlib** は、プログラミング言語Pythonおよびその科学計算用ライブラリNumPyのためのグラフ描画ライブラリである。オブジェクト指向のAPIを提供しており、様々な種類のグラフを描画する能力を持つ。描画できるのは主に2次元のプロットだが、3次元プロットの機能も追加されてきている。描画したグラフを各種形式の画像(各種ベクトル画像形式も含む)として保存することもできるし、wxPython、Qt、GTKといった一般的なGUIツールキット製のアプリケーションにグラフの描画機能を組みこむこともできる。MATLABの対話環境のようなものを提供するpylabというインタフェースも持っている。Matplotlibは、BSDスタイルのライセンスの下で配布されている。\\ [[https://ja.wikipedia.org/wiki/Matplotlib|matplotlib - Wikipedia]] より\\ ====== CuPy ====== {{:python:cupy_logo.png?200|CuPy Logo}}\\ 本家: [[https://cupy.chainer.org/|CuPy]]\\ ソースコード: [[https://github.com/cupy/cupy|GitHub - cupy/cupy: NumPy-like API accelerated with CUDA]]\\ $ git clone https://github.com/cupy/cupy.git リファレンス: [[https://docs-cupy.chainer.org/en/stable/reference/|Reference Manual — CuPy 6.2.0 documentation]]\\ \\ **CuPy** は CUDA 上の NumPy 互換の多次元配列の実装である。CuPy は cupy.ndarray、コアの多次元配列クラス、およびそれに関する多くの関数で構成されている。それは numpy.ndarray インターフェイスのサブセットをサポートする。\\ [[https://docs-cupy.chainer.org/en/stable/overview.html|Overview — CuPy 6.2.0 documentation]] より\\ ====== Pandas のインストール ====== 本家: [[https://pandas.pydata.org/|Python Data Analysis Library — pandas: Python Data Analysis Library]]\\ \\ **Pandas** は、プログラミング言語 Python において、データ解析を支援する機能を提供するライブラリである。特に、数表および時系列データを操作するためのデータ構造と演算を提供する。Pandas は BSD ライセンスのもとで提供されている。\\ ===== Linux ===== $ pip3 install pandas ===== Windows ===== > pip install pandas Collecting pandas Downloading https://files.pythonhosted.org/packages/c1/cf/58ccaa38d5670dd4d2aee5df90aa03d670ede3947b7148e72391c80d4f91/pandas-0.25.0-cp37-cp37m-win_amd64.whl (9.2MB) |████████████████████████████████| 9.2MB 1.7MB/s Requirement already satisfied: python-dateutil>=2.6.1 in c:\python37\lib\site-packages (from pandas) (2.8.0) Requirement already satisfied: numpy>=1.13.3 in c:\python37\lib\site-packages (from pandas) (1.16.4) Collecting pytz>=2017.2 (from pandas) Downloading https://files.pythonhosted.org/packages/3d/73/fe30c2daaaa0713420d0382b16fbb761409f532c56bdcc514bf7b6262bb6/pytz-2019.1-py2.py3-none-any.whl (510kB) |████████████████████████████████| 512kB 3.2MB/s Requirement already satisfied: six>=1.5 in c:\python37\lib\site-packages (from python-dateutil>=2.6.1->pandas) (1.12.0) Installing collected packages: pytz, pandas Successfully installed pandas-0.25.0 pytz-2019.1 ===== 使い方 ===== ==== 行によって列数の異なる CSV を DataFrame 化する方法 ==== col1 col2 col3 col4 col5 1 2 3 4 1 2 3 1 2 3 4 5 In [1]: import csv In [2]: reader = csv.reader('data.csv') In [3]: reader Out[3]: <_csv.reader at 0x22681e467b8> In [4]: with open('data.csv') as fp: ...: reader = csv.reader(fp, delimiter='\t') ...: data = [row for row in reader] ...: In [5]: data Out[5]: [['col1', 'col2', 'col3', 'col4', 'col5'], ['1', '2', '3', '4'], ['1', '2', '3'], ['1', '2', '3', '4', '5']] In [6]: header, values = data[0], data[1:] In [7]: import pandas as pd In [8]: df = pd.DataFrame(values, columns=header) In [9]: print(df) col1 col2 col3 col4 col5 0 1 2 3 4 None 1 1 2 3 None None 2 1 2 3 4 5 ==== データフレームのグループ化(集計) ==== In [1]: import pandas as pd In [2]: df = pd.DataFrame(columns=['col1', 'col2', 'col3', 'col4'], ...: data=[['aaa', 1, 2, 3], ...: ['bbb', 1, 2, 3], ...: ['ccc', 1, 2, 3], ...: ['aaa', 2, 4, 6], ...: ['bbb', 2, 4, 6], ...: ['ccc', 2, 4, 6], ...: ['aaa', 3, 6, 9], ...: ['bbb', 3, 6, 9], ...: ['ccc', 3, 6, 9]]) In [3]: df Out[3]: col1 col2 col3 col4 0 aaa 1 2 3 1 bbb 1 2 3 2 ccc 1 2 3 3 aaa 2 4 6 4 bbb 2 4 6 5 ccc 2 4 6 6 aaa 3 6 9 7 bbb 3 6 9 8 ccc 3 6 9 # groupby のデフォルト動作はグループラベルが index になり集計列が単一の場合は Series が返る In [4]: grouped = df.groupby(df['col1'])['col2'].sum() In [5]: type(grouped) Out[5]: pandas.core.series.Series In [6]: grouped Out[6]: col1 aaa 6 bbb 6 ccc 6 Name: col2, dtype: int64 In [7]: pd.DataFrame(grouped) Out[7]: col2 col1 aaa 6 bbb 6 ccc 6 # as_index=False にするとグループラベルが列になり DataFrame が返る In [8]: grouped = df.groupby(df['col1'], as_index=False)['col2'].sum() In [9]: type(grouped) Out[9]: pandas.core.frame.DataFrame In [10]: grouped Out[10]: col1 col2 0 aaa 6 1 bbb 6 2 ccc 6