NumPy
Penulis asliTravis Oliphant
PengembangKomunitas proyek
Rilis awalSebagai Numeric, 1995 (1995); sebagai NumPy, 2006 (2006)
Templat:Kotak info perangkat lunak/simple
Ditulis dalamPython, C
Sistem operasiCross-platform
JenisAnalisis numerik
LisensiBSD[1]
Situs webnumpy.org Sunting ini di Wikidata
Repositori

NumPy (dieja /ˈnʌmp/, atau /ˈnʌmpi/[3][4]) adalah sebuah pustaka untuk bahasa pemrograman Python, NumPy memberikan dukungan untuk himpunan dan matriks multidimensi yang besar, dan dilengkapi koleksi sejumlah besar fungsi matematika tingkat tinggi untuk beroperasi pada himpunan ini.[5] Proyek pendahulu NumPy, Numeric, awalnya dibuat oleh Jim Hugunin dengan kontribusi dari beberapa pengembang lain pada tahun 1995. Pada tahun 2005, Travis Oliphant menciptakan NumPy dengan memasukkan fitur Numarray ke dalam Numeric, serta melakukan modifikasi besar-besaran. NumPy adalah perangkat lunak sumber terbuka dan memiliki banyak kontributor.

Contoh program

sunting
Pembuatan himpunan (array)
>>> import numpy as np
>>> x = np.array([1, 2, 3])
>>> x
array([1, 2, 3])
>>> y = np.arange(10)  # seperti fungsi Python list(range(10)), tetapi menghasilkan array
>>> y
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Operasi dasar
>>> a = np.array([1, 2, 3, 6])
>>> b = np.linspace(0, 2, 4)  # buat array dengan empat titik dengan jarak yang sama dimulai dengan 0 dan diakhiri dengan 2.
>>> c = a - b
>>> c
array([ 1.        ,  1.33333333,  1.66666667,  4.        ])
>>> a**2
array([ 1,  4,  9, 36])
Fungsi universal
>>> a = np.linspace(-np.pi, np.pi, 100)
>>> b = np.sin(a)
>>> c = np.cos(a)
Aljabar linear
>>> from numpy.random import rand
>>> from numpy.linalg import solve, inv
>>> a = np.array([[1, 2, 3], [3, 4, 6.7], [5, 9.0, 5]])
>>> a.transpose()
array([[ 1. ,  3. ,  5. ],
       [ 2. ,  4. ,  9. ],
       [ 3. ,  6.7,  5. ]])
>>> inv(a)
array([[-2.27683616,  0.96045198,  0.07909605],
       [ 1.04519774, -0.56497175,  0.1299435 ],
       [ 0.39548023,  0.05649718, -0.11299435]])
>>> b =  np.array([3, 2, 1])
>>> solve(a, b)  # menyelesaikan persamaan ax = b
array([-4.83050847,  2.13559322,  1.18644068])
>>> c = rand(3, 3) * 20  # buat matriks nilai acak 3x3 dalam [0,1] dengan skala 20
>>> c
array([[  3.98732789,   2.47702609,   4.71167924],
       [  9.24410671,   5.5240412 ,  10.6468792 ],
       [ 10.38136661,   8.44968437,  15.17639591]])
>>> np.dot(a, c)  # matrix multiplication
array([[  53.61964114,   38.8741616 ,   71.53462537],
       [ 118.4935668 ,   86.14012835,  158.40440712],
       [ 155.04043289,  104.3499231 ,  195.26228855]])
>>> a @ c # Starting with Python 3.5 and NumPy 1.10
array([[  53.61964114,   38.8741616 ,   71.53462537],
       [ 118.4935668 ,   86.14012835,  158.40440712],
       [ 155.04043289,  104.3499231 ,  195.26228855]])
Tensor
>>> M = np.zeros(shape=(2, 3, 5, 7, 11))
>>> T = np.transpose(M, (4, 2, 1, 3, 0))
>>> T.shape
(11, 5, 3, 7, 2)
Kombinasi dengan OpenCV
>>> import numpy as np
>>> import cv2
>>> r = np.reshape(np.arange(256*256)%256,(256,256))  # array 256x256 piksel dengan gradien horizontalfrom 0 to 255 for the red color channel
>>> g = np.zeros_like(r)  # array dengan ukuran dan tipe yang sama dengan r tetapi diisi dengan 0 untuk kanal warna hijau
>>> b = r.T # transposed r will give a vertical gradient for the blue color channel
>>> cv2.imwrite('gradients.png', np.dstack([b,g,r]))  # Gambar OpenCV ditafsirkan sebagai BGR, array tumpuk kedalaman akan ditulis ke file PNG RGB 8bit yang dinamai 'gradients.png'
True
Pencarian Nearest Neighbour - Algoritma Python Iteratif dan versi vektorisasi NumPy yang sepadan
>>> # # # Pure iterative Python # # #
>>> points = [[9,2,8],[4,7,2],[3,4,4],[5,6,9],[5,0,7],[8,2,7],[0,3,2],[7,3,0],[6,1,1],[2,9,6]]
>>> qPoint = [4,5,3]
>>> minIdx = -1
>>> minDist = -1
>>> for idx, point in enumerate(points):  # iterate over all points
...     dist = sum([(dp-dq)**2 for dp,dq in zip(point,qPoint)])**0.5  # compute the euclidean distance for each point to q
...     if dist < minDist or minDist < 0:  # if necessary, update minimum distance and index of the corresponding point
...         minDist = dist
...         minIdx = idx

>>> print('Nearest point to q: {0}'.format(points[minIdx]))
Nearest point to q: [3, 4, 4]

>>> # # # Equivalent NumPy vectorization # # #
>>> import numpy as np
>>> points = np.array([[9,2,8],[4,7,2],[3,4,4],[5,6,9],[5,0,7],[8,2,7],[0,3,2],[7,3,0],[6,1,1],[2,9,6]])
>>> qPoint = np.array([4,5,3])
>>> minIdx = np.argmin(np.linalg.norm(points-qPoint,axis=1))  # compute all euclidean distances at once and return the index of the smallest one
>>> print('Nearest point to q: {0}'.format(points[minIdx]))
Nearest point to q: [3 4 4]

Lihat pula

sunting

Referensi

sunting
  1. ^ "NumPy — NumPy". numpy.org. NumPy developers.
  2. ^ "Releases – numpy/numpy". Diakses tanggal 8 February 2021 – via GitHub.
  3. ^ Pine, David (2014). "Python resources". Rutgers University. Diakses tanggal 2017-04-07.
  4. ^ "How do you say numpy?". Reddit. 2015. Diakses tanggal 2017-04-07.
  5. ^ Charles R Harris; K. Jarrod Millman; Stéfan J. van der Walt; et al. (16 September 2020). "Array programming with NumPy" (PDF). Nature (dalam bahasa Inggris). 585 (7825): 357–362. arXiv:2006.10256. Bibcode:2020Natur.585..357H. doi:10.1038/S41586-020-2649-2. ISSN 1476-4687. PMC 7759461. PMID 32939066. Wikidata Q99413970.

Bacaan lanjutan

sunting

Pranala luar

sunting

📚 Artikel Terkait di Wikipedia

Makna kehidupan

ISBN 0-19-508531-0. George Cappannelli; Sedena Cappannelli (2004). Authenticity: Simple Strategies for Greater Meaning and Purpose at Work and at Home. Emmis Books

Terence Tao

kombinatorika aljabar Knutson, Allen; Tao, Terence. The honeycomb model of GLn(ℂ) tensor products. I. Proof of the saturation conjecture. J. Amer. Math. Soc. 12

Inception (arsitektur pemelajaran mendalam)

keluaran dari lapisan konvolusional dan lapisan pengumpulan. Sebagai contoh, tensor berukuran 35 × 35 × 320 {\displaystyle 35\times 35\times 320} dapat diperkecil

Matematika

lapangan itu ke dalam wilayah dasar keempat, yakni perubahan. Kalkulus tensor mengkaji kesetangkupan dan perilaku vektor yang dirotasi. Sejumlah masalah

Microsoft Azure

27, 2018. "Google goes bilingual, Facebook fleshes out translation and TensorFlow is dope - And, Microsoft is assisting fish farmers in Japan". The Register

Etanol

ref bernama "m_and_b" Frederick D. Rossini (1937). "Heats of Formation of Simple Organic Molecules". Ind. Eng. Chem. 29 (12): 1424–1430. doi:10.1021/ie50336a024

TensorFlow

TensorFlow adalah pustaka perangkat lunak sumber terbuka dan gratis untuk pembelajaran mesin. Pustaka ini dapat digunakan untuk berbagai keperluan, tetapi

Determinan

1971, pp 173, 191. Harris 2014, §4.7 McConnell (1957). Applications of Tensor Analysis. Dover Publications. hlm. 10–17. Horn & Johnson 2018, §0.8.2. Lang