SyntaxError when trying to run a program from a terminal in Understanding Python.

Asked 2 years ago, Updated 2 years ago, 39 views

As mentioned above, I will show you the program and terminal.

#coding:UTF-8

from sklearn import data sets, externals, linear_model, model_selection
import time

print('Get MNIST:',end=',flush=True)
mnist=datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print ('Completed')

Train_SIZE=600
TEST_SIZE=100

t=model_selection.train_test_split(
    data, label, train_size=TRAIN_SIZE, test_size=TEST_SIZE)
train_data, test_data, train_label, test_label=t
print('Training data:',train_data.shape')
print('Test data:', test_data.shape')

print('Learning:', end=', flush=True)
old=time.time()
model=linear_model.LogisticRegression().fit(train_data,train_label)
print(time.time()-old, 'seconds')

externals.joblib.dump(model, 'lr.model')

print('Test Results:')
predict=model.predict(test_data)
count = [[0 for i in range(10)] for jin range(10)]
for i in range (TEST_SIZE):
    count[int(predict[i])][int(test_label[i])]+=1
print('Answer', end=')
for i in range (10):
    print('[{0}]'.format(i), end=')
print()
for i in range (10):
    print('Predicted [{0}]'.format(i), end=')
    for jin range (10):
        print('{0:6d}'.format(count[i][j]), end=')
    print()

print('Answer Rate:', model.score(test_data, test_label)*100, '%')

The terminals are as follows.

ShouzounoiMac: WakaruPython syamaguchi$python lr_train.py
  File "lr_train.py", line 6
    print('Get MNIST:',end=',flush=True)
                                  ^
SyntaxError: invalid syntax

I looked it up on the Internet, but I'm not sure.Please.

python python3

2022-09-30 18:13

1 Answers

Maybe it's because it's running in a version earlier than Python 3.3. Please use Python 3.3 or later.

$python
Python 2.7.12 (default, Nov 12 2018, 14:36:49) 
[GCC 5.4.0 20160609] on linux 2
Type "help", "copyright", "credits" or "license" for more information.
>> print ('Get MNIST:', end=', flush=True)
  File "<stdin>", line 1
    print('Get MNIST:',end=',flush=True)
                                  ^
SyntaxError: invalid syntax

$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>> print ('Get MNIST:', end=', flush=True)
Retrieving MNIST: > > > 

Reference
https://docs.python.org/3.3/library/functions.html#print

Changed in version 3.3: Added the flush keyword argument.


2022-09-30 18:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.