import requests as requests
import numpy as np
#查看numpy的版本
np.__version__
'1.16.4'
np.random.randint(0,100,10)
array([ 4, 14, 96, 22, 65, 66, 93, 4, 55, 94])
def sum2(x):
ret = 10
for i in x:
ret += i
return ret
x = np.arange(0,10,1)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
%timeit sum2(x)
2.41 µs ± 15.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
import numpy as np
l=[10,20,3,4,5,3,55] #创建一个列表
type(l) #查看类型 打印列表 因为列表的方法过少...所以选择numpy里面的方法(array)(求和/求平均值/求最大/最小等等...)
list
nd=np.array(l) #将列表转换为 numpy里面的array 数组 .因为有很多的操作方法..比如以下内容显示
nd
array([10, 20, 3, 4, 5, 3, 55])
type(nd) #这里查看发现类型已经被转换为numpy下面的array类型
numpy.ndarray
l2=[[1,3,5,7],[2,4,6,8]] #创建一个二维数组
l2
[[1, 3, 5, 7], [2, 4, 6, 8]]
np.array(l2) #将l2 转换为numpy里 array的方法 也可以对转换的结果进行赋值 再将其带入方法中去操作.. nd2=np.array(l2) -->array[]
array([[1, 3, 5, 7],
[2, 4, 6, 8]])
np.sum(np.array(l2)) #可以直接对numpy下面的array进行求和运算 解释:方法里面套方法...
36
np.var(l2) #计算方差??? l2=[[1,3,5,7],[2,4,6,8]] 方差为什么是5.25呢?? 再来测试下吧...
5.25
l3=[[1,2,2,7,8],[2,3,4,5,6]]
np.array(l3)
array([[3],
[9]])
np.var(l3)
5.2
l4=[1,2,2,7,8]
l5=[2,3,4,5,6]
np.array(l4)
np.array(l5)
array([2, 3, 4, 5, 6])
np.var(l4)
8.4
np.var(l5)
2.0
np.var(np.array([17,15,23,7,9,13]))
27.666666666666668
l6=[17,15,23,7,9,13]
l6
[17, 15, 23, 7, 9, 13]
np.array(l6)
array([17, 15, 23, 7, 9, 13])
np.var(l6) #方差
27.666666666666668
np.std(l6) #标准差 计算和方差的差别就是分母是n-1
5.259911279353167
np.var(np.array([7,8,8,8,9])) #方差越小说明人越稳定...
0.4
np.var(np.array([10,6,10,6,8])) # 方差的计算公式
3.2
nd2.max() # numpy方法下面nd2数组里面的最大值是多少..??? min是最小值
nd2=np.array(l2) #将二维数组转换为nparray的类型
nd2
print(np.size(nd)) #最小的数字是多少???
nd.size #数组的元素总数。这等于元素的乘积shape。
nd.itemsize #数组中每个元素的大小(以字节为单位)
nd.shape #数组的大小。这是一个整数元组,表示每个维度中数组的大小。
l2 = [[1,3,5],[2,4,6,8]] #创建一个二位数组 并输出结果
l2
nd2=np.array(l2)
nd2
np.sum(nd) # 可以使用numpy里面的方法来调用参数...同样也可以使用参数 直接调用方法
nd.sum() #可以使用参数 直接调用方法
nd2.mean() #求平均值
nd2.argmax()
nd2.sum()
np.__version__
#查看numpy的版本
np.ones(shape=(5,5),dtype=np.int8)
np.zeros(shape=(2,3,4),dtype=np.float16)
np.full(shape=(3,5),fill_value=3.14)
np.eye(N=5)
#等差数列
np.linspace(0,100,num=21)
#
np.random.randint(0,1000,size=(5,5))
#正太分布
np.random.randn(4,6)
nd=np.random.normal(loc=175,scale=10,size=100).round(3)
nd
nd.mean()
#ndarray的属性...ndim 维度 四个必须记住的参数 shape元素类型 dtype类型
nd.size
#形状,就是几维的(答案是一维)
nd.shape
ndarray的基本操作
nd2=np.random.randint(0,150,size=(4,5))
nd2
nd2[1,1]
nd2[2]
nd2[0:3]
nd2[-2]
nd2[0:3,0:3]
nd3=nd[:10]
nd3
nd3[::-1]
nd3[::2]
from PIL import Image
cat=Image.open('C:/Users/Apple280/Pictures/cat.jpg')
cat
cat_data=np.array(cat)
cat_data
cat_data.shape
cat_data.dtype
np.int8(124)
np.int8(128)
np.int8(129)
np.int8(130)
cat2=cat_data[:,:,::-1]
cat2
Image.fromarray(cat2)
Image.fromarray(cat_data[:,:,[1,0,2]])
Image.fromarray(cat_data[:,:,0])
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(cat_data[::-1])
plt.imshow(cat_data[::-10,::-10])
nd2
nd2.reshape(2,10)
#不改变总数据的情况下改变行数和列数
cat6=np.transpose(cat_data,axes=(1,0,2))
plt.imshow(cat6)
#图片的拼接
cat7=cat_data[:,:180]
plt.imshow(cat7)
cat8=cat_data[:,250:,::-1]
plt.imshow(cat8)
print(cat7.shape,cat8.shape)
cat9=np.concatenate([cat7,cat8],axis=1)
plt.imshow(cat9)
nd1=np.random.randint(0,150,size=(4,5))
nd2=np.random.randint(0,150,size=(2,5))
nd3=np.random.randint(0,150,size=(4,8))
display(nd1,nd2,nd3)
np.concatenate([nd1,nd3],axis=1)
np.hstack((nd1,nd3))
#水平,列数增加
np.vstack((nd1,nd2))
#数值 行数增加