Open keyfall opened 3 years ago
import numpy as np
import random
t1 = np.array([1, 2, 3])
print(t1)
print(t1)
t2 = np.array(range(10))
print(t2)
print(type(t2))
t3 = np.arange(4, 10, 2)
print(t3)
print(type(t3))
print(t3.dtype)
t4 = np.array(range(1, 4), dtype="i1")
print(t4)
print(t4.dtype)
t5 = np.array([1, 1, 0, 1, 0, 0], dtype=bool)
print(t5)
print(t5.dtype)
#astype转换dtype类型
t6 = t5.astype("int8")
print(t6)
print(t6.dtype)
t7 = np.array([random.random() for i in range(10)])
print(t7)
print(t7.dtype)
#取小数点后两位
t8 = np.round(t7, 2)
print(t8)
numpy读取数据
import numpy as np
t1 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(t1)
print(t1.shape)
t2 = np.arange(12)
print("t2:", t2)
t3 = t2.reshape((3, 4))
print("t3:", t3)
print(t3+2)
print(t3*2)
print(t3/2)
print("*"*100)
t4 = np.arange(4)
print(t3-t4)
t5 = np.arange(3).reshape((3, 1))
print(t3-t5)
import numpy as np
t1 = np.arange(24).reshape((4, 6))
print(t1)
print(t1.transpose())
print(t1.T)
print(t1.swapaxes(1, 0))
记录一下transpose 用于转置的
import numpy as np
t1 = np.arange(24).reshape((2, 3, 4))
print(t1)
t2 = t1.transpose(2, 1, 0)
print(t2)
结果
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
[[[ 0 12]
[ 4 16]
[ 8 20]]
[[ 1 13]
[ 5 17]
[ 9 21]]
[[ 2 14]
[ 6 18]
[10 22]]
[[ 3 15]
[ 7 19]
[11 23]]]
transpose(x,y,z)中的x,y,z表示3维(2维就是0,1) 对应的是reshape的维数,比如上面是3维的,transpose就是3维 x,y,z分别对应的是0,1,2 如果2,1,0了,就是把x和z换位置,对应到修改t1的reshape就变成了reshape((4, 2, 3))了
import numpy as np
t1 = np.array(range(70)).reshape(7, 10)
print(t1)
print("*"*100)
# #取行
# print(t1[2])
# print("*"*100)
#
# #取连续多行
# print(t1[2:])
#
# print("*"*100)
#
# #取不连续多行
# print(t1[[2, 4, 6]])
print(t1[:, 0])
print("*"*100)
#取连续的多列
print(t1[:, 2:])
print("*"*100)
#取不连续的多列
print(t1[:, [0, 2, 6]])
print("*"*100)
#取多个不相邻的点
print(t1[[0, 2], [0, 1]])
import numpy as np
t1 = np.array(range(70)).reshape(7, 10)
print(t1)
print("*"*100)
#t1中每个值是否小于10
print(t1 < 10)
print("*"*100)
#t1中小于10的赋予3
t2 = t1
t2[t2<10]=3
print(t2)
print("*"*100)
# t1中大于20的
t3 = t1[t1 > 20]
print(t3)
print("*"*100)
# numpy三元运算符where 小于30的换成100,大于30的换成300
print(np.where(t1<=30, 100, 300))
print("*"*100)
# clip裁剪 小于20的换成20,大于40的换成40
print(t1.clip(20, 40))
matplotlib
绘制折线图
绘制散点图
pyplot.scatter()
绘制条形图
pyplot.bar()
绘制直方图
pyplot.hist()