numpy学习笔记-仅供学习使用
import numpy
as np
ar
= np
.arange
(20)
print(ar
)
print(ar
[4])
print(ar
[3:6])
print('-----')
ar
= np
.arange
(16).reshape
(4,4)
print(ar
, '数组轴数为%i' %ar
.ndim
)
print(ar
[2], '数组轴数为%i' %ar
[2].ndim
)
print(ar
[2][1])
print(ar
[1:3], '数组轴数为%i' %ar
[1:3].ndim
)
print(ar
[2,2])
print(ar
[:2,1:])
print('-----')
ar
= np
.arange
(8).reshape
(2,2,2)
print(ar
, '数组轴数为%i' %ar
.ndim
)
print(ar
[0], '数组轴数为%i' %ar
[0].ndim
)
print(ar
[0][0], '数组轴数为%i' %ar
[0][0].ndim
)
print(ar
[0][0][1], '数组轴数为%i' %ar
[0][0][1].ndim
)
ar
= np
.arange
(12).reshape
(3,4)
i
= np
.array
([True,False,True])
j
= np
.array
([True,True,False,False])
print(ar
)
print(i
)
print(j
)
print(ar
[i
,:])
print(ar
[:,j
])
m
= ar
> 5
print(m
)
print(ar
[m
])
ar
= np
.arange
(10)
print(ar
)
ar
[5] = 100
ar
[7:9] = 200
print(ar
)
ar
= np
.arange
(10)
b
= ar
.copy
()
b
[7:9] = 200
print(ar
)
print(b
)
samples
= np
.random
.normal
(size
=(4,4))
print(samples
)
import matplotlib
.pyplot
as plt
a
= np
.random
.rand
()
print(a
,type(a
))
b
= np
.random
.rand
(4)
print(b
,type(b
))
c
= np
.random
.rand
(2,3)
print(c
,type(c
))
samples1
= np
.random
.rand
(1000)
samples2
= np
.random
.rand
(1000)
plt
.scatter
(samples1
,samples2
)
data1
= np
.random
.randn
(1000)
data2
= np
.random
.randn
(1000)
plt
.scatter
(data1
,data2
)
import numpy
as np
print(np
.random
.randint
(2))
print('2-5取随机数:',np
.random
.randint
(2,5))
print('生成6个[0,2)之间随机整数 :',np
.random
.randint
(2,size
=6))
print('生成5个[2,6)之间随机整数 :',np
.random
.randint
(2,6,size
=5))
print('生成一个2x3整数数组,取数范围:[0,2)随机整数 :','\n',np
.random
.randint
(2,size
=(2,3)))
print('生成一个2*3整数数组,取值范围:[2,6)随机整数 :','\n',np
.random
.randint
(2,6,(2,3)))
print('生成一个[10,50),2行4列的数组:','\n',np
.random
.randint
(10,50,size
=(2,5)))
import os
os
.chdir
('C:\\Users\\Titanic\\Desktop')
ar
= np
.random
.rand
(5,5)
print(ar
)
np
.save
('arraydata.npy',ar
)
ar_load
= np
.load
('arraydata.npy')
print(ar_load
)
ar
= np
.random
.rand
(5,5)
print(ar
)
np
.savetxt
('savatxt_text.txt',ar
,delimiter
=',')
print('text文件写入成功')
ar_loadtxt
= np
.loadtxt
('savatxt_text.txt', delimiter
=',')
print('读取文本文件成功:','\n',ar_loadtxt
)
转载请注明原文地址:https://tech.qufami.com/read-17363.html