numpy的使用

1、数据的属性¶
import numpy as np

创建ndarray

score = np.array(
[[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]])

score
array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]])
score.shape
(8, 5)
score.size
40
score.itemsize
4
score.dtype
dtype(‘int32’)
score.ndim
2
1.1.2 列表和numpy的速度比拼
import random
import time
import numpy as np
a = []
for i in range(100000000):
a.append(random.random())

通过%time魔法方法, 查看当前行的代码运行一次所花费的时间

%time sum1=sum(a)

b=np.array(a)

%time sum2=np.sum(b)
Wall time: 14.7 s
Wall time: 445 ms
a = np.array([[1,2,3], [4,5,6]], dtype=np.float32)
print(a.dtype)

a = np.array([‘python’, ‘tensorflow’], dtype=np.string_)
print(a.dtype)
float32
|S10
1.2数据的生成
np.ones([4, 8])
array([[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.]])
np.zeros_like(np.ones([4, 9]))
array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.]])
a = np.array([[1,2,3], [2,3,4]]) # 相当于深拷贝
1.2.2 array与asarray的区别:深拷贝、浅拷贝的区别·
b = np.asarray(a) # 相当于浅拷贝
b[1][1] = 0
a
array([[1, 2, 3],
[2, 0, 4]])
b = a
b
array([[1, 2, 3],
[2, 0, 4]])
b[1][1] = 1000
a
array([[ 1, 2, 3],
[ 2, 1000, 4]])
1.2.2 linspace数据生成
np.linspace(0, 100, 11) # 从0开始到100,生成11个间隔相同的数字
array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
1.2.3 np.arange(start, stop, step, dtype)
np.arange(10, 50, 2) # 不包含末尾元素
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,
44, 46, 48])
1.2.4 np.logspace(start, stop, num) 创建等比数列
np.logspace(0,2,10) # num 表示要生成的等比数列数量,默认为50 生成 10 ^ x
array([ 1. , 1.66810054, 2.7825594 , 4.64158883,
7.74263683, 12.91549665, 21.5443469 , 35.93813664,
59.94842503, 100. ])
1.2.5 random的使用
1.2.5.1 正态分布的创建
np.random.randn(d0, d1, …, dn)

功能:从标准正态分布中返回一个或多个样本值

np.random.normal(loc=0.0, scale=1.0, size=None)

loc:float

​ 此概率分布的均值(对应着整个分布的中心centre)

scale:float

​ 此概率分布的标准差(对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高)

size:int or tuple of ints

​ 输出的shape,默认为None,只输出一个值

np.random.standard_normal(size=None)

返回指定形状的标准正态分布的数组。

生成均值为1.75,标准差为1的正态分布数据,10000000个

x1 = np.random.normal(1.75, 1, 100000000)

import matplotlib.pyplot as plt
plt.figure(figsize=(20, 8), dpi=100)

绘制直方图

plt.hist(x1, 1000)

显示图像

plt.show()

1.2.5.2 均匀分布
np.random.rand(d0, d1, …, dn) 返回[0.0,1.0)内的一组均匀分布的数。 np.random.uniform(low=0.0, high=1.0, size=None) 功能:从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high. 参数介绍: low: 采样下界,float类型,默认值为0; high: 采样上界,float类型,默认值为1; size: 输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出mnk个样本,缺省时输出1个值。 返回值:ndarray类型,其形状和参数size中描述一致。 np.random.randint(low, high=None, size=None, dtype=‘l’) 从一个均匀分布中随机采样,生成一个整数或N维整数数组, 取数范围:若high不为None时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数。

import matplotlib.pyplot as plt

生成均匀分布的随机数

x2 = np.random.uniform(-1, 1, 100000000)

画图

plt.figure(figsize=(10, 10), dpi=100)

plt.hist(x=x2, bins=1000) # bins 表示要划分的区间数

plt.show()

2、数组的索引、切片和常用操作
2.1 数组的索引和切片
a1 = np.array([ [[1,2,3],[4,5,6]], [[12,3,34],[5,6,7]]])

a1[0:, 1::, 0:1:1]
array([[[4]],

   [[5]]])

2.2 reshape 的使用
返回一个具有相同数据域,但shape不一样的视图
行列不进行互换
a1.reshape([2, 6])
a1
array([[[ 1, 2, 3],
[ 4, 5, 6]],

   [[12,  3, 34],
    [ 5,  6,  7]]])

2.3 resize的使用
修改数组本身的形状,(需要保持元素个数前后相同)
行、列不进行互换
a1.resize([2, 6])
a1
array([[ 1, 2, 3, 4, 5, 6],
[12, 3, 34, 5, 6, 7]])
2.4 ndarray.T 转置,行列互换
a1.T
array([[ 1, 12],
[ 2, 3],
[ 3, 34],
[ 4, 5],
[ 5, 6],
[ 6, 7]])
3、类型修改
3.1 ndarray.astype(type)
a = np.array([1,2,4,3], dtype=np.int32)
a = a.astype(np.float32) # 进行赋值,才能真正修改
a.dtype
dtype(‘float32’)
3.2 ndarray.tostring([order])或者ndarray.tobytes([order])
arr = np.array([[1,2,3], [3,4,5]])
arr.tostring() # 转换为二进制
b’\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00’
4、数组的去重
4.1 np.unnique()
temp = np.array([[1,2,3,4], [3,4,5,6], [2,4,3,5]])
np.unique(temp)
array([1, 2, 3, 4, 5, 6])
5、数组的运算
import random

score = np.random.randint(40, 100, (10, 5))

test_score = score[6:, 0:5]
test_score > 60

test_score[test_score > 60] = 1
test_score
array([[ 1, 1, 1, 1, 1],
[ 1, 1, 1, 43, 1],
[44, 1, 1, 1, 1],
[50, 1, 1, 1, 1]])
np.all(score[0:2, :] > 60)
False
np.any(score[0:2, :] > 80)
True
temp = score[:4, :4]
np.where(temp > 60, 1, 0)
array([[0, 1, 1, 1],
[1, 0, 1, 1],
[1, 0, 0, 0],
[1, 1, 0, 1]])
np.where(np.logical_and(temp>60, temp<80), 1, 0)
np.where(np.logical_or(temp>70, temp<80), 1, 0)
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
np.min(score, axis=0)
array([1, 1, 1, 1, 1])
score.min(axis=1)
array([54, 41, 47, 55, 52, 42, 1, 1, 1, 1])
score.max(axis=0)
array([91, 92, 96, 98, 97])
np.median(score, axis=1)
array([83., 79., 57., 84., 75., 91., 1., 1., 1., 1.])
score.mean(axis=1)
array([81.4, 73.4, 63.2, 79.6, 69.6, 80.2, 1. , 9.4, 9.6, 10.8])
score.median(axis=1)

AttributeError Traceback (most recent call last)
in
----> 1 score.median(axis=1)

AttributeError: ‘numpy.ndarray’ object has no attribute ‘median’

score.std(axis=1)
array([14.89429421, 18.11739496, 13.68795091, 12.90891165, 14.20704051,
20.1831613 , 0. , 16.8 , 17.2 , 19.6 ])
score.var(axis=0)
array([ 957.05, 1374.6 , 1589.01, 1332.16, 1405.25])
score.sum(axis=0)
array([555, 430, 473, 498, 435])
score.argmax(axis=0)
array([5, 0, 1, 5, 0], dtype=int64)
score.argmin(axis=1)
array([0, 1, 3, 2, 3, 4, 0, 0, 1, 1], dtype=int64)
5.2 数组与数、数组的运算
arr = np.array([[1,2,3,2,1,4], [5,6,1,2,3,1]])
arr + 1
arr / 2
array([[0.5, 1. , 1.5, 1. , 0.5, 2. ],
[2.5, 3. , 0.5, 1. , 1.5, 0.5]])
a = [[1,2,3], [4,5,6]]
b = [[10, 11, 12], [3,34, 2]]
a = a + b
a
[[1, 2, 3], [4, 5, 6], [10, 11, 12], [3, 34, 2]]
5.3 广播机制
arr1 = np.array([[0], [1], [2], [3]])
print(arr1.shape)

arr2 = np.array([1,2,3])
print(arr2.shape)

arr2 + arr1


(4, 1)
(3,)
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
arr1 = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
arr2 = np.array([[1], [3]])
arr1 + arr2
array([[2, 3, 4, 3, 2, 5],
[8, 9, 4, 5, 6, 4]])
6、矩阵的运算
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([[4, 5], [5, 6], [8, 9]])
3*a
array([[ 3, 6],
[ 9, 12],
[15, 18]])
a + b
array([[ 5, 7],
[ 8, 10],
[13, 15]])
a * b
array([[ 4, 10],
[15, 24],
[40, 54]])
a = np.array(
[[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
b = np.array([[0.7], [0.3]])

np.matmul(a, b)

array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])
np.dot(a, b) # dot在矢量乘矢量的内积运算与np.dot没有区别。matmul中禁止矩阵与标量的乘法(比如:数字)
array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])
np.dot(a, 190)
array([[15200, 16340],
[15580, 15200],
[16150, 14820],
[17100, 17100],
[16340, 15580],
[15580, 17100],
[14820, 15200],
[17480, 17860]])
a
array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值