Udacity was born out of a Stanford University experiment in which Sebastian Thrun and Peter Norvig offered their "Introduction to Artificial Intelligence" course online to anyone, for free.
I studied at Udacity(https://www.udacity.com/).
I thank Dave Evans who is Instructors Intro to Computer Science and Sebastian Thrun who is one of the creater of udacity.
The mission of udacity is to bring accessible, affordable, engaging, and highly effective higher education to the world. They and I believe that higher education is a basic human right, and we seek to empower our students to advance their education and careers.
In [1]:
%matplotlib inline
from matplotlib import pyplot as plt, cm
from imutils.convenience import resize
from imutils.perspective import four_point_transform
from skimage.filters import threshold_adaptive
import cv2
import numpy as np
In [2]:
# Step 1: Edge Detection
# load the image and compute the ratio of the old height
# to the new height, clone it, and resize it
image = cv2.imread("./data/ReceiptSwiss.jpg")
ratio = image.shape[0] / 500.0
orig = image.copy()
image = resize(image, height = 500)
# convert the image to grayscale, blur it, and find edges in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)
# show the original image and the edge detected image
print "STEP 1: Edge Detection"
plt.figure(figsize=(16,12))
plt.subplot(121), plt.imshow(image, cmap = 'gray')
plt.title('Original Image')
plt.subplot(122), plt.imshow(edged, cmap = 'gray')
plt.title('Edge Image')
Out[2]:
In [4]:
# Step 2: Use the edges in the image to find the contour (outline) representing the piece of paper being scanned.
# find the contours in the edged image, keeping only the
# largest ones, and initialize the screen contour
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
# loop over the contours
for c in cnts:
peri = cv2.arcLength(c, True) # arcLength 곡선의 길이를 계산
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
# show the contour (outline) of the piece of paper
print "STEP 2: Find contours of paper"
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
plt.figure(figsize=(16,12))
plt.subplot(121), plt.imshow(orig, cmap = 'gray')
plt.title('Original Image')
plt.subplot(122), plt.imshow(image, cmap = 'gray')
plt.title('Edge Image')
Out[4]:
In [5]:
# Step 3: Apply a perspective transform to obtain the top-down view of the document.
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)
# convert the warped image to grayscale, then threshold it
# to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = threshold_adaptive(warped, 250, offset = 10)
warped = warped.astype("uint8") * 255
# show the original and scanned images
print "STEP 3: Apply perspective transform"
plt.figure(figsize=(16,12))
plt.subplot(121), plt.imshow(resize(orig, height = 650), cmap = 'gray')
plt.title('Original Image')
plt.subplot(122), plt.imshow(resize(warped, height = 650), cmap = 'gray')
plt.title('Scanned')
Out[5]:
In [6]:
import cv2
faceCascade = cv2.CascadeClassifier("data/haarcascade_frontalface_default.xml")
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
In [9]:
# A string represents an ordered series of characters
my_string = "a line of text"
my_string = 'a line of text'
print my_string
print type(my_string)
print len(my_string)
In [26]:
print my_string.find("text")
In [7]:
# Assign values to the three variable types
# Here, we simply assign a literal
my_int = 5
print my_int
print type(my_int)
my_float = 5.0
print my_float
print type(my_float)
my_boolean = False
print my_boolean
print type(my_boolean)
In [30]:
print my_int + 6
print my_float + 6
In [ ]:
a = 1
b = 2
if True:
print a
if False:
print b
In [37]:
a = 1
b = 2
c = 3
if a > 0:
print a
elif b == 2:
print b
elif c <= 3:
print c
else:
print nan # where?
if b >= 0:
print b
In [45]:
my_tuple = (1, 2.0, False, False, "jack", "jack", "jack")
print my_tuple
print type(my_tuple)
print len(my_tuple)
In [47]:
my_tuple.count(False)
Out[47]:
In [49]:
my_list = [True, "a string!", (1,'tuple','in', 1, 'list')]
print my_list
print type(my_list)
print len(my_list)
In [54]:
print my_list[0]
my_list[0] = "jack"
print my_list[0]
print my_list
In [53]:
my_dictionary = {1:[1,1,1], 3:'three', False:2.0}
print my_dictionary
my_dictionary = {False:2.0, 1:[1,1,1], 3:'three'}
print my_dictionary
print type(my_dictionary)
print len(my_dictionary)
In [58]:
for key in my_dictionary:
print "key : %s, value : %s" %(key, my_dictionary[key])
In [60]:
def print_something():
print "just printing"
print_something()
In [63]:
# Function definition
def print_and_sum(a, b):
print 'The first number is', a
print 'The second number is', b
return a + b
# Assign function output to a variable
c = print_and_sum(3,5)
print 'Their sum is', c
print
# Print function output directly
print print_and_sum(10,20)
In [74]:
def fee_diff(btc):
# x = 1, 고객이 빌린돈
# fee_a = 1 * 0.002 = 0.002
fee_a = btc * 0.002
# sum_a = 1 + 0.002
sum_a = btc + fee_a
# fee_b = 1.002 * 0.002
fee_b = sum_a * 0.002
# sum_b = 1.002 + (1.002 * 0.002)
sum_b = sum_a + fee_b
return fee_b - fee_a
In [77]:
# 1BTC를 고객이 빌린 경우
btc = 1
print "고객이 1BTC를 빌린 경우 수수료 차이 %.9f" %fee_diff(btc)
# 11BTC를 고객이 빌린 경우
btc = 11
print "고객이 1BTC를 빌린 경우 수수료 차이 %.9f" %fee_diff(btc)
In [80]:
import numpy as np
In [82]:
np.arange(1, 10, 0.1)
Out[82]:
In [90]:
btc = np.arange(1, 1000, 0.01)
In [85]:
len(btc)
Out[85]:
In [86]:
import pandas as pd
In [92]:
df = pd.DataFrame(btc)
df.columns = ['btc']
In [94]:
df.describe()
Out[94]:
In [106]:
np.set_printoptions(precision=9)
pd.set_option('precision', 9)
In [109]:
df['btc'][:10].map(lambda x: fee_diff(x))
Out[109]:
In [110]:
df['fee_diff'] = df['btc'].map(lambda x: fee_diff(x))
In [112]:
df.head(5)
Out[112]:
In [114]:
df.describe()
Out[114]:
In [115]:
df['fee_diff_krw'] = df['fee_diff'].map(lambda x: x * 300000)
In [117]:
df.head(5)
Out[117]:
In [116]:
df.describe()
Out[116]:
In [120]:
df.plot(x='btc', y='fee_diff_krw')
Out[120]:
In [121]:
import requests
from bs4 import BeautifulSoup
In [122]:
url = "https://api.korbit.co.kr/v1/external/exchanges/ticker/"
payload = {"client_id" : "IIqo8jjBcK6UplyL2fhVS7kqQYknTyr4fFr2qkyGNicPvKgTP0vnERZSRLptv",
"exchanges" : "btcchina"}
response = requests.get(url, params=payload)
print response.content
In [123]:
content = response.json()
In [124]:
import json
In [125]:
print json.dumps(content, indent=4, sort_keys=True)
In [126]:
url = "https://api.korbit.co.kr/v1/orderbook"
payload = {"category" : "all",}
response = requests.get(url, params=payload)
print json.dumps(response.json(), indent=4, sort_keys=True)
In [127]:
url = "https://api.korbit.co.kr/v1/ticker/detailed"
response = requests.get(url)
print json.dumps(response.json(), indent=4, sort_keys=True)
In [128]:
url = "https://api.korbit.co.kr/v1/constants"
response = requests.get(url)
print json.dumps(response.json(), indent=4, sort_keys=True)
In [129]:
url = "https://api.korbit.co.kr/v1/oauth2/login"
email = ""
password = ""
headers = {"Host" : "api.korbit.co.kr",
"Content-Type" : "application/x-www-form-urlencoded",
'Cookie': 'kbapi=\"435ae248cdecc7cca9eb5be21481ff49d413d72e-client_id=IIqo8jjBcK6UplyL2fhVS7kqQYknTyr4fFr2qkyGNicPvKgTP0vnERZSRLptv&scope=&redirect_uri=\"',
"Origin" : "https://api.korbit.co.kr",
}
payload = {"email" : email,
"password" : password}
request = requests.session()
response = request.post(url, headers = headers, data=payload, allow_redirects=False)
print request.cookies
In [130]:
url = "https://api.korbit.co.kr/v1/oauth2/code"
verification_code = "00000"
payload = {"verification_code" : verification_code,}
response = request.post(url, data=payload, allow_redirects=False)
body = response.content
soup = BeautifulSoup(body)
code = soup.find_all('title')[0].get_text()
print "[+] Code : " + str(code)
In [ ]:
url = "https://api.korbit.co.kr/v1/oauth2/access_token"
client_id = "IIqo8jjBcK6UplyL2fhVS7kqQYknTyr4fFr2qkyGNicPvKgTP0vnERZSRLptv" # 안바뀜
client_secret = "x5proQC37ZUAcEG7366LFZHOsn0G9GrqttcZrkYfoggL0R0XhoA882HlhA3dQ" # 안바뀜
grant_type = "authorization_code"
payload = {"client_id" : client_id,
"client_secret" : client_secret,
"code" : code,
"grant_type" : grant_type}
response = request.post(url, data=payload, allow_redirects=False)
access_token = response.json()['access_token']
refresh_token = response.json()['refresh_token']
print "[+] access_token : " + str(access_token)
print "[+] refresh_token : " + str(refresh_token)
In [132]:
url = "https://api.korbit.co.kr/v1/vault"
# 취약점 : key를 override 시킬 수 잇을까?
payload = {"client_id" : client_id, # exchange
"client_secret" : client_secret, # exchange
"access_token" : access_token,
# "key" : "ZZD190AC-4638-4DB8-B694-5125297628CF",
"key" : "jack_key_1",
"nonce" : "1510202144382",
"password" : "0000",
"value" : refresh_token}
response = request.post(url, data=payload, allow_redirects=False)
print "[+] status code : " + str(response.status_code)
In [ ]:
url = "https://api.korbit.co.kr/v1/user/info"
payload = {"access_token" : access_token,
"nonce" : "1510202144383"}
response = requests.get(url, params=payload)
print json.dumps(response.json(), indent=4, sort_keys=True)
In [ ]:
url = "https://api.korbit.co.kr/v1/vault"
key = "jack_key_1" # 바뀌는 값 새로 로그인 할 때 부여되며 refresh token이랑 한쌍임
password = "9999"
payload = {"client_id" : client_id,
"client_secret" : client_secret,
"key" : key,
"password" : password}
response = requests.get(url, params=payload)
refresh_token = str(response.content)
print "[+] refresh token : " + response.content
In [ ]:
# Indirect 인증2단계
url = "https://api.korbit.co.kr/v1/oauth2/access_token"
client_id = "IIqo8jjBcK6UplyL2fhVS7kqQYknTyr4fFr2qkyGNicPvKgTP0vnERZSRLptv"
client_secret = "x5proQC37ZUAcEG7366LFZHOsn0G9GrqttcZrkYfoggL0R0XhoA882HlhA3dQ"
grant_type = "refresh_token"
payload = {"client_id" : client_id,
"client_secret" : client_secret,
"grant_type" : grant_type,
"refresh_token" : refresh_token}
response = requests.post(url, data=payload)
refresh_token = response.json()['refresh_token']
access_token = response.json()['access_token']
print "[+] access_token : " + str(access_token)
In [ ]:
# 유저 정보 가져오기
url = "https://api.korbit.co.kr/v1/user/info"
payload = {"access_token" : access_token,
"nonce" : "1510202144384"}
response = requests.get(url, params=payload)
print response.content
In [137]:
# 매수하기
# 지정가
url = "https://api.korbit.co.kr/v1/user/orders/buy"
payload = {"access_token" : access_token,
"coin_amount" : "1",
"nonce" : "1510202144385",
"price" : "1000",
"type" : "limit"}
response = requests.post(url, data=payload)
print response.content
In [ ]:
# 매수 취소하기
url = "https://api.korbit.co.kr/v1/user/orders/cancel"
payload = {"access_token" : access_token,
"coin_amount" : "1",
"nonce" : "1510202144386",
"id" : "820330"}
response = requests.post(url, data=payload)
print response.content
In [2]:
from collections import OrderedDict
import bearcart
import bokeh
import bokeh.plotting as bp
from bokeh.plotting import output_notebook
import folium
import ggplot as gg
from ggplot import ggplot
from IPython.html.widgets import interact
import matplotlib.pyplot as plt
import mpld3
import numpy as np
import pandas as pd
import vincent
In [3]:
%matplotlib inline
mpld3.enable_notebook()
# bearcart.initialize_notebook()
vincent.initialize_notebook()
# folium.initialize_notebook()
# axis_color = 'black'
axis_color = '#d0d0d0'
In [4]:
df = pd.read_csv('data/USGS_WindTurbine_201307_cleaned.csv')
df.head()
Out[4]:
In [5]:
ws = pd.read_table('data/CO_WS_2011_2012.txt')
ws = ws.set_index('Date & Time Stamp')
ws.index = ws.index.to_datetime()
ws.head()
Out[5]:
In [6]:
# Rotor Diameter vs. Turbine Manufacturer
mf_grouped = df.groupby('Turbine Manufacturer')
mean_grouped = mf_grouped.mean().dropna()
mean_rd = mean_grouped.sort('Rotor Diameter')['Rotor Diameter']
rotor_diam = vincent.Bar(mean_rd)
rotor_diam.axis_titles(x='Turbine Manufacturer', y='Rotor Diameter')
# The Hard Way
from vincent.axes import AxisProperties
from vincent.properties import PropertySet
from vincent.values import ValueRef
for axis in rotor_diam.axes:
axis.properties = AxisProperties()
for prop in ['ticks', 'axis', 'major_ticks', 'minor_ticks']:
setattr(axis.properties, prop, PropertySet(stroke=ValueRef(value=axis_color)))
axis.properties.title = PropertySet(font_size=ValueRef(value=20),
fill=ValueRef(value=axis_color))
axis.properties.labels = PropertySet(fill=ValueRef(value=axis_color))
rotor_diam.axes[0].properties.labels.angle = ValueRef(value=50)
rotor_diam.axes[0].properties.labels.align = ValueRef(value='left')
rotor_diam.axes[0].properties.title.dy = ValueRef(value=115)
rotor_diam.scales[2].range = ['#b48ead']
rotor_diam
Out[6]:
In [7]:
# Total Turbine Count
turbine_ct = mf_grouped.count().dropna().sort('Unique ID', ascending=False)['Unique ID']
num_turbines = (vincent.Bar(turbine_ct[:25])
.axis_titles(x='Turbine Manufacturer',
y='Number of Turbines in the US')
.colors(range_=['#6a9fb5']))
# Shortcuts!
def lighten_axes(vis, x_offset=50):
(vis.common_axis_properties(color=axis_color, title_size=20)
.x_axis_properties(label_angle=50, label_align='left',
title_offset=x_offset)
.y_axis_properties(title_offset=-40))
# If Area Chart
# num_turbines.scales[0].type = 'ordinal'
lighten_axes(num_turbines)
num_turbines
Out[7]:
In [8]:
# Turbine Count vs. Date
grouped_date = df.groupby(['Online Year', 'Turbine Manufacturer'])
by_year = grouped_date.count()['Unique ID'].reset_index()
by_year['Online Year'] = pd.to_datetime(by_year['Online Year'], coerce=True)
by_year = by_year.rename(columns={'Unique ID': 'Turbine Count'}).dropna()
by_year = by_year.pivot(index='Online Year', columns='Turbine Manufacturer', values='Turbine Count')
by_year = by_year[turbine_ct[:10].index.tolist()]
online_by_year = (vincent.StackedArea(by_year)
.axis_titles(x='Date', y='Turbine Count')
.legend(title='Turbine Manufacturer', text_color=axis_color)
.colors(range_=['#ac4142', '#d28445', '#f4bf75', '#90a959',
'#75b5aa', '#6a9fb5', '#aa759f', '#8f5536']))
lighten_axes(online_by_year, x_offset=30)
online_by_year
Out[8]:
In [9]:
height_diam = (vincent.GroupedBar(mean_grouped[['Tower/Hub Height', 'Rotor Diameter']]
.sort(['Rotor Diameter', 'Tower/Hub Height'], ascending=False))
.axis_titles(x='Turbine Manufacturer', y='Meters')
.legend(title='Parameters', text_color=axis_color)
.colors(range_=['#f4bf75', '#75b5aa']))
lighten_axes(height_diam, 100)
height_diam
Out[9]:
In [10]:
november_2011 = ws['2011-11-15':'2011-12-01']
ws_line = (vincent.Line(november_2011['WS1_50mMean'])
.axis_titles(x='Date', y='Wind Speed (m/s)')
.colors(range_=['#d28445']))
lighten_axes(ws_line, x_offset=30)
ws_line
Out[10]:
In [11]:
november_2011.head(3)
Out[11]:
In [12]:
# Rotor Diameter vs. Power
min_heights = df[df['Rotor Diameter'] > 10]
diameter_vs_mw = (vincent.Scatter(min_heights[['Turbine MW', 'Rotor Diameter']], iter_idx='Turbine MW')
.axis_titles(x='Power (MW)', y='Rotor Diameter (m)')
.colors(range_=['#75b5aa']))
lighten_axes(diameter_vs_mw, x_offset=30)
diameter_vs_mw
Out[12]:
In [13]:
import seaborn as sb
sb.set(rc={'text.color': axis_color, 'axes.labelcolor': axis_color})
rvm = df[['Rotor Diameter', 'Turbine MW', 'Turbine Manufacturer']].dropna()
row_mask = rvm['Turbine Manufacturer'].isin(['Vestas', 'Siemens', 'GE'])
rvm = rvm[row_mask]
rvm['Rotor Diameter'] = rvm['Rotor Diameter'].apply(lambda x: int(5 * round(float(x)/5)))
grouped_rvm = rvm.groupby(['Rotor Diameter', 'Turbine Manufacturer'])
rvm = grouped_rvm.mean().reset_index()
sb.lmplot("Rotor Diameter", "Turbine MW", col="Turbine Manufacturer",
hue="Turbine Manufacturer", data=rvm)
Out[13]:
In [14]:
sb.set_palette('husl')
nov_cols = november_2011.columns.tolist()
@interact
def sb_jointplot(x=nov_cols, y=nov_cols):
sb.jointplot(x, y, data=november_2011, size=9)
In [15]:
sb.jointplot('WD1_49mMean', 'WD2_38mMean', data=november_2011, size=9, kind='kde')
Out[15]:
In [16]:
import scipy.stats as stats
dists = ['Rayleigh', 'Weibull', 'Normal']
@interact
def plot_sb_dist(column=ws.columns.tolist(), dist=dists):
plt.figure(figsize=(10, 8))
dist_map = {
'Rayleigh': stats.rayleigh,
'Weibull': stats.exponweib,
'Normal': stats.norm,
}
sb.distplot(ws[column], fit=dist_map[dist])