search
Search
Unlock 100+ guides
search toc
close
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# NumPy | repeat method

schedule Aug 12, 2023
Last updated
local_offer
PythonNumPy
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

The `np.repeat(~)` method is used to create a Numpy array by repeating the provided value.

# Parameters

1. `a` | `number` or `array-like`

✜ Data that is to be repeated.

2. `repeats`link | `int`

✜ The number of times you'd like to repeat the value.

3. `axis`link | `int` | `optional`

✜ Axis along which to repeat. By default, you would get a one-dimensional flat array.

# Return value

A Numpy array with the first parameter, `a`, repeated according to your parameters `repeats` and `axis`.

# Examples

## Repeating a number

``` np.repeat(1, 3) array([1, 1, 1]) ```

## Repeating an one-dimensional array

``` np.repeat([1,2], 3) array([1, 1, 1, 2, 2, 2]) ```

## Repeating a two-dimensional array

Repeating a two-dimensional array to create a flattened array:

``` np.repeat([[1,2], [3,4]], 2) array([1, 1, 2, 2, 3, 3, 4, 4]) ```

Since we did not specify the `axis` (i.e. the third parameter), we end up with a flattened Numpy array.

## Repeating a two-dimensional array vertically

To repeat a two-dimensional array vertically, set `axis=0` like follows:

``` np.repeat([[1,2], [3,4]], 2, axis=0) array([[1, 2], [1, 2], [3, 4], [3, 4]]) ```

## Repeating a two-dimensional array horizontally

To repeat a two-dimensional array horizontally, set `axis=1` like follows:

``` np.repeat([[1,2], [3,4]], 2, axis=1) array([[1, 1, 2, 2], [3, 3, 4, 4]]) ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!