Python编程实现的生成20个_Feature()样本的随机器
发布时间:2023-12-11 05:57:05
以下是一个生成20个_Feature()样本的随机数据集的Python编程实例:
import random
# 定义一个_Feature()类
class _Feature():
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
# 生成20个_Feature()样本的随机数据集
def generate_feature_samples():
samples = []
for _ in range(20):
# 生成随机属性值
attribute1 = random.randint(1, 10)
attribute2 = random.randint(1, 10)
# 创建_Feature()对象并添加到样本集合中
sample = _Feature(attribute1, attribute2)
samples.append(sample)
return samples
# 打印生成的样本集合
samples = generate_feature_samples()
for sample in samples:
print(f"attribute1: {sample.attribute1}, attribute2: {sample.attribute2}")
说明:
1. 代码中创建了一个名为_Feature()的类,该类具有两个属性:attribute1和attribute2。
2. generate_feature_samples()函数用于生成20个样本,并通过随机生成属性值为每个样本对象的属性进行初始化。
3. 样本生成过程中,random.randint(1, 10)用于生成范围在1到10之间的随机整数作为属性值。
4. 最后,通过循环遍历打印生成的样本集合,展示各个样本对象的属性值。
这是一个基本示例,你可以根据自己的需要进行扩展和修改。
