import
numpy
as
np
In?[9]:
#1 創(chuàng)建一個(gè)長(zhǎng)度為10的一維全為0的ndarray對(duì)象,然后讓第5個(gè)元素等于1
n
=
np
.
zeros
(
10
)
n
[
4
]
=
1
print
(
n
)
?
In?[10]:
#2 創(chuàng)建一個(gè)元素為從10到49的ndarray對(duì)象
np
.
arange
(
10
,
50
)
Out[10]:
In?[11]:
#3 將第2題的所有元素位置反轉(zhuǎn)
n3
=
np
.
arange
(
10
,
50
)
n3
[::
-
1
]
Out[11]:
In?[29]:
#4 使用np.random.random創(chuàng)建一個(gè)10*10的ndarray對(duì)象,并打印出最大最小元素
n4
=
np
.
random
.
random
((
10
,
10
))
print
(
n4
)
print
(
np
.
max
(
n4
),
np
.
min
(
n4
))
?
In?[41]:
#5 創(chuàng)建一個(gè)10*10的ndarray對(duì)象,且矩陣邊界全為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 創(chuàng)建一個(gè)每一行都是從0到4的5*5矩陣
np
.
ones
((
5
,
5
))
*
np
.
arange
(
5
)
Out[61]:
In?[72]:
#7 創(chuàng)建一個(gè)范圍在(0,1)之間的長(zhǎng)度為12的等差數(shù)列
np
.
linspace
(
0
,
1
,
12
)
Out[72]:
In?[15]:
#8 創(chuàng)建一個(gè)長(zhǎng)度為10的隨機(jī)數(shù)組并排序
n8
=
np
.
random
.
randint
(
1
,
10
,
10
)
n8
.
sort
()
print
(
n8
)
?
In?[14]:
#9 創(chuàng)建一個(gè)長(zhǎng)度為10的隨機(jī)數(shù)組并將最大值替換為0
n9
=
np
.
random
.
randint
(
0
,
150
,
10
)
index
=
n9
.
argmax
()
n9
[
index
]
=
0
n9
Out[14]:
In?[32]:
#10 如何根據(jù)第3列來(lái)對(duì)一個(gè)5*5矩陣排序?
n10
=
np
.
random
.
randint
(
0
,
20
,(
5
,
5
))
n10_sort
=
np
.
argsort
(
n10
[:,
3
])
#對(duì)第三列排序并返回索引值
n10
[
n10_sort
]
Out[32]:
In?[80]:
#11 給定一個(gè)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 給定數(shù)組[1, 2, 3, 4, 5],如何得到在這個(gè)數(shù)組的每個(gè)元素之間插入3個(gè)0后的新數(shù)組?
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 給定一個(gè)二維矩陣,如何交換其中兩行的元素?
n13
=
np
.
random
.
randint
(
0
,
10
,(
2
,
2
))
n13
Out[132]:
In?[133]:
n13
[[
1
,
0
]]
Out[133]:
In?[142]:
#14 創(chuàng)建一個(gè)100000長(zhǎng)度的隨機(jī)數(shù)組,使用三種方法對(duì)其求三次方,并比較所用時(shí)間
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 創(chuàng)建一個(gè)5*3隨機(jī)矩陣和一個(gè)3*2隨機(jī)矩陣,求矩陣積
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 打印出以下函數(shù)(要求使用np.zeros創(chuàng)建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 正則化一個(gè)5*5隨機(jī)矩陣
# 正則的概念:假設(shè)a是矩陣中的一個(gè)元素,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 將一個(gè)一維數(shù)組轉(zhuǎn)化為二進(jìn)制表示矩陣。例如
# [1,2,3]
# 轉(zhuǎn)化為
# [[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]:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
