首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

我如何随机地获取numpy数组中一定数量的元素,并且每个类至少有一个元素?

在numpy中,你可以使用numpy.random.choice函数来随机获取数组中指定数量的元素。该函数可以接受一个参数size来指定获取元素的数量。为了确保每个类至少有一个元素,你可以使用numpy.unique函数获取数组中的唯一值,并将其作为choice函数的可选项。以下是一个示例代码:

代码语言:txt
复制
import numpy as np

# 假设你有一个numpy数组arr
arr = np.array([1, 2, 3, 1, 2, 3, 4, 5, 6])

# 获取数组中的唯一值
unique_values = np.unique(arr)

# 初始化结果数组
result = []

# 对每个唯一值进行遍历
for value in unique_values:
    # 获取当前唯一值的索引
    indices = np.where(arr == value)[0]
    # 随机选择一个索引,并将对应元素添加到结果数组
    random_index = np.random.choice(indices)
    result.append(arr[random_index])

# 将结果数组转换为numpy数组
result = np.array(result)

# 输出结果
print(result)

这段代码中,首先使用np.unique函数获取数组arr中的唯一值。然后,对于每个唯一值,使用np.where函数获取其在数组中的索引,然后使用np.random.choice函数从这些索引中随机选择一个,并将对应的元素添加到结果数组中。最后,将结果数组转换为numpy数组并输出。

这里没有提及腾讯云相关产品和产品介绍链接地址,因为没有具体和云计算相关的产品和服务与该问题相关。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

Python数据分析(中英对照)·Building and Examining NumPy Arrays 构建和检查 NumPy 数组

NumPy provides a couple of ways to construct arrays with fixed,start, and end values, such that the other elements are uniformly spaced between them. NumPy提供了两种方法来构造具有固定值、起始值和结束值的数组,以便其他元素在它们之间均匀分布。 To construct an array of 10 linearly spaced elements starting with 0 and ending with 100, we can use the NumPy linspace function. 要构造一个由10个线性间隔元素组成的数组,从0开始到100结束,我们可以使用NumPy linspace函数。 In this case, I’m going to type np.linspace. 在本例中,我将键入np.linspace。 The first argument is the starting point, which is 0. 第一个参数是起点,即0。 The second is the ending point, which will be included in the NumPy array that gets generated. 第二个是结束点,它将包含在生成的NumPy数组中。 And the final argument is the number of points I would like to have in my array. 最后一个参数是数组中的点数。 In this case, NumPy has created a linearly spaced array starting at 0 and ending at 100. 在本例中,NumPy创建了一个从0开始到100结束的线性间隔阵列。 Now, to construct an average of 10 logarithmically spaced elements between 10 and 100, we can do the following. 现在,要构造10个10到100之间的对数间隔元素的平均值,我们可以执行以下操作。 In this case we use the NumPy logspace command. 在本例中,我们使用NumPy logspace命令。 But now careful, the first argument that goes into logspace is going to be the log of the starting point. 但是现在要小心,进入日志空间的第一个参数将是起点的日志。 If you want the sequence to start at 10, the first argument has to be the log of 10 which is 1. 如果希望序列从10开始,则第一个参数必须是10的log,即1。 The second argument is the endpoint of the array, which is 100. 第二个参数是数组的端点,它是100。 And again, we need to put in the log of that, which is 2. 再一次,我们需要把它放到日志中,也就是2。 And the third argument as before, is the number of elements in our array. 和前面一样,第三个参数是数组中的元素数。 in this case, what NumPy has constructed is an array consisting of 10 elements where the first element is 10 and the last element is 100. 在本例中,NumPy构造了一个由10个元素组成的数组,其中第一个元素是10,最后一个元素是100。 All of the other elements are uniformly spaced between those two extreme points in the logarithmic space. 所有其他元素均匀分布在对数空间的两个端点之间。 To construct array of ten logarithmically spaced elements between numbers say 250 and 500,

02

基于Jupyter快速入门Python|Numpy|Scipy|Matplotlib

在深入探讨 Python 之前,简要地谈谈笔记本。Jupyter 笔记本允许在网络浏览器中本地编写并执行 Python 代码。Jupyter 笔记本使得可以轻松地调试代码并分段执行,因此它们在科学计算中得到了广泛的应用。另一方面,Colab 是 Google 的 Jupyter 笔记本版本,特别适合机器学习和数据分析,完全在云端运行。Colab 可以说是 Jupyter 笔记本的加强版:它免费,无需任何设置,预装了许多包,易于与世界共享,并且可以免费访问硬件加速器,如 GPU 和 TPU(有一些限制)。 在 Jupyter 笔记本中运行教程。如果希望使用 Jupyter 在本地运行笔记本,请确保虚拟环境已正确安装(按照设置说明操作),激活它,然后运行 pip install notebook 来安装 Jupyter 笔记本。接下来,打开笔记本并将其下载到选择的目录中,方法是右键单击页面并选择“Save Page As”。然后,切换到该目录并运行 jupyter notebook。

01
领券