LoginSignup
0
0

Databricksアシスタントのサンプルタスク

Posted at

Databricks Assistant: sample tasks | Databricks on AWS [2023/7/18時点]の翻訳です。

本書は抄訳であり内容の正確性を保証するものではありません。正確な内容に関しては原文を参照ください。

プレビュー
本機能はパブリックプレビューです。

Databricksアシスタントは、あなたがノートブック、クエリー、ファイルを作成する際に、より効率的に行えるようにAIベースのペアプログラマーとして動作します。コードやクエリーの生成、最適化、コンプリート、説明、修正することで、あなたの疑問に答える助けとなります。

Databricksアシスタントの一般的な情報に関しては、DatabricksアシスタントのFAQをご覧ください。

あなたが提供するプロンプトが、アシスタントのアウトプットを大きく変化させます。あなたのプロンプトに以下のいずれかを追加するようにしてください:

  • コードを生成するときの「No explanatory text」
  • 「Explain the code to me step by step」
  • 「Show me two/three options that I can try」
  • 「Be concise」

以下のタイプのクエリーで実験することもできます。

  • 文字列を反転するSQL UDFを書いてください。
  • 過去30日に結果を限定するためにこのクエリーに日付けフィルターを追加してください。
  • SQLクエリーの結果からグラフをプロットするのを助けてください。クエリーの結果はPandasデータフレーム形式です。x軸はWeekとラベル付けをし、y軸はDistinct weekly usersとラベル付けしてください。

コードサンプル生成の例

データの解析

開始時点のコード:

import pandas as pd

# Read the sample NYC Taxi Trips dataset and load it into a DataFrame
df = spark.read.table('samples.nyctaxi.trips')

アシスタントへのプロンプト:

generate pandas code to convert the pyspark dataframe to a pandas dataframe and select the 10 most expensive trips from df based on the fare_amount column

データフレームリーダーの作成

開始時点のコード:

bikeSharingデータセットのデータを表示します。

display(dbutils.fs.ls("dbfs:/databricks-datasets/bikeSharing/data-001/"))

アシスタントへのプロンプト:

Generate code to read the day.csv file in the bikeSharing dataset

コードの変換、最適化の例

PandasからPySparkへの変換

開始時点のコード:

import pandas as pd

# Convert Spark DataFrame to Pandas DataFrame
pdf = df.toPandas()

# Select the 10 most expensive trips based on the fare_amount column
most_expensive_trips = pdf.nlargest(10, 'fare_amount')

# Show the result
most_expensive_trips

アシスタントへのプロンプト:

convert this code to PySpark

より効率的なコードの生成

アシスタントへのプロンプト:

Show me a code example of inefficient python code, explain why it is inefficient, and then show me an improved version of that code that is more efficient. Explain why it is more efficient, then give me a list of strings to test this out with and the code to benchmark trying each one out.

アシスタントへのプロンプト:

Write me a function to benchmark the execution of code in this cell, then give me another way to write this code that is more efficient and would perform better in the benchmark.

コードコンプリートの例

セルのコメントからコードを生成するためにLakeSenseを活用することができます。

  • macOSでは、セルで直接shift + option + spacecontrol + option + spaceを押します。
  • Windowsでは、セルで直接ctrl + shift + spaceを押します。

提案されたコードを受け入れるにはtabを押します。

文字列の反転

開始時点のコード:

# Write code to reverse a string.

探索的データ分析の実行

開始時点のコード:

# Load the wine dataset into a DataFrame from sklearn, bucket the data into 3 groups by quality, then visualize in a plotly barchart.

コード説明の例

基本的なコードの説明

開始時点のコード:

import pyspark.sql.functions as F

fare_by_route = df.groupBy(
'pickup_zip', 'dropoff_zip'
).agg(
    F.sum('fare_amount').alias('total_fare'),
    F.count('fare_amount').alias('num_trips')
).sort(F.col('num_trips').desc())

display(fare_by_route)

アシスタントへのプロンプト:

Explain what this code does

迅速なドキュメントの検索

アシスタントへのプロンプト:

When should I use repartition() vs. coalesce() in Apache Spark?

アシスタントへのプロンプト:

What is the difference between the various pandas_udf functions (in PySpark and Pandas on Spark/Koalas) and when should I choose each?  Can you show me an example of each with the diamonds dataset?

コード修正の例

デバッグ

開始時点のコード:

これは基本的なコードの説明で使用されているものと同じコードですが、import文が抜けています。これは、エラー「This throws the error: NameError: name ‘F’ is not defined」を発生させます。

fare_by_route = df.groupBy(
'pickup_zip', 'dropoff_zip'
).agg(
    F.sum('fare_amount').alias('total_fare'),
    F.count('fare_amount').alias('num_trips')
).sort(F.col('num_trips').desc())

display(fare_by_route)

アシスタントへのプロンプト:

How do I fix this error? What is 'F'?

エラーを用いたヘルプ

開始時点のコード:

このコードはエラー「AnalysisException: [UNRESOLVED_COLUMN.WITH_SUGGESTION]」を発生させます。

fare_by_route = df.groupBy(
'pickup_zip', 'dropoff_zip'
).agg(
    F.sum('fare_amount').alias('total_fare'),
    F.count('fare_amount').alias('num_trips')
).sort(F.col('num_trips').desc())

display(fare_by_route)

アシスタントへのプロンプト:

Why am I getting this error and how do I fix it?

Databricksクイックスタートガイド

Databricksクイックスタートガイド

Databricks無料トライアル

Databricks無料トライアル

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0