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.
import numpy as np
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]
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]]
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]]
function and the
linspace function to create such an array.
arange function to create a sequence array
sequence_array = np.arange(0, 10, 2) print(sequence_array)
linspace function to create a sequence array
sequence_array = np.linspace(0, 1, 5) print(sequence_array)
,
rand,
randn and
randint, etc.
random_array = np.random.random((2, 3)) print(random_array)
[[0.59525333 0.78593695 0.30467253] [0.83647996 0.09302248 0.85711096]]
normal_array = np.random.randn(3, 3) print(normal_array)
[[-0.96338454 -0.44881001 0.01016194] [-0.78893991 -0.32811758 0.11091332] [ 0.87585342 0.49660924 -0.52104011]]
random_int_array = np.random.randint(1, 10, (2, 4)) print(random_int_array)
[[3 9 3 3] [1 9 7 5]]
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!