<jupyter_start><jupyter_text>Open Class Python Python ITBMakers Institute13 February 2018  About Us Since 12 January 2016First open class: September 2016    About Me  <NAME> [about.me/arisbw](http://about.me/arisbw) | [@arisbw](http://twitter.com/arisbw)  Outline * [Introduction to Python](Introduction-to-Python)* [Let's use Pandas!](Pandas!)* [Introduction to Machine Learning](a-bit-Intro-to-Machine-Learning)* [What the hell is KNN?](a-bit-of-KNN) You can also view this repository from: [https://github.com/arisbw/open-class-makers](https://github.com/arisbw/open-class-makers)  Introduction to Python Say hello to dat snake. 🐍<jupyter_code>print("Hello Python!")<jupyter_output>Hello Python!<jupyter_text>Object types in Python   Number If you forgot what the simple math looks like...<jupyter_code>1 + 1
8 - 1
10 * 2
35 / 5
5 // 3
5.0 // 3.0
2**4<jupyter_output><empty_output><jupyter_text>String<jupyter_code>s = "ayam"
len(s)
s[0]<jupyter_output><empty_output><jupyter_text>Or, we can do this!<jupyter_code>s[-1]
s[-2]<jupyter_output><empty_output><jupyter_text>We can also slice that string<jupyter_code>s[0:3]
s[1:]
s[:5]
s[:-1]<jupyter_output><empty_output><jupyter_text>Or add with another string<jupyter_code>s + ' ' +'euy'
s*4<jupyter_output><empty_output><jupyter_text>Find and replace substring<jupyter_code>s.find('yam')
s.replace('ayam', 'bebek')
line = 'aaa,bbb,ccccc,dd'
line.split(',')
s
s.upper()
s.isalpha()<jupyter_output><empty_output><jupyter_text>List<jupyter_code>L = [123, 'spam', 1.23]
L[0]
L[:-1]
L + [4, 5, 6]
L
L.append('hehe')
L
L.pop(1)
L<jupyter_output><empty_output><jupyter_text>Dictionaries<jupyter_code>D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}
D['food']
D['quantity']
D = {}
D['name'] = 'Bob'# Create keys by assignment
D['job']  = 'dev'
D['age']  = 40
D<jupyter_output><empty_output><jupyter_text>What if...<jupyter_code>a = 90

if a > 50:
    print("Lebih dari 50")
else:
    print("Kurang dari 50")
a = 3
"seeep" if a > 2 else 2<jupyter_output><empty_output><jupyter_text>Now, what ifs...<jupyter_code>a = 400

if a < 100:
    print("Kurang dari 100")
elif a >= 100 and a <= 500:
    print("Antara 100 dan 500")
else:
    print("Lebih dari 500")<jupyter_output>Antara 100 dan 500<jupyter_text>... say that again?<jupyter_code>a = 5

for i in range(a):
    if a > 10:
        print("Lebih dari 10")
    else:
        print("Kurang dari 10")<jupyter_output>Kurang dari 10
Kurang dari 10
Kurang dari 10
Kurang dari 10
Kurang dari 10<jupyter_text>or<jupyter_code>a = 0

while a < 5:
    print("Gampang euy")
    a += 1<jupyter_output>Gampang euy
Gampang euy
Gampang euy
Gampang euy
Gampang euy<jupyter_text>Functions!<jupyter_code>def tambah(a=0,b=0):
    c = a+b
    return c

tambah(2,3)<jupyter_output><empty_output><jupyter_text>Pandas!  Initialize libraries that we will use later.<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline<jupyter_output><empty_output><jupyter_text>First, let's download the data we need.<jupyter_code>!curl https://raw.githubusercontent.com/fivethirtyeight/data/master/bob-ross/elements-by-episode.csv -O elements-by-episode.csv<jupyter_output>% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 66316  100 66316    0     0   121k      0 --:--:-- --:--:-- --:--:--  121k

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0<jupyter_text>Or you can use wget if you use linux.<jupyter_code>df  =  pd.read_csv("elements-by-episode.csv")<jupyter_output><empty_output><jupyter_text>Let's peek the data.<jupyter_code>df.head()
df.columns
df.shape<jupyter_output><empty_output><jupyter_text>Summary of the data:<jupyter_code>df.describe()<jupyter_output><empty_output><jupyter_text>Transpose the data<jupyter_code>df.T.head()<jupyter_output><empty_output><jupyter_text>Sort the data<jupyter_code>df.sort_values(by='TREES',ascending=False).head()<jupyter_output><empty_output><jupyter_text>Select and Slice<jupyter_code>df.EPISODE[1:5]
df['EPISODE'][1:5]
df.loc[0:2]
df.loc[100:106,['WATERFALL','TREE']]
df[(df.BRIDGE>0) & (df.WINTER>0)]<jupyter_output><empty_output><jupyter_text><jupyter_code>df1 = df.iloc[:,3:6]
df1.head()
df1.plot.hist()
df1.iloc[30:100,:].plot(figsize=(8, 8))<jupyter_output><empty_output><jupyter_text>a bit Intro to Machine Learning  What is Machine Learning?  Machine Learning: *... a method of teaching computers to make and improve predictions or behaviors based on some data.* Basically, there are 3`*` main task in machine learning: * Supervised learning: your examples must be *labeled* * Unsupervised learning: your examples are *not labeled* * Reinforcement learning: based on *reward* and *punishment*  Why Machine Learning?  I mean... *why now?*  So many algorithms that are really good right now: * SVM * Tree-based algorithms: Random forest, XGboost, catboost * Deep learning * etc.      a bit of KNN DISCLAIMER: This is the ***oversimplified*** version A classification and regression method. In this case, we will explore in the classification problem.<jupyter_code>from sklearn import neighbors
import numpy as np
%matplotlib inline
import seaborn<jupyter_output><empty_output><jupyter_text>Create dataset:<jupyter_code>training_data = pd.DataFrame()

training_data['test_1'] = [0.3051,0.4949,0.6974,0.3769,0.2231,0.341,0.4436,0.5897,0.6308,0.5]
training_data['test_2'] = [0.5846,0.2654,0.2615,0.4538,0.4615,0.8308,0.4962,0.3269,0.5346,0.6731]
training_data['outcome'] = ['win','win','win','win','win','loss','loss','loss','loss','loss']

training_data.head()<jupyter_output><empty_output><jupyter_text>Plot the data<jupyter_code>seaborn.lmplot('test_1', 'test_2', data=training_data, fit_reg=False,hue="outcome", scatter_kws={"marker": "D","s": 100})<jupyter_output><empty_output><jupyter_text>Convert data to np.arrays<jupyter_code>X = training_data.as_matrix(columns=['test_1', 'test_2'])
y = np.array(training_data['outcome'])<jupyter_output><empty_output><jupyter_text>Training!<jupyter_code>clf = neighbors.KNeighborsClassifier(3, weights = 'uniform')
trained_model = clf.fit(X, y)<jupyter_output><empty_output><jupyter_text>*drum rolls* 🥁<jupyter_code>trained_model.score(X, y)<jupyter_output><empty_output><jupyter_text>Check the model result to new data point<jupyter_code>x_test = np.array([[.4,.6]])
trained_model.predict(x_test)<jupyter_output><empty_output><jupyter_text>Check result<jupyter_code>trained_model.predict_proba(x_test)<jupyter_output><empty_output><jupyter_text>Best k?  Load libraries<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.model_selection import GridSearchCV<jupyter_output><empty_output><jupyter_text>Load Iris Flower Data<jupyter_code>iris = datasets.load_iris()
X = iris.data
y = iris.target<jupyter_output><empty_output><jupyter_text>Standardize Data<jupyter_code>standardizer = StandardScaler()

X_std = standardizer.fit_transform(X)<jupyter_output><empty_output><jupyter_text>Create knn model<jupyter_code>knn = KNeighborsClassifier(n_neighbors=5, metric='euclidean', n_jobs=-1).fit(X_std, y)<jupyter_output><empty_output><jupyter_text>Create search space of possible value of k<jupyter_code>pipe = Pipeline([('standardizer', standardizer), ('knn', knn)])

search_space = [{'knn__n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}]<jupyter_output><empty_output><jupyter_text>Search!<jupyter_code>clf = GridSearchCV(pipe, search_space, cv=5, verbose=0).fit(X_std, y)<jupyter_output><empty_output><jupyter_text>Show best k<jupyter_code>clf.best_estimator_.get_params()['knn__n_neighbors']<jupyter_output><empty_output><jupyter_text>References * [Learn X in Y Minutes](https://learnxinyminutes.com/docs/python3/)* [A Statistical Analysis of the Work of Bob Ross](https://fivethirtyeight.com/features/a-statistical-analysis-of-the-work-of-bob-ross/)* [Learning Python](https://www.safaribooksonline.com/library/view/learning-python-3rd/9780596513986/)* [10 Minutes to pandas](https://pandas.pydata.org/pandas-docs/stable/10min.htmlplotting)* [What's Driving the Machine Learning Explosion?](https://hbr.org/2017/07/whats-driving-the-machine-learning-explosion)* [What is the difference between supervised learning and unsupervised learning?](https://stackoverflow.com/questions/1832076/what-is-the-difference-between-supervised-learning-and-unsupervised-learning) * [Data Never Sleeps 5.0](https://www.domo.com/learn/data-never-sleeps-5)* [K-Nearest Neighbors Classification](https://chrisalbon.com/machine_learning/nearest_neighbors/k-nearest_neighbors_classifer/)* [What is the best way to explain the bias-variance trade-off in layman’s terms?](https://www.quora.com/What-is-the-best-way-to-explain-the-bias-variance-trade-off-in-layman%E2%80%99s-terms)  Thank you! 🙇‍🙏  Misc.<jupyter_code>%mkdir image\paintings
%cd image\paintings
#Credit to: <NAME> (https://gist.github.com/jeffThompson/6d4c45f89dc907925775972e72d9cf00)

num_images=403

import urllib.request
from tqdm import *

for i in tqdm(range(1, num_images+1)):
    try:
        url = 'http://www.twoinchbrush.com/images/painting' + str(i) + '.png'
        filename = ('%03d' % (i,)) + '.png'
        urllib.request.urlretrieve(url, filename)
    except:
        print('- ERROR!')<jupyter_output>100%|████████████████████████████████████████████████████████████████████████████████| 403/403 [14:12<00:00,  2.37s/it]
{'fraction_non_alphanumeric': 0.15680625578763246, 'fraction_numerical': 0.042802757485337996, 'mean_word_length': 5.63481228668942, 'pattern_counts': {'":': 2, '<': 157, '<?xml version=': 0, '>': 159, 'https://': 13, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '39', 'dir': 'jupyter-structured-clean-dedup', 'id': '167569', 'license': 'MIT', 'n_tokens_mistral': 4108, 'n_tokens_neox': 3742, 'n_words': 688, 'path': 'notebooks/Open-Class-Makers-Institute.ipynb', 'repo_name': 'python-itb/open-class-makers', 'size': '9736'}