LoginSignup
0
0

More than 1 year has passed since last update.

Colaboratoryを利用したPython実行環境

Last updated at Posted at 2022-06-19

Colaboratory(以下Colab)の特徴

  • Webブラウザ上のPython実行開発環境
  • 環境構築が不要
  • GPU への無料アクセス
  • 追加ライブラリの組み込みが可能
  • Markdownの文章とPythonコードを併記可能(Jupyter Notebook形式)

Colab の導入

Colabの導入は下記URLにアクセスするだけです。
https://colab.research.google.com/
image

Colabの設定

  • ランタイム-ランタイムのタイプを変更からGPUを指定可能。
    image
    image

  • ツール-設定 から環境設定可能。
    パワーレベル、コーギーモード、猫モード、カニモードを必要に応じて設定すると良いでしょう。
    image
    image

GitHubをColabから開く

下記リンクを記載することで、Open in Colabのリンクを設定できます。

<a href="https://colab.research.google.com/github/[github-id]/[github-path]" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

例)
<a href="https://colab.research.google.com/github/jun-knd/python101.colab/blob/main/Python101.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

image

Pythonコードエリアの追加

image

👇 追加されたコードエリアにPythonコードを記載する。
image

Python コードサンプル①

  • 数値計算
    $x = a \times b$
    下記コードサンプルは単純に積を求める。
x = 160 * 10000
print("掛け算")
print(x)
print("フォーマット")
print("{:,}".format(x))

image

Python コードサンプル②

  • round関数は四捨五入ではない
  • 四捨五入はDecimal#quantize()を利用する
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

target = 2.5

print("target = 2.5")
print("round(target)👇")
print(round(target))

print("あれ?")

print("Decimal(str(target)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)👇")
print(Decimal(str(target)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
print("そうそう!")

Python コードサンプル③

  • グラフ描画
import pandas as pd
import pandas_datareader.data as web
import datetime
import mplfinance as mpf

start = datetime.date(2021,1,1)
end = datetime.date(2022,6,17)

stock_code = "6502"

stockdata=web.DataReader(stock_code +".JP", "stooq",start,end)

df = stockdata.sort_index()
mpf.plot(df, title=stock_code, type='candle', mav=(5, 25), volume=True)
  • モジュール依存関係でエラー発生の場合、mplfinanceをpipを使ってインストールする。
!pip install mplfinance

image

  • pip コマンドはコードエリアに先頭"!"付きで記載することで発行可能
    image

👇GitHubは以下参照下さい

👇関連記事

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