LoginSignup
0
2

More than 5 years have passed since last update.

Udemy - ゼロからPythonで学ぶ人工知能と機械学習 : Python基礎編

Last updated at Posted at 2018-09-09

みんなのAI講座 ゼロからPythonで学ぶ人工知能と機械学習

  • python未経験
  • 主なバックグラウンドはC
  • 私用ではUnityでC#使用
  • 仕事ではPHP+HTML+CSS+JavaScript+SQLでWebサービス構築。ただしこちらはPMとして。

複数行へのコメント挿入

Ctrl + /

日本語対応(エラー止める)

ファイル先頭に

#coding: UTF-8 

文章を乗算で繰り返す

a = "Hello"

d = a * 5
print d
実行結果
HelloHelloHelloHelloHello

タプルとは?

変更できない配列。DefineやConst的に使う?

辞書とは?

連想配列的に文字列等を添字として対応する要素を呼び出すことができる。

Debugger

  • 行番号をクリックすることでブレイクポイントを入れ、指定した行で実行を止める。
  • 実行は Debug"ファイル名" で実施。
  • ブレイクポイントで止まるので、そこからStep into(F7)で1ステップずつ処理を進めていく。
  • 「その行の左辺」の値が更新されるのは次行に移った時。

多重リスト List in List

  • ベクトル・行列を描くことができる。
a = [[2012, 2013, 2014], [2015, 2016, 2017]]
b = a[1][0]
print b # 1番目リストの0番要素にアクセス

a.append([2018, 2019])
print a # リストを追加

a[2].append(2020)
print a # リスト内のリストに要素追加
実行結果
2015
[[2012, 2013, 2014], [2015, 2016, 2017], [2018, 2019]]
[[2012, 2013, 2014], [2015, 2016, 2017], [2018, 2019, 2020]]

クラス

  • クラスの定義、変数・メソッドの定義、インスタンス生成
# Define Class
class Dog:
    # property
    name = ""
    # Method
    # 必ず引数部にselfを書く
    def bark(self):
        # メソッド内でプロパティにアクセスする場合は "self." が必須
        m = self.name + " : Bow-wow!"
        print m

# Create instance
pochi = Dog()
pochi.name = "POCHI"
pochi.bark()

hachi = Dog()
hachi.name = "HACHI"
hachi.bark()

クラスとリスト、For文の組み合わせ

  • 生成したインスタンスは基本的に通常の変数、配列等と同じように宣言・アクセスすることができる。
# Value値の自乗値をReturnする
class Calculation:
    value = 0
    def square(self):
        s = self.value * self.value
        return s

calcs = [Calculation(), Calculation(), Calculation()]

calcs[0].value = 3
calcs[1].value = 4
calcs[2].value = 5

for c in calcs:
    print c.square()
実行結果
9
16
25

ファイルオープン/クローズ、ファイル読み込みおよびその際のキャスト

  • ファイルを掴んでクローズする処理はC等と同様だが、読み込みと同時にキャストで整数型等に変換可能。
score_list = []

# Read CSV file in same directory
score_list_file = open("score.csv")

for score in score_list_file :
    score = score.rstrip() # Delete line break
    score = score.split(",") # Sprit by ,
    # score_list.append(score)
    # [0] read as text, [1] cast for integer
    score_list.append([score[0], int(score[1])])

score_list_file.close()

print score_list
実行結果
[['Taro', 78], ['HHanako', 89], ['Ichiro', 68], ['Keita', 54], ['Yuko', 89], ['Reiko', 78]]
0
2
3

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
2