Commit 70c7431e authored by Wichit Sombat's avatar Wichit Sombat

initial work

parents
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"SVC(C=0.1, cache_size=200, class_weight=None, coef0=0.0,\n",
" decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf',\n",
" max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
" tol=0.001, verbose=False)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Standard scientific Python imports\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Import datasets, classifiers and performance metrics\n",
"from sklearn import datasets, svm, metrics\n",
"from PIL import Image\n",
"import numpy as np\n",
"import math\n",
"\n",
"#เตรียมข้อมูลโดยการ load ข้อมูลจากไฟล์ภาพ ในโฟลเดอร์ images/numbers/\n",
"digit_data = list()\n",
"digit_label = list()\n",
"for eachNum in range(0,10):\n",
" #print eachNum\n",
" for furtherNum in range(1,10):\n",
" #print(str(eachNum))\n",
" imgFilePath = 'images/numbers/'+str(eachNum)+'.'+str(furtherNum)+'.png'\n",
" ei = Image.open(imgFilePath)\n",
" eiar = np.array(ei)\n",
" eiarl = str(eiar.tolist())\n",
"\n",
" data = np.zeros(64)\n",
" len(eiar[0])\n",
" count = 0\n",
" for i in range(len(eiar)):\n",
" for j in range(len(eiar[i])):\n",
" #ทำ Normalize ด้วยการ (R+G+B) หาร 765 มาจาก (255+255+255)\n",
" data[count] = math.floor(((np.sum(eiar[i][j][:3])/765)))\n",
" count+=1\n",
" digit_data.append( data )\n",
" digit_label.append( eachNum )\n",
"#ข้อมูลสำหรับนำไปฝึก\n",
"digit_data = np.array(digit_data) #ข้อมูล\n",
"digit_label = np.array(digit_label) # label ของข้อมูล\n",
"\n",
"# Create a classifier: a support vector classifier\n",
"classifier = svm.SVC(gamma=0.001, C=.1)\n",
"\n",
"# We learn the digits on the first half of the digits\n",
"classifier.fit(digit_data, digit_label)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,\n",
" 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5,\n",
" 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,\n",
" 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digit_label"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#การทดสอบ\n",
"i = Image.open('images/t5.png')\n",
"i = Image.open('images/t7.png')\n",
"iar = np.array(i)\n",
"\n",
"test_data = np.zeros(64)\n",
"len(iar[0])\n",
"count = 0\n",
"for i in range(len(iar)):\n",
" for j in range(len(iar[i])):\n",
" test_data[count] = math.floor(((np.sum(iar[i][j][:3])/765)))\n",
" #print(test_data[count])\n",
" count+=1\n",
" #print('----')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([7])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predicted = classifier.predict([test_data])\n",
"predicted"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Classification report for classifier SVC(C=0.1, cache_size=200, class_weight=None, coef0=0.0,\n",
" decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf',\n",
" max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
" tol=0.001, verbose=False):\n",
" precision recall f1-score support\n",
"\n",
" 0 1.00 0.89 0.94 9\n",
" 1 0.82 1.00 0.90 9\n",
" 2 1.00 0.89 0.94 9\n",
" 3 0.69 1.00 0.82 9\n",
" 4 1.00 0.78 0.88 9\n",
" 5 1.00 0.78 0.88 9\n",
" 6 1.00 0.78 0.88 9\n",
" 7 1.00 0.89 0.94 9\n",
" 8 0.73 0.89 0.80 9\n",
" 9 0.70 0.78 0.74 9\n",
"\n",
"avg / total 0.89 0.87 0.87 90\n",
"\n",
"\n",
"Confusion matrix:\n",
"[[8 0 0 0 0 0 0 0 0 1]\n",
" [0 9 0 0 0 0 0 0 0 0]\n",
" [0 0 8 1 0 0 0 0 0 0]\n",
" [0 0 0 9 0 0 0 0 0 0]\n",
" [0 0 0 0 7 0 0 0 0 2]\n",
" [0 0 0 2 0 7 0 0 0 0]\n",
" [0 0 0 0 0 0 7 0 2 0]\n",
" [0 1 0 0 0 0 0 8 0 0]\n",
" [0 0 0 1 0 0 0 0 8 0]\n",
" [0 1 0 0 0 0 0 0 1 7]]\n",
"\n",
"accuracy = 0.866666666667\n"
]
}
],
"source": [
"#วัดประสิทธิภาพ\n",
"from sklearn.metrics import accuracy_score\n",
"expected = digit_label\n",
"predicted = classifier.predict(digit_data)\n",
"print(\"Classification report for classifier %s:\\n%s\\n\"\n",
" % (classifier, metrics.classification_report(expected, predicted)))\n",
"print(\"Confusion matrix:\\n%s\" % metrics.confusion_matrix(expected, predicted))\n",
"print(\"\\naccuracy = \",accuracy_score(digit_label, predicted))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fitting the classifier to the training set\n",
"done in 0.436s\n",
"Best estimator found by grid search:\n",
"SVC(C=1000.0, cache_size=200, class_weight='balanced', coef0=0.0,\n",
" decision_function_shape='ovr', degree=3, gamma=0.0005, kernel='rbf',\n",
" max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
" tol=0.001, verbose=False)\n"
]
}
],
"source": [
"#optimize parameter\n",
"from time import time\n",
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.svm import SVC\n",
"# #############################################################################\n",
"# Train a SVM classification model\n",
"\n",
"print(\"Fitting the classifier to the training set\")\n",
"t0 = time()\n",
"param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],\n",
" 'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }\n",
"clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)\n",
"clf = clf.fit(digit_data, digit_label)\n",
"print(\"done in %0.3fs\" % (time() - t0))\n",
"print(\"Best estimator found by grid search:\")\n",
"print(clf.best_estimator_)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
# Agenda
13:00 - 13:20 Computer Science Program
13:20 - 14:20 [Session-1] Introduction to Python
what is programming?
variables and data types [text and numbers]
input/output
module and calling functions
14:20 - 14:30 Break
14:30 - 15:00 [Session-2] Awesome Jupyter Notebook
15:00 - 15:30 [Session-3] Writing window app with pyqt5
15:30 - 16:00 [Session-4] Writing matchine learning app
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment