NumPy 数组的基础知识
NumPy代表数字Python。它是一个用于处理数组的Python库。在Python中,我们将列表用于数组,但处理起来很慢。 NumPy 数组是一个强大的 N 维数组对象,它用于线性代数、傅里叶变换和随机数功能。它提供的数组对象比传统的Python列表快得多。
数组类型:
- 一维数组
- 多维数组
一维数组:
一维数组是一种线性数组。
例子:
Python3
# importing numpy module
import numpy as np
# creating list
list = [1, 2, 3, 4]
# creating numpy array
sample_array = np.array(list1)
print("List in python : ", list)
print("Numpy Array in python :",
sample_array)
Python3
print(type(list_1))
print(type(sample_array))
Python3
# importing numpy module
import numpy as np
# creating list
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
# creating numpy array
sample_array = np.array([list_1,
list_2,
list_3])
print("Numpy multi dimensional array in python\n",
sample_array)
Python3
# importing numpy module
import numpy as np
# creating list
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
# creating numpy array
sample_array = np.array([list_1,
list_2,
list_3])
print("Numpy array :")
print(sample_array)
# print shape of the array
print("Shape of the array :",
sample_array.shape)
Python3
import numpy as np
sample_array = np.array([[0, 4, 2],
[3, 4, 5],
[23, 4, 5],
[2, 34, 5],
[5, 6, 7]])
print("shape of the array :",
sample_array.shape)
Python3
# Import module
import numpy as np
# Creating the array
sample_array_1 = np.array([[0, 4, 2]])
sample_array_2 = np.array([0.2, 0.4, 2.4])
# display data type
print("Data type of the array 1 :",
sample_array_1.dtype)
print("Data type of array 2 :",
sample_array_2.dtype)
Python3
# import module
import numpy as np
#creating a array
arr = np.array([3,4,5,5])
print("Array :",arr)
Python3
#Import numpy module
import numpy as np
# iterable
iterable = (a*a for a in range(8))
arr = np.fromiter(iterable, float)
print("fromiter() array :",arr)
Python3
import numpy as np
var = "Geekforgeeks"
arr = np.fromiter(var, dtype = 'U2')
print("fromiter() array :",
arr)
Python3
import numpy as np
np.arange(1, 20 , 2,
dtype = np.float32)
Python3
import numpy as np
np.linspace(3.5, 10, 3)
Python3
import numpy as np
np.linspace(3.5, 10, 3,
dtype = np.int32)
Python3
import numpy as np
np.empty([4, 3],
dtype = np.int32,
order = 'f')
Python3
import numpy as np
np.ones([4, 3],
dtype = np.int32,
order = 'f')
Python3
import numpy as np
np.zeros([4, 3],
dtype = np.int32,
order = 'f')
输出:
List in python : [1, 2, 3, 4]
Numpy Array in python : [1 2 3 4]
检查列表和数组的数据类型:
Python3
print(type(list_1))
print(type(sample_array))
输出:
多维数组:
多维数组中的数据以表格形式存储。
例子:
Python3
# importing numpy module
import numpy as np
# creating list
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
# creating numpy array
sample_array = np.array([list_1,
list_2,
list_3])
print("Numpy multi dimensional array in python\n",
sample_array)
输出:
Numpy multi dimensional array in python
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
注意:在 numpy.array() 中使用[ ]运算符用于多维
数组剖析:
1.轴:数组的轴描述了索引到数组中的顺序。
Axis 0 = one dimensional
Axis 1 = Two dimensional
Axis 2 = Three dimensional
2. 形状:沿每个轴的元素数量。它来自一个元组。
例子:
Python3
# importing numpy module
import numpy as np
# creating list
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
# creating numpy array
sample_array = np.array([list_1,
list_2,
list_3])
print("Numpy array :")
print(sample_array)
# print shape of the array
print("Shape of the array :",
sample_array.shape)
输出:
Numpy array :
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Shape of the array : (3, 4)
例子:
Python3
import numpy as np
sample_array = np.array([[0, 4, 2],
[3, 4, 5],
[23, 4, 5],
[2, 34, 5],
[5, 6, 7]])
print("shape of the array :",
sample_array.shape)
输出:
shape of the array : (5, 3)
3. 秩:数组的秩就是它拥有的轴(或维度)的数量。
一维数组的秩为 1。
二维数组的秩为 2。
4.数据类型对象(dtype):数据类型对象(dtype)是numpy.dtype类的一个实例。它描述了应该如何解释对应于数组项的固定大小内存块中的字节。
例子:
Python3
# Import module
import numpy as np
# Creating the array
sample_array_1 = np.array([[0, 4, 2]])
sample_array_2 = np.array([0.2, 0.4, 2.4])
# display data type
print("Data type of the array 1 :",
sample_array_1.dtype)
print("Data type of array 2 :",
sample_array_2.dtype)
输出:
Data type of the array 1 : int32
Data type of array 2 : float64
创建 Numpy Array 的一些不同方法:
1. numpy.array() : Numpy中的Numpy数组对象称为ndarray。我们可以使用numpy.array()函数创建 ndarray。
Syntax: numpy.array(parameter)
例子:
Python3
# import module
import numpy as np
#creating a array
arr = np.array([3,4,5,5])
print("Array :",arr)
输出:
Array : [3 4 5 5]
2. numpy.fromiter() : fromiter()函数从可迭代对象创建一个新的一维数组。
Syntax: numpy.fromiter(iterable, dtype, count=-1)
示例 1:
Python3
#Import numpy module
import numpy as np
# iterable
iterable = (a*a for a in range(8))
arr = np.fromiter(iterable, float)
print("fromiter() array :",arr)
输出:
fromiter() array : [ 0. 1. 4. 9. 16. 25. 36. 49.]
示例 2:
Python3
import numpy as np
var = "Geekforgeeks"
arr = np.fromiter(var, dtype = 'U2')
print("fromiter() array :",
arr)
输出:
fromiter() array : [‘G’ ‘e’ ‘e’ ‘k’ ‘f’ ‘o’ ‘r’ ‘g’ ‘e’ ‘e’ ‘k’ ‘s’]
3. numpy.arange() :这是一个内置的 NumPy函数,它在给定的间隔内返回均匀间隔的值。
Syntax: numpy.arange([start, ]stop, [step, ]dtype=None)
例子:
Python3
import numpy as np
np.arange(1, 20 , 2,
dtype = np.float32)
输出:
array([ 1., 3., 5., 7., 9., 11., 13., 15., 17., 19.], dtype=float32)
4. numpy.linspace() :此函数返回两个限制之间指定的均匀间隔的数字。
Syntax: numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
示例 1:
Python3
import numpy as np
np.linspace(3.5, 10, 3)
输出:
array([ 3.5 , 6.75, 10. ])
示例 2:
Python3
import numpy as np
np.linspace(3.5, 10, 3,
dtype = np.int32)
输出:
array([ 3, 6, 10])
5. numpy.empty() :此函数创建一个给定形状和类型的新数组,无需初始化值。
Syntax: numpy.empty(shape, dtype=float, order=’C’)
例子:
Python3
import numpy as np
np.empty([4, 3],
dtype = np.int32,
order = 'f')
输出:
array([[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11],
[ 4, 8, 12]])
6. numpy.ones():该函数用于获取给定形状和类型的新数组,用ones(1)填充。
Syntax: numpy.ones(shape, dtype=None, order=’C’)
例子:
Python3
import numpy as np
np.ones([4, 3],
dtype = np.int32,
order = 'f')
输出:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
7. numpy.zeros() :此函数用于获取给定形状和类型的新数组,用 zeros(0) 填充。
Syntax: numpy.ones(shape, dtype=None)
例子:
Python3
import numpy as np
np.zeros([4, 3],
dtype = np.int32,
order = 'f')
输出:
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])