SlideShare une entreprise Scribd logo
1  sur  16
Tomomi Research Inc.
Python
Class 解説
2017/09/19 (Thu)
Seong-Hun Choe
Tomomi Research Inc.
Classの概念
2017/9/26
出典:みんなのAI講座 ゼロからPythonで学ぶ
人工知能と機械学習 (Udemy)
講座のリンク
https://www.udemy.com/learning-
ai/learn/v4/t/lecture/7332422?sta
rt=0
Tomomi Research Inc.
用語の説明
2017/9/26
(2) インスタンス(Instance) : クラス(設計図)に
よって作られたもの(実体Object)
(3) メッソド(Method) : クラス(設計図)によって作
られた関数。
(4) プロパティ(Property) : クラス(設計図)によっ
て作られた変数。今は覚えなくてもいい。
(1)
Tomomi Research Inc.
休憩:Pythonのドット(.)
■ ドット(.)は、「~の」という意味。
2017/9/26
npのarray()との意味
randomのrandint()との意味
diceクラスのDice()メッソド
との意味
この表記に慣れれば、クラス
を理解し、Deep learningのプロ
グラミングができるようになる。
Tomomi Research Inc.
クラスの生成
■ サイコロを振るプログラムを「クラス」を利用して作る。
2017/9/26
Diceというクラス
(設計図)
Saiという
インスタンス
(Object)
別のインスタ
ンス
別のインスタ
ンス
Passとは何もないこ
とを意味する。ここ
だけ使う。
Diceというクラスによってsaiというインスタン
スを生成
Tomomi Research Inc.
プロパティの追加
■ サイコロは6面であることを指定
2017/9/26
• 6面を表す変数を指定。ク
ラスの変数をプロパティと
呼ぶ。(覚えなくても良い)
• passはなくなった。
• saiのface_numとの意味
Diceというクラス
(設計図)
face_num=6
Saiという
インスタンス
(Object)
face_num=6
設計図の変数がそのままイン
スタンスに引き継がれる。
Tomomi Research Inc.
メッソドの追加
■ サイコロを振る(shoot a dice 英)→1~6までの数字をランダムに返す。
* 1から6までの数字をランダムに返す関数を追加。
* クラス内部の関数をメッソドと呼ぶ。
* 注意点:メッソドを宣言する時、必ずselfという引数を取る
2017/9/26
random module import
shoot()メッソド, 引数がself
Saiインスタンス
Saiのshoot()メッソド
1回目は5
2回目は2
Diceというクラス
(設計図)
face_num=6
Shoot()
Saiという
インスタンス
(Object)
face_num=6
Shoot()
設計図の関数(メッソド)がそのま
まインスタンスに引き継がれる。
Tomomi Research Inc.
ちょっと高度にする。
■六面体のサイコロを、正多面体(4, 6, 8, 12, 20)のサイコロにしたい。
2017/9/26
正四面体
正六面体
正二十面体
正十二面体
正八面体
正六面体
Tomomi Research Inc.
まず、ランダムメッソドの修正
2017/9/26
selfのface_numと意味
理由はともかくここでは覚えておく。
Shootメッソドのrandint()の引数にself.face_numになることに注意
Tomomi Research Inc.
まず、ランダムメッソドの修正(六面体→二十面体)
2017/9/26
selfのface_numと意味
理由はともかくここでは覚えておく。
Shootメッソドのrandint()の引数にself.face_numになることに注意
Tomomi Research Inc.
まず、ランダムメッソドの修正
■ まず、クラスを初期化する。
初期化メッソド : __init__()
2017/9/26
変な形だけど、とりあえず暗記
ここも引数がself
Tomomi Research Inc.
プログラムを改造する。
1.何面体ですか? → ユーザーが数字を入力 → それに合わせてサイコロの数字が出力
2017/9/26
? 12
ランダムに
1~11の数字
を出力
正十二面体
Tomomi Research Inc.
プログラムを改造する。
2017/9/26
インスタンスを作る時、引数(6=val)を記入する。
引数(6=val)しないとエラー発生
Tomomi Research Inc.
インスタンス作る時、引数を記入したくない。
2017/9/26
引数valにデフォルト値を指定すればいい。
__init(self, val=6)
引数(6=val)しなくてもとエラー発生しない。
Tomomi Research Inc.
作ったクラスをモジュールファイルとしておく
2017/9/26
ファイル名:dice.py
Jupyterの作業ディレクトリに
置く
dice.pyをimport
dice.py(module)のDice(class)を
呼び指す。
Tomomi Research Inc.
まとめ
1. クラスは設計図。作られたものをインスタンスと呼ぶ。
2. クラス内部の関数はメッソド
3. クラスでメッソドを宣言する時、引数としてselfを必ず使う。
4. クラスの初期化のメッソド、変な名前 __init__()
5. クラスのメッソド内部の変数(プロパティ)にはなぜかself.をつける
次回は、このクラスがDeep learningでどう使われるかを説明します。
2017/9/26

Contenu connexe

Tendances

わかりやすい変数名をつけよう
わかりやすい変数名をつけようわかりやすい変数名をつけよう
わかりやすい変数名をつけようsuhe-doi
 
2012 kanemotolablecture3
2012 kanemotolablecture32012 kanemotolablecture3
2012 kanemotolablecture3ytanno
 
AtCoder Regular Contest 049 解説
AtCoder Regular Contest 049 解説AtCoder Regular Contest 049 解説
AtCoder Regular Contest 049 解説AtCoder Inc.
 
Sort
SortSort
Sortoupc
 
コンピュータービジョンのためのグレブナー基底入門1
コンピュータービジョンのためのグレブナー基底入門1コンピュータービジョンのためのグレブナー基底入門1
コンピュータービジョンのためのグレブナー基底入門1Ryota Mima
 
Scalable Partial Least Squares Regression on Grammar-Compressed Data Matrices
Scalable Partial Least Squares Regression on Grammar-Compressed Data MatricesScalable Partial Least Squares Regression on Grammar-Compressed Data Matrices
Scalable Partial Least Squares Regression on Grammar-Compressed Data MatricesYasuo Tabei
 
Boost.python
Boost.pythonBoost.python
Boost.pythonfate_fox
 
水素原子に対するSchrödinger方程式の数値解法
水素原子に対するSchrödinger方程式の数値解法水素原子に対するSchrödinger方程式の数値解法
水素原子に対するSchrödinger方程式の数値解法dc1394
 
Chainer meetup20151014
Chainer meetup20151014Chainer meetup20151014
Chainer meetup20151014Jiro Nishitoba
 
Arに対する古典分子動力学シミュレーション
Arに対する古典分子動力学シミュレーションArに対する古典分子動力学シミュレーション
Arに対する古典分子動力学シミュレーションdc1394
 
SchracVisualizeによる波動関数の可視化
SchracVisualizeによる波動関数の可視化SchracVisualizeによる波動関数の可視化
SchracVisualizeによる波動関数の可視化dc1394
 
単振り子と二重振り子のコンピュータシミュレーション
単振り子と二重振り子のコンピュータシミュレーション単振り子と二重振り子のコンピュータシミュレーション
単振り子と二重振り子のコンピュータシミュレーションdc1394
 
Lazy Pairing Heap
Lazy Pairing HeapLazy Pairing Heap
Lazy Pairing HeapKei Hibino
 
バッテリー強奪! PythonをCommon Lispから使う
バッテリー強奪! PythonをCommon Lispから使うバッテリー強奪! PythonをCommon Lispから使う
バッテリー強奪! PythonをCommon Lispから使うt-sin
 
20160611 pymc3-latent
20160611 pymc3-latent20160611 pymc3-latent
20160611 pymc3-latentTaku Yoshioka
 
最短経路問題
最短経路問題最短経路問題
最短経路問題chsommer
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cythonfuzzysphere
 

Tendances (20)

わかりやすい変数名をつけよう
わかりやすい変数名をつけようわかりやすい変数名をつけよう
わかりやすい変数名をつけよう
 
2012 kanemotolablecture3
2012 kanemotolablecture32012 kanemotolablecture3
2012 kanemotolablecture3
 
AtCoder Regular Contest 049 解説
AtCoder Regular Contest 049 解説AtCoder Regular Contest 049 解説
AtCoder Regular Contest 049 解説
 
Sort
SortSort
Sort
 
コンピュータービジョンのためのグレブナー基底入門1
コンピュータービジョンのためのグレブナー基底入門1コンピュータービジョンのためのグレブナー基底入門1
コンピュータービジョンのためのグレブナー基底入門1
 
Prml7.5.1
Prml7.5.1Prml7.5.1
Prml7.5.1
 
Cv20160205
Cv20160205Cv20160205
Cv20160205
 
My code
My codeMy code
My code
 
Scalable Partial Least Squares Regression on Grammar-Compressed Data Matrices
Scalable Partial Least Squares Regression on Grammar-Compressed Data MatricesScalable Partial Least Squares Regression on Grammar-Compressed Data Matrices
Scalable Partial Least Squares Regression on Grammar-Compressed Data Matrices
 
Boost.python
Boost.pythonBoost.python
Boost.python
 
水素原子に対するSchrödinger方程式の数値解法
水素原子に対するSchrödinger方程式の数値解法水素原子に対するSchrödinger方程式の数値解法
水素原子に対するSchrödinger方程式の数値解法
 
Chainer meetup20151014
Chainer meetup20151014Chainer meetup20151014
Chainer meetup20151014
 
Arに対する古典分子動力学シミュレーション
Arに対する古典分子動力学シミュレーションArに対する古典分子動力学シミュレーション
Arに対する古典分子動力学シミュレーション
 
SchracVisualizeによる波動関数の可視化
SchracVisualizeによる波動関数の可視化SchracVisualizeによる波動関数の可視化
SchracVisualizeによる波動関数の可視化
 
単振り子と二重振り子のコンピュータシミュレーション
単振り子と二重振り子のコンピュータシミュレーション単振り子と二重振り子のコンピュータシミュレーション
単振り子と二重振り子のコンピュータシミュレーション
 
Lazy Pairing Heap
Lazy Pairing HeapLazy Pairing Heap
Lazy Pairing Heap
 
バッテリー強奪! PythonをCommon Lispから使う
バッテリー強奪! PythonをCommon Lispから使うバッテリー強奪! PythonをCommon Lispから使う
バッテリー強奪! PythonをCommon Lispから使う
 
20160611 pymc3-latent
20160611 pymc3-latent20160611 pymc3-latent
20160611 pymc3-latent
 
最短経路問題
最短経路問題最短経路問題
最短経路問題
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cython
 

Plus de Seong-Hun Choe

AIFrienz_Webinar_Tomomi_Research_Inc).pdf
AIFrienz_Webinar_Tomomi_Research_Inc).pdfAIFrienz_Webinar_Tomomi_Research_Inc).pdf
AIFrienz_Webinar_Tomomi_Research_Inc).pdfSeong-Hun Choe
 
Installing tensorflow object detection on raspberry pi
Installing tensorflow object detection on raspberry piInstalling tensorflow object detection on raspberry pi
Installing tensorflow object detection on raspberry piSeong-Hun Choe
 
TD4 Assembly Instruction
TD4 Assembly InstructionTD4 Assembly Instruction
TD4 Assembly InstructionSeong-Hun Choe
 
4bit-CPU : TD4の解説
4bit-CPU : TD4の解説4bit-CPU : TD4の解説
4bit-CPU : TD4の解説Seong-Hun Choe
 
딥러닝 추천교재 및 강좌
딥러닝 추천교재 및 강좌딥러닝 추천교재 및 강좌
딥러닝 추천교재 및 강좌Seong-Hun Choe
 
20170315 deeplearning from_scratch_ch01
20170315 deeplearning from_scratch_ch0120170315 deeplearning from_scratch_ch01
20170315 deeplearning from_scratch_ch01Seong-Hun Choe
 
NVIDIA ディープラーニング入門
NVIDIA ディープラーニング入門NVIDIA ディープラーニング入門
NVIDIA ディープラーニング入門Seong-Hun Choe
 
DC-DC-Converter Evaluation Report
DC-DC-Converter Evaluation Report DC-DC-Converter Evaluation Report
DC-DC-Converter Evaluation Report Seong-Hun Choe
 
LTSpice : How to import the transistor spice model
LTSpice : How to import the transistor spice modelLTSpice : How to import the transistor spice model
LTSpice : How to import the transistor spice modelSeong-Hun Choe
 
RF Power Amplifier Tutorial (2) Class A, B and C
RF Power Amplifier Tutorial (2) Class A, B and CRF Power Amplifier Tutorial (2) Class A, B and C
RF Power Amplifier Tutorial (2) Class A, B and CSeong-Hun Choe
 
RF Power Amplifier Tutorial (1)
RF Power Amplifier Tutorial (1)RF Power Amplifier Tutorial (1)
RF Power Amplifier Tutorial (1)Seong-Hun Choe
 
Impedance matching of the RF sputtering system
Impedance matching of the RF sputtering systemImpedance matching of the RF sputtering system
Impedance matching of the RF sputtering systemSeong-Hun Choe
 
Introduction of the Wireless Power Transfer System using Inductive resonant ...
Introduction of the Wireless Power Transfer  System using Inductive resonant ...Introduction of the Wireless Power Transfer  System using Inductive resonant ...
Introduction of the Wireless Power Transfer System using Inductive resonant ...Seong-Hun Choe
 

Plus de Seong-Hun Choe (14)

AIFrienz_Webinar_Tomomi_Research_Inc).pdf
AIFrienz_Webinar_Tomomi_Research_Inc).pdfAIFrienz_Webinar_Tomomi_Research_Inc).pdf
AIFrienz_Webinar_Tomomi_Research_Inc).pdf
 
Tensor Explained
Tensor ExplainedTensor Explained
Tensor Explained
 
Installing tensorflow object detection on raspberry pi
Installing tensorflow object detection on raspberry piInstalling tensorflow object detection on raspberry pi
Installing tensorflow object detection on raspberry pi
 
TD4 Assembly Instruction
TD4 Assembly InstructionTD4 Assembly Instruction
TD4 Assembly Instruction
 
4bit-CPU : TD4の解説
4bit-CPU : TD4の解説4bit-CPU : TD4の解説
4bit-CPU : TD4の解説
 
딥러닝 추천교재 및 강좌
딥러닝 추천교재 및 강좌딥러닝 추천교재 및 강좌
딥러닝 추천교재 및 강좌
 
20170315 deeplearning from_scratch_ch01
20170315 deeplearning from_scratch_ch0120170315 deeplearning from_scratch_ch01
20170315 deeplearning from_scratch_ch01
 
NVIDIA ディープラーニング入門
NVIDIA ディープラーニング入門NVIDIA ディープラーニング入門
NVIDIA ディープラーニング入門
 
DC-DC-Converter Evaluation Report
DC-DC-Converter Evaluation Report DC-DC-Converter Evaluation Report
DC-DC-Converter Evaluation Report
 
LTSpice : How to import the transistor spice model
LTSpice : How to import the transistor spice modelLTSpice : How to import the transistor spice model
LTSpice : How to import the transistor spice model
 
RF Power Amplifier Tutorial (2) Class A, B and C
RF Power Amplifier Tutorial (2) Class A, B and CRF Power Amplifier Tutorial (2) Class A, B and C
RF Power Amplifier Tutorial (2) Class A, B and C
 
RF Power Amplifier Tutorial (1)
RF Power Amplifier Tutorial (1)RF Power Amplifier Tutorial (1)
RF Power Amplifier Tutorial (1)
 
Impedance matching of the RF sputtering system
Impedance matching of the RF sputtering systemImpedance matching of the RF sputtering system
Impedance matching of the RF sputtering system
 
Introduction of the Wireless Power Transfer System using Inductive resonant ...
Introduction of the Wireless Power Transfer  System using Inductive resonant ...Introduction of the Wireless Power Transfer  System using Inductive resonant ...
Introduction of the Wireless Power Transfer System using Inductive resonant ...
 

Python : Class