import numpy as np
"""
数组对象
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
"""
arr = np.array([1, 2, 3, 4], dtype=np.uint8)
arr1 = np.array(arr)
arr2 = np.array([1, 2, 3, 4, 5, 6], ndmin=2)
arr3 = np.array([[1, 2, 3],
[4, 5, 6]])
import numpy as np
"""
数组的属性
"""
arr = np.arange(10)
print(arr.ndim)
arr1 = arr.reshape(5, 2)
print(arr1.ndim)
print(arr.shape)
print(arr1.shape)
print(arr.size)
print(arr.dtype)
import numpy as np
"""
numpy.empty(shape, dtype = float, order = 'C')
"""
arr = np.empty([3, 4], dtype=int)
print(arr)
"""
numpy.zeros(shape, dtype = float, order = 'C')
"""
arr1 = np.zeros([2, 3], dtype=np.uint8)
print(arr1)
"""
numpy.ones(shape, dtype = None, order = 'C')
"""
arr2 = np.ones([3, 3], dtype=float)
print(arr2)
"""
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None)
numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None)
"""
x = np.zeros_like(arr)
y = np.ones_like(arr)
print(x)
print(y)
"""
numpy.asarray(a, dtype = None, order = None)
注:a可以是任意形式的输入参数,比如列表, 列表的元组, 元组, 元组的元组, 元组的列表,多维数组
"""
list1 = [[1, 2, 3], [4, 5, 6]]
arr_list = np.asarray(list1)
print(arr_list)
tuple1 = (1, 2, 3, 4, 5, 6)
arr_tuple = np.asarray(tuple1)
print(arr_tuple)
"""
numpy.fromiter(iterable, dtype, count=-1)
iterable:可迭代对象
"""
list2 = [1, 2, 3]
arr3 = np.fromiter(list2, dtype=float)
print(arr3)
"""
numpy.arange(start, stop, step, dtype)
用法基本等同于range函数
dtype:如果没有规定数据类型,则使用数据的数据的类型
"""
arr4 = np.arange(10.0)
print(arr4)
arr4 = np.arange(1, 6)
print(arr4)
arr4 = np.arange(3, 12, 3)
print(arr4)
"""
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
num:需要生成的等步长的样本数量,默认值为50
当endpoint的值为True时,范围包含终止值,为False时不包含
retstep为True时显示间距
"""
arr5 = np.linspace(1, 150, 100)
print(arr5)
import numpy as np
arr = np.linspace(1, 100)
print(arr)
a1 = arr[1]
print(a1)
a2 = arr[:5]
print(a2)
a3 = arr[45:]
print(a3)
a4 = arr[5:45:5]
print(a4)
a5 = arr[slice(5, 45, 5)]
print(a5)
arr1 = arr.reshape(10, 5)
print("arr1:\n", arr1)
print("arr1[1]:\n", arr1[1])
print("arr1[:2]:\n", arr1[:2])
print("arr1[...,2]:\n", arr1[..., 2])
import numpy as np
"""
整数数组索引
"""
arr = np.arange(9).reshape(3, 3)
print(arr)
print(arr[[2, 1, 2], [1, 0, 2]])
r, c = arr.shape
print("四个角的元素为:\n", arr[[[0, 0], [c-1, c-1]], [[0, r-1], [0, r-1]]])
print("切片和数组索引混合使用:\n", arr[:2, [1, 2]])
"""
布尔索引
"""
print(arr > 5)
print(arr[arr > 5])
def func(x):
return x % 2 == 1
print(arr[func(arr)])
"""
np.ix_:输入
"""
print("使用np.ix_:\n", arr[np.ix_([1, 0, 2], [2, 1, 0])])