![]() |
VOOZH | about |
Numpy Arrays are grid-like structures similar to lists in Python but optimized for numerical operations. The most straightforward way to create a NumPy array is by converting a regular Python list into an array using the np.array() function.
Let's understand this with the help of an example:
[1 2 3 4 5] [[1 2] [3 4]]
For assigning a specific values. NumPy provides several function to create arrays filled with zeros, ones, or a specific constant value.
Example:
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]
Example:
[[1. 1. 1.] [1. 1. 1.]]
np.full() function allows you to create an array filled with a specific value.Example:
[[7 7] [7 7]]
NumPy also has functions for generating arrays with random values, useful for simulations and testing.
Example:
[[0.67820861 0.64484802 0.48673431] [0.00263043 0.55383721 0.43240166]]
Example:
[[4 6 5] [7 4 8] [8 5 2]]
Another common method of creating arrays is using a range of values. NumPy provides functions like np.arange() and np.linspace() for this purpose.
Example:
[0 2 4 6 8]
Example:
[0. 0.25 0.5 0.75 1. ]
NumPy also provides functions for creating identity matrices and diagonal matrices, which are often used in linear algebra.
Example:
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
Example:
[[1 0 0] [0 2 0] [0 0 3]]
| Function | Description |
|---|---|
| empty() | Return a new array of given shape and type, without initializing entries |
| empty_like() | Return a new array with the same shape and type as a given array |
| eye() | Return a 2-D array with ones on the diagonal and zeros elsewhere. |
| identity() | Return the identity array |
| ones() | Return a new array of given shape and type, filled with ones |
| ones_like() | Return an array of ones with the same shape and type as a given array |
| zeros() | Return a new array of given shape and type, filled with zeros |
| zeros_like() | Return an array of zeros with the same shape and type as a given array |
| full_like() | Return a full array with the same shape and type as a given array. |
| array() | Create an array |
| asarray() | Convert the input to an array |
| asanyarray() | Convert the input to an ndarray, but pass ndarray subclasses through |
| ascontiguousarray() | Return a contiguous array in memory (C order) |
| asmatrix() | Interpret the input as a matrix |
| copy() | Return an array copy of the given object |
| frombuffer() | Interpret a buffer as a 1-dimensional array |
| fromfile() | Construct an array from data in a text or binary file |
| fromfunction() | Construct an array by executing a function over each coordinate |
| fromiter() | Create a new 1-dimensional array from an iterable object |
| fromstring() | A new 1-D array initialized from text data in a string |
| loadtxt() | Load data from a text file |
| arange() | Return evenly spaced values within a given interval |
| linspace() | Return evenly spaced numbers over a specified interval |
| logspace() | Return numbers spaced evenly on a log scale |
| geomspace() | Return numbers spaced evenly on a log scale (a geometric progression) |
| meshgrid() | Return coordinate matrices from coordinate vectors |
| mgrid() | nd_grid instance which returns a dense multi-dimensional “meshgrid |
| ogrid() | nd_grid instance which returns an open multi-dimensional “meshgrid |
| diag() | Extract a diagonal or construct a diagonal array |
| diagflat() | Create a two-dimensional array with the flattened input as a diagonal |
| tri() | An array with ones at and below the given diagonal and zeros elsewhere |
| tril() | Lower triangle of an array |
| triu() | Upper triangle of an array |
| vander() | Generate a Vandermonde matrix |
| mat() | Interpret the input as a matrix |
| bmat() | Build a matrix object from a string, nested sequence, or array |