第一章 Python语言
1.Python的安装
1)查看是否已经安装
[root@hadoop ~]# which pythonPython 2.6.6
2)如果没有安装,我们用yum来在线安装。
由于centos6.4已经安装了Python,但不是最新版我们在线升级最新版
[root@hadoop ~]# yum -y install python
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: centos.ustc.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package python.i686 0:2.6.6-36.el6 will be updated
--> Processing Dependency: python = 2.6.6-36.el6 for package: python-libs-2.6.6-36.el6.i686
---> Package python.i686 0:2.6.6-51.el6 will be an update
--> Running transaction check
---> Package python-libs.i686 0:2.6.6-36.el6 will be updated
---> Package python-libs.i686 0:2.6.6-51.el6 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================
Updating:
python i686 2.6.6-51.el6 base 74 k
Updating for dependencies:
python-libs i686 2.6.6-51.el6 base 5.3 M
Transaction Summary
=============================================================================================================================================================
Upgrade 2 Package(s)
Total download size: 5.4 M
Downloading Packages:
(1/2): python-2.6.6-51.el6.i686.rpm | 74 kB 00:00
(2/2): python-libs-2.6.6-51.el6.i686.rpm | 5.3 MB 00:09
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 557 kB/s | 5.4 MB 00:09
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : python-libs-2.6.6-51.el6.i686 1/4
Updating : python-2.6.6-51.el6.i686 2/4
Cleanup : python-libs-2.6.6-36.el6.i686 3/4
Cleanup : python-2.6.6-36.el6.i686 4/4
Verifying : python-2.6.6-51.el6.i686 1/4
Verifying : python-libs-2.6.6-51.el6.i686 2/4
Verifying : python-libs-2.6.6-36.el6.i686 3/4
Verifying : python-2.6.6-36.el6.i686 4/4
Updated:
python.i686 0:2.6.6-51.el6
Dependency Updated:
python-libs.i686 0:2.6.6-51.el6
Complete!
[root@hadoop ~]#
3)安装完毕后,我们运行Python
[root@hadoop ~]# python
Python 2.6.6 (r266:84292, Feb 21 2013, 23:54:59)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
4)NumPy函数库安装
由于机器学习会用到很多线性代数知识,因此必须安装NumPy函数库。
[root@hadoop ~]# yum install numpy
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: centos.ustc.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package numpy.i686 0:1.4.1-9.el6 will be installed
--> Processing Dependency: python-nose for package: numpy-1.4.1-9.el6.i686
--> Processing Dependency: libptf77blas.so.3 for package: numpy-1.4.1-9.el6.i686
--> Processing Dependency: libptcblas.so.3 for package: numpy-1.4.1-9.el6.i686
--> Processing Dependency: liblapack.so.3 for package: numpy-1.4.1-9.el6.i686
--> Processing Dependency: libgfortran.so.3 for package: numpy-1.4.1-9.el6.i686
--> Processing Dependency: libatlas.so.3 for package: numpy-1.4.1-9.el6.i686
--> Running transaction check
---> Package atlas.i686 0:3.8.4-2.el6 will be installed
---> Package libgfortran.i686 0:4.4.7-4.el6 will be installed
---> Package python-nose.noarch 0:0.10.4-3.1.el6 will be installed
--> Processing Dependency: python-setuptools for package: python-nose-0.10.4-3.1.el6.noarch
--> Running transaction check
---> Package python-setuptools.noarch 0:0.6.10-3.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
numpy i686 1.4.1-9.el6 base 2.3 M
Installing for dependencies:
atlas i686 3.8.4-2.el6 base 2.4 M
libgfortran i686 4.4.7-4.el6 base 243 k
python-nose noarch 0.10.4-3.1.el6 base 220 k
python-setuptools noarch 0.6.10-3.el6 base 336 k
Transaction Summary
================================================================================
Install 5 Package(s)
Total download size: 5.5 M
Installed size: 21 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): atlas-3.8.4-2.el6.i686.rpm | 2.4 MB 00:04
(2/5): libgfortran-4.4.7-4.el6.i686.rpm | 243 kB 00:00
(3/5): numpy-1.4.1-9.el6.i686.rpm | 2.3 MB 00:05
(4/5): python-nose-0.10.4-3.1.el6.noarch.rpm | 220 kB 00:00
(5/5): python-setuptools-0.6.10-3.el6.noarch.rpm | 336 kB 00:00
--------------------------------------------------------------------------------
Total 455 kB/s | 5.5 MB 00:12
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libgfortran-4.4.7-4.el6.i686 1/5
Installing : python-setuptools-0.6.10-3.el6.noarch 2/5
Installing : python-nose-0.10.4-3.1.el6.noarch 3/5
Installing : atlas-3.8.4-2.el6.i686 4/5
Installing : numpy-1.4.1-9.el6.i686 5/5
Verifying : libgfortran-4.4.7-4.el6.i686 1/5
Verifying : python-nose-0.10.4-3.1.el6.noarch 2/5
Verifying : python-setuptools-0.6.10-3.el6.noarch 3/5
Verifying : numpy-1.4.1-9.el6.i686 4/5
Verifying : atlas-3.8.4-2.el6.i686 5/5
Installed:
numpy.i686 0:1.4.1-9.el6
Dependency Installed:
atlas.i686 0:3.8.4-2.el6 libgfortran.i686 0:4.4.7-4.el6
python-nose.noarch 0:0.10.4-3.1.el6 python-setuptools.noarch 0:0.6.10-3.el6
Complete!
进入Python控制台
[root@hadoop ~]# python
Python 2.6.6 (r266:84292, Feb 21 2013, 23:54:59)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
上述命令将NumPy函数库所以模块引入当前命名空间。
测试NumPy函数库是否安装成功。
>>> numpy.test('full')
Running unit tests for numpy
NumPy version 1.4.1
NumPy is installed in /usr/lib/python2.6/site-packages/numpy
Python version 2.6.6 (r266:84292, Feb 21 2013, 23:54:59) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]
nose version 0.10.4
.................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................K........................................................................................K......................K.K...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
----------------------------------------------------------------------
Ran 2483 tests in 13.305s
OK (KNOWNFAIL=4)
<nose.result.TextTestResult run=2483 errors=0 failures=0>
下面命令构造了一个4*4的随机数组
>>> random.rand(4,4)
array([[ 0.6090939 , 0.18511639, 0.00620341, 0.00928445],
[ 0.53209241, 0.94277941, 0.64429863, 0.71429985],
[ 0.49386549, 0.58188894, 0.12636753, 0.87682062],
[ 0.76079263, 0.99819895, 0.29772295, 0.22701777]])
>>> randMat = mat(random.rand(4,4))
>>> randMat.I
.I操作符矩阵求逆,非常简单吧。
matrix([[-0.75169967, 1.56551245, 0.37303986, -0.28334512],
[ 0.62448373, 1.72969671, -0.79332998, -0.58548171],
[ 0.52886854, -2.02672024, 0.94809578, 0.30486986],
[ 0.47476475, -1.6900491 , -0.13291917, 2.17600247]])
>>> randMat*invRandMat
执行矩阵的乘法,结果是一个单位矩阵,除了对角线元素是1,4*4其他元素都是0,
matrix([[ 1.00000000e+00, 2.76200503e-17, -2.04304347e-18,
1.08962318e-17],
[ -2.06404989e-17, 1.00000000e+00, -1.00205692e-16,
-3.26615904e-17],
[ -2.27682456e-18, 1.83812926e-16, 1.00000000e+00,
-2.73625523e-17],
[ -2.31748214e-17, 1.12865446e-16, -1.75911802e-17,
1.00000000e+00]])
>>> myEye = invRandMat = randMat.I
>>> myEye - eye(4)
matrix([[-1.75169967, 1.56551245, 0.37303986, -0.28334512],
[ 0.62448373, 0.72969671, -0.79332998, -0.58548171],
[ 0.52886854, -2.02672024, -0.05190422, 0.30486986],
[ 0.47476475, -1.6900491 , -0.13291917, 1.17600247]])
2 .安装Matplotlib
[root@hadoop ~]# yum install python-matplotlib
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: centos.ustc.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package python-matplotlib.i686 0:0.99.1.2-1.el6 will be installed
--> Processing Dependency: pytz for package: python-matplotlib-0.99.1.2-1.el6.i686
--> Processing Dependency: python-dateutil for package: python-matplotlib-0.99.1.2-1.el6.i686
--> Running transaction check
---> Package python-dateutil.noarch 0:1.4.1-6.el6 will be installed
---> Package pytz.noarch 0:2010h-2.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================
Installing:
python-matplotlib i686 0.99.1.2-1.el6 base 3.2 M
Installing for dependencies:
python-dateutil noarch 1.4.1-6.el6 base 84 k
pytz noarch 2010h-2.el6 base 35 k
Transaction Summary
=============================================================================================================================================================
Install 3 Package(s)
Total download size: 3.4 M
Installed size: 13 M
Is this ok [y/N]: Y
Downloading Packages:
(1/3): python-dateutil-1.4.1-6.el6.noarch.rpm | 84 kB 00:00
(2/3): python-matplotlib-0.99.1.2-1.el6.i686.rpm | 3.2 MB 00:09
(3/3): pytz-2010h-2.el6.noarch.rpm | 35 kB 00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 355 kB/s | 3.4 MB 00:09
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : python-dateutil-1.4.1-6.el6.noarch 1/3
Installing : pytz-2010h-2.el6.noarch 2/3
Installing : python-matplotlib-0.99.1.2-1.el6.i686 3/3
Verifying : pytz-2010h-2.el6.noarch 1/3
Verifying : python-dateutil-1.4.1-6.el6.noarch 2/3
Verifying : python-matplotlib-0.99.1.2-1.el6.i686 3/3
Installed:
python-matplotlib.i686 0:0.99.1.2-1.el6
Dependency Installed:
python-dateutil.noarch 0:1.4.1-6.el6 pytz.noarch 0:2010h-2.el6
Complete!
[root@hadoop ~]#