What Is NumPy?
NumPy (Numerical Python) is the foundational library for numerical computing in Python. It provides the ndarray — a fast, flexible n-dimensional array — along with a comprehensive suite of mathematical functions for working with arrays. Almost every major data science library in Python (pandas, scikit-learn, SciPy, TensorFlow, PyTorch) is built on top of NumPy or relies on it for numerical operations.
The core advantage of NumPy over plain Python lists is performance. NumPy arrays are stored in contiguous blocks of memory with a fixed data type, enabling vectorized operations that execute in compiled C code rather than interpreted Python loops. For large datasets, this difference can be 10–100x faster than equivalent pure Python implementations.
Installing and Importing NumPy
NumPy is included in most data science environments (Anaconda, Google Colab, etc.) and can be installed with pip install numpy. The universal import convention is import numpy as np.
The ndarray
The ndarray is the central data structure in NumPy. Every array has a shape (dimensions and sizes), a dtype (data type of elements), and an ndim (number of dimensions).
�F��6��6WC��F���F���V��s��F���F��W���S��F����G#���F�VC��F&�G���G#��FC�6�S��FC��FC�GW�RFW67&�&��r6��R�bV6�F��V�6�����FC��FC�2f�"B�b2V�V�V�G3��B�R�f�"$B�G&����FC���G#��G#��FC�GG�S��FC��FC�FFG�R�b��V�V�V�G2��W7B&RV�f�&ғ��FC��FC�f��CcB���C3"�&����6���W�#���FC���G#��G#��FC��F����FC��FC��V�&W"�bF��V�6���2��W2���FC��FC�f�"fV7F�"�"f�"�G&���2f�"FV�6�#��FC���G#��G#��FC�6��S��FC��FC�F�F��V�&W"�bV�V�V�G3��FC��FC�#f�"�B�R�'&���FC���G#��G#��FC�FV�6��S��FC��FC�'�FW2W"V�V�V�C��FC��FC�f�"f��CcC��FC���G#���F&�G����F&�Sࠣƃ#�7&VF��r'&�3���#��F&�R&�&FW#�#"6V��FF��s�#b"6V��76��s�##��F�VC��G#��F��gV�7F�����F���F��FW67&�F�����F���F��W���S��F����G#���F�VC��F&�G���G#��FC���'&�����FC��FC�7&VFR'&�g&���F���Ɨ7B�"�W7FVBƗ7C��FC��FC���'&����"�5ғ��FC���G#��G#��FC���W&�2�6�R���FC��FC�'&��b���W&�3��FC��FC���W&�2��2�B����FC���G#��G#��FC�����W2�6�R���FC��FC�'&��b����W3��FC��FC�����W2��"�R����FC���G#��G#��FC���gV�6�R�f��FC��FC�'&�f���VBv�F�6��7F�Bf�VS��FC��FC���gV��2�2��r���FC���G#��G#��FC���&�vR�7F'B�7F��7FW���FC��FC�WfV�ǒ76VBf�VW2�Ɩ�R�F���&�vR���FC��FC���&�vR���"���FC���G#��G#��FC���Ɩ�76R�7F'B�7F�����FC��FC��WfV�ǒ76VBf�VW2&WGvVV�7F'B�B7F���FC��FC���Ɩ�76R������FC���G#��G#��FC���W�R����FC��FC��9v��FV�F�G��G&����FC��FC���W�R�2���FC���G#��G#��FC���&�F���&�B�6�R���FC��FC�V�f�&�&�F��f�VW2�������FC��FC���&�F���&�B�B�B���FC���G#��G#��FC���&�F���&�F�6�R���FC��FC�7F�F&B��&���vW76��&�F��f�VW3��FC��FC���&�F���&�F����FC���G#��G#��FC���&�F���&�F��B���r���v��6��R���FC��FC�&�F����FVvW'2�����r���v����FC��FC���&�F���&�F��B��r����FC���G#���F&�G����F&�Sࠣƃ#��FW���r�B6Ɩ6��s���#����V��'&�27W�'B�vW&gV���FW���r�V6��6�2&W���B�F���Ɨ7G2�f�"$B'&��6�FS���6�FS�����F&�R&�&FW#�#"6V��FF��s�#b"6V��76��s�##��F�VC��G#��F��W�&W76�����F���F��&W7V�C��F����G#���F�VC��F&�G���G#��FC��"�5���FC��FC�V�V�V�BB&�r"�6��V��3��FC���G#��G#��FC���2�����FC��FC�f�'7B2&�w2���6��V��3��FC���G#��G#��FC�������FC��FC���&�w2�6��V���&WGW&�2B'&����FC���G#��G#��FC���U���FC��FC�&���V���FW���s�V�V�V�G2w&VFW"F��S��FC���G#�
Concept | Meaning | Example |
|---|---|---|
a[[0, 2, 4], :] | Fancy indexing: rows 0, 2, and 4 | |
a[..., -1] | Last column (ellipsis selects all preceding dimensions) |
Important: NumPy slices return views (not copies) of the original array. Modifying a slice modifies the original. Use .copy() to create an independent copy.
Vectorized Operations and Broadcasting
NumPy operations apply element-wise across arrays without explicit loops. Arithmetic operators (+, -, *, /, **) all work element-wise on arrays of the same shape.
Broadcasting extends element-wise operations to arrays of different but compatible shapes. The rules are: NumPy compares shapes from the rightmost dimension; dimensions are compatible if they are equal or one of them is 1; a size-1 dimension is "stretched" to match the other array's dimension. For example, adding a shape (4, 3) array to a shape (3,) array broadcasts the 1D array across all 4 rows.
Broadcasting enables efficient operations like subtracting the column mean from every row without any Python loops: centered = a - a.mean(axis=0).
Aggregation Functions
Function | Description | axis=0 behavior |
|---|---|---|
np.sum(a) | Sum of all elements | Sum per column |
np.mean(a) | Arithmetic mean | Mean per column |
np.std(a) | Standard deviation | Std per column |
np.var(a) | Variance | Variance per column |
np.min(a) / np.max(a) | Minimum / maximum | Min/max per column |
np.argmin(a) / np.argmax(a) | Index of min/max | Index per column |
np.median(a) | Median value | Median per column |
np.percentile(a, q) | qth percentile | Percentile per column |
np.cumsum(a) | Cumulative sum | Cumulative sum per column |
Reshaping and Stacking
Operation | Description |
|---|---|
a.reshape(new_shape) | Change shape without changing data; total elements must match |
a.flatten() | Collapse to 1D array (returns a copy) |
a.ravel() | Collapse to 1D array (returns a view when possible) |
a.T or a.transpose() | Transpose (swap axes) |
np.concatenate([a, b], axis=0) | Join arrays along an existing axis |
np.vstack([a, b]) | Stack arrays vertically (row-wise) |
np.hstack([a, b]) | Stack arrays horizontally (column-wise) |
np.split(a, n, axis) | Split array into n equal sub-arrays |
np.newaxis | Add a new axis (increase dimensions by 1) |
Linear Algebra with numpy.linalg
The numpy.linalg module provides essential linear algebra operations used in machine learning and statistics:
F�����F���F��FW67&�F�����F����G#���F�VC��F&�G���G#��FC���F�B��"��"#��FC��FC��G&���V�F�Ɩ6F����F�B&�GV7B���FC���G#��G#��FC���Ɩ��r��b����FC��FC��G&����fW'6S��FC���G#��G#��FC���Ɩ��r�FWB����FC��FC�FWFW&֖��C��FC���G#��G#��FC���Ɩ��r�V�r����FC��FC�V�vV�f�VW2�BV�vV�fV7F�'3��FC���G#��G#��FC���Ɩ��r�7fB����FC��FC�6��wV�"f�VRFV6���6�F�����FC���G#��G#��FC���Ɩ��r�6��fR��"���FC��FC�6��fRƖ�V"7�7FV���#��FC���G#��G#��FC���Ɩ��r���&҆���FC��FC��G&���"fV7F�"��&���FC���G#���F&�G����F&�Sࠣƃ#�&7F�6�GFW&�2f�"FF�Ǘ6�3���#����7G&��s���&�Ɨ�RfVGW&RF���ӣ��7G&��s�������6�FS���&�Ɨ�VB������֖ₒ������������֖ₒ���6�FS�������7G&��s�7F�F&F��R���66�&R����7G&��s�������6�FS�7F�F&F��VB�������Vₒ����7FB����6�FS�������7G&��s�6�V�Bf�VW2�VWF��r6��F�F�����7G&��s�������6�FS�6�V�B���7V҇��F�&W6���B���6�FS��"�6�FS�&��'F�������V⇂�F�&W6���B���6�FS�������7G&��s�&W�6Rf�VW2�VWF��r6��F�F������v�W&R����7G&��s�������6�FS�6VB���v�W&R���������6�FS�������7G&��s�6��WFR6�'&V�F����G&�����7G&��s�������6�FS���6�'&6�Vb��G&���&�wf#�f�6R���6�FS�( B6��V��2&Rf&�&�W2�&�w2&R�'6W'fF���2���ࠣƃ#��V��g2�F3���#����V���B�F26W'fR6���V�V�F'�&��W2��V��W�6V�2B����vV�V�W2�V�W&�6�6��WFF���'&�2�W7B&R6��v�RGG�R��BF�R��2�F�֗�VBf�"�F�V�F�6��W&F���2��F2�2'V��B���V���BFG2�&V�VB�W2���FW��6��V����W2��֗�VBGG�W2W"6��V���֗76��rf�VR��FƖ�r��B��v���WfV��W&F���2Ɩ�Rw&�W'��B�W&vR�f�"W&R�V�W&�6�v�&���G&���W&F���2��V�W&�6�6��V�F�����6���R�V&��rfVGW&R'&�2���V���2F�R&�v�BF����f�"F'V�"FFv�F�֗�VBG�W2�B��VB6��V��2�W6R�F2( B�B6��2�V��V�FW"F�R���Bf�"F�R�Vg�ƖgF��r���ࠣƃ#�7V��'����#����V���2F�R�V�W&�6�&6�&��R�bF�R�F���FF66�V�6R7F6���7FW&��r�G2'&�7&VF������FW���r�'&�F67F��r��Bvw&VvF���GFW&�2���w2�Ǘ7G2F�w&�FRf7B�W�&W76�fR�V�W&�6�6�FRv�F��WBW�Ɩ6�B�F������2�V�FW'7F�F��r�V���6���W2�BV6�W"F�v�&�v�F���v�W"��WfV�Ɩ'&&�W2Ɩ�R�F2�B66���B��V&��6��6RF��6RƖ'&&�W2W��6R�V��'&�2�B&W7V7B�V��6��fV�F���2BF�V�"6�&R��FW&f6W2����
Create a free reader account to keep reading.