赞
踩
import numpy as np
(1)输出a 的类型(type)
(2)输出a的各维度的大小(shape)
(3)输出 a的第一个元素(值为4)
a = np.array([4, 5, 6])
print(a)
print(type(a))
print(a.shape)
print(a[0])
[4 5 6]
<class ‘numpy.ndarray’>
(3,)
4
(1)输出各维度的大小(shape)
(2)输出 b(0,0),b(0,1),b(1,1) 这三个元素(对应值分别为4,5,2)
b = np.array([[4, 5, 6], [1, 2, 3]])
print(b)
print(b.shape)
print(b[0,0], b[0][1], b[1][1])
[[4 5 6]
[1 2 3]]
(2, 3)
4 5 2
(1)建立一个全0矩阵 a, 大小为 3x3; 类型为整型(提示: dtype = int)
(2)建立一个全1矩阵b,大小为4x5;
(3)建立一个单位矩阵c ,大小为4x4;
(4)生成一个随机数矩阵d,大小为 3x2.
a = np.zeros([3, 3], dtype=int)
b = np.ones([4, 5])
c = np.identity(4, dtype=int)
d = np.random.rand(3, 2)
print(a)
print(b)
print(c)
print(d)
[[0 0 0]
[0 0 0]
[0 0 0]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]]
[[0.07074796 0.30542296]
[0.25368568 0.35339041]
[0.33151254 0.35920238]]
(1)打印a;
(2)输出 下标为(2,3),(0,0) 这两个数组元素的值
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
print(a[2, 3],a[0, 0])
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
12 1
(1),输出b;
(2) 输出b 的(0,0)这个元素的值
b = a[0:2, 2:4] # 二维数组也能做切边范围,类似与一维数组(i:j表示从i行/列到j-1行/列)
print(b)
print(b[0, 0])
[[3 4]
[7 8]]
3
(1)输出 c ;
(2) 输出 c 中第一行的最后一个元素(提示,使用 -1 表示最后一个元素)
c = a[1:3] # 或者 c = a[1:3, :]
print(c)
print(c[0, -1])
[[ 5 6 7 8]
[ 9 10 11 12]]
8
a = np.array([[1, 2], [3, 4], [5, 6]])
print(a[[0, 1, 2], [0, 1, 0]])
[1 4 5]
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
print(a)
b = np.array([0, 2, 0, 1])
print(a[np.arange(4), b])
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[ 1 6 7 11]
(提示: a[np.arange(4), b] += 10 )
b = np.array([0, 2, 0, 1])
a[np.arange(4), b] += 10
print(a)
[[11 2 3]
[ 4 5 16]
[17 8 9]
[10 21 12]]
import numpy as np
x = np.array([1.2])
print(x.shape)
(1,)
import numpy as np
x = np.array([1.0, 2.0])
print(x.shape)
(2,)
import numpy as np
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
y = np.array([[5, 6], [7, 8]], dtype=np.float64)
print(x)
print(y)
print(x+y)
np.add(x, y)
[[1. 2.]
[3. 4.]]
[[5. 6.]
[7. 8.]]
[[ 6. 8.]
[10. 12.]]
array([[ 6., 8.],
[10., 12.]])
print(x-y)
np.subtract(x, y)
[[-4. -4.]
[-4. -4.]]
[[5. 6.]
[7. 8.]]
print(x)
print(y)
print(x*y)
np.multiply(x, y) # 两个矩阵的相同位置的元素相乘(卷积运算:y是卷积核)
np.dot(x, y) # 线性代数中的矩阵乘法运算
print(y)
[[1. 2.]
[3. 4.]]
[[5. 6.]
[7. 8.]]
[[ 5. 12.]
[21. 32.]]
[[5. 6.]
[7. 8.]]
print(y)
print(x/y)
np.divide(x, y)
[[5. 6.]
[7. 8.]]
[[0.2 0.33333333]
[0.42857143 0.5 ]]
[[5. 6.]
[7. 8.]]
array([[0.2 , 0.33333333],
[0.42857143, 0.5 ]])
print(np.sqrt(x))
[[1. 1.41421356]
[1.73205081 2. ]]
print(x)
print(y)
print(x.dot(y))
print(np.dot(x,y))
[[1. 2.]
[3. 4.]]
[[5. 6.]
[7. 8.]]
[[19. 22.]
[43. 50.]]
[[19. 22.]
[43. 50.]]
(1)print(np.sum(x)):
(2)print(np.sum(x,axis =0 ));
(3)print(np.sum(x,axis = 1))
print(x)
print(np.sum(x)) # 数:各个元素相加
print(np.sum(x, axis=0)) # 数组:行求和
print(np.sum(x, axis=1)) # 数组:列求和
[[1. 2.]
[3. 4.]]
10.0
[4. 6.]
[3. 7.]
(1)print(np.mean(x))
(2)print(np.mean(x,axis = 0))
(3) print(np.mean(x,axis =1)))
print(x)
print(np.mean(x)) # 数:各个元素相加之后再求平均
print(np.mean(x, axis=0)) # 一维数组:行求平均
print(np.mean(x, axis=1)) # 一维数组:列求平均
[[1. 2.]
[3. 4.]]
2.5
[2. 3.]
[1.5 3.5]
print(x)
print(x.T)
[[1. 2.]
[3. 4.]]
[[1. 3.]
[2. 4.]]
print(x)
print(np.exp(x))
[[1. 2.]
[3. 4.]]
[[ 2.71828183 7.3890561 ]
[20.08553692 54.59815003]]
print(x)
print(np.argmax(x))
print(np.argmax(x, axis=0))
print(np.argmax(x, axis=1))
[[1. 2.]
[3. 4.]]
3
[1 1]
[1 1]
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100, 0.1)
y = x*x
plt.figure(1) # 表示定位(创建)第一个画板,如果没有参数默认创建一个新的画板
plt.plot(x, y) # x为x轴数据, y为y轴数据
plt.show() # 显示图像
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 3 * np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
# plt.figure(1)
plt.plot(x, y1)
# plt.figure(2)
plt.plot(x, y2)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 3 * np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
plt.figure(1)
plt.plot(x, y1)
plt.figure(2)
plt.plot(x, y2)
plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。