numpy常用命令

官方文档的搬运工

numpy.random.choice生产一系列随机数

numpy.random.choice(a, size=None, replace=True, p=None)

parameter介绍

  • a: 数组或者整数:如果是数组则从其中采样,若是整数则从range(a)中进行采样

  • size:生成随机数的个数

  • replace:是否重复采样

  • p:a中每个元素的权重=被pick的概率,若果不设置,则默认平均采样

examples:

samples = np.random.choice(20, 10, replace=False)
print(samples)
samples = np.random.choice(range(20),10,replace=False) # 同上
print(samples)
samples = np.random.choice(range(20),10,replace=True) # 可重复采样
print(samples)

结果
在这里插入图片描述

numpy.concatenate

相对于append,extend方法,concatenate效率更高,适合大规模数据的拼接,使用方法如下

import numpy as np
arr1 = np.array([[1,2,3,4],[4,5,6,7]])
print("shape of arr1:{}".format(arr1.shape))
arr2 = np.array([[1,9,8,6],[5,6,7,8]])
print("shape of arr2:{}".format(arr2.shape))
arr3 = np.concatenate((arr1,arr2),axis=1) #按照维度1拼接,如果不设定axis,默认axis=0
print("shape of arr3:{}".format(arr3.shape))
arr4 = np.concatenate((arr1, arr2), axis=0)#按照维度0拼接
print("shape of arr4:{}".format(arr4.shape))

打印结果
shape of arr1:(2, 4)
shape of arr2:(2, 4)
shape of arr3:(2, 8)
shape of arr4:(4, 4)

可拼接多个数组

arr5 = np.concatenate((arr1,arr1,arr1),axis=1)
print("shape of arr5:{}".format(arr5.shape))

打印结果
shape of arr5:(2, 12)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值