赞
踩
目录
- import numpy as np
-
- # shape 数组的维度元组,ndim维数,size元素个数
- # 一维数组
- data1 = np.array([1, 2, 3.5, 4, 5]) # 一行五列
- print(np.shape(data1))
- print(np.ndim(data1))
- print(np.size(data1))
- print(data1.dtype)
- print('-' * 100)
-
- # 二维数组
- data2 = np.array(
- [[1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]]
- ) # 三行三列
-
- print(np.shape(data2)) # 维度元组
- print(np.ndim(data2)) # 维数
- print(np.size(data2)) # 元素个数
- print(data2.dtype) # 类型
- print(data2.itemsize) # 大小,单位:字节
- import numpy as np
- import matplotlib.pyplot as plt
-
- # 生成数组的方法
-
- # 1)生成0和1
- data1 = np.zeros(shape=(3, 4), dtype='int64')
- print(data1)
- data2 = np.ones(shape=(3, 4), dtype='int64')
- print(data2)
- print('-' * 100)
-
- # 2)生成固定范围的数组
- data3 = np.linspace(0, 20, 5, )
- print(data3)
- data4 = np.arange(0, 10, 5)
- print(data4)
- print('-'*100)
-
- # 3)生成随机数组
-
- # 均匀分布
- data5 = np.random.uniform(low=-1, high=1, size=1000000) # [-1,1]之间均匀分布
- # print(data5)
- # 创建画布
- plt.figure(figsize=(20,8),dpi= 80)
- # 绘制直方图
- plt.hist(data5,bins = 1000)
- # 展示图片
- plt.show()
-
- # 正态分布
- data6 = np.random.normal(loc= 1.75, scale= 0.1,size=1000000)
- # 创建画布
- plt.figure(figsize=(20,8),dpi= 80)
- # 绘制直方图
- plt.hist(data6,bins= 1000)
- # 展示图片
- plt.show()
- import numpy as np
- # 二维数组
- data2 = np.array(
- [[1, 2],
- [4, 5],
- [7, 8]]
- )
-
- # 修改形状,行和列
- print(data2)
- data2.resize((2,3))
- print(data2)
- print('-'*100)
-
- # 矩阵的逆置
- data2 = data2.T
- print(data2)
- import numpy as np
- import matplotlib.pyplot as plt
-
- data1 = np.array(
- [[1, 2],
- [2, 5],
- [7, 8]]
- )
- data2 = np.array(
- [[1,2,3,4],
- [3,4,5,6]]
- )
-
-
- # 修改类型
- print(data1.dtype)
- data1 = data1.astype('int64')
- print(data1.dtype)
-
- # 去重
- data2 = np.unique(data2)
- print(data2)
- import numpy as np
- import matplotlib.pyplot as plt
-
- # 准备数据
- x = np.linspace(-1,1,10000)
- y = (x**2)/2
-
- # 创建画布
- plt.figure(figsize=(20,8),dpi=80)
- # 绘制和展示图像
- plt.plot(x,y)
- plt.show()
- import numpy as np
-
- data1 = np.array(
- [[1, 2],
- [2, 5],
- [7, 8]]
- )
-
- # bool索引
- print(data1 > 3)
- print('-' * 100)
-
- # bool-与 数组中全部满足条件才为真,否则为假
- print(np.all(data1 > 3))
- print(np.all(data1 > 0))
- print('-' * 100)
-
- # bool-或 有一个满足条件则为真,否则为假
- print(np.any(data1 > 3))
- print('-' * 100)
-
- # where三目运算符
- print(np.where(data1 > 3, 1, 0))
- print(np.where(np.logical_and(data1 > 3, data1 < 8), 1, 0))
- print(np.where(np.logical_or(data1 < 3, data1 > 5), 1, 0))
- import numpy as np
-
- data1 = np.array(
- [[1, 2],
- [2, 5],
- [7, 8]]
- )
-
- # 最大值
- a1 = np.max(data1,axis=1) # 每行最大值
- a2 = np.max(data1,axis=0) # 每列最大值
- a3 = np.max(data1) # 矩阵最大值
- print(a1,a2,a3)
-
- b1 = np.argmax(data1, axis=1) # 最大值位置
- print(b1)
-
- import numpy as np
-
- # 矩阵乘法运算
- # 形状
- # (m, n) * (n, l) = (m, l)
- # 运算规则
- # A (2, 3) B(3, 2)
- # A * B = (2, 2)
- data1 = np.array(
- [[1, 2],
- [2, 5],
- [7, 8]]
- )
-
- data2 = np.array(
- [[1, 2, 3],
- [4, 5, 6]]
- )
-
- print(np.matmul(data1, data2))
-
-
- print('-'*100)
- # 矩阵加法
- data3 = np.array(
- [[1, 2, 3],
- [4, 5, 6]]
- ) # (2,3)
-
- data4 = np.array(
- [ [1],
- [3] ]
- ) # (2,1)
-
- print(data3 + data4)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。