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 --version
conda 4.3.30
C:\Users\lenovo>conda info --envs
# conda environments:
#
root * G:\ProgramData\Anaconda3
C:\Users\lenovo>conda create --name python27 python=2.7
Fetching package metadata ...............
Solving package specifications: .
Package plan for installation in environment G:\ProgramData\Anaconda3\envs\python27:
.........
C:\Users\lenovo>python --version
Python 3.6.4 :: Anaconda, Inc.
C:\Users\lenovo>activate python27
(python27) C:\Users\lenovo>python --version
Python 2.7.13 :: Continuum Analytics, Inc.
(python27) C:\Users\lenovo>deactivate python27
C:\Users\lenovo>python --version
Python 3.6.4 :: Anaconda, Inc.
C:\Users\lenovo>activate python27
(python27) C:\Users\lenovo>python
Python 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 pip
pip install numpy
pip install scipy
pip install pandas
pip install matplotlib
6,执行numpy方法成功。
Microsoft Windows [版本 10.0.15063]
(c) 2017 Microsoft Corporation。保留所有权利。
C:\Users\lenovo>python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "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 "
", 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.7
Fetching 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/free
Proceed ([y]/n)? y
vs2008_runtime 100% |###############################| Time: 0:00:00 3.03 MB/s
vc-9-0.tar.bz2 100% |###############################| Time: 0:00:00 729.58 kB/s
python-2.7.13- 100% |###############################| Time: 0:00:03 8.20 MB/s
certifi-2016.2 100% |###############################| Time: 0:00:00 3.34 MB/s
wheel-0.29.0-p 100% |###############################| Time: 0:00:00 4.43 MB/s
wincertstore-0 100% |###############################| Time: 0:00:00 7.10 MB/s
setuptools-36. 100% |###############################| Time: 0:00:00 2.93 MB/s
pip-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\python27
root * G:\ProgramData\Anaconda3
C:\Users\lenovo>python --version
Python 3.6.4 :: Anaconda, Inc.
C:\Users\lenovo>activate python27
(python27) C:\Users\lenovo>python --version
Python 2.7.13 :: Continuum Analytics, Inc.
(python27) C:\Users\lenovo>deactivate python27
C:\Users\lenovo>python --version
Python 3.6.4 :: Anaconda, Inc.
C:\Users\lenovo>activate python27
(python27) C:\Users\lenovo>python
Python 2.7.13 |Continuum Analytics, Inc.| (default, May 11 2017, 13:17:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "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 "
", line 2, in
File "
", 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 "
", line 4, in inner
ImportError: No module named numpy
>>> import numpy
Traceback (most recent call last):
File "
", line 1, in
ImportError: No module named numpy
>>>
^C
(python27) C:\Users\lenovo>pip install numpy
Collecting numpy
Downloading numpy-1.14.2-cp27-none-win_amd64.whl (13.3MB)
100% |████████████████████████████████| 13.3MB 31kB/s
Installing collected packages: numpy
Successfully installed numpy-1.14.2
Cache entry deserialization failed, entry ignored
You 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 scipy
Collecting scipy
Downloading scipy-1.0.1-cp27-none-win_amd64.whl (31.2MB)
100% |████████████████████████████████| 31.2MB 20kB/s
Requirement already satisfied: numpy>=1.8.2 in g:\programdata\anaconda3\envs\python27\lib\site-packages (from scipy)
Installing collected packages: scipy
Successfully installed scipy-1.0.1
You 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 pandas
Collecting pandas
Downloading pandas-0.22.0-cp27-cp27m-win_amd64.whl (9.1MB)
100% |████████████████████████████████| 9.1MB 44kB/s
Collecting pytz>=2011k (from pandas)
Downloading pytz-2018.3-py2.py3-none-any.whl (509kB)
100% |████████████████████████████████| 512kB 57kB/s
Requirement 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/s
Collecting six>=1.5 (from python-dateutil->pandas)
Downloading six-1.11.0-py2.py3-none-any.whl
Installing collected packages: pytz, six, python-dateutil, pandas
Successfully installed pandas-0.22.0 python-dateutil-2.7.2 pytz-2018.3 six-1.11.0
You 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 matplotlib
Collecting matplotlib
Downloading matplotlib-2.2.2-cp27-cp27m-win_amd64.whl (8.4MB)
100% |████████████████████████████████| 8.4MB 33kB/s
Collecting backports.functools-lru-cache (from matplotlib)
Downloading backports.functools_lru_cache-1.5-py2.py3-none-any.whl
Collecting 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/s
Requirement 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.whl
Collecting kiwisolver>=1.0.1 (from matplotlib)
Downloading kiwisolver-1.0.1-cp27-none-win_amd64.whl (64kB)
100% |████████████████████████████████| 71kB 41kB/s
Requirement 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, matplotlib
Successfully installed backports.functools-lru-cache-1.5 cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.2 pyparsing-2.2.0
You 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 pip
Cache entry deserialization failed, entry ignored
Collecting pip
Cache entry deserialization failed, entry ignored
Downloading pip-9.0.3-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 79kB/s
Installing collected packages: pip
Found existing installation: pip 9.0.1
Uninstalling pip-9.0.1:
Successfully uninstalled pip-9.0.1
Successfully installed pip-9.0.3
(python27) C:\Users\lenovo>python
Python 2.7.13 |Continuum Analytics, Inc.| (default, May 11 2017, 13:17:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "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.944630636958
0.132346823532
1.1896890924
array:
1.88733700807
1.22107083901
4.84144049432
numpy:
3.09788283824
2.42338712602
0.0146385004209
>>>