I'm having trouble reading Python's introduction to machine learning.
P119 6.2.2 Learning
When I ran trial_handsign_SVM.py, I received the following error message:
Run the script as follows:
>>run trial_handsign_SVM.py./data/my_learn8/./data/my_test2/
SyntaxError: invalid syntax
is displayed for a command prompt.Incidentally, when python Spyder
ran the code exactly as it was, the error was Index Error: list index out of range
.What should I do with the code to solve this problem?Also, is there any difference between the two errors?Thank you very much for your impudence.
The code is as follows:
#-*-coding:utf-8-*-
importos
import sys
import glob
import numpy as np
from image importio
from sklearn import datasets
IMAGE_SIZE=40
COLOR_BYTE=3
CATEGORY_NUM = 6
## Load image files classified into directory labeled (0~)
## The input path is the top directory of the label name.
defload_handimage(path):
# Get list of files
files=glob.glob(os.path.join(path, '*/*.png')))
# Gain image and label space
images=np.ndarray(len(files), IMAGE_SIZE, IMAGE_SIZE,
COLOR_BYTE), dtype=np.uint8)
labels=np.ndarray(len(files), dtype=np.int)
# Load Images and Labels
for idx, file in enumerate (files):
# image loading
image=io.imread(file)
images [idx] = image
# Obtain label from directory name
label=os.path.split(os.path.dirname(file))[-1]
labels [idx] = int(label)
# Match the format of other data sets in scikit-learn
flat_data=images.reshape(-1,IMAGE_SIZE*IMAGE_SIZE*COLOR_BYTE))
images=flat_data.view()
return data.base.Bunch(data=flat_data,
target=labels.astype(np.int),
target_names=np.range(CATEGORY_NUM),
images=images,
DESCR=None)
from sklearn import svm, metrics
## Specify a directory of learning data and a directory of test data
if__name__=='__main__':
argvs=sys.argv
train_path=argvs[1]
test_path=argvs[2]
# Reading Learning Data
train=load_handimage(train_path)
# methodology:linear SVM
classifier = svm.LinearSVC()
# learning
classifier.fit (train.data, train.target)
# Loading Test Data
test=load_handimage(test_path)
# test
predicted=classifier.predict(test.data)
# result display
print("Accuracy:\n%s"%metrics.accuracy_score(test.target,predicted))
I thought the code was wrong
I downloaded it from here and tried to run it in my environment, but I was able to confirm that it worked fine.
http://www.ohmsha.co.jp/data/link/978-4-274-21963-4/
$pythontrial_handsign_SVM.py./data/my_learn8/./data/my_test2/
Accuracy:
0.916666666667
Also, is there any difference between the two errors?
Two errors in the questioner's question
Index Error:list index out of range
This error occurs when an element in the list that does not exist is accessed.
SyntaxError: invalid syntax
Python grammar error.
What should I do with the code to resolve it?
As I mentioned above, there is no problem with the code.
If you could tell me in more detail about the error that occurred when executing the code, I think I can help you solve the problem.
For example, I would appreciate it if you could paste the contents of the error as shown below.
$pythontrial_handsign_SVM.py./data/my_learn8/
Traceback (most recent call last):
File "trial_handsign_SVM.py", line 51, in <module>
test_path=argvs[2]
IndexError:list index out of range
© 2025 OneMinuteCode. All rights reserved.