| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Python NumPy」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
4行目: 4行目:
  
 
===ドキュメント===
 
===ドキュメント===
 +
----
 
*http://docs.scipy.org/doc/
 
*http://docs.scipy.org/doc/
 
====リファレンス====
 
====リファレンス====
 +
----
 
*https://docs.scipy.org/doc/numpy-1.14.2/genindex.html
 
*https://docs.scipy.org/doc/numpy-1.14.2/genindex.html
  
 
===概要===
 
===概要===
 +
----
 
====NumPy====
 
====NumPy====
 +
----
 
*http://ja.wikipedia.org/wiki/NumPy
 
*http://ja.wikipedia.org/wiki/NumPy
 
*大部分がCで書かれた[[Python]]用の拡張モジュール
 
*大部分がCで書かれた[[Python]]用の拡張モジュール
 
*数値配列、行列を定義し、基本的な操作が可能
 
*数値配列、行列を定義し、基本的な操作が可能
 
====[[Python Scipy|SciPy]]====
 
====[[Python Scipy|SciPy]]====
 +
----
 
*http://ja.wikipedia.org/wiki/SciPy
 
*http://ja.wikipedia.org/wiki/SciPy
 
*先進的な数学をNumPyを使って行うための[[Python]]のライブラリ
 
*先進的な数学をNumPyを使って行うための[[Python]]のライブラリ
25行目: 30行目:
 
*配列は同じ型の要素のセットであり、正の整数のベクターによりインデックス付けされる
 
*配列は同じ型の要素のセットであり、正の整数のベクターによりインデックス付けされる
 
====Install====
 
====Install====
 +
----
 
*http://www.scipy.org/Installing_SciPy/[[Linux]]
 
*http://www.scipy.org/Installing_SciPy/[[Linux]]
 
=====apt-getを使ってubuntuへインストール=====
 
=====apt-getを使ってubuntuへインストール=====
 +
----
 
  sudo apt-get install python-numpy python-scipy
 
  sudo apt-get install python-numpy python-scipy
 
=====[[PIP]]を使ってインストール=====
 
=====[[PIP]]を使ってインストール=====
 +
----
 
*https://numpy.org/install/
 
*https://numpy.org/install/
 
  # pip install numpy
 
  # pip install numpy
36行目: 44行目:
 
  C:\[[Python]]27\Scripts>pip install numpy
 
  C:\[[Python]]27\Scripts>pip install numpy
 
===配列の生成===
 
===配列の生成===
 +
----
 
====リストから生成====
 
====リストから生成====
 +
----
 
  >>> from numpy import *
 
  >>> from numpy import *
 
  >>> a = array( [ 10, 20, 30, 40 ] )
 
  >>> a = array( [ 10, 20, 30, 40 ] )
42行目: 52行目:
 
  array([10, 20, 30, 40])
 
  array([10, 20, 30, 40])
 
====arange を使い配列を生成====
 
====arange を使い配列を生成====
 +
----
 
  arange([start,] stop[, step,], dtype=None)
 
  arange([start,] stop[, step,], dtype=None)
  
50行目: 61行目:
  
 
=====0から3まで 0.5 きざみの配列を生成=====
 
=====0から3まで 0.5 きざみの配列を生成=====
 +
----
 
  >>> np.arange(0,3,0.5)
 
  >>> np.arange(0,3,0.5)
 
  array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5])
 
  array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5])
 
====等しく割り付けられた配列を作成====
 
====等しく割り付けられた配列を作成====
 +
----
 
  >>> c = linspace(-pi,pi,3)
 
  >>> c = linspace(-pi,pi,3)
 
  >>> c
 
  >>> c
64行目: 77行目:
  
 
====多次元配列====
 
====多次元配列====
 +
----
 
  >>> x = ones( (3,4) )
 
  >>> x = ones( (3,4) )
 
  >>> x
 
  >>> x
73行目: 87行目:
  
 
====既存の配列の次元を変更====
 
====既存の配列の次元を変更====
 +
----
 
  >>> y = arange(12)
 
  >>> y = arange(12)
 
  >>> y
 
  >>> y
85行目: 100行目:
 
*https://qiita.com/supersaiakujin/items/d63c73bb7b5aac43898a
 
*https://qiita.com/supersaiakujin/items/d63c73bb7b5aac43898a
 
====箇所を指定====
 
====箇所を指定====
 +
----
 
=====1次元=====
 
=====1次元=====
 +
----
 
  x[n]
 
  x[n]
 
=====2次元=====
 
=====2次元=====
 +
----
 
  x[n,m]
 
  x[n,m]
 
====範囲を指定====
 
====範囲を指定====
 +
----
 
=====1次元=====
 
=====1次元=====
 +
----
 
  x[start:end:step]
 
  x[start:end:step]
 
=====2次元=====
 
=====2次元=====
 +
----
 
  x[start:end:step,start:end:step]
 
  x[start:end:step,start:end:step]
 
====行を抽出====
 
====行を抽出====
 +
----
 
  x[r]
 
  x[r]
 
  x[r,]
 
  x[r,]
 
  x[r,:]
 
  x[r,:]
 
====列を抽出====
 
====列を抽出====
 +
----
 
  x[,:c]
 
  x[,:c]
 
<blockquote>取り出した値が1次元の配列になるため注意 reshape()</blockquote>
 
<blockquote>取り出した値が1次元の配列になるため注意 reshape()</blockquote>
 
====条件を満たすデータを取り出す====
 
====条件を満たすデータを取り出す====
+
----
 
 
 
 
 
 
 
===操作===
 
===操作===
 +
----
 
====次元が異なる配列の演算====
 
====次元が異なる配列の演算====
 +
----
 
=====それぞれの列に掛ける=====
 
=====それぞれの列に掛ける=====
 +
----
 
  >>> x = arange(4)
 
  >>> x = arange(4)
 
  >>> x
 
  >>> x
114行目: 137行目:
 
  >>> x * 2
 
  >>> x * 2
 
  array([0, 2, 4, 6])
 
  array([0, 2, 4, 6])
 
 
=====それぞれの行に足し込む=====
 
=====それぞれの行に足し込む=====
 +
----
 
  >>> y = arange(10)
 
  >>> y = arange(10)
 
  >>> y.shape = (2,5)
 
  >>> y.shape = (2,5)

2021年6月19日 (土) 13:39時点における版

Python NumPy

Python | Python matplotlib |

ドキュメント


リファレンス


概要


NumPy


SciPy


matplotlib

  • 出版物のクオリティで、インタラクティブにプロットするのを容易にするライブラリ

NumPy

  • NumPyは多次元配列を扱うライブラリで、主に扱うデータ型は配列である
  • 配列は同じ型の要素のセットであり、正の整数のベクターによりインデックス付けされる

Install


apt-getを使ってubuntuへインストール

sudo apt-get install python-numpy python-scipy
PIPを使ってインストール

# pip install numpy
PIPを使ってWindowsにインストール
C:\Python27\Scripts>pip install numpy

配列の生成


リストから生成


>>> from numpy import *
>>> a = array( [ 10, 20, 30, 40 ] )
>>> a
array([10, 20, 30, 40])

arange を使い配列を生成


arange([start,] stop[, step,], dtype=None)
0から始まる整数の配列を生成
>>> b = arange(4)
>>> b
array([0, 1, 2, 3])
0から3まで 0.5 きざみの配列を生成

>>> np.arange(0,3,0.5)
array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5])

等しく割り付けられた配列を作成


>>> c = linspace(-pi,pi,3)
>>> c
array([-3.14159265,  0.        ,  3.14159265])

既存の配列から生成

>>> a1 = array([10,20,30])
>>> a2 = array([1,2,3])
>>> a3 = (a1 + a2) * 2
>>> a3
array([22, 44, 66])

多次元配列


>>> x = ones( (3,4) )
>>> x
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
>>> x.shape # タプルで次元を取得
(3, 4)

既存の配列の次元を変更


>>> y = arange(12)
>>> y
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> y.shape = (3,4)
>>> y
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

参照

箇所を指定


1次元

x[n]
2次元

x[n,m]

範囲を指定


1次元

x[start:end:step]
2次元

x[start:end:step,start:end:step]

行を抽出


x[r]
x[r,]
x[r,:]

列を抽出


x[,:c]

<blockquote>取り出した値が1次元の配列になるため注意 reshape()</blockquote>

条件を満たすデータを取り出す


操作


次元が異なる配列の演算


それぞれの列に掛ける

>>> x = arange(4)
>>> x
array([0, 1, 2, 3])
>>> x * 2
array([0, 2, 4, 6])
それぞれの行に足し込む

>>> y = arange(10)
>>> y.shape = (2,5)
>>> y
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> y1 = array([10,20,30,40,50])
>>> y2 = y + y1
>>> y2
array([[10, 21, 32, 43, 54],
       [15, 26, 37, 48, 59]])