Numpy 在Python 2.7.3、 Python 3.6.4不同版本的應用
1,安裝Anaconda,安裝Anaconda時建立的預設Python環境,環境名稱是root,對應的Python版本是3.6.4
2,使用numpy,進行print列印時出錯,python 3.6列印要加上圓括弧。
為便於numpy實驗,建立python 2.7.3環境,在python2.7.3中使用numpy。
3,在Anaconda建立python 2.7.3 的使用環境。
C:\Users\lenovo>conda --versionconda 4.3.30C:\Users\lenovo>conda info --envs# conda environments:#root * G:\ProgramData\Anaconda3C:\Users\lenovo>conda create --name python27 python=2.7Fetching package metadata ...............Solving package specifications: .Package plan for installation in environment G:\ProgramData\Anaconda3\envs\python27:.........C:\Users\lenovo>python --versionPython 3.6.4 :: Anaconda, Inc.C:\Users\lenovo>activate python27(python27) C:\Users\lenovo>python --versionPython 2.7.13 :: Continuum Analytics, Inc.(python27) C:\Users\lenovo>deactivate python27C:\Users\lenovo>python --versionPython 3.6.4 :: Anaconda, Inc.C:\Users\lenovo>activate python27(python27) C:\Users\lenovo>pythonPython 2.7.13 |Continuum Analytics, Inc.| (default, May 11 2017, 13:17:26) [MSC v.1500 64 bit (AMD64)] on win32如果剛才添加的Python27環境,不再使用,可通過執行命令:conda remove --name python27 --all,進行刪除
4,在python 2.7.3 環境中載入numpy報錯,numpy還沒安裝。
5,在python 2.7.3 環境中安裝numpy、 scipy、pandas、matplotlib等包。
python -m pip install --upgrade pippip install numpypip install scipypip install pandaspip install matplotlib
6,執行numpy方法成功。
Microsoft Windows [版本 10.0.15063](c) 2017 Microsoft Corporation。著作權所有,並保留一切權利。C:\Users\lenovo>pythonPython 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import timeit>>>>>> common_for = """... for d in data:... s += d... """>>>>>> common_sum = """... sum(data)... """>>>>>> common_numpy_sum = """... numpy.sum(data)... """>>>>>> def timeit_list(n, loops):... list_setup = """... import numpy... data = [1] * {}... s = 0... """.format(n)... print 'list:' File "<stdin>", line 7 print 'list:' ^SyntaxError: Missing parentheses in call to 'print'. Did you mean print(int 'list:')? C:\Users\lenovo>conda create --name python27 python=2.7Fetching package metadata ...............Solving package specifications: .Package plan for installation in environment G:\ProgramData\Anaconda3\envs\python27:The following NEW packages will be INSTALLED: certifi: 2016.2.28-py27_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free pip: 9.0.1-py27_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free python: 2.7.13-1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free setuptools: 36.4.0-py27_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free vc: 9-0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free vs2008_runtime: 9.00.30729.5054-0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free wheel: 0.29.0-py27_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free wincertstore: 0.2-py27_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/freeProceed ([y]/n)? yvs2008_runtime 100% |###############################| Time: 0:00:00 3.03 MB/svc-9-0.tar.bz2 100% |###############################| Time: 0:00:00 729.58 kB/spython-2.7.13- 100% |###############################| Time: 0:00:03 8.20 MB/scertifi-2016.2 100% |###############################| Time: 0:00:00 3.34 MB/swheel-0.29.0-p 100% |###############################| Time: 0:00:00 4.43 MB/swincertstore-0 100% |###############################| Time: 0:00:00 7.10 MB/ssetuptools-36. 100% |###############################| Time: 0:00:00 2.93 MB/spip-9.0.1-py27 100% |###############################| Time: 0:00:00 7.38 MB/s## To activate this environment, use:# > activate python27## To deactivate an active environment, use:# > deactivate## * for power-users using bash, you must source#C:\Users\lenovo>conda info --envs# conda environments:#python27 G:\ProgramData\Anaconda3\envs\python27root * G:\ProgramData\Anaconda3C:\Users\lenovo>python --versionPython 3.6.4 :: Anaconda, Inc.C:\Users\lenovo>activate python27(python27) C:\Users\lenovo>python --versionPython 2.7.13 :: Continuum Analytics, Inc.(python27) C:\Users\lenovo>deactivate python27C:\Users\lenovo>python --versionPython 3.6.4 :: Anaconda, Inc.C:\Users\lenovo>activate python27(python27) C:\Users\lenovo>pythonPython 2.7.13 |Continuum Analytics, Inc.| (default, May 11 2017, 13:17:26) [MSC v.1500 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.Anaconda is brought to you by Continuum Analytics.Please check out: http://continuum.io/thanks and https://anaconda.org>>>>>>>>>>>>>>>>>> import timeit>>>>>> common_for = """... for d in data:... s += d... """>>>>>> common_sum = """... sum(data)... """>>>>>> common_numpy_sum = """... numpy.sum(data)... """>>>>>> def timeit_list(n, loops):... list_setup = """... import numpy... data = [1] * {}... s = 0... """.format(n)... print 'list:'... print timeit.timeit(common_for, list_setup, number = loops)... print timeit.timeit(common_sum, list_setup, number = loops)... print timeit.timeit(common_numpy_sum, list_setup, number = loops)...>>> def timeit_array(n, loops):... array_setup = """... import numpy... import array... data = array.array('L', [1] * {})... s = 0... """.format(n)... print 'array:'... print timeit.timeit(common_for, array_setup, number = loops)... print timeit.timeit(common_sum, array_setup, number = loops)... print timeit.timeit(common_numpy_sum, array_setup, number = loops)...>>> def timeit_numpy(n, loops):... numpy_setup = """... import numpy... data = numpy.array([1] * {})... s = 0... """.format(n)... print 'numpy:'... print timeit.timeit(common_for, numpy_setup, number = loops)... print timeit.timeit(common_sum, numpy_setup, number = loops)... print timeit.timeit(common_numpy_sum, numpy_setup, number = loops)...>>> if __name__ == '__main__':... timeit_list(50000, 500)... timeit_array(50000, 500)... timeit_numpy(50000, 500)...list:Traceback (most recent call last): File "<stdin>", line 2, in <module> File "<stdin>", line 8, in timeit_list File "G:\ProgramData\Anaconda3\envs\python27\lib\timeit.py", line 237, in timeit return Timer(stmt, setup, timer).timeit(number) File "G:\ProgramData\Anaconda3\envs\python27\lib\timeit.py", line 202, in timeit timing = self.inner(it, self.timer) File "<timeit-src>", line 4, in innerImportError: No module named numpy>>> import numpyTraceback (most recent call last): File "<stdin>", line 1, in <module>ImportError: No module named numpy>>>^C(python27) C:\Users\lenovo>pip install numpyCollecting numpy Downloading numpy-1.14.2-cp27-none-win_amd64.whl (13.3MB) 100% |████████████████████████████████| 13.3MB 31kB/sInstalling collected packages: numpySuccessfully installed numpy-1.14.2Cache entry deserialization failed, entry ignoredYou are using pip version 9.0.1, however version 9.0.3 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command.(python27) C:\Users\lenovo>pip install scipyCollecting scipy Downloading scipy-1.0.1-cp27-none-win_amd64.whl (31.2MB) 100% |████████████████████████████████| 31.2MB 20kB/sRequirement already satisfied: numpy>=1.8.2 in g:\programdata\anaconda3\envs\python27\lib\site-packages (from scipy)Installing collected packages: scipySuccessfully installed scipy-1.0.1You are using pip version 9.0.1, however version 9.0.3 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command.(python27) C:\Users\lenovo>pip install pandasCollecting pandas Downloading pandas-0.22.0-cp27-cp27m-win_amd64.whl (9.1MB) 100% |████████████████████████████████| 9.1MB 44kB/sCollecting pytz>=2011k (from pandas) Downloading pytz-2018.3-py2.py3-none-any.whl (509kB) 100% |████████████████████████████████| 512kB 57kB/sRequirement already satisfied: numpy>=1.9.0 in g:\programdata\anaconda3\envs\python27\lib\site-packages (from pandas)Collecting python-dateutil (from pandas) Downloading python_dateutil-2.7.2-py2.py3-none-any.whl (212kB) 100% |████████████████████████████████| 215kB 95kB/sCollecting six>=1.5 (from python-dateutil->pandas) Downloading six-1.11.0-py2.py3-none-any.whlInstalling collected packages: pytz, six, python-dateutil, pandasSuccessfully installed pandas-0.22.0 python-dateutil-2.7.2 pytz-2018.3 six-1.11.0You are using pip version 9.0.1, however version 9.0.3 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command.(python27) C:\Users\lenovo>pip install matplotlibCollecting matplotlib Downloading matplotlib-2.2.2-cp27-cp27m-win_amd64.whl (8.4MB) 100% |████████████████████████████████| 8.4MB 33kB/sCollecting backports.functools-lru-cache (from matplotlib) Downloading backports.functools_lru_cache-1.5-py2.py3-none-any.whlCollecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib) Downloading pyparsing-2.2.0-py2.py3-none-any.whl (56kB) 100% |████████████████████████████████| 61kB 28kB/sRequirement already satisfied: numpy>=1.7.1 in g:\programdata\anaconda3\envs\python27\lib\site-packages (from matplotlib)Collecting cycler>=0.10 (from matplotlib) Downloading cycler-0.10.0-py2.py3-none-any.whlCollecting kiwisolver>=1.0.1 (from matplotlib) Downloading kiwisolver-1.0.1-cp27-none-win_amd64.whl (64kB) 100% |████████████████████████████████| 71kB 41kB/sRequirement already satisfied: python-dateutil>=2.1 in g:\programdata\anaconda3\envs\python27\lib\site-packages (from matplotlib)Requirement already satisfied: six>=1.10 in g:\programdata\anaconda3\envs\python27\lib\site-packages (from matplotlib)Requirement already satisfied: pytz in g:\programdata\anaconda3\envs\python27\lib\site-packages (from matplotlib)Requirement already satisfied: setuptools in g:\programdata\anaconda3\envs\python27\lib\site-packages (from kiwisolver>=1.0.1->matplotlib)Installing collected packages: backports.functools-lru-cache, pyparsing, cycler, kiwisolver, matplotlibSuccessfully installed backports.functools-lru-cache-1.5 cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.2 pyparsing-2.2.0You are using pip version 9.0.1, however version 9.0.3 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command.(python27) C:\Users\lenovo>python -m pip install --upgrade pipCache entry deserialization failed, entry ignoredCollecting pip Cache entry deserialization failed, entry ignored Downloading pip-9.0.3-py2.py3-none-any.whl (1.4MB) 100% |████████████████████████████████| 1.4MB 79kB/sInstalling collected packages: pip Found existing installation: pip 9.0.1 Uninstalling pip-9.0.1: Successfully uninstalled pip-9.0.1Successfully installed pip-9.0.3(python27) C:\Users\lenovo>pythonPython 2.7.13 |Continuum Analytics, Inc.| (default, May 11 2017, 13:17:26) [MSC v.1500 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.Anaconda is brought to you by Continuum Analytics.Please check out: http://continuum.io/thanks and https://anaconda.org>>>>>> import timeit>>>>>> common_for = """... for d in data:... s += d... """>>>>>> common_sum = """... sum(data)... """>>>>>> common_numpy_sum = """... numpy.sum(data)... """>>>>>> def timeit_list(n, loops):... list_setup = """... import numpy... data = [1] * {}... s = 0... """.format(n)... print 'list:'... print timeit.timeit(common_for, list_setup, number = loops)... print timeit.timeit(common_sum, list_setup, number = loops)... print timeit.timeit(common_numpy_sum, list_setup, number = loops)...>>> def timeit_array(n, loops):... array_setup = """... import numpy... import array... data = array.array('L', [1] * {})... s = 0... """.format(n)... print 'array:'... print timeit.timeit(common_for, array_setup, number = loops)... print timeit.timeit(common_sum, array_setup, number = loops)... print timeit.timeit(common_numpy_sum, array_setup, number = loops)...>>> def timeit_numpy(n, loops):... numpy_setup = """... import numpy... data = numpy.array([1] * {})... s = 0... """.format(n)... print 'numpy:'... print timeit.timeit(common_for, numpy_setup, number = loops)... print timeit.timeit(common_sum, numpy_setup, number = loops)... print timeit.timeit(common_numpy_sum, numpy_setup, number = loops)...>>> if __name__ == '__main__':... timeit_list(50000, 500)... timeit_array(50000, 500)... timeit_numpy(50000, 500)...list:0.9446306369580.1323468235321.1896890924array:1.887337008071.221070839014.84144049432numpy:3.097882838242.423387126020.0146385004209>>>