.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\numpy\demo_create.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_numpy_demo_create.py: Create array =============== This demo shows the features of ``numpy`` (https://numpy.org/doc/stable/user/quickstart.html). .. GENERATED FROM PYTHON SOURCE LINES 6-13 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt array = np.arange(15).reshape(3, 5) .. GENERATED FROM PYTHON SOURCE LINES 15-17 .. code-block:: Python array.shape .. rst-class:: sphx-glr-script-out .. code-block:: none (3, 5) .. GENERATED FROM PYTHON SOURCE LINES 18-20 .. code-block:: Python array.ndim .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 21-23 .. code-block:: Python array.dtype.name .. rst-class:: sphx-glr-script-out .. code-block:: none 'int32' .. GENERATED FROM PYTHON SOURCE LINES 24-26 .. code-block:: Python array.itemsize .. rst-class:: sphx-glr-script-out .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 27-29 .. code-block:: Python array.size .. rst-class:: sphx-glr-script-out .. code-block:: none 15 .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python type(array) .. GENERATED FROM PYTHON SOURCE LINES 33-34 **dtype, dimensions** .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python array2 = np.array([1.2, 3.5, 5.1]) array2.dtype.name .. rst-class:: sphx-glr-script-out .. code-block:: none 'float64' .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: Python np.array([(1.5, 2, 3), (4, 5, 6)]) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[1.5, 2. , 3. ], [4. , 5. , 6. ]]) .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python np.array([[1, 2], [3, 4]], dtype=complex) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j]]) .. GENERATED FROM PYTHON SOURCE LINES 44-45 **Zeros, ones, empty** .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python np.zeros((3, 4)) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python np.ones((2, 3, 4), dtype=np.int16) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]], dtype=int16) .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python np.empty((2, 10)) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[-2.3107758 , -2.47187343, -2.47187343, -2.63261058, -2.63261058, -2.79310345, -2.79310345, -2.95344595, -2.95344595, -3. ], [-2.22936756, -2.34787573, -2.34787573, -2.46687606, -2.46687606, -2.5862069 , -2.5862069 , -2.70573978, -2.70573978, -2.74047929]]) .. GENERATED FROM PYTHON SOURCE LINES 54-55 **Sequence of numbers** .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python np.arange(10, 30, 5) np.arange(0, 2, 0.3) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]) .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python np.linspace(0, 2, 9) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ]) .. GENERATED FROM PYTHON SOURCE LINES 62-69 .. code-block:: Python x = np.linspace(0, 2 * np.pi, 100) f = np.sin(x) plt.figure() plt.plot(f) plt.show() .. image-sg:: /11_demos/python_packages/numpy/images/sphx_glr_demo_create_001.png :alt: demo create :srcset: /11_demos/python_packages/numpy/images/sphx_glr_demo_create_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-71 **Reshape** .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python np.arange(12).reshape(4, 3) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) .. GENERATED FROM PYTHON SOURCE LINES 74-75 .. code-block:: Python c = np.arange(24).reshape(2, 3, 4) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.063 seconds) .. _sphx_glr_download_11_demos_python_packages_numpy_demo_create.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_create.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_create.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_create.zip `