common.title

Docs
Quantum Circuit
TYTAN CLOUD

QUANTUM GAMING


autoQAOA
Desktop RAG

Overview
Terms of service

Privacy policy

Contact
Research

Sign in
Sign up
common.title

ChatGPTでGPT-4のCode Interpreterを触ってみる

Yuichiro Minato

2023/07/10 02:26

量子コンピュータ関連がちょっと落ち着いたので、遅ればせながら触ってみました。ChatGPTがPython実行してくれます。

https://chat.openai.com/?model=gpt-4-code-interpreter

通常の利用では出てないので、左のメニューのSettingsからBeta FeaturesからCode InterpreterをONにする必要があります(2023/7月) 。あとはGPT4からCode Interpreterを選んで実行します。Pythonを使う人にとっては結構衝撃的な機能ではないでしょうか?

ChatGPTの場合実行環境がないので、提案だけで結果は確認できませんでしたが、エラー込みで実行して計算結果を表示してくれます。早速使ってみます。Pythonで実行できそうなプログラムを指定して実行すれば良いだけです。

適当にXY座標を持つ点を50ほど準備して、散布図に表示してもらえますか?

少しすると実行をしてくれて計算結果を表示してくれます。

タイトルまでつけてくれました。計算結果の上部の、Show Workのプルダウンから実行されたPythonコードが手に入ります。

import matplotlib.pyplot as plt
import numpy as np

Randomly generate 50 points for X and Y

np.random.seed(0)
X = np.random.rand(50)
Y = np.random.rand(50)

plt.figure(figsize=(8,6))
plt.scatter(X, Y, color='blue')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Scatter plot of 50 random points')
plt.grid(True)
plt.show()

上記の点を分類してみます。

上記の点をk-means法で2分類してください

もうChatGPTで仕事がなくなりそうな人がたくさんいそうな予感がします。。。

from sklearn.cluster import KMeans

Prepare the data

data = np.array(list(zip(X, Y)))

Apply k-means clustering

kmeans = KMeans(n_clusters=2, random_state=0).fit(data)

Get the cluster labels and centroids

labels = kmeans.labels_
centroids = kmeans.cluster_centers_

Plot the points with different colors for each cluster

plt.figure(figsize=(8,6))
plt.scatter(data[:, 0], data[:, 1], c=labels, cmap='viridis')

Plot the centroids

plt.scatter(centroids[:, 0], centroids[:, 1], color='red', marker='X', s=300)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('K-means clustering with 2 clusters')
plt.grid(True)
plt.show()

ラベル分けされましたので、これを使って予測をしたいので、指定してみました。

上記2分類したラベルを使って線形回帰で、線形分類してもらえますか?

from sklearn.linear_model import LogisticRegression

Apply logistic regression for linear classification

log_reg = LogisticRegression(random_state=0).fit(data, labels)

Create a mesh grid for plotting decision boundary

h = .02 # step size in the mesh
x_min, x_max = X.min() - .5, X.max() + .5
y_min, y_max = Y.min() - .5, Y.max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

Predict the labels for each point on the grid

Z = log_reg.predict(np.c_[xx.ravel(), yy.ravel()])

Put the result into a color plot

Z = Z.reshape(xx.shape)
plt.figure(figsize=(8,6))
plt.pcolormesh(xx, yy, Z, cmap='viridis', alpha=0.2)

Plot the original points

plt.scatter(data[:, 0], data[:, 1], c=labels, edgecolors='k', cmap='viridis')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear classification using logistic regression')
plt.grid(True)
plt.show()

すごいです。多くの失業者が出るというイメージしか浮かびません。

© 2025, blueqat Inc. All rights reserved