综述由AI生成面向已有编程基础但熟悉度较低的读者,介绍 Python 在 AI 开发中的快速入门。内容涵盖 Python 安装、数据类型(字符串、数字、列表、字典)、变量、脚本创建、循环与条件语句、函数定义以及库管理(pip、venv)。最后通过一个从 PDF 提取摘要并调用 OpenAI API 生成关键词的示例项目,演示了如何结合基础语法与外部库完成简单的 AI 任务。
赛博朋克15 浏览
Python QuickStart for People Learning AI
Python 已经成为 AI 和数据科学的默认编程语言。尽管存在无代码解决方案,但学习如何编码仍然是构建完全定制的 AI 项目和产品所必需的。在这篇文章中,我分享了一个 Python AI 开发的入门级快速入门指南。我将介绍基础知识,然后分享一个具体的示例代码。
"this is a string"# 'this is a string''so is this:-1*!@&04"(*&^}":>?'# 'so is this:-1*!@&04"(*&^}":>?'"""and this is too!!11!"""# 'andn this isn too!!11!'"we can even " + "add strings together"# 'we can even add strings together'
# we can't add strings to other data types (BTW this is how you write comments in Python)"I am " + 29# TypeError: can only concatenate str(not"int") to str# so we have to write 29 as a string"I am " + "29"# 'I am 29'
# a list of strings
["a", "b", "c"]
# a list of ints
[1, 2, 3]
# list with a string, int, and float
["a", 2, 3.14]
# a list of lists
[["a", "b"], [1, 2], [1.0, 2.0]]
# creating a variable and printing it
user_name = "Shaw"print(user_name)
#>> Shaw
我们可以用其他数据类型做同样的事情,例如整数和列表。
# defining more variables and printing them as a formatted string.
user_age = 29
user_interests = ["AI", "Music", "Bread"]
print(f"{user_name} is {user_age} years old. His interests include {user_interests}.")
#>> Shaw is 29 years old. His interests include ['AI', 'Music', 'Bread'].
# a simple for loop iterating over a sequence of numbersfor i inrange(5):
print(i) # print ith element# for loop iterating over a list
user_interests = ["AI", "Music", "Bread"]
for interest in user_interests:
print(interest) # print each item in list# for loop iterating over items in a dictionary
user_dict = {"Name": "Shaw", "Age": 29, "Interests": ["AI", "Music", "Bread"]}
for key, value in user_dict.items():
print(key, "=", value) # print each key and corresponding value
# check if user is 18 or olderif user_dict["Age"] >= 18:
print("User is an adult")
# check if user is 1000 or older, if not print they have much to learnif user_dict["Age"] >= 1000:
print("User is wise")
else:
print("User has much to learn")
在 for 循环中使用条件是常见的,可以根据特定条件应用不同的操作,例如计算对面包感兴趣的用户数量。
# count the number of users interested in bread
user_list = [
{"Name": "Shaw", "Age": 29, "Interests": ["AI", "Music", "Bread"]},
{"Name": "Ify", "Age": 27, "Interests": ["Marketing", "YouTube", "Shopping"]}
]
count = 0# intialize countfor user in user_list:
if"Bread"in user["Interests"]:
count = count + 1# update countprint(count, "user(s) interested in Bread")
# print(), a function we've used several times alreadyfor key in user_dict.keys():
print(key, ":", user_dict[key])
# type(), getting the data type of a variablefor key in user_dict.keys():
print(key, ":", type(user_dict[key]))
# len(), getting the length of a variablefor key in user_dict.keys():
print(key, ":", len(user_dict[key]))
# TypeError: object of type 'int' has no len()
# string methods# make string all lowercaseprint(user_dict["Name"].lower())
# make string all uppercaseprint(user_dict["Name"].upper())
# split string into list based on a specific character sequenceprint(user_dict["Name"].split("ha"))
# replace a character sequence with anotherprint(user_dict["Name"].replace("w", "whin"))
# list methods# add an element to the end of a list
user_dict["Interests"].append("Entrepreneurship")
print(user_dict["Interests"])
# remove a specific element from a list
user_dict["Interests"].pop(0)
print(user_dict["Interests"])
# insert an element into a specific place in a list
user_dict["Interests"].insert(1, "AI")
print(user_dict["Interests"])
# define a custom functiondefuser_description(user_dict):
""" Function to return a sentence (string) describing input user """returnf'{user_dict["Name"]} is {user_dict["Age"]} years old and is interested in {user_dict["Interests"][0]}.'# print user description
description = user_description(user_dict)
print(description)
# print description for a new user!
new_user_dict = {"Name": "Ify", "Age": 27, "Interests": ["Marketing", "YouTube", "Shopping"]}
print(user_description(new_user_dict))
# define another custom functiondefinterested_user_count(user_list, topic):
""" Function to count number of users interested in an arbitrary topic """
count = 0for user in user_list:
if topic in user["Interests"]:
count = count + 1return count
# define user list and topic
user_list = [user_dict, new_user_dict]
topic = "Shopping"# compute interested user count and print it
count = interested_user_count(user_list, topic)
print(f"{count} user(s) interested in {topic}")
# Function to read the first page of a PDF and extract the abstractdefextract_abstract(pdf_path):
# Open the PDF file and grab text from the 1st pagewith fitz.open(pdf_path) as pdf:
first_page = pdf[0]
text = first_page.get_text("text")
# Extract the abstract (assuming the abstract starts with 'Abstract')# find where abstract starts
start_idx = text.lower().find('abstract')
# end abstract at introduction if it exists on 1st pageif'introduction'in text.lower():
end_idx = text.lower().find('introduction')
else:
end_idx = None# extract abstract text
abstract = text[start_idx:end_idx].strip()
# if abstract appears on 1st page return it, if not resturn Noneif start_idx != -1:
abstract = text[start_idx:end_idx].strip()
return abstract
else:
returnNone
# Function to summarize the abstract and generate keywords using OpenAI APIdefsummarize_and_generate_keywords(abstract):
# Use OpenAI Chat Completions API to summarize and generate keywords
prompt = f"Summarize the following paper abstract and generate (no more than 5) keywords:\n\n{abstract}"# make api call
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
temperature = 0.25
)
# extract response summary
summary = response.choices[0].message.content
return summary
将所有内容整合在一起
最后,我们可以使用我们定义的用户函数为任何通过命令行传递给程序的论文生成摘要和关键词。
# Get the PDF path from the command-line arguments
pdf_path = sys.argv[1]
# Extract abstract from the PDF
abstract = extract_abstract(pdf_path)
# if abstract exists on first page, print summary.if abstract:
# Summarize and generate keywords
summary = summarize_and_generate_keywords(abstract)
print(summary)
else:
print("Abstract not found on the first page.")
Output: The paper introduces the Transformer, a novel network architecture for sequence transduction tasks that relies solely on attention mechanisms, eliminating the need for recurrent and convolutional structures. The Transformer demonstrates superior performance in machine translation tasks, achieving a BLEU score of28.4on the WMT 2014 English-to-German translation and a state-of-the-art score of41.8on the English-to-French translation task,while also being more efficient in training time. Additionally, the Transformer shows versatility by successfully applying to English constituency parsing with varying amounts of training data.**Keywords:** Transformer, attention mechanisms, machine translation, BLEU score, neural networks.
接下来是什么?
在这里,我们涵盖了 Python 的基础知识并实现了我们的第一个 AI 项目!尽管我们已经覆盖了很多内容,但还有更多要学习。