Anda mungkin telah memperhatikan bahwa, dalam beberapa kasus, elemen array ditampilkan dengan tanda titik (mis. g. >>> import numpy as np 8 vs >>> import numpy as np 9). Hal ini disebabkan oleh perbedaan tipe data yang digunakan
In [1]: L = range(1000) In [2]: %timeit [i**2 for i in L] 1000 loops, best of 3: 403 us per loop In [3]: a = np.arange(1000) In [4]: %timeit a**2 100000 loops, best of 3: 12.7 us per loop 2
Tip
Tipe data yang berbeda memungkinkan kita untuk menyimpan data dengan lebih kompak di memori, tetapi sebagian besar waktu kita hanya bekerja dengan angka floating point. Perhatikan bahwa, pada contoh di atas, NumPy secara otomatis mendeteksi tipe data dari input
Anda dapat secara eksplisit menentukan tipe data yang Anda inginkan
In [1]: L = range(1000) In [2]: %timeit [i**2 for i in L] 1000 loops, best of 3: 403 us per loop In [3]: a = np.arange(1000) In [4]: %timeit a**2 100000 loops, best of 3: 12.7 us per loop _3
Tipe data default adalah floating point
In [1]: L = range(1000) In [2]: %timeit [i**2 for i in L] 1000 loops, best of 3: 403 us per loop In [3]: a = np.arange(1000) In [4]: %timeit a**2 100000 loops, best of 3: 12.7 us per loop _4
Ada juga jenis lainnya
KompleksIn [1]: L = range(1000) In [2]: %timeit [i**2 for i in L] 1000 loops, best of 3: 403 us per loop In [3]: a = np.arange(1000) In [4]: %timeit a**2 100000 loops, best of 3: 12.7 us per loop 5
BoolIn [1]: L = range(1000) In [2]: %timeit [i**2 for i in L] 1000 loops, best of 3: 403 us per loop In [3]: a = np.arange(1000) In [4]: %timeit a**2 100000 loops, best of 3: 12.7 us per loop 6
StringIn [1]: L = range(1000) In [2]: %timeit [i**2 for i in L] 1000 loops, best of 3: 403 us per loop In [3]: a = np.arange(1000) In [4]: %timeit a**2 100000 loops, best of 3: 12.7 us per loop 7
Lebih banyak- >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 _0
- >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 _1
- >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 _2
- >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 _3
Sekarang kita memiliki array data pertama kita, kita akan memvisualisasikannya
Mulailah dengan meluncurkan IPython
In [1]: L = range(1000) In [2]: %timeit [i**2 for i in L] 1000 loops, best of 3: 403 us per loop In [3]: a = np.arange(1000) In [4]: %timeit a**2 100000 loops, best of 3: 12.7 us per loop _8
Atau buku catatan
In [1]: L = range(1000) In [2]: %timeit [i**2 for i in L] 1000 loops, best of 3: 403 us per loop In [3]: a = np.arange(1000) In [4]: %timeit a**2 100000 loops, best of 3: 12.7 us per loop _9
Setelah IPython dimulai, aktifkan plot interaktif
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _0
Atau, dari notebook, aktifkan plot di notebook
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _1
>>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 4 penting untuk notebook, sehingga plot ditampilkan di notebook dan bukan di jendela baru
Matplotlib adalah paket perencanaan 2D. Kita dapat mengimpor fungsinya seperti di bawah ini
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _2
Dan kemudian gunakan (perhatikan bahwa Anda harus menggunakan >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 5 secara eksplisit jika Anda belum mengaktifkan plot interaktif dengan >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 6)
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _3
Atau, jika Anda telah mengaktifkan plot interaktif dengan >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 6
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _4
- merencanakan 1D
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _5
- Array 2D (seperti gambar)
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _6
Lihat juga
Lebih banyak di.
Latihan. Visualisasi sederhana
- Plot beberapa array sederhana. cosinus sebagai fungsi waktu dan matriks 2D
- Coba gunakan peta warna >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 _8 pada matriks 2D
Item dari array dapat diakses dan ditetapkan dengan cara yang sama seperti urutan Python lainnya (mis. g. daftar)
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _7
Peringatan
Indeks dimulai dari 0, seperti urutan Python lainnya (dan C/C++). Sebaliknya, di Fortran atau Matlab, indeks dimulai dari 1
Ungkapan python biasa untuk membalikkan urutan didukung
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _8
Untuk array multidimensi, indeks adalah tupel bilangan bulat
In [5]: np.array? String Form:<built-in function array> Docstring: array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, ... _9
Catatan
- Dalam 2D, dimensi pertama sesuai dengan baris, yang kedua sesuai dengan kolom
- untuk multidimensi >>> a = np.array([0, 1, 2, 3]) >>> a array([0, 1, 2, 3]) >>> a.ndim 1 >>> a.shape (4,) >>> len(a) 4 9, >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) 0 diartikan dengan mengambil semua elemen dalam dimensi yang tidak ditentukan
Mengiris. Array, seperti urutan Python lainnya juga bisa diiris
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 0
Perhatikan bahwa indeks terakhir tidak disertakan.
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 1
Ketiga komponen irisan tidak diperlukan. secara default, awal adalah 0, akhir adalah yang terakhir dan langkah adalah 1
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 2
Ringkasan bergambar kecil tentang pengindeksan dan pengirisan NumPy…
Anda juga dapat menggabungkan penugasan dan pemotongan
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 3
Latihan. Mengindeks dan mengiris
Cobalah berbagai rasa mengiris, menggunakan >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) 1, >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) 2 dan >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) 3. mulai dari linspace, usahakan mendapatkan bilangan ganjil yang menghitung mundur, dan bilangan genap yang menghitung maju
Reproduksi irisan dalam diagram di atas. Anda dapat menggunakan ekspresi berikut untuk membuat array
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 4
Latihan. Pembuatan larik
Buat array berikut (dengan tipe data yang benar)
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 5
Sama saja. masing-masing 3 pernyataan
Petunjuk. Elemen array individu dapat diakses mirip dengan daftar, mis. g. >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) 4 atau >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) 5
Petunjuk. Periksa docstring untuk >>> import numpy as np 6
Latihan. Ubin untuk pembuatan array
Telusuri dokumentasi untuk >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) _7, dan gunakan fungsi ini untuk membuat array
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 6
Operasi pemotongan membuat tampilan pada larik asli, yang hanya merupakan cara untuk mengakses data larik. Dengan demikian array asli tidak disalin dalam memori. Anda dapat menggunakan >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) _8 untuk memeriksa apakah dua larik berbagi blok memori yang sama. Namun perhatikan, bahwa ini menggunakan heuristik dan dapat memberi Anda positif palsu
Saat memodifikasi tampilan, larik asli juga dimodifikasi
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 7
Perilaku ini mungkin mengejutkan pada pandangan pertama… tetapi memungkinkan untuk menghemat memori dan waktu
Contoh yang berhasil. Saringan bilangan prima
Hitung bilangan prima dalam 0–99, dengan saringan
- Membangun bentuk (100,) array boolean >>> b = np.array([[0, 1, 2], [3, 4, 5]]) # 2 x 3 array >>> b array([[0, 1, 2], [3, 4, 5]]) >>> b.ndim 2 >>> b.shape (2, 3) >>> len(b) # returns the size of the first dimension 2 >>> c = np.array([[[1], [2]], [[3], [4]]]) >>> c array([[[1], [2]], [[3], [4]]]) >>> c.shape (2, 2, 1) 9, diisi dengan True di awal
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. 8
- Coret 0 dan 1 yang bukan bilangan prima
>>> np.lookfor('create array') Search results for 'create array' --------------------------------- numpy.array Create an array. numpy.memmap Create a memory-map to an array stored in a *binary* file on disk. _9
- Untuk setiap bilangan bulat >>> a = np.arange(10) # 0 . n-1 (!) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = np.arange(1, 9, 2) # start, end (exclusive), step >>> b array([1, 3, 5, 7]) _0 mulai dari 2, coret kelipatannya yang lebih tinggi
In [6]: np.con*? np.concatenate np.conj np.conjugate np.convolve 0
Telusuri >>> a = np.arange(10) # 0 . n-1 (!) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = np.arange(1, 9, 2) # start, end (exclusive), step >>> b array([1, 3, 5, 7]) _1, dan cetak bilangan prima
Menindaklanjuti
- Pindahkan kode di atas ke dalam file skrip bernama >>> a = np.arange(10) # 0 . n-1 (!) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = np.arange(1, 9, 2) # start, end (exclusive), step >>> b array([1, 3, 5, 7]) 2
- Jalankan untuk memeriksa apakah berfungsi
- Gunakan pengoptimalan yang disarankan dalam saringan Eratosthenes
- Lewati >>> a = np.arange(10) # 0 . n-1 (!) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = np.arange(1, 9, 2) # start, end (exclusive), step >>> b array([1, 3, 5, 7]) _0 yang sudah diketahui bukan bilangan prima
- Angka pertama yang dicoret adalah
Tip
Array NumPy dapat diindeks dengan irisan, tetapi juga dengan array boolean atau integer (topeng). Metode ini disebut pengindeksan mewah. Itu menciptakan salinan bukan tampilan
Menggunakan topeng boolean
In [6]: np.con*? np.concatenate np.conj np.conjugate np.convolve 1
Pengindeksan dengan topeng bisa sangat berguna untuk menetapkan nilai baru ke sub-array
In [6]: np.con*? np.concatenate np.conj np.conjugate np.convolve 2
Pengindeksan dengan array bilangan bulat
In [6]: np.con*? np.concatenate np.conj np.conjugate np.convolve 3
Pengindeksan dapat dilakukan dengan array bilangan bulat, di mana indeks yang sama diulang beberapa kali
In [6]: np.con*? np.concatenate np.conj np.conjugate np.convolve 4
Nilai baru dapat diberikan dengan pengindeksan semacam ini
In [6]: np.con*? np.concatenate np.conj np.conjugate np.convolve 5
Tip
Ketika array baru dibuat dengan mengindeks dengan array bilangan bulat, array baru memiliki bentuk yang sama dengan array bilangan bulat