Python Faker2024 年 8 月 29 日 | 阅读 6 分钟 Faker 简介Python 提供了一个名为 Faker 的开源库,可以帮助用户构建他们的数据集。我们可以使用名称、年龄、位置等随机属性生成随机数据。Faker 库支持所有中心位置和语言,有利于根据本地情况生成数据。 我们可以利用 Faker 数据来调整机器学习模型、对模型进行压力测试等等。我们可以根据自己的需求生成数据。我们还可以将 Faker 数据用于训练和学习目的,例如,我们可以对各种数据类型执行各种操作。 我们还可以使用生成的模型数据集来调整机器学习模型、验证模型和测试模型。 在本教程中,我们将了解 Faker 及其功能,并创建我们自己的数据集。 让我们开始实现 Faker 库。 Faker 库的实现在我们开始使用 faker 之前,有必要安装该库。我们可以使用命令提示符或终端中的 pip 安装程序来完成,如下所示。 语法 导入必要的库 为了发现 faker 库的各种功能,我们必须导入 faker 库。我们还导入了 pandas 库,因为我们将对数据集执行一些操作。 语法 使用各种函数 导入所需库后,让我们尝试使用 Faker 库中提供的各种函数。为了执行此类活动,我们必须通过一个变量来初始化 Faker 函数,如下所示。 语法 下面列出了一些我们将要使用的函数。 语法 让我们看一个演示这些函数工作原理的例子。 示例 输出 Your Name: Teresa Hill Your Date of Birth: 1950-03-12 Your Address: 430 Bauer Turnpike Suite 931 Annaton, OR 12319 Your Country: Angola Your E-mail Address: [email protected] 说明 在上面的示例中,我们导入了所需的库,并为 **Faker()** 模块定义了一个变量。然后,我们使用 **name、date_of_birth、address、country** 和 **email** 等函数来生成一些假数据集。这个生成的数据集是随机的,所以每次执行代码时,我们都会得到一个不同的数据集作为输出。 我们还可以根据不同地区和语言生成信息。我们所要做的就是指定我们想要的语言。让我们看一个用印地语、法语和日语生成一些数据的例子。 示例 输出 Thomas Schneider ?????? ?????? ?? ?? Lucas Poulain ????? ?????? Aurélie Merle-Menard ?? ?? ????????, ?????? Stéphane Lefebvre-Alves ????? ????? 说明 在上面的示例中,我们再次定义了所需的库,并为 **Faker()** 模块定义了一个变量,其中我们提供了一些语言作为参数。然后,我们使用“**for**”语句打印指定次数的不同生成名称。因此,该程序为用户生成了十个不同语言的名称。 我们还可以使用 **text** 和 **sentences** 等函数生成我们自己的文本或句子。 让我们看一个示例来理解这些函数如何工作。 示例 输出 Text: Size plant task we through score name. Whose learn drop ground. Option entire some surface seek film involve. Billion body really common decade man. Worker foreign your then likely beat. Sentence: Project star plant she energy them leave. 说明 在上面的示例中,我们再次导入了所需的模块,并为 **Faker()** 模块定义了一个变量。然后,我们使用 **text** 和 **sentence** 函数来创建我们自己的句子并为用户打印它们。因此,我们成功创建了自己的句子。 但是,我们还可以定义一个存储单词列表的单词库,该库允许我们使用这些指定的单词生成新的假句子。让我们看一个用于生成假句子的示例。 示例 输出 Sentence: Cow is domestic domestic domestic animal animal. 说明 在上面的示例中,我们再次导入了所需的库,并定义了 **Faker()** 模块变量。我们定义了一个单词列表,并使用 **sentence()** 函数使用我们创建的单词库生成一个句子。因此,使用列表中的单词生成了一个假句子。 此外,**Faker()** 模块还提供了一个函数,可以生成不同不存在的个人的完整配置文件,而不是单独生成姓名和地址。这个函数被称为 **profile** 函数,它生成一个人的假配置文件。 让我们看一个示例来理解这个函数的工作原理。 示例 输出 Complete Profile: {'job': 'Minerals surveyor', 'company': 'Nichols and Sons', 'ssn': '715-16-7081', 'residence': '550 Moore Locks\nSouth Andrea, SD 94842', 'current_location': (Decimal('-78.730969'), Decimal('-151.109875')), 'blood_group': 'B+', 'website': ['https://www.smith-avila.com/', 'http://bennett-scott.com/', 'https://www.nguyen.com/'], 'username': 'joseph04', 'name': 'Toni Martin', 'sex': 'F', 'address': '29676 Mann Rapid\nWilkinsonbury, MN 35916', 'mail': '[email protected]', 'birthdate': datetime.date(2016, 10, 1)} 说明 在上面的示例中,我们再次导入了所需的库并定义了变量。然后,我们使用 **profile** 函数为用户生成了一个人的假配置文件并打印出来。 现在,让我们使用 faker 库创建一个假数据集。 使用 faker 库创建假数据集由于我们已经探索了大多数函数,并且已经在上一节中学习了 **profile** 函数,让我们尝试生成一个包含 20 个唯一人物假配置文件的的数据集。为了将这些配置文件存储到数据帧中,我们还将使用 **pandas** 库。 示例 输出 job company ... mail birthdate 0 Housing manager/officer Cross LLC ... [email protected] 1983-03-26 1 Learning disability nurse Bennett-Sellers ... [email protected] 1923-04-14 2 Agricultural engineer Patrick PLC ... [email protected] 1941-01-13 3 Research scientist (life sciences) Coleman, Shaw and Owens ... [email protected] 1927-07-07 4 Haematologist Jefferson-Bailey ... [email protected] 2001-06-06 5 Chartered legal executive (England and Wales) Torres-Andersen ... [email protected] 1956-05-12 6 Statistician Rodriguez-Chung ... [email protected] 1955-07-06 7 Paediatric nurse Simmons, Acosta and Gates ... [email protected] 1984-02-29 8 Dispensing optician Bauer Inc ... [email protected] 1935-03-30 9 Equality and diversity officer Martinez, Allen and Davis ... [email protected] 2019-06-28 10 Secondary school teacher Greene, Gonzalez and Hill ... [email protected] 1913-10-02 11 TEFL teacher Smith and Sons ... [email protected] 1989-06-17 12 Planning and development surveyor Smith, Lee and Reyes ... [email protected] 1905-09-05 13 Product designer Taylor, Davis and Wilson ... [email protected] 1938-11-27 14 Development worker, community Carlson-Evans ... [email protected] 1929-03-08 15 Engineer, building services Pham Group ... [email protected] 1984-12-31 16 Therapist, horticultural Anderson-Gonzalez ... [email protected] 1929-03-16 17 Geographical information systems officer Burke-Burton ... [email protected] 1997-06-12 18 Retail manager Rivera-Lucas ... [email protected] 2016-03-20 19 Therapeutic radiographer Holloway Group ... [email protected] 2011-02-23 [20 rows x 13 columns] 说明 在上面的示例中,我们再次导入了所需的库并定义了一个变量。然后,我们定义了包含 20 个人配置文件的数据。最后,我们将这些数据转换为数据帧并为用户打印。因此,生成的数据集存储了工作、公司、地点、电子邮件等各种属性。我们可以根据我们的需求利用这个数据集。 |
我们请求您订阅我们的新闻通讯以获取最新更新。