Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a numpy array of N numbers of the same value?

This is surely an easy question:

How does one create a numpy array of N values, all the same value?

For instance, numpy.arange(10) creates 10 values of integers from 0 to 9.

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

I would like to create a numpy array of 10 values of the same integer,

array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
like image 388
ShanZhengYang Avatar asked Jul 07 '15 14:07

ShanZhengYang


2 Answers

Use numpy.full():

import numpy as np

np.full(
  shape=10,
  fill_value=3,
  dtype=np.int
)
    
> array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
like image 166
Daniel Lenz Avatar answered Sep 20 '22 15:09

Daniel Lenz


Very easy 1)we use arange function :-

arr3=np.arange(0,10)
output=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

2)we use ones function whose provide 1 and after we will do multiply 3 :-

arr4=np.ones(10)*3
output=array([3., 3., 3., 3., 3., 3., 3., 3., 3., 3.])
like image 42
Sumit Bansal Avatar answered Sep 16 '22 15:09

Sumit Bansal