关于程序员:从-数据工程-到-Prompt-工程

1次阅读

共计 6633 个字符,预计需要花费 17 分钟才能阅读完成。

动动发财的小手,点个赞吧!

数据工程形成了数据迷信过程的很大一部分。在 CRISP-DM 中,这个过程阶段称为“数据筹备”。它包含数据摄取、数据转换和数据质量保证等工作。在本文章中,咱们应用 ChatGPT 和 Python 解决了典型的数据工程工作。通过这样做,咱们摸索了数据工程与提醒工程新学科之间的分割。

简介

2022 年 5 月,斯蒂芬·沃尔夫勒姆 (Stephen Wolfram) 和莱克斯·弗里德曼 (Lex Fridman) 发表了题为“编程已死吗?”的粗浅见解。他们探讨了将来开发者是否还会应用高级语言。依据 Wolfram 的说法,许多编程工作能够通过大型语言模型 (LLM) 实现自动化。在撰写本文时,此类模型最突出的例子是 ChatGPT。自 2022 年底推出以来,它产生了惊人的成果。指定要由 LLM 执行的操作称为“提醒工程”。如果 Wolfram 是对的,至多软件开发的一部分将从编写代码转向编写提醒。

在数据迷信方面,数据筹备可能是一项耗时且乏味的工作。那么,为什么不尝试应用 LLM 使其自动化呢?在以下局部中,咱们将应用 ChatGPT 和 Python 解决不同的数据工程问题。咱们没有本人编写 Python 代码,而是应用提醒工程来生成它。咱们的试验是在 2023 年 5 月 19 日基于过后最新的收费 ChatGPT 版本 (GPT-3.5) 进行的。

创立数据框

让咱们从一个简略的问题开始,并从样本数据集创立一个 Pandas 数据框。表 1 蕴含例如世界银行提供的国家指标。

为了从下面的示例中创立数据框,咱们开始了与 ChatGPT 的新对话并公布了以下提醒:

Create a Pandas table with the following data:
Continent, Country, Year, Category, Variable, Value
Asia, China, 2022, Economy, GDP, 18.1
Asia, China, 2022, Demographics, Population, 1412
North America, USA, 2022, Economy, GDP, 25.5
North America, USA, 2022, Demographics, Population, 333
Europe, Germany, 2022, Economy, GDP, 3.9
Europe, Germany, 2022, Demographics, Population, 84

作为响应,该模型生成了以下 Python 代码:

import pandas as pd

data = {'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
    'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
    'Year': [2022, 2022, 2022, 2022, 2022, 2022],
    'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
    'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
    'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}

df = pd.DataFrame(data)
print(df)

运行脚本时,咱们看到输入合乎咱们的标准:

只管咱们在试验中应用了示例,但下一节中生成的代码原则上也能够解决更大的数据集。

旋转数据框

对于数据迷信,咱们在第一个提醒中创立的表构造并不是最优的。相同,所谓的“平板”会更不便。在表 1 中,指标“GDP”和“人口”别离用名为“Variable”和“Value”的两列示意。相比之下,立体表格将为每个指标蕴含独自的列,如表 2 所示。

将表 1 的格局转换为表 2 的格局称为“透视”。让咱们尝试应用 ChatGPT 来解决这个工作。为此,咱们在对话中公布了第二个提醒:

Transform the table into the following format:
Continent, Country, Year, GDP, Population
Asia, China, 2022, 18.1, 1412
North America, USA, 2022, 25.5, 333
Europe, Germany, 2022, 3.9, 84

上述音讯基于所需数据格式的示例。或者,咱们也能够纯口头表白咱们的要求。与经典编程一样,在提醒工程中有实现目标的代替解决方案。在不久的未来,寻找最佳形式将是一项激动人心的工作。依据咱们的提醒,ChatGPT 对 Python 代码进行了如下调整:

import pandas as pd

data = {'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
    'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
    'Year': [2022, 2022, 2022, 2022, 2022, 2022],
    'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
    'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
    'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}

df = pd.DataFrame(data)

# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'Year'], columns='Category', values='Value').reset_index()

# Rename the columns
pivot_df.columns.name = None
pivot_df.columns = [column if column != 'Variable' else None for column in pivot_df.columns]

print(pivot_df)

然而,运行脚本的第二个版本显示输入不是咱们所要求的:

该表蕴含名为“人口统计”和“经济”的两列,而不是预期的“GDP”和“人口”列。为了解决这个问题,咱们公布了下一个提醒:

The columns are not correct, you should have used 
the variables GDP and population

作为响应,模型纠正了谬误并生成了另一个版本的 Python 代码:

import pandas as pd

data = {'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
    'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
    'Year': [2022, 2022, 2022, 2022, 2022, 2022],
    'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
    'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
    'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}

df = pd.DataFrame(data)

# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'Year'], columns='Variable', values='Value').reset_index()

# Rename the columns
pivot_df.columns.name = None
pivot_df.columns = ['Continent', 'Country', 'Year', 'GDP', 'Population']

print(pivot_df)

运行脚本表明这次旋转已正确实现:

富集数据框

另一个典型的数据工程工作是用附加信息丰盛数据集。为此,咱们要求 ChatGPT 通过公布以下提醒将 ISO 国家代码增加到咱们的数据中:

For each country, in the table above, add its ISO code

后果,模型调整了响应并返回了以下代码片段:

import pandas as pd

data = {'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
    'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
    'Year': [2022, 2022, 2022, 2022, 2022, 2022],
    'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
    'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
    'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}

iso_codes = {
    'China': 'CN',
    'USA': 'US',
    'Germany': 'DE'
}

df = pd.DataFrame(data)

# Add ISO code column
df['ISO Code'] = df['Country'].map(iso_codes)

# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'ISO Code', 'Year'], columns='Variable', values='Value').reset_index()

# Rename the columns
pivot_df.columns.name = None
pivot_df.columns = ['Continent', 'Country', 'ISO Code', 'Year', 'GDP', 'Population']

print(pivot_df)

运行脚本显示一个带有 ISO 代码的新列已增加到数据框中。

派生列

接下来让咱们通过将一个国家的 GDP 除以其人口规模来得出一个新的列“人均 GDP”。为此,咱们发送了以下提醒:

Derive a new column "GDP per capita" by dividing the GDP 
of a country by its population size

对此,模型批改了代码如下:

import pandas as pd

data = {'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
    'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
    'Year': [2022, 2022, 2022, 2022, 2022, 2022],
    'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
    'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
    'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}

iso_codes = {
    'China': 'CN',
    'USA': 'US',
    'Germany': 'DE'
}

df = pd.DataFrame(data)

# Add ISO code column
df['ISO Code'] = df['Country'].map(iso_codes)

# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'ISO Code', 'Year'], columns='Variable', values='Value').reset_index()

# Calculate GDP per capita
pivot_df['GDP per capita'] = pivot_df['GDP'] / pivot_df['Population']

# Rename the columns
pivot_df.columns.name = None
pivot_df.columns = ['Continent', 'Country', 'ISO Code', 'Year', 'GDP', 'Population', 'GDP per capita']

print(pivot_df)

输入表明脚本产生了预期的后果:

剖析数据

一旦数据转换实现,理论的剖析就能够开始了。最初让咱们计算一下各国 GDP 与人口的相关系数。为此,咱们贴出最初一个提醒:

Use the table above to calculate the correlation coefficient between 
the GDP and the population of the countries

后果,模型返回了以下代码片段:

import pandas as pd

data = {'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
    'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
    'Year': [2022, 2022, 2022, 2022, 2022, 2022],
    'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
    'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
    'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}

df = pd.DataFrame(data)

# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'Year'], columns='Variable', values='Value').reset_index()

# Calculate correlation coefficient
correlation_coefficient = pivot_df['GDP'].corr(pivot_df['Population'])

print("Correlation Coefficient: {:.3f}".format(correlation_coefficient))

运行脚本时,咱们发现两个变量之间存在很强的正相关性,这并不奇怪:

总结

不可否认,咱们的试验是基于一个简略的数据集。然而,后果是显著的。咱们执行了几项数据工程工作,而没有编写一行代码。ChatGPT 不仅可能在大多数状况下正确执行咱们的提醒。但即便模型犯了谬误,它也可能反映和修复谬误。与软件开发一样,生成的代码必须通过测试。此外,它可能须要重构和优化。在 AI 时代应用 pylint 依然是一个好主见。然而,总而言之,咱们必须批准 Wolfram 的观点:在将来,数据工程的重要局部将从编码转向提醒工程。这种新办法不会取代数据工程师,但会进步他们的效率。

本文由 mdnice 多平台公布

正文完
 0