pythonに慣れよう4

pythonのif文を麻雀の役の計算で書いてみる。

chair = "parent"
yaku = int(input("yaku ? "))

if yaku == 1:
	print("lon, 2000")
elif yaku == 2:
	print("lon, 3900")
elif yaku == 3:
	print("lon, 7700")
elif yaku < 6:
	print("mangan, 12000")
elif yaku < 8:
	print("haneman, 18000")
elif yaku < 11:
	print("baiman, 24000")
elif yaku < 13:
	print("sanbaiman, 36000")
else:
	print("48000, shuryodesu")
&#91;/python&#93;

&#91;vagrant@localhost python&#93;$ python app.py
yaku ? 6
haneman, 18000
yaku ? 24
48000, shuryodesu

ifを入れ子にする場合。インデントでずらす。
&#91;python&#93;
chair = "child"
yaku = int(input("yaku ? "))

if chair == "parent":
	if yaku == 1:
		print("lon, 2000")
	elif yaku == 2:
		print("lon, 3900")
	elif yaku == 3:
		print("lon, 7700")
	elif yaku < 6:
		print("mangan, 12000")
	elif yaku < 8:
		print("haneman, 18000")
	elif yaku < 11:
		print("baiman, 24000")
	elif yaku < 13:
		print("sanbaiman, 36000")
	else:
		print("48000, shuryodesu")
elif chair == "child":
	if yaku == 1:
		print("lon, 1500")
	elif yaku == 2:
		print("lon, 2900")
	elif yaku == 3:
		print("lon, 5800")
	elif yaku < 6:
		print("mangan, 8000")
	elif yaku < 8:
		print("haneman, 12000")
	elif yaku < 11:
		print("baiman, 18000")
	elif yaku < 13:
		print("sanbaiman, 24000")
	else:
		print("36000, shuryodesu")
else:
	print("see you again")
&#91;/python&#93;

&#91;vagrant@localhost python&#93;$ python app.py
yaku ? 4
mangan, 8000

Yes sir!

改行せずに1行で書くこともできる。
&#91;python&#93;
chair = "child"
yaku = int(input("yaku ? "))

print("tobimashita" if yaku > 4 else "I'm winner")

while文

i = 1
while i < 29: print("2月{0}日".format(i)) i += 1 [/python]

pythonに慣れよう3

# coding: utf-8
name = "Gennadiy"
skill = "3D modeling"
location = "West Hollywood, Russia"

print("name: %s, skill: %s, location: %s" % (name, skill, location))

[vagrant@localhost python]$ python app.py
name: Gennadiy, skill: 3D modeling, location: West Hollywood, Russia

この記法は、mysqlのset valueみたいですね。変数をstringからnumberに変えます。

# coding: utf-8
name = "Gennadiy"
skill = "3D modeling"
age = 18

print("name: %s, skill: %s, age: %d" % (name, skill, age))

[vagrant@localhost python]$ python app.py
name: Gennadiy, skill: 3D modeling, age: 18

%f はfloatになるので、コンパイルエラーになります。
Traceback (most recent call last):
File “app.py”, line 6, in
print(“name: %s, skill: %s, age: %f” % (name, skill, age))
TypeError: float argument required, not str

型指定しない記法もある。

print("name: {0}, skill: {1}, age: {2}".format(name, skill, age))

pythonに慣れよう2

# coding: utf-8
x = 598
print(x / 3)

[vagrant@localhost python]$ python app.py
199

print(True and False)

プログラミングの基礎やるとき、いつも思うんだが、これ何でFalseになるかね?
[vagrant@localhost python]$ python app.py
False

TrueとFalseなら、TrueFalseでいいと思うんだが。
美女 and 野獣 => 美女と野獣 でしょ。
美女 and 野獣 => 野獣 でもあながち間違いではないが。 あ、例がまずかった。

print("Data Scraping" + " +S\n")
print("Data Science" + " +A\n")

改行コードいれなくても勝手に改行されますね。この辺は、phpと違うようです。
[vagrant@localhost python]$ python app.py
Data Scraping +S

Data Science +A

pythonに慣れよう

# coding: utf-8
print("hello world")

[vagrant@localhost python]$ python app.py
hello world

変数

# coding: utf-8

msg = "機械学習頑張るぞ!"
print(msg)

[vagrant@localhost python]$ python app.py
機械学習頑張るぞ!

Python では 定数 はサポートされていない。慣習的に大文字とアンダーバー(_)のみの変数が固定値を表現する。

PI = 3.14
MAX_BUFFER_SIZE = 1024
msg = "機械学習\n頑張る\tぞ!"
print(msg)

[vagrant@localhost python]$ python app.py
機械学習
頑張る ぞ!

html = """<html><body>machine learning</body>
</html>"""
print(html)

[vagrant@localhost python]$ python app.py
machine learning

python api sample2

import os
import urllib
import webapp2
import jinja2

from apiclient.discovery import build
from optparse import OptionParser

JINJA_ENVIRONMENT = jinja2.Environment(
	loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
	extensions=['jinja2.ext.autoescape'])

DEVELPOER_KEY = "REPLACE_ME"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

class MainHandler(webapp2.RequestHandler):

	def get(self):
		if DEVELOPER_KEY == "REPLACE_ME":
			self.response.write("""You must set up a project and get an API key
									to run this project. Please visit 
									<landing page> to do so."""
		else:
			youtube = build(
				YOUTUBE_API_SERVICE_NAME,
				YOUTUBE_API_VERSION,
				developerKey=DEVELOPER_KEY)
			search_response = youtube.search().list(
				q="Hello",
				part="id,snippet",
				maxResults=5
				).execute()

				videos = []
				channels = []
				playlists = []

				for search_result in search_response.get("items", []):
					if search_result["id"]["kind"] == "youtube#video":
						videos.append("%s (%s)" % (search_result["snippet"]["title"],
							search_result["id"]["videoId"]))
					elif search_result["id"]["kind"] == "youtube#channel":
						channels.append("%s (%s)" % (search_result["snippet"]["title"],
							search_result["id"]["channelId"]))
					elif search_result["id"]["kind"] == "youtube#playlist":
						playlists.append("%s (%s)" % (search_result["snippet"]["title"],
							search_result["id"]["playlistId"]))

				template_values = {
					'videos': videos,
					'channels': channels,
					'playlists': playlists
				}

				self.response.headers['Content-type'] = 'text/plain'
				template = JINJA_ENVIRONMENT.get_template('index.html')
				self.response.write(template.render(template_values))				

			app = webapp2.WSGIApplication([
				('/.*', MainHandler)
				], debug=True)

Optimizer

an optimizer
– find minimum values of functions
– build parameterized models based on data
– refine allocations to stocks in portfolios
f(x) = x^2 + x^3 + s
f(x) = (x-1.5)^2 + 0.5

"""Minimize an objective function, using SciPy."""

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as spo

def f(X):
	"""Given a scalar X, return some value (a real number)."""
	Y = (X - 1.5)**2 + 0.5
	print "X = {}, Y = {}".format(X, Y)
	return Y

def test_run():
	Xguess = 2.0
	min_result = spo.minimize(f, Xguess, method='SLSQP', options={'disp': True})
	print "Minima found at:"
	print "X = {}, Y = {}".format(min_result.x, min_result.fun)

if __name__ == "__main__":
	test_run()

Pandas Fillna()

Pandas Fillna() documentation
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html
DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)

fillna(method='ffill')
"""Plot a histogram"""

import pandas as pd
import matplotlib.pyplot as plt

from util import get_data, plot_data

def compute_daily_returns(df):
	daily_returns = df.copy()
	daily_returns[1:] = (df[1:] / df[:-1].values) - 1
	daily_returns.ix[0, :] = 0
	return daily_returns

def test_run():
	dates = pd.date_range('2009-01-01','2012-12-31')
	symbols = ['SPY']
	df = get_data(symbols, dates)
	plot_data(df)

	daily_returns - compute_daily_returns(df)
	plot_data(daily_returns, title="Daily returns", ylabel="Daily returns")

if __name__ == "__main__":
	test_run()

scatterplots in python

"""Scatterplot."""

import pandas as pd
import matplotlib.pyplot as plt

from util import get_data, plot_data

def compute_daily_returns(df):
	daily_returns = df.copy()
	daily_returns[1:] = (df[1:] / df[:-1].values) - 1
	daily_returns.ix[0, :] = 0
	return daily_returns

def test_run():
	dates = pd.date_range('2009-01-01', '2012-12-31')
	symbols = ['SPY', 'XOM', 'GLD']
	df = get_data(symbols, dates)
	
	daily_returns = compute_daily_returns(df)

	daily_returns.plot(kind='scatter',x='SPY',y='XOM')
	plt.show()

if __name__ == "__main__":
	test_run()

Arithmetic operations

import numpy as np

def test_run():
	a = np.array([(1, 2, 3, 4, 5),(10, 20, 30, 40, 50)])
	print "Original array a:\n", a

	print "\nMultiply a by 2:\n", 2 * a

if __name__ == "__main__":
	test_run()

Rolling statistics is buying opportunity
rolling standard dev

def test_run():
	dates = pd.date_range('2012-01-01','2012-12-31')
	symbols = ['SPY']
	df = get_data(symbols, dates)

	ax = df['SPY'].plot(title="SPY rolling mean", label='SPY')

	rm_SPY = pd.rolling_mean(df['SPY'], window=20)

	rm_SPY.plot(label='Rolling mean', ax=ax)

Array attributes

import numpy as np

def test_run():
	a = np.random.random((5, 4))
	print a
	print a.shape

if __name__ == "__main__":
	test_run()
import numpy as np

def test_run():
	a = np.random.random((5, 4))
	print a.size

if __name__ == "__main__":
	test_run()
import numpy as np

def test_run():
	np.random.seed(693)
	a = np.random.randint(0, 10, size=(5, 4))
	print "Array:\n", a

if __name__ == "__main__":
	test_run()

Locate maximum value

import numpy as np

def get_max_index(a):
	return a.argmax()

def test_run():
	a = np.array([9, 6, 2, 3, 12, 14, 7, 10], dtype=np.int32)
	print "Array:", a

	print "Maximum value", a.max()
	print "Index of max.:", get_max_index(a)


if __name__ == "__main__":
	test_run()

Timing python operations

import time

def time_run():
	t1 = time.time()
	print "ML4T"
	t2 = time.time()
	print "The time taken by print statement is ",t2 - t1," seconds"

if __name__ == "__main__":
	test_run()

Accessing array element

import numpy as np

def test_run():
	a = np.random.rand(5, 4)
	print "Array:\n", a

	element = a[3, 2]
	print element

if __name__ == "__main__":
	test_run()

Indexing an array with another array

import numpy as np

def test_run():
	a = np.random.rand(5)
	indices = np.array([1,1,2,3])
	print a[indices]

if __name__ == "__main__":
	test_run()

Replace a slice

nd1[0:2,0:2]=nd2[-2:,2:4]

"""Creating NumPy arrays."""
import numpy as np

def test_run():
	print np.array([(2, 3, 4),(5, 6, 7)])

if __name__ == "__main__":
	test_run()
"""Creating NumPy arrays."""
import numpy as np

def test_run():
	print np.empty(5)
	print np.empty((5,4,3))

if __name__ == "__main__":
	test_run()
import numpy as np

def test_run():
	print np.ones((5, 4), dtype=np.int_)

if __name__ == "__main__":
	test_run()