Skip to main content
コードになるように修正
ソース リンク
raccy
  • 8,272
  • 1
  • 14
  • 41

上記の通りですがプログラムとターミナルをご提示します。

# coding: UTF-8

from sklearn import datasets, externals, linear_model, model_selection
import time

print('MNISTの取得:', end='', flush=True)
mnist = datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print('完了')

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('訓練データ:', train_data.shape)
print('テストデータ:', test_data.shape)

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

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

print('テスト結果:')
predict = model.predict(test_data)
count = [[0 for i in range(10)] for j in range(10)]
for i in range(TEST_SIZE):
    count[int(predict[i])][int(test_label[i])] += 1
print('正解   ', end='')
for i in range(10):
    print('   [{0}]'.format(i), end='')
print()
for i in range(10):
    print('予測[{0}]'.format(i), end='')
    for j in range(10):
        print('{0:6d}'.format(count[i][j]), end='')
    print()

print('正解率:', model.score(test_data, test_label)*100, '%')

ターミナルは以下の通りです。

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

インターネットで調べてみましたがよくわかりません。お願いします。

上記の通りですがプログラムとターミナルをご提示します。

# coding: UTF-8

from sklearn import datasets, externals, linear_model, model_selection
import time

print('MNISTの取得:', end='', flush=True)
mnist = datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print('完了')

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('訓練データ:', train_data.shape)
print('テストデータ:', test_data.shape)

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

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

print('テスト結果:')
predict = model.predict(test_data)
count = [[0 for i in range(10)] for j in range(10)]
for i in range(TEST_SIZE):
    count[int(predict[i])][int(test_label[i])] += 1
print('正解   ', end='')
for i in range(10):
    print('   [{0}]'.format(i), end='')
print()
for i in range(10):
    print('予測[{0}]'.format(i), end='')
    for j in range(10):
        print('{0:6d}'.format(count[i][j]), end='')
    print()

print('正解率:', model.score(test_data, test_label)*100, '%')

ターミナルは以下の通りです。

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

インターネットで調べてみましたがよくわかりません。お願いします。

上記の通りですがプログラムとターミナルをご提示します。

# coding: UTF-8

from sklearn import datasets, externals, linear_model, model_selection
import time

print('MNISTの取得:', end='', flush=True)
mnist = datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print('完了')

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('訓練データ:', train_data.shape)
print('テストデータ:', test_data.shape)

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

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

print('テスト結果:')
predict = model.predict(test_data)
count = [[0 for i in range(10)] for j in range(10)]
for i in range(TEST_SIZE):
    count[int(predict[i])][int(test_label[i])] += 1
print('正解   ', end='')
for i in range(10):
    print('   [{0}]'.format(i), end='')
print()
for i in range(10):
    print('予測[{0}]'.format(i), end='')
    for j in range(10):
        print('{0:6d}'.format(count[i][j]), end='')
    print()

print('正解率:', model.score(test_data, test_label)*100, '%')

ターミナルは以下の通りです。

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

インターネットで調べてみましたがよくわかりません。お願いします。

装飾しました。タグを追加しました。
ソース リンク
nekketsuuu
  • 2.4万
  • 12
  • 53
  • 117
# coding: UTF-8 

from sklearn import datasets, externals, linear_model, model_selection
import time

print('MNISTの取得:', end='', flush=True)
mnist = datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print('完了')

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('訓練データ:', train_data.shape)
print('テストデータ:', test_data.shape)

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

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

print('テスト結果:')
predict = model.predict(test_data)
count = [[0 for i in range(10)] for j in range(10)]
for i in range(TEST_SIZE):
    count[int(predict[i])][int(test_label[i])] += 1
print('正解   ', end='')
for i in range(10):
    print('   [{0}]'.format(i), end='')
print()
for i in range(10):
    print('予測[{0}]'.format(i), end='')
    for j in range(10):
        print('{0:6d}'.format(count[i][j]), end='')
    print()

print('正解率:', model.score(test_data, test_label)*100, '%')

from sklearn import datasets, externals, linear_model, model_selection
import time

print('MNISTの取得:', end='', flush=True)
mnist = datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print('完了')

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('訓練データ:', train_data.shape)
print('テストデータ:', test_data.shape)

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

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

print('テスト結果:')
predict = model.predict(test_data)
count = [[0 for i in range(10)] for j in range(10)]
for i in range(TEST_SIZE):
count[int(predict[i])][int(test_label[i])] += 1
print('正解 ', end='')
for i in range(10):
print(' [{0}]'.format(i), end='')
print()
for i in range(10):
print('予測[{0}]'.format(i), end='')
for j in range(10):
print('{0:6d}'.format(count[i][j]), end='')
print()

print('正解率:', model.score(test_data, test_label)*100, '%')

ターミナルは以下の通りです。

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

File "lr_train.py", line 6
print('MNISTの取得:', end='', flush=True)
^
SyntaxError: invalid syntax

# coding: UTF-8

from sklearn import datasets, externals, linear_model, model_selection
import time

print('MNISTの取得:', end='', flush=True)
mnist = datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print('完了')

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('訓練データ:', train_data.shape)
print('テストデータ:', test_data.shape)

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

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

print('テスト結果:')
predict = model.predict(test_data)
count = [[0 for i in range(10)] for j in range(10)]
for i in range(TEST_SIZE):
count[int(predict[i])][int(test_label[i])] += 1
print('正解 ', end='')
for i in range(10):
print(' [{0}]'.format(i), end='')
print()
for i in range(10):
print('予測[{0}]'.format(i), end='')
for j in range(10):
print('{0:6d}'.format(count[i][j]), end='')
print()

print('正解率:', model.score(test_data, test_label)*100, '%')

ターミナルは以下の通りです。

ShouzounoiMac:WakaruPython syamaguchi$ python lr_train.py

File "lr_train.py", line 6
print('MNISTの取得:', end='', flush=True)
^
SyntaxError: invalid syntax

# coding: UTF-8 

from sklearn import datasets, externals, linear_model, model_selection
import time

print('MNISTの取得:', end='', flush=True)
mnist = datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print('完了')

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('訓練データ:', train_data.shape)
print('テストデータ:', test_data.shape)

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

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

print('テスト結果:')
predict = model.predict(test_data)
count = [[0 for i in range(10)] for j in range(10)]
for i in range(TEST_SIZE):
    count[int(predict[i])][int(test_label[i])] += 1
print('正解   ', end='')
for i in range(10):
    print('   [{0}]'.format(i), end='')
print()
for i in range(10):
    print('予測[{0}]'.format(i), end='')
    for j in range(10):
        print('{0:6d}'.format(count[i][j]), end='')
    print()

print('正解率:', model.score(test_data, test_label)*100, '%')

ターミナルは以下の通りです。

ShouzounoiMac:WakaruPython syamaguchi$ python lr_train.py
  File "lr_train.py", line 6
    print('MNISTの取得:', end='', flush=True)
                                  ^
SyntaxError: invalid syntax
ソース リンク

『わかるPython』中のプログラムをターミナルから実行しようとするとSyntaxErrorとなります。

上記の通りですがプログラムとターミナルをご提示します。

# coding: UTF-8

from sklearn import datasets, externals, linear_model, model_selection
import time

print('MNISTの取得:', end='', flush=True)
mnist = datasets.fetch_mldata('MNIST original', data_home='.')
data, label = mnist.data, mnist.target
print('完了')

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('訓練データ:', train_data.shape)
print('テストデータ:', test_data.shape)

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

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

print('テスト結果:')
predict = model.predict(test_data)
count = [[0 for i in range(10)] for j in range(10)]
for i in range(TEST_SIZE):
count[int(predict[i])][int(test_label[i])] += 1
print('正解 ', end='')
for i in range(10):
print(' [{0}]'.format(i), end='')
print()
for i in range(10):
print('予測[{0}]'.format(i), end='')
for j in range(10):
print('{0:6d}'.format(count[i][j]), end='')
print()

print('正解率:', model.score(test_data, test_label)*100, '%')

ターミナルは以下の通りです。

ShouzounoiMac:WakaruPython syamaguchi$ python lr_train.py

File "lr_train.py", line 6
print('MNISTの取得:', end='', flush=True)
^
SyntaxError: invalid syntax

インターネットで調べてみましたがよくわかりません。お願いします。