import
numpy
as
np
In [9]:
#1 创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1
n
=
np
.
zeros
(
10
)
n
[
4
]
=
1
print
(
n
)
In [10]:
#2 创建一个元素为从10到49的ndarray对象
np
.
arange
(
10
,
50
)
Out[10]:
In [11]:
#3 将第2题的所有元素位置反转
n3
=
np
.
arange
(
10
,
50
)
n3
[::
-
1
]
Out[11]:
In [29]:
#4 使用np.random.random创建一个10*10的ndarray对象,并打印出最大最小元素
n4
=
np
.
random
.
random
((
10
,
10
))
print
(
n4
)
print
(
np
.
max
(
n4
),
np
.
min
(
n4
))
In [41]:
#5 创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0
# n5 = np.zeros((10,10))
# for i in n5:
# i[0],i[9] = 1,1
# n5[0] = 1
# n5[9] = 1
# print(n5)
n5
=
np
.
ones
((
10
,
10
))
n5
[
1
:
-
1
,
1
:
-
1
]
=
0
n5
Out[41]:
In [61]:
#6 创建一个每一行都是从0到4的5*5矩阵
np
.
ones
((
5
,
5
))
*
np
.
arange
(
5
)
Out[61]:
In [72]:
#7 创建一个范围在(0,1)之间的长度为12的等差数列
np
.
linspace
(
0
,
1
,
12
)
Out[72]:
In [15]:
#8 创建一个长度为10的随机数组并排序
n8
=
np
.
random
.
randint
(
1
,
10
,
10
)
n8
.
sort
()
print
(
n8
)
In [14]:
#9 创建一个长度为10的随机数组并将最大值替换为0
n9
=
np
.
random
.
randint
(
0
,
150
,
10
)
index
=
n9
.
argmax
()
n9
[
index
]
=
0
n9
Out[14]:
In [32]:
#10 如何根据第3列来对一个5*5矩阵排序?
n10
=
np
.
random
.
randint
(
0
,
20
,(
5
,
5
))
n10_sort
=
np
.
argsort
(
n10
[:,
3
])
#对第三列排序并返回索引值
n10
[
n10_sort
]
Out[32]:
In [80]:
#11 给定一个4维矩阵,如何得到最后两维的和?
n11
=
np
.
random
.
randint
(
0
,
100
,(
2
,
2
,
2
,
2
))
n11
Out[80]:
In [90]:
n11
.
sum
(
axis
=
(
2
,
3
))
Out[90]:
In [110]:
#12 给定数组[1, 2, 3, 4, 5],如何得到在这个数组的每个元素之间插入3个0后的新数组?
n12
=
np
.
arange
(
1
,
6
)
n12_1
=
np
.
zeros
(
17
,
dtype
=
int
)
n12_1
[::
4
]
=
n12
n12_1
Out[110]:
In [113]:
n12
=
np
.
arange
(
1
,
6
)
.
reshape
(
5
,
1
)
n12_2
=
np
.
zeros
((
5
,
3
))
np
.
concatenate
([
n12
,
n12_2
],
axis
=
1
)
Out[113]:
In [132]:
#13 给定一个二维矩阵,如何交换其中两行的元素?
n13
=
np
.
random
.
randint
(
0
,
10
,(
2
,
2
))
n13
Out[132]:
In [133]:
n13
[[
1
,
0
]]
Out[133]:
In [142]:
#14 创建一个100000长度的随机数组,使用三种方法对其求三次方,并比较所用时间
n14
=
np
.
random
.
randint
(
0
,
1000000
,
100000
)
n14
Out[142]:
In [143]:
%
timeit n14**3
In [144]:
%
timeit np.power(n14,3)
In [153]:
n14_1
=
np
.
dot
(
n14
,
n14
)
%
timeit np.dot(n14_1,n14)
In [146]:
#15 创建一个5*3随机矩阵和一个3*2随机矩阵,求矩阵积
n15_1
=
np
.
random
.
randint
(
0
,
100
,(
5
,
3
))
n15_2
=
np
.
random
.
randint
(
0
,
100
,(
3
,
2
))
display
(
n15_1
,
n15_2
)
In [149]:
np
.
dot
(
n15_1
,
n15_2
)
Out[149]:
In [155]:
#16 矩阵的每一行的元素都减去该行的平均值
n16
=
np
.
random
.
randint
(
0
,
100
,(
2
,
2
))
n16
Out[155]:
In [166]:
n16_mean
=
np
.
mean
(
n16
,
axis
=
1
)
.
reshape
(
2
,
1
)
n16_mean
Out[166]:
In [167]:
n16
-
n16_mean
Out[167]:
In [211]:
#17 打印出以下函数(要求使用np.zeros创建8*8的矩阵):
# [[0 1 0 1 0 1 0 1]
# [1 0 1 0 1 0 1 0]
# [0 1 0 1 0 1 0 1]
# [1 0 1 0 1 0 1 0]
# [0 1 0 1 0 1 0 1]
# [1 0 1 0 1 0 1 0]
# [0 1 0 1 0 1 0 1]
# [1 0 1 0 1 0 1 0]]
n17
=
np
.
zeros
((
8
,
8
),
dtype
=
int
)
n17
[::
2
,
1
::
2
]
=
1
n17
[
1
::
2
,::
2
]
=
1
n17
Out[211]:
In [219]:
#18 正则化一个5*5随机矩阵
# 正则的概念:假设a是矩阵中的一个元素,max/min分别是矩阵元素的最大最小值,
# 则正则化后a = (a - min)/(max - min)
n18
=
np
.
random
.
randint
(
0
,
100
,(
5
,
5
))
n18_max
,
n18_min
=
n18
.
max
(),
n18
.
min
()
n18_re
=
(
n18
-
n18_min
)
/
(
n18_max
-
n18_min
)
n18_re
Out[219]:
In [241]:
# 19 将一个一维数组转化为二进制表示矩阵。例如
# [1,2,3]
# 转化为
# [[0,0,1],
# [0,1,0],
# [0,1,1]]
display
(
1
and
0
,
1
or
1
,
1
&
2
)
In [237]:
I
=
np
.
array
([
1
,
2
,
3
])
A
=
I
.
reshape
(
-
1
,
1
)
A
Out[237]:
In [230]:
B
=
2
**
np
.
arange
(
3
)
B
Out[230]:
In [231]:
M
=
A
&
B
M
Out[231]:
In [232]:
M
!=
0
Out[232]:
In [235]:
M
[
M
!=
0
]
=
1
M
Out[235]:
In [236]:
M
[:,::
-
1
]
Out[236]: