Различные методы создания массивов NumPy, заполненных единицами

Чтобы создать массив NumPy, заполненный единицами, вы можете использовать несколько методов. Вот несколько примеров:

Метод 1: использование numpy.ones()

import numpy as np
# Create a 1D array with 5 ones
array_1d = np.ones(5)
# Create a 2D array with shape (3, 4) filled with ones
array_2d = np.ones((3, 4))

Метод 2: использование numpy.ones_like()

import numpy as np
# Create a new array with the same shape and data type as an existing array, filled with ones
existing_array = np.array([1, 2, 3])
new_array = np.ones_like(existing_array)

Метод 3: использование numpy.full()

import numpy as np
# Create a 1D array with 7 ones
array_1d = np.full(7, 1)
# Create a 2D array with shape (2, 3) filled with ones
array_2d = np.full((2, 3), 1)

Метод 4: использование numpy.ones()и умножение на скаляр

import numpy as np
# Create a 1D array with 6 ones
array_1d = np.ones(6) * 1
# Create a 2D array with shape (2, 4) filled with ones
array_2d = np.ones((2, 4)) * 1