Learn and apply the main functions in the numpy function library

WBOY
Release: 2024-01-03 09:20:21
Original
1130 people have browsed it

Learn and apply the main functions in the numpy function library

Master the key functions and their applications in the numpy function library

In the fields of data science and machine learning, numpy is a very important Python library that provides high-level Performance of multidimensional array objects and various mathematical functions. This article will introduce some key functions in numpy and provide specific code examples to help readers better understand and use these functions.

  1. numpy array creation and initialization

numpy provides a variety of methods to create and initialize arrays. Among them, the most basic one is to use the numpy.array() function:

import numpy as np

# 创建一维数组
arr1d = np.array([1, 2, 3, 4, 5])
print(arr1d)

# 创建二维数组
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2d)

# 创建全零数组
zeros = np.zeros((3, 3))
print(zeros)

# 创建全一数组
ones = np.ones((2, 2))
print(ones)

# 创建指定范围的数组
range_arr = np.arange(1, 10)
print(range_arr)
Copy after login
  1. Array operation

numpy provides many functions for operating on arrays, including calculating array elements sum, mean, standard deviation, etc. The following are examples of some commonly used array operation functions:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

# 计算数组元素的和
print(np.sum(arr))

# 计算数组元素的平均值
print(np.mean(arr))

# 计算数组元素的标准差
print(np.std(arr))

# 沿指定轴计算数组元素的和
print(np.sum(arr, axis=0))  # 沿着列的方向求和
print(np.sum(arr, axis=1))  # 沿着行的方向求和

# 数组的合并和分割
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])

# 合并数组
concat_arr = np.concatenate((arr1, arr2))
print(concat_arr)

# 按指定轴分割数组
split_arr = np.split(concat_arr, 2, axis=1)
print(split_arr)
Copy after login
  1. Array indexing and slicing

Using numpy, you can easily index and slice arrays. The following are Some commonly used examples:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

# 获取指定位置的元素
print(arr[2])  # 输出:3

# 切片操作
print(arr[1:4] ) # 输出:[2, 3, 4]

# 多维数组的索引和切片
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# 获取指定位置的元素
print(arr[0, 1])  # 输出:2

# 切片操作
print(arr[:2, 1:])  # 输出:[[2,3], [5,6]]
Copy after login
  1. Array shape and reshaping

numpy provides a series of functions and methods for manipulating array shapes, such as changing the dimensions of the array, reshaping Array etc. Examples are as follows:

import numpy as np

# 改变数组形状
arr = np.array([[1, 2, 3], [4, 5, 6]])
reshaped_arr = np.reshape(arr, (3, 2))
print(reshaped_arr)

# 获取数组的形状
print(arr.shape)  # 输出:(2, 3)

# 将多维数组展平为一维数组
flatten_arr = arr.flatten()
print(flatten_arr)
Copy after login
  1. Operations on array elements

numpy provides a series of functions for performing mathematical operations on array elements, such as calculating squares, rooting, and taking logarithms wait. The following are some examples:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

# 计算数组元素的平方
print(np.power(arr, 2))

# 计算数组元素的开方
print(np.sqrt(arr))

# 计算数组元素的对数
print(np.log(arr))
Copy after login

Summary:

This article introduces some key functions and their applications in the numpy function library, and provides specific code examples. These functions include array creation and initialization, array operations, array indexing and slicing, array shape and reshaping, and array element operations. By mastering these functions, readers will be able to better use numpy for data processing and analysis and improve work efficiency.

The above is the detailed content of Learn and apply the main functions in the numpy function library. 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