This works:
a = [[1, 2, 3], [4, 5, 6]]
nd_a = np.array(a)
So this should work too:
nd_a = np.array([[x for x in y] for y in a])
Answer from Marijn van Vliet on Stack Overflow Top answer 1 of 3
21
This works:
a = [[1, 2, 3], [4, 5, 6]]
nd_a = np.array(a)
So this should work too:
nd_a = np.array([[x for x in y] for y in a])
2 of 3
11
To create a new array, it seems numpy.zeros is the way to go
import numpy as np
a = np.zeros(shape=(x, y))
You can also set a datatype to allocate it sensibly
>>> np.zeros(shape=(5,2), dtype=np.uint8)
array([[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]], dtype=uint8)
>>> np.zeros(shape=(5,2), dtype="datetime64[ns]")
array([['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000']],
dtype='datetime64[ns]')
See also
- How do I create an empty array/matrix in NumPy?
- np.full(size, 0) vs. np.zeros(size) vs. np.empty()
DataCamp
campus.datacamp.com โบ courses โบ intro-to-python-for-data-science โบ chapter-4-numpy
2D NumPy Arrays | Python
The arrays np_height and np_weight are one-dimensional arrays, but it's perfectly possible to create 2 dimensional, three dimensional, heck even seven dimensional arrays! Let's stick to 2 in this video though. You can create a 2D numpy array from a regular Python list of lists.
07:43
NumPy multidimensional arrays are easy! ๐ง - YouTube
08:16
2D Numpy Arrays for Data Science | Complete Python Tutorial - YouTube
Create a Matrix (2D array) in NumPy Python
17:50
How to create a 2-dimensional array in NumPy Python | Create ...
03:09
How to create a Numpy 2D Array in Python | Complete Guide | Examples ...
13:38
Creating Two Dimensional Array | User Input | Numpy | In Hindi ...
NumPy
numpy.org โบ doc โบ 2.5 โบ user โบ basics.creation.html
Array creation โ NumPy v2.5 Manual
NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...), respectively. Lists and tuples can define ndarray creation: a list of numbers will create a 1D array, a list of lists will create a 2D array, further nested lists ...
Python Examples
pythonexamples.org โบ python-numpy-create-2d-array
Create 2D Array in NumPy
Pass shape of the required 2D array, as a tuple, as argument to numpy.ones() function. The function returns a numpy array with specified shape, and all elements in the array initialised to ones. import numpy as np # create a 2D array with shape (3, 4) shape = (3, 4) arr = np.ones(shape) print(arr)
NumPy
numpy.org โบ doc โบ stable โบ reference โบ arrays.ndarray.html
The N-dimensional array (ndarray) โ NumPy v2.5 Manual
The array can be indexed using Python container-like syntax: >>> # The element of x in the *second* row, *third* column, namely, 6. >>> x[1, 2] 6 ยท For example slicing can produce views of the array: >>> y = x[:,1] >>> y array([2, 5], dtype=int32) >>> y[0] = 9 # this also changes the corresponding element in x >>> y array([9, 5], dtype=int32) >>> x array([[1, 9, 3], [4, 5, 6]], dtype=int32) ... New arrays can be constructed using the routines detailed in Array creation routines, and also by using the low-level ndarray constructor:
NumPy
numpy.org โบ devdocs โบ user โบ absolute_beginners.html
NumPy: the absolute basics for beginners โ NumPy v2.6.dev0 Manual
Using np.newaxis will increase the dimensions of your array by one dimension when used once. This means that a 1D array will become a 2D array, a 2D array will become a 3D array, and so on.
w3resource
w3resource.com โบ python-exercises โบ numpy โบ basic โบ numpy-basic-exercise-46.php
NumPy: Create a two-dimensional array of specified format - w3resource
Construct a 2D array with alternating row patterns and validate its structure with shape checks. Create a 2D array following a defined format and then reshape it into a flat array.
Programiz
programiz.com โบ python-programming โบ numpy โบ ndarray-creation
NumPy N-D Array Creation (With Examples)
We saw how to create N-d NumPy arrays from Python lists. Now we'll see how we can create them from scratch. To create multidimensional arrays from scratch we use functions such as ... The np.zeros() function allows us to create N-D arrays filled with all zeros. For example, ... # create 2D array with 2 rows and 3 columns filled with zeros array1 = np.zeros((2, 3)) print("2-D Array: ") print(array1)
Drbeane
drbeane.github.io โบ python_dsci โบ pages โบ array_2d.html
2-Dimensional Arrays โ Python for Data Science
Numpy provides two functions for concatenating arrays: hstack(), or horizontal stack, and vstack(), or vertical stack. As the names imply, these functions allow us to create new arrays by horizontally or vertically stacking arrays, as long as stacked arrays have the same sizes along the dimension ...
OpenGenus
iq.opengenus.org โบ 2d-array-in-numpy
2D Arrays in NumPy (Python)
October 28, 2022 - Random Array random.rand(r,c) - this function will generate an array with all random elements.
MLJAR
mljar.com โบ answers โบ define-two-dimensional-array-python
Define two-dimensional array in Python
array[2][2] = 5 # valid array[7][2] = 3 # error ยท This way is much easier. You can use zeros function from numpy to create 2D array with all values set to zero: import numpy array = numpy.zeros((4,4)) REMEMBER Even though the second version is easier, the first one is better for this operation unless we need any matrix operations.
NumPy
numpy.org โบ devdocs โบ user โบ basics.creation.html
Array creation โ NumPy v2.6.dev0 Manual
NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...), respectively. Lists and tuples can define ndarray creation: a list of numbers will create a 1D array, a list of lists will create a 2D array, further nested lists ...