注:题目来源于网络,如有侵权,请联系删除,谢谢!
(1)创建一个1到10的数组,然后逆序输出。
import numpy as np
a = np.arange(1, 11)
a[::-1]
array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
(2)创建一个长度为20的全1数组,然后变成一个4×5的二维矩阵并转置。
import numpy as np
a = np.ones(20, dtype = int)
a.shape = (4, 5)
a.T
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
(3)创建一个3x3x3的随机数组。
(提示: np.random.random)
import numpy as np
a = np.random.random((3, 3, 3))
a
array([[[0.43877706, 0.242368 , 0.14242108],
[0.51700049, 0.85408251, 0.29600834],
[0.14520178, 0.51788282, 0.87235969]],
[[0.16778399, 0.10128641, 0.92149835],
[0.60309495, 0.35521549, 0.20291252],
[0.36673908, 0.28250977, 0.02232971]],
[[0.55730836, 0.80135733, 0.61995728],
[0.96536056, 0.10491618, 0.49470302],
[0.19893524, 0.94691896, 0.44063629]]])
(4)从1到10中随机选取10个数,构成一个长度为10的数组,并将其排序。获取其最大值最小值,求和,求方差。
import numpy as np
a = np.random.randint(1, 10, 10)
print(a)
print(np.sort(a))
print(np.max(a))
print(np.min(a))
print(np.sum(a))
print(np.var(a))
[2 2 7 5 6 4 4 4 8 6]
[2 2 4 4 4 5 6 6 7 8]
8
2
48
3.5599999999999996
(5)从1到10中随机选取10个数,构成一个长度为10的数组,选出其中的奇数。
import numpy as np
a = np.random.randint(1, 10, 10)
a[a%2==1]
array([7, 9, 9, 9, 5, 9])
(6)生成0到100,差为5的一个等差数列,然后将数据类型转化为整数。
a = np.arange(0, 101, 5)
a.astype(int)
array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60,
65, 70, 75, 80, 85, 90, 95, 100])
(7)从1到10中随机选取10个数,大于3和小于8的取负数。
import numpy as np
a = np.random.randint(1, 10, 10)
a[(a>3) & (a < 8)] *= -1
a
array([-7, -6, -4, 3, -5, -5, -5, 8, -6, -4])
(8)在数组[1, 2, 3, 4, 5]中相邻两个数字中间插入1个0。
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.zeros(9, dtype = int)
b[::2] = a
b
array([1, 0, 2, 0, 3, 0, 4, 0, 5])
(9)新建一个5乘5的随机二位数组,交换其中两行?比如交换第一二行。
a = np.random.randint(1, 10, (5, 5))
print(a)
a[[1, 0, 2, 3, 4]]
[[8 4 5 4 8]
[6 7 8 1 8]
[6 5 6 6 4]
[5 2 1 4 8]
[4 1 7 4 6]]
array([[6, 7, 8, 1, 8],
[8, 4, 5, 4, 8],
[6, 5, 6, 6, 4],
[5, 2, 1, 4, 8],
[4, 1, 7, 4, 6]])
(10)把一个10*2的随机生成的笛卡尔坐标转换成极坐标。
a = np.random.rand(10 ,2)
x = a[:, 0]
y = a[:, 1]
r = np.sqrt(x ** 2, y ** 2)
o = np.arctan2(y, x)
print(r)
print(o)
print(a)
np.array([r, o]).T
[0.84291022 0.42672061 0.65944322 0.70151503 0.04310632 0.10753246
0.03330536 0.96755072 0.42486924 0.66509878]
[0.44489904 0.63939468 0.85875199 0.64009333 1.46518518 1.38930248
1.20680477 0.23067667 1.13275139 0.49641033]
[[0.84291022 0.4018821 ]
[0.42672061 0.31731089]
[0.65944322 0.76404955]
[0.70151503 0.52241046]
[0.04310632 0.40664211]
[0.10753246 0.5859656 ]
[0.03330536 0.08742325]
[0.96755072 0.2272363 ]
[0.42486924 0.90707592]
[0.66509878 0.36025115]
array([[0.84291022, 0.44489904],
[0.42672061, 0.63939468],
[0.65944322, 0.85875199],
[0.70151503, 0.64009333],
[0.04310632, 1.46518518],
[0.10753246, 1.38930248],
[0.03330536, 1.20680477],
[0.96755072, 0.23067667],
[0.42486924, 1.13275139],
[0.66509878, 0.49641033]])
(11)创建一个长度为10并且除了第五个值为1其余的值为2的向量。
a = np.ones(10, dtype = int) * 2
a[4] = 1
a
array([2, 2, 2, 2, 1, 2, 2, 2, 2, 2])
(12)创建一个长度为10的随机向量,并求其累计和。
a = np.random.randint(1, 10, 10)
a.cumsum()
66
(13)将数组中的所有奇数替换成-1。
a = np.random.randint(1, 10, 10)
a[a%2==1] *= -1
a
array([-9, -9, -9, 6, 4, 4, -3, -1, -1, 2])
(14)构造两个4乘3的二维数组,按照3种方法进行连接?
a = np.array([[0, 1, 2],
[4, 5, 6],
[8, 9 ,10],
[12, 13, 14]])
b = a * 2
np.hstack((a,b))
np.vstack((a, b))
np.dstack((a, b))
array([[[ 0, 0],
[ 1, 2],
[ 2, 4]],
[[ 4, 8],
[ 5, 10],
[ 6, 12]],
[[ 8, 16],
[ 9, 18],
[10, 20]],
[[12, 24],
[13, 26],
[14, 28]]])
(15)获取数组 a 和 b 中的共同项(索引位置相同,值也相同)。
a = np.array([1,2,3,2,3,4,3,4,5,6]),b = np.array([7,2,10,2,7,4,9,4,9,8])
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
a[a == b]
array([2, 2, 4, 4])
(16)从数组 a 中提取 5 和 10 之间的所有项。a=np.array([7,2,10,2,7,4,9,4,9,8])
a=np.array([7,2,10,2,7,4,9,4,9,8])
a[(a >5) & (a < 10)]
array([7, 7, 9, 9, 8])