Web制作、Web開発の歩き方

初心者のためのDjango入門

■第14話:NumPyの統計的関数

(最終更新日:2023.06.9)

Djangoフレームワークのイメージ
この記事は3分で読めます!
(絵が小さい場合はスマホを横に)

「NumPyと組み合わせて使おう!」

前回、NumPyの四則演算やブロードキャスティング、内積、行列積の計算方法について学んだ。 中でもブロードキャスティングは効率的な計算を可能にする非常に強力な機能であった。

今回は、それらの結果を分析する統計的関数や、図、表への描画ができるライブラリについて紹介する。


1.NumPyの統計的関数

NumPyには、配列のデータに対して統計的な分析を行うための多くの関数が含まれている。 以下にいくつかの基本的な統計的関数を示す。各関数についての解説は以下の通りである。 以下のような機能を用いることで、NumPyは高度なデータ操作と分析を容易に可能にする。

  1. np.sum: 配列内の全要素の合計を計算する
  2. np.mean: 配列内の全要素の平均を計算する
  3. np.median: 配列内の全要素の中央値を計算する
  4. np.sum: 配列内の全要素の標準偏差を計算する
  5. np.mean: 配列内の全要素の分散を計算する
  6. np.median: 配列内の全要素の最小値または最大値を見つける
  7. np.argmin / np.argmax: 配列内の全要素の最小値または最大値のインデックスを見つける


NumPy配列のマスキング

NumPyにおける統計的関数

2.NumPyと科学計算ライブラリ

NumPyはPythonの科学計算ライブラリの中心的な役割を果たしており、 その効率的な多次元配列オブジェクトとこれを操作するツールは、多くの他のPythonライブラリで使用されている。 以下に、NumPyと他の主要なPythonライブラリとの関連について述べる。 (括弧内の%は2019年にGitHubに上がった機械学習系のプロジェクトで使用されているライブラリの割合。NumPyは74%でトップで 殆どのプロジェクトで使われている。)

  1. SciPy(47%): SciPyは、科学計算と技術計算をサポートするPythonライブラリである。 SciPyはNumPyの配列を基にし、多くの数学的関数を提供する。これには、線形代数、確率論、積分、最適化、統計などが含まれる。
  2. Pandas(41%): Pandasは、データ分析と操作を行うための強力なPythonライブラリである。 PandasのデータフレームオブジェクトはNumPy配列を基にしており、 多次元配列の上にラベルを追加したり、欠損データの扱いを改善したりする。
  3. Matplotlib(40%): Matplotlibは、Pythonでグラフやチャートを作成するためのライブラリである。 MatplotlibはNumPy配列を用いて、各種のプロットや視覚化を作成する。
  4. Scikit-learn(38%): Scikit-learnは、Pythonで機械学習を行うためのライブラリである。 Scikit-learnの多くの機能はNumPy配列を入力として受け取り、また出力として返す。
  5. TensorFlow(24%)/PyTorch: これらのライブラリはディープラーニングのためのフレームワークで、 NumPy互換の配列操作とAPIを提供し、NumPy配列を直接利用することも可能である。

以上のように、NumPyはPythonの科学計算とデータ分析のエコシステムにおいて、中心的な役割を果たしている。 これらのライブラリは互いに深く関連しており、一緒に使用されることで、強力なデータ分析ツールセットを形成している。


3.まとめ

今回、NumPyの統計的関数とNumPyと共に用いる科学計算ライブラリについて紹介した。 NumPyは機械学習を始め、科学計算や統計の分野において多くの人に使用されている。 それは、計算機能の豊富さ、効率の良さ、処理速度の速さによるだろう。 今後、このような分野に挑戦しようと思っている方は、ぜひNumPyから身に着けてみては如何だろうか。


▼参考図書、サイト

NumPyで使える統計関数の使い方まとめ  エビワークス
Pythonによる科学技術計算の概要  神嶌敏弘


Introduction to Django for Beginners ■ Episode 14: NumPy Statistical Functions (Last Updated: 2023.06.9) Image of the Django framework This article takes about 3 minutes to read! (Rotate your smartphone for a larger image) “Let’s use it together with NumPy!” In the previous episode, we learned about arithmetic operations, broadcasting, dot product, and matrix multiplication in NumPy. Among them, broadcasting was a powerful feature that enabled efficient computation. In this article, we will introduce statistical functions for analyzing such results, and libraries that allow drawing graphs and tables. [Table of Contents] NumPy Statistical Functions NumPy and Scientific Libraries Summary 1. NumPy Statistical Functions NumPy includes many functions for performing statistical analysis on arrays. Below are some basic statistical functions. Here's an explanation of each function. These functions enable advanced data manipulation and analysis with NumPy. np.sum: Calculates the sum of all elements in the array np.mean: Calculates the mean of all elements in the array np.median: Calculates the median of all elements in the array np.std: Calculates the standard deviation of all elements in the array np.var: Calculates the variance of all elements in the array np.min / np.max: Finds the minimum or maximum value in the array np.argmin / np.argmax: Finds the index of the minimum or maximum value in the array Statistical functions in NumPy Statistical functions in NumPy 2. NumPy and Scientific Libraries NumPy plays a central role in Python's scientific computing ecosystem. Its efficient multidimensional array object and manipulation tools are used in many other Python libraries. Below are examples of how NumPy is related to other major libraries. (The percentages in parentheses indicate usage rates in machine learning projects on GitHub in 2019. NumPy was used in 74% of the projects.) SciPy (47%): A library supporting scientific and technical computing. It is built on top of NumPy and provides functions for linear algebra, probability, integration, optimization, statistics, and more. Pandas (41%): A powerful library for data analysis and manipulation. Its DataFrame object is based on NumPy arrays and offers labeling and better handling of missing data. Matplotlib (40%): A library for creating graphs and charts in Python. It uses NumPy arrays to generate plots and visualizations. Scikit-learn (38%): A library for machine learning in Python. Many of its functions take NumPy arrays as input and return NumPy arrays as output. TensorFlow (24%) / PyTorch: Deep learning frameworks. They provide NumPy-compatible array operations and APIs, and can directly use NumPy arrays. As shown above, NumPy plays a central role in the Python scientific and data analysis ecosystem. These libraries are deeply interconnected and, when used together, form a powerful data analysis toolset. 3. Summary In this article, we introduced NumPy’s statistical functions and scientific libraries often used alongside it. NumPy is widely used in fields like machine learning, scientific computation, and statistics. This is due to its rich features, efficiency, and speed. If you're thinking about diving into these areas, mastering NumPy is a great place to start! ▼References   Summary of Statistical Functions in NumPy - Ebi Works   Overview of Scientific Computing with Python - Toshihiro Kamishima