Python实现的随机生成20个_Feature()样本的脚本
发布时间:2023-12-11 05:55:41
下面是一个使用Python实现的随机生成20个_Feature()样本的脚本,同时还附带了一个使用例子。
import random
class Feature:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
def __repr__(self):
return f'Feature(attribute1={self.attribute1}, attribute2={self.attribute2})'
def generate_samples(num_samples):
samples = []
for _ in range(num_samples):
attribute1 = random.randint(1, 10)
attribute2 = random.uniform(0, 1)
sample = Feature(attribute1, attribute2)
samples.append(sample)
return samples
if __name__ == '__main__':
samples = generate_samples(20)
for sample in samples:
print(sample)
上面的代码定义了一个Feature类,该类具有两个属性attribute1和attribute2。generate_samples函数用于生成指定数量的Feature样本,并返回样本列表。在示例的主程序中,我们生成了20个Feature样本,并将每个样本打印出来。
使用例子:
$ python generate_samples.py Feature(attribute1=7, attribute2=0.7264417060989711) Feature(attribute1=1, attribute2=0.3272179311870929) Feature(attribute1=10, attribute2=0.7926220174428112) Feature(attribute1=5, attribute2=0.8885183380383689) Feature(attribute1=2, attribute2=0.9875373550191897) Feature(attribute1=7, attribute2=0.0822860895055438) ... (省略部分输出) ... Feature(attribute1=9, attribute2=0.24909174122559648) Feature(attribute1=3, attribute2=0.3390072783284962) Feature(attribute1=5, attribute2=0.14490348205799894) Feature(attribute1=8, attribute2=0.01085922727628526) Feature(attribute1=2, attribute2=0.7620496825548907)
在上面的输出示例中,我们可以看到生成的20个样本,每个样本都包含两个属性attribute1和attribute2。这些属性的值是随机生成的,符合指定的范围。
