NumPy(Numeric Python)系統是Python的一種開源的數值計算擴展,一個用python實現的科學計算包。它提供了許多高級的數值編程工具,如:矩陣數據類型、矢量處理,以及精密的運算庫。專為進行嚴格的數字處理而產生。內容包括:①一個強大的N維數組對象Array;②比較成熟的(廣播)函數庫;③用於整合C/C++和Fortran代碼的工具包;④實用的線性代數、傅里葉變換和隨機數生成函數。numpy和稀疏矩陣運算包scipy配合使用更加方便。
SciPy (Scientific Library for Python,pronounced "Sigh Pie") 是一個開源的數學、科學和工程計算包。它是一款方便、易於使用、專為科學和工程設計的Python工具包,包括統計、優化、整合、線性代數模塊、傅里葉變換、信號和圖像處理、常微分方程求解器等等。
Matplotlib是一個Python的圖形框架,類似於MATLAB和R語言。它是python最著名的繪圖庫,它提供了一整套和matlab相似的命令API,十分適合交互式地進行制圖。而且也可以方便地將它作為繪圖控件,嵌入GUI應用程序中。
Scikit-Learn是基於python的機器學習模塊,基於BSD開源許可。Scikit-learn的基本功能主要被分為六個部分,分類,回歸,聚類,數據降維,模型選擇,數據預處理,具體可以參考官方網站上的文檔。
第一步:卸載原始版本,包括Numpy、Scipy、Matlotlib、Scikit-Learn
pip uninstall scikit-learn
pip uninstall numpy
pip uninstall scipy
pip uninstall matplotlib
第二步:不使用"pip install package"或"easy_install package"安裝,或者去百度下載exe文件,而是去到官網下載相應版本,安裝過程中最重要的地方就是版本需要兼容。
第三步:去到Python安裝Scripts目錄下,再使用pip install xxx.whl安裝,先裝Numpy\Scipy\Matlotlib包,再安裝Scikit-Learn。
Numpy
下載地址:https://pypi.python.org/pypi/numpy/#downloads
這里我沒有使用pip install numpy 進行安裝,而是在Python的Scripts目錄D:\Program Files\Python27\Scripts下使用
pip install D:\python64\numpy-1.11.2+mkl-cp27-cp27m-win_amd64.whl命令。
安裝成功。
Scipy
下載地址:https://pypi.python.org/pypi/scipy/
安裝命令:pip install D:\python64\scipy-0.18.1-cp27-cp27m-win_amd64.whl。
安裝成功。
Matplotlib
下載地址:https://pypi.python.org/pypi/matplotlib/
安裝命令:pip install D:\python64\matplotlib-1.5.3-cp27-cp27m-win_amd64.whl。
安裝成功。
Scikit-learn
下載地址:https://pypi.python.org/simple/scikit-learn/
安裝命令:pip install D:\python64\scikit_learn-0.18-cp27-cp27m-win_amd64.whl
安裝成功。
測試運行環境
第一個代碼:斜線坐標,測試matplotlib
import matplotlib
import numpy
import scipy
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.ylabel('some numbers')
plt.show()
運行結果:
第二個代碼:桃心程序,測試numpy和matplotlib
代碼參考:Windows 下 Python easy_install 的安裝 - KingsLanding
import numpy as np
import matplotlib.pyplot as plt
X = np.arange(-5.0, 5.0, 0.1)
Y = np.arange(-5.0, 5.0, 0.1)
x, y = np.meshgrid(X, Y)
f = 17 * x ** 2 - 16 * np.abs(x) * y + 17 * y ** 2 - 225
fig = plt.figure()
cs = plt.contour(x, y, f, 0, colors = 'r')
plt.show()
運行結果:
第三個程序:顯示Matplotlib強大繪圖交互功能
代碼參考:Python-Matplotlib安裝及簡單使用 - bery
import numpy as np
import matplotlib.pyplot as plt
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)
# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()
運行結果:
第四個代碼:矩陣數據集,測試sklearn
from sklearn import datasets
iris = datasets.load_iris()
digits = datasets.load_digits()
print digits.data
運行結果:
ImportError: numpy.core.multiarray failed to import
python安裝numpy時出現的錯誤,這個通過stackoverflow和百度也是需要python版本與numpy版本一致,解決的方法包括"pip install -U numpy"升級或下載指定版本"pip install numpy==1.8"。但這顯然還涉及到更多的包,沒有前面的卸載下載安裝統一版本的whl靠譜。
Microsoft Visual C++ 9.0 is required(unable to find vcvarsall.bat)
因為Numpy內部矩陣運算是用C語言實現的,所以需要安裝編譯工具,這和電腦安裝的VC++或VS2012有關,解決方法:如果已安裝Visual Studio則添加環境變量VS90COMNTOOLS即可,不同的VS版本對應不同的環境變量值:
Visual Studio 2010 (VS10)設置 VS90COMNTOOLS=%VS100COMNTOOLS%
Visual Studio 2012 (VS11)設置 VS90COMNTOOLS=%VS110COMNTOOLS%
Visual Studio 2013 (VS12)設置 VS90COMNTOOLS=%VS120COMNTOOLS%
但是這並沒有解決,另一種方法是下載Micorsoft Visual C++ Compiler for Python 2.7的包。
PS:這些問題基本解決方法使用pip升級、版本一致、重新下載相關版本文件再安裝。