Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Select random value from a list-Python
Next article icon

To create an array filled withrandom numbers, given the shape and type of array, we can usenumpy.empty() method.

Example:

Python3
importnumpyasnpb=np.empty(3,dtype=float)print("Matrix b :\n",b)

Output:

[6.79249278e-310 6.79249278e-310 1.69375695e+190]

Syntax

Syntax:np.empty(shape, dtype=None, order='C', *, like=None)

Parameter:

  • shape : Number of rows
  • order : C_contiguous or F_contiguous
  • dtype : [optional, float(by Default)] Data type of returned array.
  • like: [optional] allows you to create an array with the same shape and data type as another array-like object

More Examples

Let's see some examples of how to create an array with random values inNumPy.

Example 1

We create 2 NumPy arrays with random values in this example. Array 'b' with size 2 and array 'a' which is a 2D array.

Python3
# Python Program to create numpy array# filled with random valuesimportnumpyasgeekb=geek.empty(2,dtype=int)print("Matrixb: \n",b)a=geek.empty([2,2],dtype=int)print("\nMatrixa: \n",a)

Output:

Matrix b :  [140489599921032        21301024]Matrix a :  [[140489599921048        18650592] [       10738656 140489568798064]]

Example 2

In this example, we create 2 random value arrays in NumPy. Array 'c' which is a 3x3 array and array 'd' which is a 5x3 array.

Python3
# Python Program to create numpy array# filled with random valuesimportnumpyasgeek# Python Program to create numpy array# filled with random valuesimportnumpyasgeekc=geek.empty([3,3])print("\nMatrixc: \n",c)d=geek.empty([5,3],dtype=int)print("\nMatrixd: \n",d)

Output:

Matrix c :  [[  1.37596097e-316   5.39314154e-317   5.39307830e-317] [  5.39345774e-317   5.39345774e-317   6.93325440e-310] [  5.39481741e-317   6.93325440e-310   8.69555537e-322]]Matrix d :  [[140330665569272        23735792               0] [       10739936 140330589556496               0] [              0               0        10739904] [140330587337872               0        10915968] [              0        10739904               0]]

Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp