Day: September 26, 2017

  • เตาะแตะไปกับ Docker ตอนที่ 12 Clone Docker Files From GitHub

    จากบล็อกเรื่อง “เตาะแตะไปกับ Docker” ในตอนที่แล้ว เราได้เรียนรู้ขั้นตอนในการสร้างและรัน LDAP services อย่างละเอียด ก่อนที่ผมจะเขียนบทความในตอนนี้ผมได้นำไฟล์ทุกไฟล์นั้นขึ้นไปไว้บน GitHub เพื่อให้พวกเราไม่ต้องสร้างไฟล์เหล่านั้นเอง แต่จะทำด้วยคำสั่ง git clone ครับ

    เรายังคงทดสอบด้วย VM โดยตั้งค่า network เป็น NAT network เพราะว่า เราใช้ domain สมมติ คือ ldap.example.com และทดสอบในสภาพแวดล้อมที่เป็นเครือข่ายภายใน

    ขั้นตอนดังนี้

    1. ติดตั้ง Ubuntu 16.04 64 bit แล้วติดตั้ง docker 17.06.2-ce แล้วต้องเปลี่ยนชื่อ hostname เป็น ldap.example.com จากนั้นรีบูตเครื่อง

    2. ติดตั้ง docker-compose 1.8.0-2 (ถ้ายังไม่ได้ทำ) ด้วยคำสั่งดังนี้

    sudo apt install docker-compose

    3. ทำการ Clone project จาก github ด้วยคำสั่ง

    git clone https://github.com/woonpsu/ex1-openldap.git

    4. ทำคำสั่งตามนี้

    cd ex1-openldap
    docker-compose up -d

    หมายเหตุ
    หากยังไม่มี image ชื่อ ubuntu:16.04 ก็จะใช้เวลานานสักหน่อยเมื่อรันคำสั่ง docker-compose เพราะว่าจะมีการสร้าง image ubuntu:16.04 ให้ใหม่

    5. ใช้คำสั่งเหล่านี้เพื่อตรวจสอบ

    docker images
    docker ps
    docker volume ls

    6. เพิ่ม users ตัวอย่าง

    sudo apt install ldap-utils
    ldapadd -H ldap://ldap.example.com -f ./openldap/src/create-users.ldif -x -D "cn=admin,dc=example,dc=com" -w 123456

    7. ทดสอบเข้าใช้งานเว็บจากเครื่องใน NAT network เดียวกัน หรือที่เครื่อง VM server ที่ผมได้ติดตั้ง Desktop Environment ไว้แล้ว ก็ต้องเปลี่ยนจาก text เป็น graphic ด้วยคำสั่ง

    $ startx

    เข้า browser ไปที่

    http://ldap.example.com:8080/phpldapadmin/

     

    สำหรับวิธีการทำ GitHub Repository ชื่อ ex1-openldap ของผมอย่างคร่าว ๆ คือ
    1. เข้า browser ไป sign up แบบ free ที่ github.com ด้วย username ชือ woonpsu

    2. สร้าง Repository ชื่อ ex1-openldap

    3. ไปที่เครื่อง server ใช้คำสั่ง

    git clone https://github.com/woonpsu/ex1-openldap.git

    4. เตรียมไฟล์

    cd ex1-openldap
    git init

    5. สร้างไฟล์ docker-compose.yml และไดเรกทอรี openldap กับ phpldapadmin ให้เสร็จ

    6. ทำการส่งขึ้นไปไว้บน github

    git add .
    git commit -m “This is a first commit”
    git push -u origin master

    ใส่ username และ password ของเราที่ sign up กับ github

     

    หมายเหตุ

    • หากต้องการใช้ VM ที่ผมเตรียมไว้ จะไม่ต้องติดตั้ง Ubuntu server ก็ download docker-vm file size 1.2G แล้ว import เข้า Oracle VM Virtualbox ทดสอบดูได้ (เข้าด้วย mama/123456)

     

  • Machine Learning #01 – Python with iris dataset

    ในบทความนี้ จะแนะนำวิธีการสร้างกระบวนการ Machine Learning ด้วย Python โดยใช้ iris dataset ตั้งแต่การโหลดข้อมูล, สร้าง  Model,  Cross Validation, วัด Accuracy และการนำ Model ไปใช้งาน

    เพื่อความสะดวกในการเรียนรู้ เราจะเลือกใช้ Anaconda ซึ่งเป็น Python Data Science Platform ซึ่งจะรวบรวมเครื่องมือ และ Library ที่จำเป็นต่อการพัฒนา โดยสามารถเลือก Download รุ่นที่เหมาะกับระบบปฏบัติการของท่านได้ที่ https://www.anaconda.com/download/

    สามารถ Clone Repository ตัวอย่างทั้งหมดที่กล่าวถึงในบทความนี้ได้จาก https://github.com/nagarindkx/pythonml

    และ แนะนำให้ใช้งาน jupyter-notebook เพื่อสะดวกในการเรียนรู้

    บทความนี้ใช้ Notebook: 01 – SVM with iris dataset.ipynb

     

    เริ่มจาก import dataset “iris” จาก SciKit

    ซึ่งเป็น dataset ตัวอย่างทีดี ในการสร้างระบบ Predict ชนิดของดอกไม้ จากการป้อนค่า ความกว้างและความยาวของกลีบดอก Iris (รายละเอียดอ่านได้จาก https://en.wikipedia.org/wiki/Iris_flower_data_set) ซึ่งเป็นการวัดความกว้าง และ ความยาวของกลีบดอก ของดอก “iris” (sepal width, sepal length, petal width, petal length) ใน 3 Spicy

    Image Source: https://en.wikipedia.org/wiki/Iris_flower_data_set

    ชุด iris dataset นี้ มักจะใช้ในการเริ่มต้นเรียนรู้ กระบวนการสร้าง Machine Learning เพื่อการ Classification โดยในตัวอย่างนี้จะใช้ Support Vector Machine (SVM) โดยเมื่อสร้างและ Train Model เสร็จแล้ว สามารถนำ Model นี้ไปใช้ในการ จำแนก Species ได้ โดยการระบุ ความกว้างและความยาวดังกล่าว แล้วระบบจะตอบมาได้ว่า เป็น Species ใด

    ในการเริ่มต้นนี้ เราจะใช้ iris dataset ที่มาพร้อมกับ SciKit  (sklearn) ซึ่งเป็น Machine Learning Package ในภาษา Python (ซึ่งติดตั้งมาในชุดของ Anaconda เรียบร้อยแล้ว)

    นำเข้าข้อมูล

    from sklearn import datasets
    iris = datasets.load_iris()

    สำรวจข้อมูล

    print(iris.data)
    print(iris.target)
    print(iris.data.shape)
    print(iris.target.shape)

    ใช้งาน SVM (Support Vector Machine)

    สร้าง SVC (Support Vector Classification) เพื่อทำการ Training ด้วยคำสั่ง fit โดยใส่ค่า data และ target ลงไป

    from sklearn import svm
    clf = svm.SVC()
    clf.fit(iris.data, iris.target)
    

    ผลที่ได้คือ

    SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
      decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
      max_iter=-1, probability=False, random_state=None, shrinking=True,
      tol=0.001, verbose=False)

    ทดลองทำการ Predict

    ด้วยคำสั่ง predict แล้วใส่ Array ข้อมูลลงไป

    print(clf.predict([[ 6.3 , 2.5, 5., 1.9]]))

    ซึ่งระบบจะตอบออกมาเป็น

    [2]

    ต้องการแสดงผลเป็นชื่อของ Target

    ต้องทำในขั้นตอน fit ดังนี้

    clf.fit(iris.data, iris.target_names[ iris.target])
    print(clf.predict([[ 6.3 , 2.5, 5., 1.9]]))

    ผลที่ได้คือ

    ['virginica']

    ทำการ Cross Validation

    โดยแบ่งข้อมูลเป็นสองส่วน คือ ส่วน Train และ Test ทั้ง X และ Y จากนั้น ใช้ Function “fit” ในการ Train

    from sklearn.model_selection import train_test_split
    x_train,x_test,y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.4 , random_state=0)
    
    print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
    
    clf.fit(x_train, y_train)

    ผลที่ได้คือ

    (90, 4) (60, 4) (90,) (60,)
    SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape=None, degree=3, gamma='auto', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)

    ทดสอบความแม่นยำ

    ด้วยการ นำข้อมูลส่วน Test ไปทดสอบใน Model ด้วย Function “score”

    print(clf.score(x_test, y_test))

    ผลที่ได้คือ

    0.95

    นำ Model ที่สร้างเสร็จไปใช้ต่อ

    ใช้กระบวนการ pickle หรือ serialization

    import pickle
    pickle.dump(clf, open("myiris.pickle","wb"))

    ซึ่ง ก็จะได้ไฟล์ “myiris.pickle” สามารถนำไปใช้งานต่อได้

    ในบทความต่อไป จะกล่าวถึง การนำ Model นี้ไปใช้งานผ่าน django REST Framework