ひとり勉強ログ

ITエンジニアの勉強したことメモ

Python

【ゼロから作るDeepLearning】2章パーセプトロン

2.1 パーセプトロンとは def AND(x1, x2): w1, w2, theta = 0.5, 0.5, 0.7 tmp = x1*w1 + x2*w2 if tmp <= theta: return 0 elif tmp > theta: return 1 print(AND(0, 0)) print(AND(1, 0)) print(AND(0, 1)) print(AND(1, 1)) パラメータのw1、w2、thetaは…

【ゼロから作るDeepLearning】1章 python入門

1.5 Numpy 1.5.5 ブロードキャスト >>> import numpy as np >>> A = np.array([[1,2], [3,4]]) >>> B = np.array([10,20]) >>> A*B array([[10, 40], [30, 80]]) 1.6 Matplotlib 1.6.1 単純なグラフの描画 >>> import numpy as np >>> import matplotlib.pyp…

8章 ファイルの読み書き【退屈なことはPythonにやらせよう】

8.1 ファイルとファイルパス 8.1.1 Windowsのバックスラッシュ、MacやLinuxのスラッシュ >>> import os >>> os.path.join('user', 'bin', 'spam') 'user/bin/spam' os.path.join()関数は、ファイル操作関数に渡して用いるような、フルパスのファイル名を作る…

7章 正規表現によるパターンマッチング【退屈なことはPythonにやらせよう】

# 正規表現を使わないテキストパターン検索 from pyexpat.errors import messages def is_phone_number(text): if (len(text) != 12): # 文字列長がぴったり12文字かどうか調べる return False for i in range(0,3): if not text[i].isdecimal(): return Fal…

6章 文字列操作【退屈なことはPythonにやらせよう】

# エスケープ文字 spam = 'Say hi to Bob\'s mother.' print(spam) # Say hi to Bob's mother. print("Hello there!\nHow are you?\nI\'m doing fine.") # Hello there! # How are you? # I'm doing fine. # raw文字列 print(r'That is Carol\'s cat.') # Th…

5章 辞書型【退屈なことはPythonにやらせよう】

# 辞書型 my_cat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'} print(my_cat['size']) print('My cat has ' + my_cat['color'] + 'fur.') # My cat has grayfur. # 辞書とリストの比較 spam = ['cats', 'dogs', 'moose'] bacon = ['dogs', 'm…

4章 リスト【退屈なことはPythonにやらせよう】

spam = ['cat', 'bat', 'rat', 'elephant'] print(spam[0]) # 'cat' print(spam[1]) # 'bat' print(spam[2]) # 'rat' print(spam[3]) # 'elephant' print(['cat', 'bat', 'rat', 'elephant'][3]) # 'elephant' print('Hello ' + spam[0]) # 'Hello cat' prin…

3章 関数【退屈なことはPythonにやらせよう】

# 関数 def hello(): # 関数を定義 print('Howdy!') print('Howdy!!!') print('Hello there.') hello() # 関数の呼び出し # パラメータのあるdef文 def hello(name): print('Hello ' + name) hello('Alice') hello('Bob') # 戻り値とreturn文 import random …

2章 フロー制御【退屈なことはPythonにやらせよう】

# if文 # 「Hello, world.」が1回表示されるのみ spam = 0 if spam < 5: print('Hello, world.') spam = spam + 1 # while文 # 「Hello, world.」が5回表示される spam = 0 while spam < 5: print('Hello, world.') spam = spam + 1 # 「あなたの名前」と入…

1章 Python入門【退屈なことはPythonにやらせよう】

print('Hello world') print('What is your name?') my_name = input() print('It is good to meet you, ' + my_name) print('The length of your name is:') print(len(my_name)) print('What is your age?') my_age = input() print('You will be ' + str(…

【Python】Pythonでスクレイピングして日経平均を取得する方法

import requests from bs4 import BeautifulSoup target_url = "https://www.nikkei.com/markets/kabu/" r = requests.get(target_url) soup = BeautifulSoup(r.text, 'html.parser') td = soup.find_all("span") for tag in td: try: string_ = tag.get("cl…