Home  >  Article  >  Backend Development  >  Numpy Tutorial: Learn Array Creation from Scratch

Numpy Tutorial: Learn Array Creation from Scratch

PHPz
PHPzOriginal
2024-02-20 09:32:24819browse

Numpy Tutorial: Learn Array Creation from Scratch

Numpy Tutorial: Learn to create arrays from scratch, specific code examples are required

Overview:
Numpy is an open source mathematics library for Python that provides a large number of Mathematical functions and data structures, especially arrays (Arrays). Arrays are a very common and important data structure in machine learning and data analysis, so learning how to create and manipulate arrays is critical. This tutorial aims to introduce the creation of arrays in Numpy from scratch to help readers get started quickly.

  1. Import Numpy library
    Before we begin, we first need to import the Numpy library. Usually, we use the import statement to import the Numpy library into our Python code.
import numpy as np
  1. Create a one-dimensional array
    In Numpy, a one-dimensional array is a list containing elements of the same data type. We can use the ndarray function provided by Numpy to create a one-dimensional array.
array_1d = np.array([1, 2, 3, 4, 5])
print(array_1d)

Output: [1 2 3 4 5]

  1. Create a two-dimensional array
    A two-dimensional array is a table structure containing multiple rows and columns. We can create a two-dimensional array using a variety of methods, the most common of which is from a list of lists.
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(array_2d)

Output:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
  1. Create an array of a specific type
    In some cases, we need to create an array of a specific type, such as an array of all 0s, An array of all ones or an empty array. Numpy provides some functions to create these special types of arrays.
  • Create an array of all 0s

    zeros_array = np.zeros((3, 4))
    print(zeros_array)

    Output:

    [[0. 0. 0. 0.]
     [0. 0. 0. 0.]
     [0. 0. 0. 0.]]
  • Create an array of all 1s

    ones_array = np.ones((2, 3))
    print(ones_array)

    Output:

    [[1. 1. 1.]
     [1. 1. 1.]]
  • Create empty array

    empty_array = np.empty((2, 2))
    print(empty_array)

    Output:

    [[4.94e-323 9.88e-323]
     [1.48e-322 1.97e-322]]
    ##Create sequence array
  1. In some cases, we want to create a sequence array, that is, an evenly spaced array. Numpy provides the
    arange function and the linspace function to create such an array.
  • Use the

    arange function to create a sequence array

    sequence_array = np.arange(0, 10, 2)
    print(sequence_array)

    Output: [0 2 4 6 8]

  • Use the

    linspace function to create a sequence array

    sequence_array = np.linspace(0, 1, 5)
    print(sequence_array)

    Output: [0. 0.25 0.5 0.75 1. ]

    Creation of random array
  1. In addition to the above methods, we can also use the random function provided by Numpy to create a random array. Commonly used random functions include
    random, rand, randn and randint, etc.
  • Create a random array

    random_array = np.random.random((2, 3))
    print(random_array)

    Output:

    [[0.59525333 0.78593695 0.30467253]
     [0.83647996 0.09302248 0.85711096]]

  • Create a random array that obeys the normal distribution

    normal_array = np.random.randn(3, 3)
    print(normal_array)

    Output:

    [[-0.96338454 -0.44881001  0.01016194]
     [-0.78893991 -0.32811758  0.11091332]
     [ 0.87585342  0.49660924 -0.52104011]]

  • Create an array of random integers

    random_int_array = np.random.randint(1, 10, (2, 4))
    print(random_int_array)

    Output:

    [[3 9 3 3]
     [1 9 7 5]]

Introduction to this article It explains the creation of arrays in the Numpy library, including the creation of one-dimensional arrays and two-dimensional arrays, as well as the creation methods of specific types of arrays, sequence arrays and random arrays, and provides specific code examples. I hope this tutorial can help readers understand and master the creation of arrays in Numpy.

The above is the detailed content of Numpy Tutorial: Learn Array Creation from Scratch. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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