import numpy as np
a=np.array([1,2,3,4])
a
array([1, 2, 3, 4])
print(a)
[1 2 3 4]
b = np.array((2,4,6,8))
print(b)
[2 4 6 8]
c=np.array([[1,2,3,4],[5,6,7,8]])
print(c)
[[1 2 3 4]
[5 6 7 8]]
d=np.array([1,2,3,4],dtype='float64')
print(d)
[1. 2. 3. 4.]
print(d.dtype)
float64
e=np.arange(10)
print(e)
[0 1 2 3 4 5 6 7 8 9]
f=np.arange(0,1,0.2)
print(f)
[0. 0.2 0.4 0.6 0.8]
# 等差数列
g=np.linspace(0,1,5)
print(g)
[0. 0.25 0.5 0.75 1. ]
g=np.linspace(0,1,10)
print(g)
[0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
0.66666667 0.77777778 0.88888889 1. ]
# 等比(用得比较少)
h=np.logspace(0,1,5) # 以10为底,10的0次方为1
print(h)
[ 1. 1.77827941 3.16227766 5.62341325 10. ]
# 产生全0一维数组
arr1=np.zeros(4)
print(arr1)
print(arr1.dtype)
[0. 0. 0. 0.]
float64
# 产生全0二维数组
arr2=np.zeros([4,3],dtype='int32')
print(arr2)
[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]
arr3 = np.ones(4)
print(arr3)
[1. 1. 1. 1.]
arr4 = np.ones([4,3])
print(arr4)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
# 对角阵(用得较少)
arr5=np.diag([1,2,3,4])
print(arr5)
[[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
# 创建对角为1其余为0的数组
arr6=np.eye(5) # 5行5列
print(arr6)
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
arr7=np.array([[1,2,3],[4,5,6]])
print(arr7)
print('ndim:',arr7.ndim)
[[1 2 3]
[4 5 6]]
ndim: 2
print('size:',arr7.size)
size: 6
print(arr7.dtype)
int32
arr8=np.array([[1,2,3],[4,5,6]])
print(arr8)
[[1 2 3]
[4 5 6]]
# 设置数组的shape属性
arr8.shape=(3,2)
print(arr8)
[[1 2]
[3 4]
[5 6]]
# 修改数据类型
arr9=np.arange(8)
print(arr9.dtype)
arr10 = arr9.astype(np.float64)
print(arr10.dtype)
int32
float64
arr1=np.random.randint(100,200,10)
print(arr1)
[192 185 187 156 162 174 143 106 150 140]
# randint 产生随机整数
arr2 = np.random.randint(100,200,size=(3,3))
print(arr2)
[[160 142 167]
[127 134 163]
[125 126 150]]
arr3=np.random.rand(5)
print(arr3)
[0.25881521 0.91028959 0.35347205 0.27666183 0.37972008]
# 产生3行3列0到1的随机数
arr4=np.random.rand(3,3)
print(arr4)
[[0.96559617 0.03875879 0.46359674]
[0.106237 0.96643687 0.78471041]
[0.81012802 0.89996791 0.51034123]]
# 确定随机生成器的种子
np.random.seed(0)
arr5=np.random.rand(3,3)
print(arr5)
[[0.5488135 0.71518937 0.60276338]
[0.54488318 0.4236548 0.64589411]
[0.43758721 0.891773 0.96366276]]
# 确定随机生成器的种子
np.random.seed(10)
arr5=np.random.rand(3,3)
print(arr5)
[[0.77132064 0.02075195 0.63364823]
[0.74880388 0.49850701 0.22479665]
[0.19806286 0.76053071 0.16911084]]
# 用于生成一个0到1的随机浮点数0到1
np.random.random(5)
array([0.38344152, 0.79172504, 0.52889492, 0.56804456, 0.92559664])
# 返回一个随机的浮点数
np.random.uniform(1,5)
1.2841442327915478
# 从序列中随机选取一个整数,返回的是一个字符,而不是列表
names=['Mike','Tom','Peter','Jimay']
np.random.choice(names)
‘Mike’
# random.shuffle将一个列表(只针对列表)中的数据随机打乱
names=['Mike','Tom','Peter','Jimay']
print(names)
np.random.shuffle(names)
print(names)
[Mike’, ‘Tom’, ‘Peter’, ‘Jimay’]
[‘Peter’, ‘Jimay’, ‘Mike’, ‘Tom’]
# random.sample()用于无重复的随机抽样
from random import sample
number = [1,2,3,4,5,6,7,8,9,10]
# 从中随机选择6个数据
sample(number,6)
[10, 2, 1, 5, 3, 7]
# import random
from random import sample
list1=[1,2,3,4,5]
print(sample(list1,3))
[4, 3, 2]
import numpy as np
arr1=np.arange(8)
print(arr1)
[0 1 2 3 4 5 6 7]
arr2=arr1.reshape(2,4)
print(arr2)
[[0 1 2 3]
[4 5 6 7]]
arr3=arr1.reshape(4,-1)
print(arr3)
[[0 1]
[2 3]
[4 5]
[6 7]]
# 数组的重塑 一维数组变二维数组
arr4=np.arange(12).reshape(3,4)
print(arr4)
[ 0 1 2 3 4 5 6 7 8 9 10 11]
# 数组的重塑 二维数组变一维数组
arr5=arr4.ravel()
print(arr5)
[ 0 1 2 3 4 5 6 7 8 9 10 11]
# 数组的合并
arr6=np.arange(6).reshape(3,2)
print(arr6)
[[0 1]
[2 3]
[4 5]]
arr7=arr6*2
print(arr7)
[[ 0 2]
[ 4 6]
[ 8 10]]
# 横向合并
arr8=np.hstack((arr6,arr7))
print(arr8)
[[ 0 1 0 2]
[ 2 3 4 6]
[ 4 5 8 10]]
# 纵向合并
arr9=np.vstack((arr6,arr7))
print(arr9)
[[ 0 1]
[ 2 3]
[ 4 5]
[ 0 2]
[ 4 6]
[ 8 10]]
arr10=np.concatenate((arr6,arr7),axis=1)# 横向合并
print(arr10)
[[ 0 1 0 2]
[ 2 3 4 6]
[ 4 5 8 10]]
arr11=np.concatenate((arr6,arr7),axis=0) # 纵向合并
print(arr11)
[[ 0 1]
[ 2 3]
[ 4 5]
[ 0 2]
[ 4 6]
[ 8 10]]
arr1=np.arange(36).reshape(6,6)
print(arr1)
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]
[24 25 26 27 28 29]
[30 31 32 33 34 35]]
# 横向分割
np.hsplit(arr1,3)
[array([[ 0, 1],
[ 6, 7],
[12, 13],
[18, 19],
[24, 25],
[30, 31]]),
array([[ 2, 3],
[ 8, 9],
[14, 15],
[20, 21],
[26, 27],
[32, 33]]),
array([[ 4, 5],
[10, 11],
[16, 17],
[22, 23],
[28, 29],
[34, 35]])]
# 纵向分割
np.vsplit(arr1,3)
[array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]]),
array([[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]),
array([[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])]
np.split(arr1,3,axis=1)
[array([[ 0, 1],
[ 6, 7],
[12, 13],
[18, 19],
[24, 25],
[30, 31]]),
array([[ 2, 3],
[ 8, 9],
[14, 15],
[20, 21],
[26, 27],
[32, 33]]),
array([[ 4, 5],
[10, 11],
[16, 17],
[22, 23],
[28, 29],
[34, 35]])]
# 纵向分割
np.split(arr1,3,axis=0)
[array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]]),
array([[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]),
array([[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])]
# 数组的转置
arr1=np.arange(6).reshape(3,2)
print(arr1)
[[0 1]
[2 3]
[4 5]]
# 数组的转置
arr1.transpose((1,0)) # 轴编号
array([[0, 2, 4],
[1, 3, 5]])
arr1.T
array([[0, 2, 4],
[1, 3, 5]])
# 轴对换
arr1.swapaxes(0,1)
array([[0, 2, 4],
[1, 3, 5]])