In-depth analysis of numpy random number generation method

WBOY
Release: 2024-01-26 08:08:05
Original
1057 people have browsed it

In-depth analysis of numpy random number generation method

Detailed explanation of numpy random number generation method

numpy is an important scientific computing library for Python, which contains many functions for processing arrays and matrices. One of them is the random number generation function, which can easily generate various types of random numbers. This article will introduce numpy's random number generation method in detail and provide specific code examples.

  1. numpy.random.rand()
    This function can generate a uniformly distributed random number of a specified size, with a value range between 0 and 1, including 0 but not including 1 . The following is a code example:

    import numpy as np
    
    # 生成一个2x3的均匀分布随机数矩阵
    arr = np.random.rand(2, 3)
    print(arr)
    Copy after login

    Output result:

    [[0.34537909 0.43261776 0.78323571]
     [0.99208808 0.44671096 0.19721267]]
    Copy after login
  2. numpy.random.randn()
    This function can generate a standard normal distribution of a specified size Random numbers are normally distributed random numbers with a mean of 0 and a standard deviation of 1. The following is a code example:

    import numpy as np
    
    # 生成一个1x5的标准正态分布随机数数组
    arr = np.random.randn(1, 5)
    print(arr)
    Copy after login

    Output result:

    [[-0.21346802 -0.65486627 -0.80993532  1.30767345 -0.51662313]]
    Copy after login
  3. numpy.random.randint()
    This function can generate an integer random number within the specified range , you can specify the upper and lower limits of the value range. Here is a code example:

    import numpy as np
    
    # 生成一个1x5的整数随机数数组,范围在0和10之间
    arr = np.random.randint(0, 10, size=(1, 5))
    print(arr)
    Copy after login

    Output result:

    [[3 9 1 8 6]]
    Copy after login
  4. numpy.random.choice()
    This function can select from a given set of elements Randomly select one or more elements, you can specify the number of selections. The following is a code example:

    import numpy as np
    
    # 从列表中随机选择2个元素
    arr = np.random.choice(['apple', 'banana', 'orange', 'pear'], size=2)
    print(arr)
    Copy after login

    Output result:

    ['banana' 'apple']
    Copy after login

Through the above example, we can see that numpy provides many convenient random number generation methods, which can be based on Different needs generate different types of random numbers. These random number generation methods are widely used in scientific computing, machine learning, statistical analysis and other fields. Understanding and mastering these methods is of great benefit to data analysis and scientific computing programming work.

The above is the detailed content of In-depth analysis of numpy random number generation method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!