From 48a174a688258229bd941a1275ee39b050961500 Mon Sep 17 00:00:00 2001 From: cclauss Date: Sun, 30 Sep 2018 07:31:10 +0200 Subject: [PATCH] Use print() function in Python 2 and 3 --- src/py2.x/dl/bp.py | 11 +++--- src/py2.x/dl/cnn.py | 15 ++++---- src/py2.x/dl/fc.py | 13 +++---- src/py2.x/dl/linear_unit.py | 11 +++--- src/py2.x/dl/lstm.py | 5 +-- src/py2.x/dl/mnist.py | 9 ++--- src/py2.x/dl/perceptron.py | 11 +++--- src/py2.x/dl/recursive.py | 13 +++---- src/py2.x/dl/rnn.py | 5 +-- src/py2.x/ml/1.MLFoundation/NumPy.py | 13 +++---- src/py2.x/ml/10.kmeans/kMeans.py | 25 ++++++------- src/py2.x/ml/11.Apriori/apriori.py | 35 ++++++++++--------- .../ml/12.FrequentPattemTree/fpGrowth.py | 29 +++++++-------- src/py2.x/ml/13.PCA/pca.py | 5 +-- src/py2.x/ml/14.SVD/svdRecommend.py | 31 ++++++++-------- .../ml/15.BigData_MapReduce/mrMeanMapper.py | 3 +- .../ml/15.BigData_MapReduce/mrMeanReducer.py | 3 +- src/py2.x/ml/15.BigData_MapReduce/pegasos.py | 5 +-- .../ml/15.BigData_MapReduce/proximalSVM.py | 5 +-- src/py2.x/ml/2.KNN/kNN.py | 21 +++++------ src/py2.x/ml/2.KNN/sklearn-knn-demo.py | 1 + src/py2.x/ml/3.DecisionTree/DTSklearn.py | 7 ++-- src/py2.x/ml/3.DecisionTree/DecisionTree.py | 13 +++---- .../skelearn_dts_regressor_demo.py | 1 + .../sklearn_dts_classify_demo.py | 1 + src/py2.x/ml/4.NaiveBayes/bayes.py | 25 ++++++------- src/py2.x/ml/4.NaiveBayes/sklearn-nb-demo.py | 5 +-- src/py2.x/ml/5.Logistic/logistic.py | 5 +-- .../sklearn_logisticRegression_demo.py | 1 + src/py2.x/ml/6.SVM/sklearn-svm-demo.py | 9 ++--- src/py2.x/ml/6.SVM/svm-complete.py | 11 +++--- src/py2.x/ml/6.SVM/svm-complete_Non-Kernel.py | 11 +++--- src/py2.x/ml/6.SVM/svm-simple.py | 11 +++--- src/py2.x/ml/7.AdaBoost/adaboost.py | 15 ++++---- .../ml/7.AdaBoost/sklearn-adaboost-demo.py | 11 +++--- src/py2.x/ml/7.RandomForest/randomForest.py | 9 ++--- src/py2.x/ml/8.Regression/regression.py | 15 ++++---- .../8.Regression/sklearn-regression-demo.py | 1 + src/py2.x/ml/9.RegTrees/RTSklearn.py | 1 + src/py2.x/ml/9.RegTrees/regTrees.py | 9 ++--- .../ml/9.RegTrees/sklearn-regressTree-demo.py | 1 + src/py2.x/ml/9.RegTrees/treeExplore.py | 5 +-- .../ml/15.BigData_MapReduce/mrMeanMapper.py | 2 ++ .../ml/15.BigData_MapReduce/mrMeanReducer.py | 2 ++ .../test.py" | 2 ++ tools/python2libsvm.py | 5 +-- 46 files changed, 248 insertions(+), 199 deletions(-) diff --git a/src/py2.x/dl/bp.py b/src/py2.x/dl/bp.py index 9bd0d9a14..4fa120bf5 100644 --- a/src/py2.x/dl/bp.py +++ b/src/py2.x/dl/bp.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: UTF-8 -*- +from __future__ import print_function import random from numpy import * @@ -273,7 +274,7 @@ def dump(self): ''' # 遍历层的所有的节点 nodes,将节点信息打印出来 for node in self.nodes: - print node + print(node) # Connection 对象类,主要负责记录连接的权重,以及这个连接所关联的上下游的节点 @@ -396,7 +397,7 @@ def dump(self): None ''' for conn in self.connections: - print conn + print(conn) # Network 对象,提供相应 API @@ -743,7 +744,7 @@ def gradient_check(network, sample_feature, sample_label): expected_gradient = (error2 - error1) / (2 * epsilon) # 打印 - print 'expected gradient: \t%f\nactual gradient: \t%f' % (expected_gradient, actual_gradient) + print('expected gradient: \t%f\nactual gradient: \t%f' % (expected_gradient, actual_gradient)) def train_data_set(): @@ -804,7 +805,7 @@ def test(network, data): # 对测试数据进行预测 predict_data = network.predict(norm_data) # 将结果打印出来 - print '\ttestdata(%u)\tpredict(%u)' % (data, normalizer.denorm(predict_data)) + print('\ttestdata(%u)\tpredict(%u)' % (data, normalizer.denorm(predict_data))) def correct_ratio(network): @@ -821,7 +822,7 @@ def correct_ratio(network): for i in range(256): if normalizer.denorm(network.predict(normalizer.norm(i))) == i: correct += 1.0 - print 'correct_ratio: %.2f%%' % (correct / 256 * 100) + print('correct_ratio: %.2f%%' % (correct / 256 * 100)) def gradient_check_test(): diff --git a/src/py2.x/dl/cnn.py b/src/py2.x/dl/cnn.py index de81257eb..f56122f66 100644 --- a/src/py2.x/dl/cnn.py +++ b/src/py2.x/dl/cnn.py @@ -2,6 +2,7 @@ # -*- coding: UTF-8 -*- +from __future__ import print_function import numpy as np from activators import ReluActivator, IdentityActivator @@ -387,14 +388,14 @@ def init_test(): def test(): a, b, cl = init_test() cl.forward(a) - print cl.output_array + print(cl.output_array) def test_bp(): a, b, cl = init_test() cl.backward(a, b, IdentityActivator()) cl.update() - print cl.filters[0] - print cl.filters[1] + print(cl.filters[0]) + print(cl.filters[1]) def gradient_check(): @@ -427,8 +428,8 @@ def gradient_check(): err2 = error_function(cl.output_array) expect_grad = (err1 - err2) / (2 * epsilon) cl.filters[0].weights[d,i,j] += epsilon - print 'weights(%d,%d,%d): expected - actural %f - %f' % ( - d, i, j, expect_grad, cl.filters[0].weights_grad[d,i,j]) + print('weights(%d,%d,%d): expected - actural %f - %f' % ( + d, i, j, expect_grad, cl.filters[0].weights_grad[d,i,j])) def init_pool_test(): @@ -456,10 +457,10 @@ def init_pool_test(): def test_pool(): a, b, mpl = init_pool_test() mpl.forward(a) - print 'input array:\n%s\noutput array:\n%s' % (a, mpl.output_array) + print('input array:\n%s\noutput array:\n%s' % (a, mpl.output_array)) def test_pool_bp(): a, b, mpl = init_pool_test() mpl.backward(a, b) - print 'input array:\n%s\nsensitivity array:\n%s\ndelta array:\n%s' % (a, b, mpl.delta_array) \ No newline at end of file + print('input array:\n%s\nsensitivity array:\n%s\ndelta array:\n%s' % (a, b, mpl.delta_array)) \ No newline at end of file diff --git a/src/py2.x/dl/fc.py b/src/py2.x/dl/fc.py index 569ca26d6..eb3dd7ab0 100644 --- a/src/py2.x/dl/fc.py +++ b/src/py2.x/dl/fc.py @@ -2,6 +2,7 @@ # -*- coding: UTF-8 -*- +from __future__ import print_function import random import numpy as np from activators import SigmoidActivator, IdentityActivator @@ -57,7 +58,7 @@ def update(self, learning_rate): self.b += learning_rate * self.b_grad def dump(self): - print 'W: %s\nb:%s' % (self.W, self.b) + print('W: %s\nb:%s' % (self.W, self.b)) # 神经网络类 @@ -149,8 +150,8 @@ def gradient_check(self, sample_feature, sample_label): err2 = self.loss(sample_label, output) expect_grad = (err1 - err2) / (2 * epsilon) fc.W[i,j] += epsilon - print 'weights(%d,%d): expected - actural %.4e - %.4e' % ( - i, j, expect_grad, fc.W_grad[i,j]) + print('weights(%d,%d): expected - actural %.4e - %.4e' % ( + i, j, expect_grad, fc.W_grad[i,j])) from bp import train_data_set @@ -197,7 +198,7 @@ def correct_ratio(network): for i in range(256): if normalizer.denorm(network.predict(normalizer.norm(i))) == i: correct += 1.0 - print 'correct_ratio: %.2f%%' % (correct / 256 * 100) + print('correct_ratio: %.2f%%' % (correct / 256 * 100)) def test(): @@ -208,10 +209,10 @@ def test(): epoch = 10 for i in range(epoch): net.train(labels, data_set, rate, mini_batch) - print 'after epoch %d loss: %f' % ( + print('after epoch %d loss: %f' % ( (i + 1), net.loss(labels[-1], net.predict(data_set[-1])) - ) + )) rate /= 2 correct_ratio(net) diff --git a/src/py2.x/dl/linear_unit.py b/src/py2.x/dl/linear_unit.py index 7a8c7446e..c14f544c5 100644 --- a/src/py2.x/dl/linear_unit.py +++ b/src/py2.x/dl/linear_unit.py @@ -2,6 +2,7 @@ # -*- coding: UTF-8 -*- # 引入 Perceptron 类 +from __future__ import print_function from perceptron import Perceptron # 定义激活函数 f @@ -112,10 +113,10 @@ def plot(linear_unit): # 首先训练我们的线性单元 linear_unit = train_linear_unit() # 打印训练获得的权重 和 偏置 - print linear_unit + print(linear_unit) # 测试 - print 'Work 3.4 years, monthly salary = %.2f' % linear_unit.predict([3.4]) - print 'Work 15 years, monthly salary = %.2f' % linear_unit.predict([15]) - print 'Work 1.5 years, monthly salary = %.2f' % linear_unit.predict([1.5]) - print 'Work 6.3 years, monthly salary = %.2f' % linear_unit.predict([6.3]) + print('Work 3.4 years, monthly salary = %.2f' % linear_unit.predict([3.4])) + print('Work 15 years, monthly salary = %.2f' % linear_unit.predict([15])) + print('Work 1.5 years, monthly salary = %.2f' % linear_unit.predict([1.5])) + print('Work 6.3 years, monthly salary = %.2f' % linear_unit.predict([6.3])) plot(linear_unit) diff --git a/src/py2.x/dl/lstm.py b/src/py2.x/dl/lstm.py index 90039c82d..df4d7c8ee 100644 --- a/src/py2.x/dl/lstm.py +++ b/src/py2.x/dl/lstm.py @@ -2,6 +2,7 @@ # -*- coding: UTF-8 -*- +from __future__ import print_function import matplotlib.pyplot as plt import numpy as np from cnn import element_wise_op @@ -319,8 +320,8 @@ def gradient_check(): err2 = error_function(lstm.h_list[-1]) expect_grad = (err1 - err2) / (2 * epsilon) lstm.Wfh[i,j] += epsilon - print 'weights(%d,%d): expected - actural %.4e - %.4e' % ( - i, j, expect_grad, lstm.Wfh_grad[i,j]) + print('weights(%d,%d): expected - actural %.4e - %.4e' % ( + i, j, expect_grad, lstm.Wfh_grad[i,j])) return lstm diff --git a/src/py2.x/dl/mnist.py b/src/py2.x/dl/mnist.py index af61e0f06..cdbbe8c33 100644 --- a/src/py2.x/dl/mnist.py +++ b/src/py2.x/dl/mnist.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: UTF-8 -*- +from __future__ import print_function import struct from fc import * from datetime import datetime @@ -124,7 +125,7 @@ def show(sample): else: str += ' ' str += '\n' - print str + print(str) def get_result(vec): @@ -162,11 +163,11 @@ def train_and_evaluate(): while True: epoch += 1 network.train(train_labels, train_data_set, 0.01, 1) - print '%s epoch %d finished, loss %f' % (now(), epoch, - network.loss(train_labels[-1], network.predict(train_data_set[-1]))) + print('%s epoch %d finished, loss %f' % (now(), epoch, + network.loss(train_labels[-1], network.predict(train_data_set[-1])))) if epoch % 2 == 0: error_ratio = evaluate(network, test_data_set, test_labels) - print '%s after epoch %d, error ratio is %f' % (now(), epoch, error_ratio) + print('%s after epoch %d, error ratio is %f' % (now(), epoch, error_ratio)) if error_ratio > last_error_ratio: break else: diff --git a/src/py2.x/dl/perceptron.py b/src/py2.x/dl/perceptron.py index e0f5a4916..ed58b2466 100644 --- a/src/py2.x/dl/perceptron.py +++ b/src/py2.x/dl/perceptron.py @@ -3,6 +3,7 @@ # 神经元 / 感知器 +from __future__ import print_function class Perceptron(): ''' Desc: @@ -178,9 +179,9 @@ def train_and_perceptron(): # 训练 and 感知器 and_perceptron = train_and_perceptron() # 打印训练获得的权重 - print and_perceptron + print(and_perceptron) # 测试 - print '1 and 1 = %d' % and_perceptron.predict([1, 1]) - print '0 and 0 = %d' % and_perceptron.predict([0, 0]) - print '1 and 0 = %d' % and_perceptron.predict([1, 0]) - print '0 and 1 = %d' % and_perceptron.predict([0, 1]) \ No newline at end of file + print('1 and 1 = %d' % and_perceptron.predict([1, 1])) + print('0 and 0 = %d' % and_perceptron.predict([0, 0])) + print('1 and 0 = %d' % and_perceptron.predict([1, 0])) + print('0 and 1 = %d' % and_perceptron.predict([0, 1])) \ No newline at end of file diff --git a/src/py2.x/dl/recursive.py b/src/py2.x/dl/recursive.py index cdd5314dc..530b4c0ce 100644 --- a/src/py2.x/dl/recursive.py +++ b/src/py2.x/dl/recursive.py @@ -2,6 +2,7 @@ # -*- coding: UTF-8 -*- +from __future__ import print_function import numpy as np from activators import IdentityActivator @@ -114,11 +115,11 @@ def calc_gradient(self, parent): return W_grad, b_grad def dump(self, **kwArgs): - print 'root.data: %s' % self.root.data - print 'root.children_data: %s' % self.root.children_data + print('root.data: %s' % self.root.data) + print('root.children_data: %s' % self.root.children_data) if kwArgs.has_key('dump_grad'): - print 'W_grad: %s' % self.W_grad - print 'b_grad: %s' % self.b_grad + print('W_grad: %s' % self.W_grad) + print('b_grad: %s' % self.b_grad) def data_set(): @@ -167,8 +168,8 @@ def gradient_check(): err2 = error_function(rnn.root.data) expect_grad = (err1 - err2) / (2 * epsilon) rnn.W[i,j] += epsilon - print 'weights(%d,%d): expected - actural %.4e - %.4e' % ( - i, j, expect_grad, rnn.W_grad[i,j]) + print('weights(%d,%d): expected - actural %.4e - %.4e' % ( + i, j, expect_grad, rnn.W_grad[i,j])) return rnn diff --git a/src/py2.x/dl/rnn.py b/src/py2.x/dl/rnn.py index c21e9ba07..0bbba4abd 100644 --- a/src/py2.x/dl/rnn.py +++ b/src/py2.x/dl/rnn.py @@ -2,6 +2,7 @@ # -*- coding: UTF-8 -*- +from __future__ import print_function import numpy as np from cnn import element_wise_op from activators import ReluActivator, IdentityActivator @@ -143,8 +144,8 @@ def gradient_check(): err2 = error_function(rl.state_list[-1]) expect_grad = (err1 - err2) / (2 * epsilon) rl.W[i,j] += epsilon - print 'weights(%d,%d): expected - actural %f - %f' % ( - i, j, expect_grad, rl.gradient[i,j]) + print('weights(%d,%d): expected - actural %f - %f' % ( + i, j, expect_grad, rl.gradient[i,j])) def test(): diff --git a/src/py2.x/ml/1.MLFoundation/NumPy.py b/src/py2.x/ml/1.MLFoundation/NumPy.py index 2ec1aaa4a..577a2d804 100644 --- a/src/py2.x/ml/1.MLFoundation/NumPy.py +++ b/src/py2.x/ml/1.MLFoundation/NumPy.py @@ -7,6 +7,7 @@ Author: Peter Harrington/1988/片刻 GitHub: https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import random, mat, eye @@ -43,15 +44,15 @@ TraRandMat = randMat.T ArrRandMat = randMat.A # 输出结果 -print 'randArray=(%s) \n' % type(randArray), randArray -print 'randMat=(%s) \n' % type(randMat), randMat -print 'invRandMat=(%s) \n' % type(invRandMat), invRandMat -print 'TraRandMat=(%s) \n' % type(TraRandMat), TraRandMat -print 'ArrRandMat=(%s) \n' % type(ArrRandMat), ArrRandMat +print('randArray=(%s) \n' % type(randArray), randArray) +print('randMat=(%s) \n' % type(randMat), randMat) +print('invRandMat=(%s) \n' % type(invRandMat), invRandMat) +print('TraRandMat=(%s) \n' % type(TraRandMat), TraRandMat) +print('ArrRandMat=(%s) \n' % type(ArrRandMat), ArrRandMat) # 矩阵和逆矩阵 进行求积 (单位矩阵,对角线都为1嘛,理论上4*4的矩阵其他的都为0) myEye = randMat*invRandMat # 误差 -print myEye - eye(4) +print(myEye - eye(4)) ''' 如果上面的代码运行没有问题,说明numpy安装没有问题 diff --git a/src/py2.x/ml/10.kmeans/kMeans.py b/src/py2.x/ml/10.kmeans/kMeans.py index 590188b8e..60903bc6a 100644 --- a/src/py2.x/ml/10.kmeans/kMeans.py +++ b/src/py2.x/ml/10.kmeans/kMeans.py @@ -7,6 +7,7 @@ @author: Peter Harrington/那伊抹微笑 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import * @@ -62,7 +63,7 @@ def kMeans(dataMat, k, distMeas=distEclud, createCent=randCent): clusterChanged = True # 簇改变 clusterAssment[ i, :] = minIndex, minDist**2 # 更新簇分配结果为最小质心的 index(索引),minDist(最小距离)的平方 - print centroids + print(centroids) for cent in range(k): # 更新质心 ptsInClust = dataMat[nonzero( clusterAssment[:, 0].A == cent)[0]] # 获取该簇中的所有点 @@ -90,7 +91,7 @@ def biKMeans(dataMat, k, distMeas=distEclud): sseNotSplit = sum( clusterAssment[nonzero(clusterAssment[:, 0].A != i)[0], 1]) # 将未参与二分 kMeans 分配结果中的平方和的距离进行求和 - print "sseSplit, and notSplit: ", sseSplit, sseNotSplit + print("sseSplit, and notSplit: ", sseSplit, sseNotSplit) if (sseSplit + sseNotSplit) < lowestSSE: bestCentToSplit = i bestNewCents = centroidMat @@ -101,8 +102,8 @@ def biKMeans(dataMat, k, distMeas=distEclud): centList) # 调用二分 kMeans 的结果,默认簇是 0,1. 当然也可以改成其它的数字 bestClustAss[nonzero(bestClustAss[:, 0].A == 0)[0], 0] = bestCentToSplit # 更新为最佳质心 - print 'the bestCentToSplit is: ', bestCentToSplit - print 'the len of bestClustAss is: ', len(bestClustAss) + print('the bestCentToSplit is: ', bestCentToSplit) + print('the len of bestClustAss is: ', len(bestClustAss)) # 更新质心列表 centList[bestCentToSplit] = bestNewCents[0, :].tolist()[ 0] # 更新原质心 list 中的第 i 个质心为使用二分 kMeans 后 bestNewCents 的第一个质心 @@ -119,16 +120,16 @@ def testBasicFunc(): # 测试 randCent() 函数是否正常运行。 # 首先,先看一下矩阵中的最大值与最小值 - print 'min(dataMat[:, 0])=', min(dataMat[:, 0]) - print 'min(dataMat[:, 1])=', min(dataMat[:, 1]) - print 'max(dataMat[:, 1])=', max(dataMat[:, 1]) - print 'max(dataMat[:, 0])=', max(dataMat[:, 0]) + print('min(dataMat[:, 0])=', min(dataMat[:, 0])) + print('min(dataMat[:, 1])=', min(dataMat[:, 1])) + print('max(dataMat[:, 1])=', max(dataMat[:, 1])) + print('max(dataMat[:, 0])=', max(dataMat[:, 0])) # 然后看看 randCent() 函数能否生成 min 到 max 之间的值 - print 'randCent(dataMat, 2)=', randCent(dataMat, 2) + print('randCent(dataMat, 2)=', randCent(dataMat, 2)) # 最后测试一下距离计算方法 - print ' distEclud(dataMat[0], dataMat[1])=', distEclud(dataMat[0], dataMat[1]) + print(' distEclud(dataMat[0], dataMat[1])=', distEclud(dataMat[0], dataMat[1])) def testKMeans(): @@ -140,7 +141,7 @@ def testKMeans(): # 运行结果(多次运行结果可能会不一样,可以试试,原因为随机质心的影响,但总的结果是对的, 因为数据足够相似) myCentroids, clustAssing = kMeans(dataMat, 4) - print 'centroids=', myCentroids + print('centroids=', myCentroids) def testBiKMeans(): @@ -149,7 +150,7 @@ def testBiKMeans(): centList, myNewAssments = biKMeans(dataMat, 3) - print 'centList=', centList + print('centList=', centList) if __name__ == "__main__": diff --git a/src/py2.x/ml/11.Apriori/apriori.py b/src/py2.x/ml/11.Apriori/apriori.py index 34eadebe2..aa6e95acd 100644 --- a/src/py2.x/ml/11.Apriori/apriori.py +++ b/src/py2.x/ml/11.Apriori/apriori.py @@ -8,6 +8,7 @@ @author: Peter/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function print(__doc__) from numpy import * @@ -171,7 +172,7 @@ def calcConf(freqSet, H, supportData, brl, minConf=0.7): conf = supportData[freqSet]/supportData[freqSet-conseq] # 支持度定义: a -> b = support(a | b) / support(a). 假设 freqSet = frozenset([1, 3]), conseq = [frozenset([1])],那么 frozenset([1]) 至 frozenset([3]) 的可信度为 = support(a | b) / support(a) = supportData[freqSet]/supportData[freqSet-conseq] = supportData[frozenset([1, 3])] / supportData[frozenset([1])] if conf >= minConf: # 只要买了 freqSet-conseq 集合,一定会买 conseq 集合(freqSet-conseq 集合和 conseq集合 是全集) - print freqSet-conseq, '-->', conseq, 'conf:', conf + print(freqSet-conseq, '-->', conseq, 'conf:', conf) brl.append((freqSet-conseq, conseq, conf)) prunedH.append(conseq) return prunedH @@ -201,8 +202,8 @@ def rulesFromConseq(freqSet, H, supportData, brl, minConf=0.7): Hmp1 = aprioriGen(H, m+1) # 返回可信度大于最小可信度的集合 Hmp1 = calcConf(freqSet, Hmp1, supportData, brl, minConf) - print 'Hmp1=', Hmp1 - print 'len(Hmp1)=', len(Hmp1), 'len(freqSet)=', len(freqSet) + print('Hmp1=', Hmp1) + print('len(Hmp1)=', len(Hmp1), 'len(freqSet)=', len(freqSet)) # 计算可信度后,还有数据大于最小可信度的话,那么继续递归调用,否则跳出递归 if (len(Hmp1) > 1): # print '----------------------', Hmp1 @@ -251,11 +252,11 @@ def getActionIds(): for action in billDetail.actions: if action.level == 'House' and (action.stage == 'Passage' or action.stage == 'Amendment Vote'): actionId = int(action.actionId) - print 'bill: %d has actionId: %d' % (billNum, actionId) + print('bill: %d has actionId: %d' % (billNum, actionId)) actionIdList.append(actionId) billTitleList.append(line.strip().split('\t')[1]) except: - print "problem getting bill %d" % billNum + print("problem getting bill %d" % billNum) sleep(1) # delay to be polite return actionIdList, billTitleList @@ -269,7 +270,7 @@ def getTransList(actionIdList, billTitleList): #this will return a list of lists voteCount = 2 for actionId in actionIdList: sleep(3) - print 'getting votes for actionId: %d' % actionId + print('getting votes for actionId: %d' % actionId) try: voteList = votesmart.votes.getBillActionVotes(actionId) for vote in voteList: @@ -284,7 +285,7 @@ def getTransList(actionIdList, billTitleList): #this will return a list of lists elif vote.action == 'Yea': transDict[vote.candidateName].append(voteCount + 1) except: - print "problem getting actionId: %d" % actionId + print("problem getting actionId: %d" % actionId) voteCount += 2 return transDict, itemMeaning @@ -303,33 +304,33 @@ def getTransList(actionIdList, billTitleList): #this will return a list of lists def testApriori(): # 加载测试数据集 dataSet = loadDataSet() - print 'dataSet: ', dataSet + print('dataSet: ', dataSet) # Apriori 算法生成频繁项集以及它们的支持度 L1, supportData1 = apriori(dataSet, minSupport=0.7) - print 'L(0.7): ', L1 - print 'supportData(0.7): ', supportData1 + print('L(0.7): ', L1) + print('supportData(0.7): ', supportData1) - print '->->->->->->->->->->->->->->->->->->->->->->->->->->->->' + print('->->->->->->->->->->->->->->->->->->->->->->->->->->->->') # Apriori 算法生成频繁项集以及它们的支持度 L2, supportData2 = apriori(dataSet, minSupport=0.5) - print 'L(0.5): ', L2 - print 'supportData(0.5): ', supportData2 + print('L(0.5): ', L2) + print('supportData(0.5): ', supportData2) def testGenerateRules(): # 加载测试数据集 dataSet = loadDataSet() - print 'dataSet: ', dataSet + print('dataSet: ', dataSet) # Apriori 算法生成频繁项集以及它们的支持度 L1, supportData1 = apriori(dataSet, minSupport=0.5) - print 'L(0.7): ', L1 - print 'supportData(0.7): ', supportData1 + print('L(0.7): ', L1) + print('supportData(0.7): ', supportData1) # 生成关联规则 rules = generateRules(L1, supportData1, minConf=0.5) - print 'rules: ', rules + print('rules: ', rules) def main(): # 测试 Apriori 算法 diff --git a/src/py2.x/ml/12.FrequentPattemTree/fpGrowth.py b/src/py2.x/ml/12.FrequentPattemTree/fpGrowth.py index 781689e86..b48eb2868 100644 --- a/src/py2.x/ml/12.FrequentPattemTree/fpGrowth.py +++ b/src/py2.x/ml/12.FrequentPattemTree/fpGrowth.py @@ -12,6 +12,7 @@ @author: Peter/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function print(__doc__) @@ -33,7 +34,7 @@ def disp(self, ind=1): """disp(用于将树以文本形式显示) """ - print ' '*ind, self.name, ' ', self.count + print(' '*ind, self.name, ' ', self.count) for child in self.children.values(): child.disp(ind+1) @@ -218,30 +219,30 @@ def mineTree(inTree, headerTable, minSup, preFix, freqItemList): # 通过value进行从小到大的排序, 得到频繁项集的key # 最小支持项集的key的list集合 bigL = [v[0] for v in sorted(headerTable.items(), key=lambda p: p[1])] - print '-----', sorted(headerTable.items(), key=lambda p: p[1]) - print 'bigL=', bigL + print('-----', sorted(headerTable.items(), key=lambda p: p[1])) + print('bigL=', bigL) # 循环遍历 最频繁项集的key,从小到大的递归寻找对应的频繁项集 for basePat in bigL: # preFix为newFreqSet上一次的存储记录,一旦没有myHead,就不会更新 newFreqSet = preFix.copy() newFreqSet.add(basePat) - print 'newFreqSet=', newFreqSet, preFix + print('newFreqSet=', newFreqSet, preFix) freqItemList.append(newFreqSet) - print 'freqItemList=', freqItemList + print('freqItemList=', freqItemList) condPattBases = findPrefixPath(basePat, headerTable[basePat][1]) - print 'condPattBases=', basePat, condPattBases + print('condPattBases=', basePat, condPattBases) # 构建FP-tree myCondTree, myHead = createTree(condPattBases, minSup) - print 'myHead=', myHead + print('myHead=', myHead) # 挖掘条件 FP-tree, 如果myHead不为空,表示满足minSup {所有的元素+(value, treeNode)} if myHead is not None: myCondTree.disp(1) - print '\n\n\n' + print('\n\n\n') # 递归 myHead 找出频繁项集 mineTree(myCondTree, myHead, minSup, newFreqSet, freqItemList) - print '\n\n\n' + print('\n\n\n') # import twitter @@ -305,7 +306,7 @@ def mineTree(inTree, headerTable, minSup, preFix, freqItemList): # print simpDat, '\n' # frozen set 格式化 并 重新装载 样本数据,对所有的行进行统计求和,格式: {行:出现次数} initSet = createInitSet(simpDat) - print initSet + print(initSet) # 创建FP树 # 输入:dist{行:出现次数}的样本数据 和 最小的支持度 @@ -315,14 +316,14 @@ def mineTree(inTree, headerTable, minSup, preFix, freqItemList): # 抽取条件模式基 # 查询树节点的,频繁子项 - print 'x --->', findPrefixPath('x', myHeaderTab['x'][1]) - print 'z --->', findPrefixPath('z', myHeaderTab['z'][1]) - print 'r --->', findPrefixPath('r', myHeaderTab['r'][1]) + print('x --->', findPrefixPath('x', myHeaderTab['x'][1])) + print('z --->', findPrefixPath('z', myHeaderTab['z'][1])) + print('r --->', findPrefixPath('r', myHeaderTab['r'][1])) # 创建条件模式基 freqItemList = [] mineTree(myFPtree, myHeaderTab, 3, set([]), freqItemList) - print freqItemList + print(freqItemList) # # 项目实战 # # 1.twitter项目案例 diff --git a/src/py2.x/ml/13.PCA/pca.py b/src/py2.x/ml/13.PCA/pca.py index d16cea77c..8f64de94c 100644 --- a/src/py2.x/ml/13.PCA/pca.py +++ b/src/py2.x/ml/13.PCA/pca.py @@ -7,6 +7,7 @@ Author: Peter Harrington/片刻 GitHub:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import * import matplotlib.pyplot as plt print(__doc__) @@ -129,7 +130,7 @@ def analyse_data(dataMat): 最后,我们可能会注意到有一些小的负值,他们主要源自数值误差应该四舍五入成0. ''' - print '主成分:%s, 方差占比:%s%%, 累积方差占比:%s%%' % (format(i+1, '2.0f'), format(line_cov_score/cov_all_score*100, '4.2f'), format(sum_cov_score/cov_all_score*100, '4.1f')) + print('主成分:%s, 方差占比:%s%%, 累积方差占比:%s%%' % (format(i+1, '2.0f'), format(line_cov_score/cov_all_score*100, '4.2f'), format(sum_cov_score/cov_all_score*100, '4.1f'))) if __name__ == "__main__": @@ -144,7 +145,7 @@ def analyse_data(dataMat): # 利用PCA对半导体制造数据降维 dataMat = replaceNanWithMean() - print shape(dataMat) + print(shape(dataMat)) # 分析数据 analyse_data(dataMat) # lowDmat, reconMat = pca(dataMat, 20) diff --git a/src/py2.x/ml/14.SVD/svdRecommend.py b/src/py2.x/ml/14.SVD/svdRecommend.py index 539fbc138..18c6d4abc 100644 --- a/src/py2.x/ml/14.SVD/svdRecommend.py +++ b/src/py2.x/ml/14.SVD/svdRecommend.py @@ -7,6 +7,7 @@ @author: Peter Harrington/山上有课树/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import linalg as la from numpy import * @@ -165,11 +166,11 @@ def svdEst(dataMat, user, simMeas, item): # 利用U矩阵将物品转换到低维空间中,构建转换后的物品(物品+4个主要的特征) xformedItems = dataMat.T * U[:, :4] * Sig4.I - print 'dataMat', shape(dataMat) - print 'U[:, :4]', shape(U[:, :4]) - print 'Sig4.I', shape(Sig4.I) - print 'VT[:4, :]', shape(VT[:4, :]) - print 'xformedItems', shape(xformedItems) + print('dataMat', shape(dataMat)) + print('U[:, :4]', shape(U[:, :4])) + print('Sig4.I', shape(Sig4.I)) + print('VT[:4, :]', shape(VT[:4, :])) + print('xformedItems', shape(xformedItems)) # 对于给定的用户,for循环在用户对应行的元素上进行遍历 # 这和standEst()函数中的for循环的目的一样,只不过这里的相似度计算时在低维空间下进行的。 @@ -180,7 +181,7 @@ def svdEst(dataMat, user, simMeas, item): # 相似度的计算方法也会作为一个参数传递给该函数 similarity = simMeas(xformedItems[item, :].T, xformedItems[j, :].T) # for 循环中加入了一条print语句,以便了解相似度计算的进展情况。如果觉得累赘,可以去掉 - print 'the %d and %d similarity is: %f' % (item, j, similarity) + print('the %d and %d similarity is: %f' % (item, j, similarity)) # 对相似度不断累加求和 simTotal += similarity # 对相似度及对应评分值的乘积求和 @@ -239,7 +240,7 @@ def analyse_data(Sigma, loopNum=20): 通常保留矩阵 80% ~ 90% 的能量,就可以得到重要的特征并取出噪声。 ''' - print '主成分:%s, 方差占比:%s%%' % (format(i+1, '2.0f'), format(SigmaI/SigmaSum*100, '4.2f')) + print('主成分:%s, 方差占比:%s%%' % (format(i+1, '2.0f'), format(SigmaI/SigmaSum*100, '4.2f'))) # 图像压缩函数 @@ -263,10 +264,10 @@ def printMat(inMat, thresh=0.8): for i in range(32): for k in range(32): if float(inMat[i, k]) > thresh: - print 1, + print(1, end=' ') else: - print 0, - print '' + print(0, end=' ') + print('') # 实现图像压缩,允许基于任意给定的奇异值数目来重构图像 @@ -280,7 +281,7 @@ def imgCompress(numSV=3, thresh=0.8): # 构建一个列表 myMat = imgLoadData('input/14.SVD/0_5.txt') - print "****original matrix****" + print("****original matrix****") # 对原始图像进行SVD分解并重构图像e printMat(myMat, thresh) @@ -296,7 +297,7 @@ def imgCompress(numSV=3, thresh=0.8): SigRecon = mat(eye(numSV) * Sigma[: numSV]) reconMat = U[:, :numSV] * SigRecon * VT[:numSV, :] - print "****reconstructed matrix using %d singular values *****" % numSV + print("****reconstructed matrix using %d singular values *****" % numSV) printMat(reconMat, thresh) @@ -338,12 +339,12 @@ def imgCompress(numSV=3, thresh=0.8): myMat = mat(loadExData3()) # print myMat # 计算相似度的第一种方式 - print recommend(myMat, 1, estMethod=svdEst) + print(recommend(myMat, 1, estMethod=svdEst)) # 计算相似度的第二种方式 - print recommend(myMat, 1, estMethod=svdEst, simMeas=pearsSim) + print(recommend(myMat, 1, estMethod=svdEst, simMeas=pearsSim)) # 默认推荐(菜馆菜肴推荐示例) - print recommend(myMat, 2) + print(recommend(myMat, 2)) """ # 利用SVD提高推荐效果 diff --git a/src/py2.x/ml/15.BigData_MapReduce/mrMeanMapper.py b/src/py2.x/ml/15.BigData_MapReduce/mrMeanMapper.py index e7a57603b..0654dede7 100644 --- a/src/py2.x/ml/15.BigData_MapReduce/mrMeanMapper.py +++ b/src/py2.x/ml/15.BigData_MapReduce/mrMeanMapper.py @@ -8,6 +8,7 @@ @author: Peter/ApacheCN-xy/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function import sys from numpy import mat, mean, power @@ -37,4 +38,4 @@ def read_input(file): # 第二行识标准错误输出,即对主节点作出的响应报告,表明本节点工作正常。 # 【这不就是面试的装逼重点吗?如何设计监听架构细节】注意:一个好的习惯是想标准错误输出发送报告。如果某任务10分钟内没有报告输出,则将被Hadoop中止。 print("%d\t%f\t%f" % (numInputs, mean(input), mean(sqInput))) # 计算均值 -print >> sys.stderr, "map report: still alive" +print("map report: still alive", file=sys.stderr) diff --git a/src/py2.x/ml/15.BigData_MapReduce/mrMeanReducer.py b/src/py2.x/ml/15.BigData_MapReduce/mrMeanReducer.py index 1dcb97a06..b998bbe6d 100644 --- a/src/py2.x/ml/15.BigData_MapReduce/mrMeanReducer.py +++ b/src/py2.x/ml/15.BigData_MapReduce/mrMeanReducer.py @@ -9,6 +9,7 @@ @author: Peter/ApacheCN-xy/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function import sys ''' @@ -43,4 +44,4 @@ def read_input(file): varSum = (cumSumSq - 2*mean_*cumVal + cumN*mean_*mean_)/cumN # 输出 数据总量,均值,平方的均值(方差) print ("数据总量:%d\t均值:%f\t方差:%f" % (cumN, mean_, varSum)) -print >> sys.stderr, "reduce report: still alive" +print("reduce report: still alive", file=sys.stderr) diff --git a/src/py2.x/ml/15.BigData_MapReduce/pegasos.py b/src/py2.x/ml/15.BigData_MapReduce/pegasos.py index 115a0fee7..632aa1c70 100644 --- a/src/py2.x/ml/15.BigData_MapReduce/pegasos.py +++ b/src/py2.x/ml/15.BigData_MapReduce/pegasos.py @@ -6,6 +6,7 @@ the input T is k*T in Batch Pegasos @author: Peter/ApacheCN-xy ''' +from __future__ import print_function from numpy import * @@ -32,7 +33,7 @@ def seqPegasos(dataSet, labels, lam, T): w = (1.0 - 1/t)*w + eta*labels[i]*dataSet[i, :] else: w = (1.0 - 1/t)*w - print w + print(w) return w @@ -81,7 +82,7 @@ def batchPegasos(dataSet, labels, lam, T, k): datMat = mat(datArr) # finalWs = seqPegasos(datMat, labelList, 2, 5000) finalWs = batchPegasos(datMat, labelList, 2, 50, 100) -print finalWs +print(finalWs) import matplotlib import matplotlib.pyplot as plt diff --git a/src/py2.x/ml/15.BigData_MapReduce/proximalSVM.py b/src/py2.x/ml/15.BigData_MapReduce/proximalSVM.py index 19fdb645c..f59b7da6c 100644 --- a/src/py2.x/ml/15.BigData_MapReduce/proximalSVM.py +++ b/src/py2.x/ml/15.BigData_MapReduce/proximalSVM.py @@ -6,6 +6,7 @@ @author: Peter/ApacheCN-xy/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function import base64 import pickle @@ -32,7 +33,7 @@ def map(key, value): # note: a single constant key "producedkey" sends to only one reducer # somewhat "atypical" due to low degree of parallism on reducer side - print "producedkey\t%s" % (producedvalue) + print("producedkey\t%s" % (producedvalue)) def reduce(key, values, mu=0.1): sumETE = None @@ -56,4 +57,4 @@ def reduce(key, values, mu=0.1): # note: omega = result[:-1] and gamma = result[-1] # but printing entire vector as output result = sumETE.I*sumETDe - print "%s\t%s" % (key, str(result.tolist())) + print("%s\t%s" % (key, str(result.tolist()))) diff --git a/src/py2.x/ml/2.KNN/kNN.py b/src/py2.x/ml/2.KNN/kNN.py index 71c04f735..22223bd5a 100644 --- a/src/py2.x/ml/2.KNN/kNN.py +++ b/src/py2.x/ml/2.KNN/kNN.py @@ -6,6 +6,7 @@ @author: Peter Harrington/羊三/小瑶 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import * # 导入科学计算包numpy和运算符模块operator import operator @@ -149,9 +150,9 @@ def test1(): 第一个例子演示 """ group, labels = createDataSet() - print str(group) - print str(labels) - print classify0([0.1, 0.1], group, labels, 3) + print(str(group)) + print(str(labels)) + print(classify0([0.1, 0.1], group, labels, 3)) # ---------------------------------------------------------------------------------------- @@ -229,15 +230,15 @@ def datingClassTest(): m = normMat.shape[0] # 设置测试的样本数量, numTestVecs:m表示训练样本的数量 numTestVecs = int(m * hoRatio) - print 'numTestVecs=', numTestVecs + print('numTestVecs=', numTestVecs) errorCount = 0.0 for i in range(numTestVecs): # 对数据测试 classifierResult = classify0(normMat[i, :], normMat[numTestVecs:m, :], datingLabels[numTestVecs:m], 3) - print "the classifier came back with: %d, the real answer is: %d" % (classifierResult, datingLabels[i]) + print("the classifier came back with: %d, the real answer is: %d" % (classifierResult, datingLabels[i])) if (classifierResult != datingLabels[i]): errorCount += 1.0 - print "the total error rate is: %f" % (errorCount / float(numTestVecs)) - print errorCount + print("the total error rate is: %f" % (errorCount / float(numTestVecs))) + print(errorCount) def img2vector(filename): @@ -282,10 +283,10 @@ def handwritingClassTest(): classNumStr = int(fileStr.split('_')[0]) vectorUnderTest = img2vector('input/2.KNN/testDigits/%s' % fileNameStr) classifierResult = classify0(vectorUnderTest, trainingMat, hwLabels, 3) - print "the classifier came back with: %d, the real answer is: %d" % (classifierResult, classNumStr) + print("the classifier came back with: %d, the real answer is: %d" % (classifierResult, classNumStr)) if (classifierResult != classNumStr): errorCount += 1.0 - print "\nthe total number of errors is: %d" % errorCount - print "\nthe total error rate is: %f" % (errorCount / float(mTest)) + print("\nthe total number of errors is: %d" % errorCount) + print("\nthe total error rate is: %f" % (errorCount / float(mTest))) if __name__ == '__main__': diff --git a/src/py2.x/ml/2.KNN/sklearn-knn-demo.py b/src/py2.x/ml/2.KNN/sklearn-knn-demo.py index 71f859f6c..ee38693bd 100644 --- a/src/py2.x/ml/2.KNN/sklearn-knn-demo.py +++ b/src/py2.x/ml/2.KNN/sklearn-knn-demo.py @@ -8,6 +8,7 @@ @author: 小瑶 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning """ +from __future__ import print_function print(__doc__) import numpy as np diff --git a/src/py2.x/ml/3.DecisionTree/DTSklearn.py b/src/py2.x/ml/3.DecisionTree/DTSklearn.py index 0e194cfc2..7e15e5081 100644 --- a/src/py2.x/ml/3.DecisionTree/DTSklearn.py +++ b/src/py2.x/ml/3.DecisionTree/DTSklearn.py @@ -2,6 +2,7 @@ # coding: utf8 # 原始链接: http://blog.csdn.net/lsldd/article/details/41223147 # 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning +from __future__ import print_function import numpy as np from sklearn import tree from sklearn.metrics import precision_recall_curve @@ -28,7 +29,7 @@ def createDataSet(): ''' 标签转换为0/1 ''' y[labels == 'fat'] = 1 - print data, '-------', x, '-------', labels, '-------', y + print(data, '-------', x, '-------', labels, '-------', y) return x, y @@ -41,7 +42,7 @@ def predict_train(x_train, y_train): # print(clf) clf.fit(x_train, y_train) ''' 系数反映每个特征的影响力。越大表示该特征在分类中起到的作用越大 ''' - print 'feature_importances_: %s' % clf.feature_importances_ + print('feature_importances_: %s' % clf.feature_importances_) '''测试结果的打印''' y_pre = clf.predict(x_train) @@ -105,7 +106,7 @@ def show_pdf(clf): ''' 拆分训练数据与测试数据, 80%做训练 20%做测试 ''' x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2) - print '拆分数据:', x_train, x_test, y_train, y_test + print('拆分数据:', x_train, x_test, y_train, y_test) # 得到训练的预测结果集 y_pre, clf = predict_train(x_train, y_train) diff --git a/src/py2.x/ml/3.DecisionTree/DecisionTree.py b/src/py2.x/ml/3.DecisionTree/DecisionTree.py index bad37eb5b..3cd8eb026 100755 --- a/src/py2.x/ml/3.DecisionTree/DecisionTree.py +++ b/src/py2.x/ml/3.DecisionTree/DecisionTree.py @@ -7,6 +7,7 @@ @author: Peter Harrington/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function print(__doc__) import operator from math import log @@ -171,7 +172,7 @@ def chooseBestFeatureToSplit(dataSet): # gain[信息增益]: 划分数据集前后的信息变化, 获取信息熵最大的值 # 信息增益是熵的减少或者是数据无序度的减少。最后,比较所有特征中的信息增益,返回最好特征划分的索引值。 infoGain = baseEntropy - newEntropy - print 'infoGain=', infoGain, 'bestFeature=', i, baseEntropy, newEntropy + print('infoGain=', infoGain, 'bestFeature=', i, baseEntropy, newEntropy) if (infoGain > bestInfoGain): bestInfoGain = infoGain bestFeature = i @@ -278,7 +279,7 @@ def classify(inputTree, featLabels, testVec): # 测试数据,找到根节点对应的label位置,也就知道从输入的数据的第几位来开始分类 key = testVec[featIndex] valueOfFeat = secondDict[key] - print '+++', firstStr, 'xxx', secondDict, '---', key, '>>>', valueOfFeat + print('+++', firstStr, 'xxx', secondDict, '---', key, '>>>', valueOfFeat) # 判断分枝是否结束: 判断valueOfFeat是否是dict类型 if isinstance(valueOfFeat, dict): classLabel = classify(valueOfFeat, featLabels, testVec) @@ -324,12 +325,12 @@ def fishTest(): import copy myTree = createTree(myDat, copy.deepcopy(labels)) - print myTree + print(myTree) # [1, 1]表示要取的分支上的节点位置,对应的结果值 - print classify(myTree, labels, [1, 1]) + print(classify(myTree, labels, [1, 1])) # 获得树的高度 - print get_tree_height(myTree) + print(get_tree_height(myTree)) # 画图可视化展现 dtPlot.createPlot(myTree) @@ -353,7 +354,7 @@ def ContactLensesTest(): lensesLabels = ['age', 'prescript', 'astigmatic', 'tearRate'] # 使用上面的创建决策树的代码,构造预测隐形眼镜的决策树 lensesTree = createTree(lenses, lensesLabels) - print lensesTree + print(lensesTree) # 画图可视化展现 dtPlot.createPlot(lensesTree) diff --git a/src/py2.x/ml/3.DecisionTree/skelearn_dts_regressor_demo.py b/src/py2.x/ml/3.DecisionTree/skelearn_dts_regressor_demo.py index 056f41928..5aee85b6c 100644 --- a/src/py2.x/ml/3.DecisionTree/skelearn_dts_regressor_demo.py +++ b/src/py2.x/ml/3.DecisionTree/skelearn_dts_regressor_demo.py @@ -8,6 +8,7 @@ @author: 小瑶 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning """ +from __future__ import print_function print(__doc__) diff --git a/src/py2.x/ml/3.DecisionTree/sklearn_dts_classify_demo.py b/src/py2.x/ml/3.DecisionTree/sklearn_dts_classify_demo.py index 39e4e8c78..ad3196bfc 100644 --- a/src/py2.x/ml/3.DecisionTree/sklearn_dts_classify_demo.py +++ b/src/py2.x/ml/3.DecisionTree/sklearn_dts_classify_demo.py @@ -8,6 +8,7 @@ @author: 小瑶 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning """ +from __future__ import print_function print(__doc__) import numpy as np diff --git a/src/py2.x/ml/4.NaiveBayes/bayes.py b/src/py2.x/ml/4.NaiveBayes/bayes.py index 9b59c36bb..267f85d8a 100755 --- a/src/py2.x/ml/4.NaiveBayes/bayes.py +++ b/src/py2.x/ml/4.NaiveBayes/bayes.py @@ -6,6 +6,7 @@ @author: Peter Harrington/羊三/小瑶 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import * """ p(xy)=p(x|y)p(y)=p(y|x)p(x) @@ -57,7 +58,7 @@ def setOfWords2Vec(vocabList, inputSet): if word in vocabList: returnVec[vocabList.index(word)] = 1 else: - print "the word: %s is not in my Vocabulary!" % word + print("the word: %s is not in my Vocabulary!" % word) return returnVec @@ -191,10 +192,10 @@ def testingNB(): # 5. 测试数据 testEntry = ['love', 'my', 'dalmation'] thisDoc = array(setOfWords2Vec(myVocabList, testEntry)) - print testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb) + print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb)) testEntry = ['stupid', 'garbage'] thisDoc = array(setOfWords2Vec(myVocabList, testEntry)) - print testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb) + print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb)) # ------------------------------------------------------------------------------------------ @@ -259,13 +260,13 @@ def spamTest(): wordVector = setOfWords2Vec(vocabList, docList[docIndex]) if classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 - print 'the errorCount is: ', errorCount - print 'the testSet length is :', len(testSet) - print 'the error rate is :', float(errorCount)/len(testSet) + print('the errorCount is: ', errorCount) + print('the testSet length is :', len(testSet)) + print('the error rate is :', float(errorCount)/len(testSet)) def testParseTest(): - print textParse(open('input/4.NaiveBayes/email/ham/1.txt').read()) + print(textParse(open('input/4.NaiveBayes/email/ham/1.txt').read())) # ----------------------------------------------------------------------------------- @@ -327,7 +328,7 @@ def localWords(feed1,feed0): wordVector=bagOfWords2VecMN(vocabList,docList[docIndex]) if classifyNB(array(wordVector),p0V,p1V,pSpam)!=classList[docIndex]: errorCount+=1 - print 'the error rate is:',float(errorCount)/len(testSet) + print('the error rate is:',float(errorCount)/len(testSet)) return vocabList,p0V,p1V @@ -340,13 +341,13 @@ def getTopWords(ny,sf): if p0V[i]>-6.0:topSF.append((vocabList[i],p0V[i])) if p1V[i]>-6.0:topNY.append((vocabList[i],p1V[i])) sortedSF=sorted(topSF,key=lambda pair:pair[1],reverse=True) - print "SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**" + print("SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**") for item in sortedSF: - print item[0] + print(item[0]) sortedNY=sorted(topNY,key=lambda pair:pair[1],reverse=True) - print "NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**" + print("NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**") for item in sortedNY: - print item[0] + print(item[0]) if __name__ == "__main__": diff --git a/src/py2.x/ml/4.NaiveBayes/sklearn-nb-demo.py b/src/py2.x/ml/4.NaiveBayes/sklearn-nb-demo.py index 7d11f2439..d1a564cdf 100644 --- a/src/py2.x/ml/4.NaiveBayes/sklearn-nb-demo.py +++ b/src/py2.x/ml/4.NaiveBayes/sklearn-nb-demo.py @@ -8,6 +8,7 @@ @author: 小瑶 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning """ +from __future__ import print_function # GaussianNB_高斯朴素贝叶斯 @@ -17,10 +18,10 @@ from sklearn.naive_bayes import GaussianNB clf = GaussianNB() clf.fit(X, Y) -print clf.predict([[-0.8, -1]]) +print(clf.predict([[-0.8, -1]])) clf_pf = GaussianNB() clf_pf.partial_fit(X, Y, np.unique(Y)) -print clf_pf.predict([[-0.8, -1]]) +print(clf_pf.predict([[-0.8, -1]])) # MultinomialNB_多项朴素贝叶斯 ''' diff --git a/src/py2.x/ml/5.Logistic/logistic.py b/src/py2.x/ml/5.Logistic/logistic.py index 6ebc07d03..743940154 100644 --- a/src/py2.x/ml/5.Logistic/logistic.py +++ b/src/py2.x/ml/5.Logistic/logistic.py @@ -7,6 +7,7 @@ Author: Peter Harrington/羊三/小瑶 GitHub: https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import * import matplotlib.pyplot as plt @@ -279,7 +280,7 @@ def colicTest(): currLine[21]): errorCount += 1 errorRate = (float(errorCount) / numTestVec) - print "the error rate of this test is: %f" % errorRate + print("the error rate of this test is: %f" % errorRate) return errorRate @@ -289,7 +290,7 @@ def multiTest(): errorSum = 0.0 for k in range(numTests): errorSum += colicTest() - print "after %d iterations the average error rate is: %f" % (numTests, errorSum / float(numTests)) + print("after %d iterations the average error rate is: %f" % (numTests, errorSum / float(numTests))) if __name__ == "__main__": diff --git a/src/py2.x/ml/5.Logistic/sklearn_logisticRegression_demo.py b/src/py2.x/ml/5.Logistic/sklearn_logisticRegression_demo.py index f4ebc59b5..61eaa95f0 100644 --- a/src/py2.x/ml/5.Logistic/sklearn_logisticRegression_demo.py +++ b/src/py2.x/ml/5.Logistic/sklearn_logisticRegression_demo.py @@ -179,6 +179,7 @@ def line(x0): plt.show() ''' +from __future__ import print_function # Logistic Regression 3-class Classifier 逻辑回归 3-类 分类器 diff --git a/src/py2.x/ml/6.SVM/sklearn-svm-demo.py b/src/py2.x/ml/6.SVM/sklearn-svm-demo.py index 428fae747..474889f81 100644 --- a/src/py2.x/ml/6.SVM/sklearn-svm-demo.py +++ b/src/py2.x/ml/6.SVM/sklearn-svm-demo.py @@ -9,6 +9,7 @@ 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning sklearn-SVM译文链接: http://cwiki.apachecn.org/pages/viewpage.action?pageId=10031359 """ +from __future__ import print_function import matplotlib.pyplot as plt import numpy as np from sklearn import svm @@ -44,8 +45,8 @@ def loadDataSet(fileName): X, Y = loadDataSet('input/6.SVM/testSet.txt') X = np.mat(X) -print("X=", X) -print("Y=", Y) +print(("X=", X)) +print(("Y=", Y)) # 拟合一个SVM模型 clf = svm.SVC(kernel='linear') @@ -60,11 +61,11 @@ def loadDataSet(fileName): xx = np.linspace(-2, 10) # , num=50) # 二维的直线方程 yy = a * xx - (clf.intercept_[0]) / w[1] -print("yy=", yy) +print(("yy=", yy)) # plot the parallels to the separating hyperplane that pass through the support vectors # 通过支持向量绘制分割超平面 -print("support_vectors_=", clf.support_vectors_) +print(("support_vectors_=", clf.support_vectors_)) b = clf.support_vectors_[0] yy_down = a * xx + (b[1] - a * b[0]) b = clf.support_vectors_[-1] diff --git a/src/py2.x/ml/6.SVM/svm-complete.py b/src/py2.x/ml/6.SVM/svm-complete.py index 708cc6aa6..53e06920e 100644 --- a/src/py2.x/ml/6.SVM/svm-complete.py +++ b/src/py2.x/ml/6.SVM/svm-complete.py @@ -8,6 +8,7 @@ @author: Peter/geekidentity/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning """ +from __future__ import print_function from numpy import * import matplotlib.pyplot as plt @@ -511,13 +512,13 @@ def plotfig_SVM(xArr, yArr, ws, b, alphas): # b是常量值, alphas是拉格朗日乘子 b, alphas = smoP(dataArr, labelArr, 0.6, 0.001, 40) - print '/n/n/n' - print 'b=', b - print 'alphas[alphas>0]=', alphas[alphas > 0] - print 'shape(alphas[alphas > 0])=', shape(alphas[alphas > 0]) + print('/n/n/n') + print('b=', b) + print('alphas[alphas>0]=', alphas[alphas > 0]) + print('shape(alphas[alphas > 0])=', shape(alphas[alphas > 0])) for i in range(100): if alphas[i] > 0: - print dataArr[i], labelArr[i] + print(dataArr[i], labelArr[i]) # 画图 ws = calcWs(alphas, dataArr, labelArr) plotfig_SVM(dataArr, labelArr, ws, b, alphas) diff --git a/src/py2.x/ml/6.SVM/svm-complete_Non-Kernel.py b/src/py2.x/ml/6.SVM/svm-complete_Non-Kernel.py index 5e8b88fd6..a9c0449cb 100644 --- a/src/py2.x/ml/6.SVM/svm-complete_Non-Kernel.py +++ b/src/py2.x/ml/6.SVM/svm-complete_Non-Kernel.py @@ -8,6 +8,7 @@ @author: Peter/geekidentity/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning """ +from __future__ import print_function from numpy import * import matplotlib.pyplot as plt @@ -362,13 +363,13 @@ def plotfig_SVM(xArr, yArr, ws, b, alphas): # b是常量值, alphas是拉格朗日乘子 b, alphas = smoP(dataArr, labelArr, 0.6, 0.001, 40) - print '/n/n/n' - print 'b=', b - print 'alphas[alphas>0]=', alphas[alphas > 0] - print 'shape(alphas[alphas > 0])=', shape(alphas[alphas > 0]) + print('/n/n/n') + print('b=', b) + print('alphas[alphas>0]=', alphas[alphas > 0]) + print('shape(alphas[alphas > 0])=', shape(alphas[alphas > 0])) for i in range(100): if alphas[i] > 0: - print dataArr[i], labelArr[i] + print(dataArr[i], labelArr[i]) # 画图 ws = calcWs(alphas, dataArr, labelArr) plotfig_SVM(dataArr, labelArr, ws, b, alphas) diff --git a/src/py2.x/ml/6.SVM/svm-simple.py b/src/py2.x/ml/6.SVM/svm-simple.py index 3419a57c3..e8f0e7d53 100644 --- a/src/py2.x/ml/6.SVM/svm-simple.py +++ b/src/py2.x/ml/6.SVM/svm-simple.py @@ -8,6 +8,7 @@ @author: Peter/geekidentity/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning """ +from __future__ import print_function from numpy import * import matplotlib.pyplot as plt @@ -242,13 +243,13 @@ def plotfig_SVM(xMat, yMat, ws, b, alphas): # b是常量值, alphas是拉格朗日乘子 b, alphas = smoSimple(dataArr, labelArr, 0.6, 0.001, 40) - print '/n/n/n' - print 'b=', b - print 'alphas[alphas>0]=', alphas[alphas > 0] - print 'shape(alphas[alphas > 0])=', shape(alphas[alphas > 0]) + print('/n/n/n') + print('b=', b) + print('alphas[alphas>0]=', alphas[alphas > 0]) + print('shape(alphas[alphas > 0])=', shape(alphas[alphas > 0])) for i in range(100): if alphas[i] > 0: - print dataArr[i], labelArr[i] + print(dataArr[i], labelArr[i]) # 画图 ws = calcWs(alphas, dataArr, labelArr) plotfig_SVM(dataArr, labelArr, ws, b, alphas) diff --git a/src/py2.x/ml/7.AdaBoost/adaboost.py b/src/py2.x/ml/7.AdaBoost/adaboost.py index c88e1415d..22c5cfccd 100644 --- a/src/py2.x/ml/7.AdaBoost/adaboost.py +++ b/src/py2.x/ml/7.AdaBoost/adaboost.py @@ -8,6 +8,7 @@ @author: Peter/片刻 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import * @@ -210,8 +211,8 @@ def plotROC(predStrengths, classLabels): predStrengths 最终预测结果的权重值 classLabels 原始数据的分类结果集 """ - print 'predStrengths=', predStrengths - print 'classLabels=', classLabels + print('predStrengths=', predStrengths) + print('classLabels=', classLabels) import matplotlib.pyplot as plt # variable to calculate AUC @@ -226,7 +227,7 @@ def plotROC(predStrengths, classLabels): # get sorted index, it's reverse sortedIndicies = predStrengths.argsort() # 测试结果是否是从小到大排列 - print 'sortedIndicies=', sortedIndicies, predStrengths[0, 176], predStrengths.min(), predStrengths[0, 293], predStrengths.max() + print('sortedIndicies=', sortedIndicies, predStrengths[0, 176], predStrengths.min(), predStrengths[0, 293], predStrengths.max()) # 开始创建模版对象 fig = plt.figure() @@ -245,7 +246,7 @@ def plotROC(predStrengths, classLabels): ySum += cur[1] # draw line from cur to (cur[0]-delX, cur[1]-delY) # 画点连线 (x1, x2, y1, y2) - print cur[0], cur[0]-delX, cur[1], cur[1]-delY + print(cur[0], cur[0]-delX, cur[1], cur[1]-delY) ax.plot([cur[0], cur[0]-delX], [cur[1], cur[1]-delY], c='b') cur = (cur[0]-delX, cur[1]-delY) # 画对角的虚线线 @@ -262,7 +263,7 @@ def plotROC(predStrengths, classLabels): 这些小矩形的宽度是xStep,因此可以先对所有矩形的高度进行累加,最后再乘以xStep得到其总面积。 所有高度的和(ySum)随着x轴的每次移动而渐次增加。 ''' - print "the Area Under the Curve is: ", ySum*xStep + print("the Area Under the Curve is: ", ySum*xStep) if __name__ == "__main__": @@ -299,7 +300,7 @@ def plotROC(predStrengths, classLabels): # 训练集合 dataArr, labelArr = loadDataSet("input/7.AdaBoost/horseColicTraining2.txt") weakClassArr, aggClassEst = adaBoostTrainDS(dataArr, labelArr, 40) - print weakClassArr, '\n-----\n', aggClassEst.T + print(weakClassArr, '\n-----\n', aggClassEst.T) # 计算ROC下面的AUC的面积大小 plotROC(aggClassEst.T, labelArr) # 测试集合 @@ -308,4 +309,4 @@ def plotROC(predStrengths, classLabels): predicting10 = adaClassify(dataArrTest, weakClassArr) errArr = mat(ones((m, 1))) # 测试:计算总样本数,错误样本数,错误率 - print m, errArr[predicting10 != mat(labelArrTest).T].sum(), errArr[predicting10 != mat(labelArrTest).T].sum()/m + print(m, errArr[predicting10 != mat(labelArrTest).T].sum(), errArr[predicting10 != mat(labelArrTest).T].sum()/m) diff --git a/src/py2.x/ml/7.AdaBoost/sklearn-adaboost-demo.py b/src/py2.x/ml/7.AdaBoost/sklearn-adaboost-demo.py index 723498516..4ec0f4cc3 100644 --- a/src/py2.x/ml/7.AdaBoost/sklearn-adaboost-demo.py +++ b/src/py2.x/ml/7.AdaBoost/sklearn-adaboost-demo.py @@ -7,6 +7,7 @@ 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning sklearn-AdaBoost译文链接: http://cwiki.apachecn.org/pages/viewpage.action?pageId=10813457 """ +from __future__ import print_function import matplotlib.pyplot as plt # importing necessary libraries @@ -47,15 +48,15 @@ plt.legend() plt.show() -print 'y---', type(y[0]), len(y), y[:4] -print 'y_1---', type(y_1[0]), len(y_1), y_1[:4] -print 'y_2---', type(y_2[0]), len(y_2), y_2[:4] +print('y---', type(y[0]), len(y), y[:4]) +print('y_1---', type(y_1[0]), len(y_1), y_1[:4]) +print('y_2---', type(y_2[0]), len(y_2), y_2[:4]) # 适合2分类 y_true = np.array([0, 0, 1, 1]) y_scores = np.array([0.1, 0.4, 0.35, 0.8]) -print 'y_scores---', type(y_scores[0]), len(y_scores), y_scores -print metrics.roc_auc_score(y_true, y_scores) +print('y_scores---', type(y_scores[0]), len(y_scores), y_scores) +print(metrics.roc_auc_score(y_true, y_scores)) # print "-" * 100 # print metrics.roc_auc_score(y[:1], y_2[:1]) diff --git a/src/py2.x/ml/7.RandomForest/randomForest.py b/src/py2.x/ml/7.RandomForest/randomForest.py index 47e3136a3..05145ed95 100644 --- a/src/py2.x/ml/7.RandomForest/randomForest.py +++ b/src/py2.x/ml/7.RandomForest/randomForest.py @@ -11,6 +11,7 @@ 源代码网址:http://www.tuicool.com/articles/iiUfeim Flying_sfeng博客地址:http://blog.csdn.net/flying_sfeng/article/details/64133822 (感谢作者贡献) ''' +from __future__ import print_function from random import seed, randrange, random @@ -340,7 +341,7 @@ def evaluate_algorithm(dataset, algorithm, n_folds, *args): scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features) # 每一次执行本文件时都能产生同一个随机数 seed(1) - print 'random=', random() - print 'Trees: %d' % n_trees - print 'Scores: %s' % scores - print 'Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores))) + print('random=', random()) + print('Trees: %d' % n_trees) + print('Scores: %s' % scores) + print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores)))) diff --git a/src/py2.x/ml/8.Regression/regression.py b/src/py2.x/ml/8.Regression/regression.py index 7390fa2da..aae2a7d75 100644 --- a/src/py2.x/ml/8.Regression/regression.py +++ b/src/py2.x/ml/8.Regression/regression.py @@ -6,6 +6,7 @@ Author: Peter Harrington/小瑶 GitHub: https://github.com/apachecn/AiLearning ''' +from __future__ import print_function from numpy import * import matplotlib.pylab as plt @@ -546,22 +547,22 @@ def abaloneTest(): oldyHat1 = lwlrTest(abX[0:99], abX[0:99], abY[0:99], 1) oldyHat10 = lwlrTest(abX[0:99], abX[0:99], abY[0:99], 10) # 打印出不同的核预测值与训练数据集上的真实值之间的误差大小 - print("old yHat01 error Size is :", rssError(abY[0:99], oldyHat01.T)) - print("old yHat1 error Size is :", rssError(abY[0:99], oldyHat1.T)) - print("old yHat10 error Size is :", rssError(abY[0:99], oldyHat10.T)) + print(("old yHat01 error Size is :", rssError(abY[0:99], oldyHat01.T))) + print(("old yHat1 error Size is :", rssError(abY[0:99], oldyHat1.T))) + print(("old yHat10 error Size is :", rssError(abY[0:99], oldyHat10.T))) # 打印出 不同的核预测值 与 新数据集(测试数据集)上的真实值之间的误差大小 newyHat01 = lwlrTest(abX[100:199], abX[0:99], abY[0:99], 0.1) - print("new yHat01 error Size is :", rssError(abY[0:99], newyHat01.T)) + print(("new yHat01 error Size is :", rssError(abY[0:99], newyHat01.T))) newyHat1 = lwlrTest(abX[100:199], abX[0:99], abY[0:99], 1) - print("new yHat1 error Size is :", rssError(abY[0:99], newyHat1.T)) + print(("new yHat1 error Size is :", rssError(abY[0:99], newyHat1.T))) newyHat10 = lwlrTest(abX[100:199], abX[0:99], abY[0:99], 10) - print("new yHat10 error Size is :", rssError(abY[0:99], newyHat10.T)) + print(("new yHat10 error Size is :", rssError(abY[0:99], newyHat10.T))) # 使用简单的 线性回归 进行预测,与上面的计算进行比较 standWs = standRegres(abX[0:99], abY[0:99]) standyHat = mat(abX[100:199]) * standWs - print("standRegress error Size is:", rssError(abY[100:199], standyHat.T.A)) + print(("standRegress error Size is:", rssError(abY[100:199], standyHat.T.A))) # test for ridgeRegression diff --git a/src/py2.x/ml/8.Regression/sklearn-regression-demo.py b/src/py2.x/ml/8.Regression/sklearn-regression-demo.py index 5f33711d5..a1cc0edda 100644 --- a/src/py2.x/ml/8.Regression/sklearn-regression-demo.py +++ b/src/py2.x/ml/8.Regression/sklearn-regression-demo.py @@ -7,6 +7,7 @@ @author: Peter Harrington/小瑶 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning ''' +from __future__ import print_function # Isotonic Regression 等式回归 diff --git a/src/py2.x/ml/9.RegTrees/RTSklearn.py b/src/py2.x/ml/9.RegTrees/RTSklearn.py index 42e23b454..7402c2d90 100644 --- a/src/py2.x/ml/9.RegTrees/RTSklearn.py +++ b/src/py2.x/ml/9.RegTrees/RTSklearn.py @@ -62,6 +62,7 @@ author: jiangzhonglian content: 模型树 ''' +from __future__ import print_function print(__doc__) diff --git a/src/py2.x/ml/9.RegTrees/regTrees.py b/src/py2.x/ml/9.RegTrees/regTrees.py index 02ac71b7c..b57110889 100644 --- a/src/py2.x/ml/9.RegTrees/regTrees.py +++ b/src/py2.x/ml/9.RegTrees/regTrees.py @@ -7,6 +7,7 @@ Author: Peter Harrington/片刻/小瑶/zh0ng GitHub: https://github.com/apachecn/AiLearning ''' +from __future__ import print_function print(__doc__) from numpy import * @@ -242,7 +243,7 @@ def prune(tree, testData): errorMerge = sum(power(testData[:, -1] - treeMean, 2)) # 如果 合并的总方差 < 不合并的总方差,那么就进行合并 if errorMerge < errorNoMerge: - print "merging" + print("merging") return treeMean # 两个return可以简化成一个 else: @@ -404,10 +405,10 @@ def createForeCast(tree, testData, modelEval=regTreeEval): if __name__ == "__main__": # 测试数据集 testMat = mat(eye(4)) - print testMat - print type(testMat) + print(testMat) + print(type(testMat)) mat0, mat1 = binSplitDataSet(testMat, 1, 0.5) - print mat0, '\n-----------\n', mat1 + print(mat0, '\n-----------\n', mat1) # # 回归树 # myDat = loadDataSet('input/9.RegTrees/data1.txt') diff --git a/src/py2.x/ml/9.RegTrees/sklearn-regressTree-demo.py b/src/py2.x/ml/9.RegTrees/sklearn-regressTree-demo.py index 025399e6c..d2c2f243b 100644 --- a/src/py2.x/ml/9.RegTrees/sklearn-regressTree-demo.py +++ b/src/py2.x/ml/9.RegTrees/sklearn-regressTree-demo.py @@ -8,6 +8,7 @@ @author: 小瑶 《机器学习实战》更新地址:https://github.com/apachecn/AiLearning """ +from __future__ import print_function print(__doc__) diff --git a/src/py2.x/ml/9.RegTrees/treeExplore.py b/src/py2.x/ml/9.RegTrees/treeExplore.py index 474139f64..43d1c6be9 100644 --- a/src/py2.x/ml/9.RegTrees/treeExplore.py +++ b/src/py2.x/ml/9.RegTrees/treeExplore.py @@ -8,6 +8,7 @@ Author: Peter/片刻 GitHub: https://github.com/apachecn/AiLearning ''' +from __future__ import print_function import regTrees from Tkinter import * from numpy import * @@ -52,14 +53,14 @@ def getInputs(): tolN = int(tolNentry.get()) except: tolN = 10 - print "enter Integer for tolN" + print("enter Integer for tolN") tolNentry.delete(0, END) tolNentry.insert(0, '10') try: tolS = float(tolSentry.get()) except: tolS = 1.0 - print "enter Float for tolS" + print("enter Float for tolS") tolSentry.delete(0, END) tolSentry.insert(0, '1.0') return tolN, tolS diff --git a/src/py3.x/ml/15.BigData_MapReduce/mrMeanMapper.py b/src/py3.x/ml/15.BigData_MapReduce/mrMeanMapper.py index ac273f239..a09a093b4 100644 --- a/src/py3.x/ml/15.BigData_MapReduce/mrMeanMapper.py +++ b/src/py3.x/ml/15.BigData_MapReduce/mrMeanMapper.py @@ -8,6 +8,8 @@ GitHub: https://github.com/apachecn/AiLearning ''' +from __future__ import print_function + import sys from numpy import mat, mean, power diff --git a/src/py3.x/ml/15.BigData_MapReduce/mrMeanReducer.py b/src/py3.x/ml/15.BigData_MapReduce/mrMeanReducer.py index f55db1307..263a9b909 100644 --- a/src/py3.x/ml/15.BigData_MapReduce/mrMeanReducer.py +++ b/src/py3.x/ml/15.BigData_MapReduce/mrMeanReducer.py @@ -8,6 +8,8 @@ GitHub: https://github.com/apachecn/AiLearning ''' +from __future__ import print_function + import sys ''' diff --git "a/src/py3.x/nlp/1.\350\257\255\350\250\200\345\244\204\347\220\206\344\270\216Python/test.py" "b/src/py3.x/nlp/1.\350\257\255\350\250\200\345\244\204\347\220\206\344\270\216Python/test.py" index cea5e5d92..b4462a39a 100644 --- "a/src/py3.x/nlp/1.\350\257\255\350\250\200\345\244\204\347\220\206\344\270\216Python/test.py" +++ "b/src/py3.x/nlp/1.\350\257\255\350\250\200\345\244\204\347\220\206\344\270\216Python/test.py" @@ -12,6 +12,8 @@ """ the first example for nltk book """ +from __future__ import print_function + from nltk.book import * diff --git a/tools/python2libsvm.py b/tools/python2libsvm.py index 5e75c7b9f..5b53fe47c 100644 --- a/tools/python2libsvm.py +++ b/tools/python2libsvm.py @@ -1,6 +1,7 @@ #!/usr/bin/python # coding:utf8 +from __future__ import print_function import os import sklearn.datasets as datasets @@ -46,7 +47,7 @@ def dump_data(x, y, file_output): # 获取数据集 x, y = get_data(file_input, separator='\t') - print x[3, :] - print y + print(x[3, :]) + print(y) # 导出数据为 libsvm dump_data(x, y, file_output)