Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Array Concatenation | Commonly used NumPy Functions
Ultimate NumPy
course content

Зміст курсу

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
Array Concatenation

Array concatenation is a fundamental operation in NumPy that combines arrays along a specified axis to create larger, more comprehensive datasets. This is especially useful in machine learning, where data is often split across multiple arrays or stored separately, such as when it comes from different sources.

Essentially, concatenation involves joining arrays together to form a new array.

NumPy has a concatenate() function that enables you to concatenate arrays along a specified axis:

  • axis=0 (the default value) concatenates the arrays by rows;
  • axis=1 concatenates the arrays by columns.

The first parameter of this function is the sequence of arrays (a tuple or list of arrays) to concatenate, while axis is the second parameter.

123456
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenating 1D arrays along their only axis 0 concatenated_array = np.concatenate((array1, array2)) print(concatenated_array)
copy

Concatenation creates a 1D array with the elements of the first array followed by the elements of the second array.

Concatenating 2D arrays is performed in a similar way, but you also have to specify the axis parameter:

123456789
import numpy as np array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenating along the axis 0 (rows) concatenated_array_rows = np.concatenate((array1, array2)) print(f'Axis = 0:\n{concatenated_array_rows}') # Concatenating along the axis 1 (columns) concatenated_array_columns = np.concatenate((array1, array2), axis=1) print(f'Axis = 1:\n{concatenated_array_columns}')
copy

The purple elements correspond to array1, and the green ones to array2.

In fact, we can concatenate any number of arrays, and it will work the same way.

Завдання
test

Swipe to show code editor

You are analyzing the simulated quarterly sales data for two products in 2021 and 2022. The data is stored in two 2D arrays:

  • sales_data_2021: сontains the sales data for each quarter of 2021 for both products;
  • sales_data_2022: contains the sales data for each quarter of 2022 for both products.
  1. Concatenate the sales data for both products by columns, combining the data for both years.

  2. Ensure that the 2022 sales data follows the 2021 sales data.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 6
toggle bottom row

book
Array Concatenation

Array concatenation is a fundamental operation in NumPy that combines arrays along a specified axis to create larger, more comprehensive datasets. This is especially useful in machine learning, where data is often split across multiple arrays or stored separately, such as when it comes from different sources.

Essentially, concatenation involves joining arrays together to form a new array.

NumPy has a concatenate() function that enables you to concatenate arrays along a specified axis:

  • axis=0 (the default value) concatenates the arrays by rows;
  • axis=1 concatenates the arrays by columns.

The first parameter of this function is the sequence of arrays (a tuple or list of arrays) to concatenate, while axis is the second parameter.

123456
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenating 1D arrays along their only axis 0 concatenated_array = np.concatenate((array1, array2)) print(concatenated_array)
copy

Concatenation creates a 1D array with the elements of the first array followed by the elements of the second array.

Concatenating 2D arrays is performed in a similar way, but you also have to specify the axis parameter:

123456789
import numpy as np array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenating along the axis 0 (rows) concatenated_array_rows = np.concatenate((array1, array2)) print(f'Axis = 0:\n{concatenated_array_rows}') # Concatenating along the axis 1 (columns) concatenated_array_columns = np.concatenate((array1, array2), axis=1) print(f'Axis = 1:\n{concatenated_array_columns}')
copy

The purple elements correspond to array1, and the green ones to array2.

In fact, we can concatenate any number of arrays, and it will work the same way.

Завдання
test

Swipe to show code editor

You are analyzing the simulated quarterly sales data for two products in 2021 and 2022. The data is stored in two 2D arrays:

  • sales_data_2021: сontains the sales data for each quarter of 2021 for both products;
  • sales_data_2022: contains the sales data for each quarter of 2022 for both products.
  1. Concatenate the sales data for both products by columns, combining the data for both years.

  2. Ensure that the 2022 sales data follows the 2021 sales data.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 6
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt