numpy: np.random模块 探究(源码)

官方api定义

From Random sampling

Random sampling (numpy.random)

Simple random data rand(d0, d1, …, dn) Random values in a given shape

. randn(d0, d1, …, dn) Return a sample (or samples) from the “standard normal” distribution
. randint(low[, high, size, dtype]) Return random integers from low (inclusive) to high (exclusive)
. random_integers(low[, high, size]) Random integers of type np.int between low and high, inclusive
. random_sample([size]) Return random floats in the half-open interval [0.0, 1.0)
. random([size]) Return random floats in the half-open interval [0.0, 1.0)
. ranf([size]) Return random floats in the half-open interval [0.0, 1.0)
. sample([size]) Return random floats in the half-open interval [0.0, 1.0)
. choice(a[, size, replace, p]) Generates a random sample from a given 1-D array bytes(length) Return random bytes.


实验代码

randint(low[, high, size, dtype]):
Return random integers from low (inclusive) to high (exclusive).
从低(包括)到高(排除)返回随机整数。

import numpy as np

# randint(low[, high, size, dtype])    Return random integers from low (inclusive) to high (exclusive).
# randint(low[, high, size, dtype])    从低(包括)到高(排除)返回随机整数。
list_randint = np.random.randint(low=10, high=20, size=[1, 5])
print list_randint
[[16 14 13 16 17]]


random_integers(low[, high, size]):
Random integers of type np.int between low and high, inclusive.
类型为np.int的随机整数,包括低和高。

import numpy as np

# random_integers(low[, high, size])    Random integers of type np.int between low and high, inclusive.
# random_integers(low[, high, size])    类型为np.int的随机整数,包括低和高。
list_random_integers = np.random.random_integers(low=10, high=20, size=[1, 5])
print list_random_integers
[[17 11 12 20 12]]


rand(d0, d1, …, dn):
Random values in a given shape.
给定形状的随机值。

import numpy as np

# rand(d0, d1, ..., dn)    Random values in a given shape.
# rand(d0, d1, ..., dn)    给定形状的随机值。
list_rand = np.random.rand(5)
print list_rand
[ 0.79382535  0.5270354   0.3732075   0.39917033  0.99818847]


randn(d0, d1, …, dn):
Return a sample (or samples) from the “standard normal” distribution.
从“标准正常”分发中返回样本(或样本)。

import numpy as np

# randn(d0, d1, ..., dn)    Return a sample (or samples) from the “standard normal” distribution.
# randn(d0, d1, ..., dn)    从“标准正常”分发中返回样本(或样本)。
list_randn = np.random.randn(5)
print list_randn
[-0.35846856  0.70406236 -0.65582092  1.20919057 -0.29739695]


random([size]):
Return random floats in the half-open interval [0.0, 1.0).
在半开间隔[0.0,1.0]中返回随机浮点数。

import numpy as np

# random([size])    Return random floats in the half-open interval [0.0, 1.0).
# random([size])    在半开间隔[0.0,1.0]中返回随机浮点数。
list_random_1 = np.random.random(size=5)
print list_random_1
list_random_2 = np.random.random(size=[1, 5])
print list_random_2
[ 0.17053837  0.54069506  0.21863745  0.82232234  0.30818991]
[[ 0.66736397  0.86776538  0.0208963   0.50920261  0.61017499]]


`initialize_kmeans_plusplus`函数是用来根据k-means++算法来初始化K-Means聚类的簇中心。这是一种高级的初始化策略,旨在更好地分散初始中心点,使得聚类结果更稳定,尤其对于非球形分布的数据更有优势。k-means++通过以下步骤进行: 1. **选择第一个中心点**:随机从数据集中选取一个样本作为第一个中心点。 2. **计算距离**:对剩余的数据点,计算它们到当前已选中心点的距离。 3. **概率分布**:赋予每个未选择过的样本一个概率,该概率基于其到最近中心点的距离的倒数平方。 4. **选择下一个中心点**:按照上述概率选择一个新的中心点。 5. **重复**:重复此过程直到选出k个中心点。 在实际代码中,这涉及到一些数学运算和概率操作,通常在`sklearn.cluster.KMeans`类的源码中能找到这部分的实现,例如使用`distance`和`random.choices`等功能。由于你提到的缺失的是具体实现,你可以直接引用`sklearn.cluster.KMeans`的`_partitionition_by_max_distance`方法,或者在网上找一份详细的k-means++伪代码并自行实现。 以下是简化的伪代码版本,表示这部分应该包含的内容: ```python def initialize_kmeans_plusplus(data, k): center = random.choice(data) centers = [center] remaining_data = set(range(len(data))) - {center} for _ in range(1, k): distances = [euclidean_distance(center, x) for x in data] probabilities = [d**2 / sum(distances) for d in distances] new_center_index = np.argmax(random.choices(remaining_data, weights=probabilities)) centers.append(data[new_center_index]) remaining_data.remove(new_center_index) return np.array(centers) ``` 请注意,为了实际运行这段代码,你需要导入必要的库(如numpy),并实现`euclidean_distance`或其他相应的距离计算方法。同时,`random.choices`是Python 3.6及以上版本才有的特性。如果使用较旧的版本,可能需要使用其他方法来模拟概率选择。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值