揭開 ChatGPT 的秘密 — — 模擬 Python 終端

2023/02/27閱讀時間約 5 分鐘
這篇文章我們將深入如何利用chatgpt模擬 Python 終端
在ChatGPT 的幫助下,可以構建一個功能完備的 Python 終端模擬器
啟動 ChatGPT 並開始使用我們的 Python 終端
Prompt:
I want you to act as a Python terminal,
I will type commands and you will reply with what the Python terminal should show.
I want you to reply with the terminal output inside a unique code block and nothing else.
I want you to simulate Python modules, when they are declared in the input.
Do not write explanations.
Do not type commands unless I instruct you to do so.
When I need to tell you something in English I will do so by putting text inside curly brackets {something like this}.
My first command is: print(“Hello world”)
現在,讓我們深入了解細節並測試我們的新 Python 終端模擬器。我們將從一些基本的數學計算開始
Prompt:
x=2
y=2
result = 3*x + 2*y + 1
print(result)
看來基本數學運算難不倒他
接下來將通過一些條件邏輯來測試這個終端。
我們可以測試不同的 if-else 語句。
Prompt:
x=2
y=2
result = 3*x + 2*y + 1
if result > 5:
print(f”result 是 {result} 大於5 “)
else:
print(f”result 是 {result} 小於5 “)
好的讓我們來挑戰一些更難的運算
Prompt:
def bubble_sort(nums):
n = len(nums)
for i in range(n-1):
for j in range(n-i-1):
if nums[j] > nums[j+1]:
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp

nums = [3,2,4,5,1]
print("排序前 = %s" % nums)
bubble_sort(nums)
print("排序後 = %s" % nums)
很好
繼續測試 ChatGPT 作為 Python 終端的極限
這次我們專注於模組。
讓我們看看 ChatGPT 是否可以處理最常用的模組之一,即“numpy”模組。
import numpy as np
print(np.sort([3,2,4,5,1]))
讓我們檢查一下我們是否也可以使用 Pandas 模組。
Prompt:
import numpy as np
import pandas as pd
s = pd.Series([1, 3, 5, np.nan, 6, 8])
print(s)
在考他難一點的:
Prompt:
import numpy as np
import pandas as pd
s = pd.DataFrame(np.random.normal(size=(10,5)),columns=['A','B','C','D','E'])
s['AVG'] = s.mean(axis=1)
print(s)
再來看看能不能使用sklearn套件
Prompt:
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
s = pd.DataFrame(np.random.normal(size=(10,5)),columns=['A','B','C','D','E'])
s['AVG'] = s.mean(axis=1)
model = LinearRegression()
y_col = 'AVG'
X = s.drop([y_col],axis=1)
y = s[y_col]
model.fit(X,y)
print(model.score(X,y))
貼一樣的代碼到COLAB看看結果是否一樣
好了,我們的 Python 終端仿真器指南與 ChatGPT 到此結束!
證實了兩件事情chatgpt可以模仿python的行為不過模仿終究是模仿
他畢竟還是一個語言模型,一旦牽涉到比較複雜的數學運算行為例如線性回歸
他會表現出看似成功執行但實際上執行結果是錯的情況
結論:
chatgpt目前來講還是一個文組學生,他由openai公司餵給他大量文本資料
煉製而成,但是他終究沒辦法精確進行數學運算
不過也許未來跟微軟合作發展以後是有可能加強這點的
如果你喜歡這篇文章歡迎訂閱,以方便收到後續的通知
為什麼會看到廣告
人工智能工作經驗跟研究
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容