這篇文章主要講述Python如何安裝Numpy、Scipy、Matlotlib、Scikit-learn等庫(kù)的過(guò)程及遇到的問(wèn)題解決方法。最近安裝這個(gè)真是一把淚啊,各種不兼容問(wèn)題和報(bào)錯(cuò),希望文章對(duì)你有所幫助吧!你可能遇到的問(wèn)題包括:
ImportError: No module named sklearn 未安裝sklearn包
ImportError: DLL load failed: 找不到指定的模塊
ImportError: DLL load failed: The specified module could not be found
Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
Numpy Install RuntimeError: Broken toolchain: cannot link a simple C program
ImportError: numpy.core.multiarray failed to import
ImportError: cannot import name __check_build
ImportError: No module named matplotlib.pyplot
一. 安裝過(guò)程
最早我是使用"pip install scikit-learn"命令安裝的Scikit-Learn程序,并沒(méi)有注意需要安裝Numpy、Scipy、Matlotlib,然后在報(bào)錯(cuò)"No module named Numpy"后,我接著使用PIP或者下載exe程序安裝相應(yīng)的包,同時(shí)也不理解安裝順序和版本的重要性。其中最終都會(huì)報(bào)錯(cuò)" ImportError: DLL load failed: 找不到指定的模塊",此時(shí)我的解決方法是:
錯(cuò)誤:sklearn ImportError: DLL load failed: 找不到指定的模塊
重點(diǎn):安裝python第三方庫(kù)時(shí)總會(huì)出現(xiàn)各種兼容問(wèn)題,應(yīng)該是版本問(wèn)題,版本需要一致。
第一步:卸載原始版本,包括Numpy、Scipy、Matlotlib、Scikit-Learn
pip uninstall scikit-learn
pip uninstall numpy
pip uninstall scipy
pip uninstall matplotlib
第二步:不使用"pip install package"或"easy_install package"安裝,或者去百度\CSDN下載exe文件,而是去到官網(wǎng)下載相應(yīng)版本。
http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy
http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib
http://www.lfd.uci.edu/~gohlke/pythonlibs/#scikit-learn
安裝過(guò)程中最重要的地方就是版本需要兼容。其中操作系統(tǒng)為64位,Python為2.7.8 64位,下載的四個(gè)whl文件如下,其中cp27表示CPython 2.7版本,cp34表示CPython 3.4,win_arm64指的是64位版本。
numpy-1.10.2-cp27-none-win_amd64.whl
scipy-0.16.1-cp27-none-win_amd64.whl
matplotlib-1.5.0-cp27-none-win_amd64.whl
scikit_learn-0.17-cp27-none-win_amd64.whl
PS:不推薦使用"pip install numpy"安裝或下載如"numpy-MKL-1.8.0.win-amd64-py2.7.exe"類(lèi)似文件,地址如:
http://sourceforge.net/projects/numpy/files/NumPy
http://sourceforge.net/projects/scipy/files/Scipy
第三步:去到Python安裝Scripts目錄下,再使用pip install xxx.whl安裝,先裝Numpy\Scipy\Matlotlib包,再安裝Scikit-Learn。
其中我的python安裝路徑"G:\software\Program software\Python\python insert\Scripts",同時(shí)四個(gè)whl文件安裝核心代碼:
pip install G:\numpy+scipy+matplotlib\numpy-1.10.2-cp27-none-win_amd64.whl
pip install G:\numpy+scipy+matplotlib\scikit_learn-0.17-cp27-none-win_amd64.whl
C:\>G:G:\>cd G:\software\Program software\Python\python insert\ScriptsG:\software\Program software\Python\python insert\Scripts>pip install G:\numpy+scipy+matplotlib\numpy-1.10.2-cp27-none-win_amd64.whlProcessing g:\numpy+scipy+matplotlib\numpy-1.10.2-cp27-none-win_amd64.whlInstalling collected packages: numpySuccessfully installed numpy-1.10.2G:\software\Program software\Python\python insert\Scripts>pip install G:\numpy+scipy+matplotlib\matplotlib-1.5.0-cp27-none-win_amd64.whlInstalling collected packages: matplotlibSuccessfully installed matplotlib-1.5.0G:\software\Program software\Python\python insert\Scripts>pip install G:\numpy+scipy+matplotlib\scipy-0.16.1-cp27-none-win_amd64.whlProcessing g:\numpy+scipy+matplotlib\scipy-0.16.1-cp27-none-win_amd64.whlInstalling collected packages: scipySuccessfully installed scipy-0.16.1
G:\software\Program software\Python\python insert\Scripts>pip install G:\numpy+s
cipy+matplotlib\scikit_learn-0.17-cp27-none-win_amd64.whl
Processing g:\numpy+scipy+matplotlib\scikit_learn-0.17-cp27-none-win_amd64.whl
Installing collected packages: scikit-learn
Successfully installed scikit-learn-0.17
第四步:此時(shí)配置完成,關(guān)鍵是Python64位版本兼容問(wèn)題和Scripts目錄。最后用北郵論壇一個(gè)神人的回復(fù)結(jié)束這個(gè)安裝過(guò)程:“傻孩子,用套件啊,給你介紹一個(gè)Anaconda或winpython。只能幫你到這里了! ”
二. 測(cè)試運(yùn)行環(huán)境
搞了這么半天,為什么要裝這些呢?給幾個(gè)用例驗(yàn)證它的正確安裝和強(qiáng)大吧!
Scikit-Learn是基于python的機(jī)器學(xué)習(xí)模塊,基于BSD開(kāi)源許可。Scikit-learn的基本功能主要被分為六個(gè)部分,分類(lèi),回歸,聚類(lèi),數(shù)據(jù)降維,模型選擇,數(shù)據(jù)預(yù)處理,具體可以參考官方網(wǎng)站上的文檔。
NumPy(Numeric Python)系統(tǒng)是Python的一種開(kāi)源的數(shù)值計(jì)算擴(kuò)展,一個(gè)用python實(shí)現(xiàn)的科學(xué)計(jì)算包。它提供了許多高級(jí)的數(shù)值編程工具,如:矩陣數(shù)據(jù)類(lèi)型、矢量處理,以及精密的運(yùn)算庫(kù)。專(zhuān)為進(jìn)行嚴(yán)格的數(shù)字處理而產(chǎn)生。
內(nèi)容包括:1、一個(gè)強(qiáng)大的N維數(shù)組對(duì)象Array;2、比較成熟的(廣播)函數(shù)庫(kù);3、用于整合C/C++和Fortran代碼的工具包;4、實(shí)用的線(xiàn)性代數(shù)、傅里葉變換和隨機(jī)數(shù)生成函數(shù)。numpy和稀疏矩陣運(yùn)算包scipy配合使用更加方便。
SciPy (pronounced "Sigh Pie") 是一個(gè)開(kāi)源的數(shù)學(xué)、科學(xué)和工程計(jì)算包。它是一款方便、易于使用、專(zhuān)為科學(xué)和工程設(shè)計(jì)的Python工具包,包括統(tǒng)計(jì)、優(yōu)化、整合、線(xiàn)性代數(shù)模塊、傅里葉變換、信號(hào)和圖像處理、常微分方程求解器等等。
Matplotlib是一個(gè)Python的圖形框架,類(lèi)似于MATLAB和R語(yǔ)言。它是python最著名的繪圖庫(kù),它提供了一整套和matlab相似的命令A(yù)PI,十分適合交互式地進(jìn)行制圖。而且也可以方便地將它作為繪圖控件,嵌入GUI應(yīng)用程序中。
第一個(gè)代碼:斜線(xiàn)坐標(biāo),測(cè)試matplotlib
import matplotlibimport numpyimport scipyimport matplotlib.pyplot as pltplt.plot([1,2,3])plt.ylabel('some numbers')plt.show()
運(yùn)行結(jié)果:
第二個(gè)代碼:桃心程序,測(cè)試numpy和matplotlib
代碼參考:Windows 下 Python easy_install 的安裝 - KingsLanding
import numpy as npimport matplotlib.pyplot as pltX = 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 - 225fig = plt.figure()cs = plt.contour(x, y, f, 0, colors = 'r')plt.show()
運(yùn)行結(jié)果:
第三個(gè)程序:顯示Matplotlib強(qiáng)大繪圖交互功能
代碼參考:Python-Matplotlib安裝及簡(jiǎn)單使用 - bery
import numpy as npimport matplotlib.pyplot as plt N = 5menMeans = (20, 35, 30, 35, 27)menStd = (2, 3, 4, 1, 2) ind = np.arange(N) # the x locations for the groupswidth = 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 someax.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()
運(yùn)行結(jié)果:
第四個(gè)代碼:矩陣數(shù)據(jù)集,測(cè)試sklearn
from sklearn import datasetsiris = datasets.load_iris()digits = datasets.load_digits()print digits.data
運(yùn)行結(jié)果:
第五個(gè)代碼:計(jì)算TF-IDF詞語(yǔ)權(quán)重,測(cè)試scikit-learn數(shù)據(jù)分析
參考代碼:http://blog.csdn.net/liuxuejiang158blog/article/details/31360765
# coding:utf-8__author__ = "liuxuejiang"import jiebaimport jieba.posseg as psegimport osimport sysfrom sklearn import feature_extractionfrom sklearn.feature_extraction.text import TfidfTransformerfrom sklearn.feature_extraction.text import CountVectorizerif __name__ == "__main__": corpus=["我 來(lái)到 北京 清華大學(xué)", #第一類(lèi)文本切詞后的結(jié)果 詞之間以空格隔開(kāi) "他 來(lái)到 了 網(wǎng)易 杭研 大廈", #第二類(lèi)文本的切詞結(jié)果 "小明 碩士 畢業(yè) 與 中國(guó) 科學(xué)院", #第三類(lèi)文本的切詞結(jié)果 "我 愛(ài) 北京 天安門(mén)"] #第四類(lèi)文本的切詞結(jié)果 #該類(lèi)會(huì)將文本中的詞語(yǔ)轉(zhuǎn)換為詞頻矩陣,矩陣元素a[i][j] 表示j詞在i類(lèi)文本下的詞頻 vectorizer=CountVectorizer() #該類(lèi)會(huì)統(tǒng)計(jì)每個(gè)詞語(yǔ)的tf-idf權(quán)值 transformer=TfidfTransformer() #第一個(gè)fit_transform是計(jì)算tf-idf,第二個(gè)fit_transform是將文本轉(zhuǎn)為詞頻矩陣 tfidf=transformer.fit_transform(vectorizer.fit_transform(corpus)) #獲取詞袋模型中的所有詞語(yǔ) word=vectorizer.get_feature_names() #將tf-idf矩陣抽取出來(lái),元素a[i][j]表示j詞在i類(lèi)文本中的tf-idf權(quán)重 weight=tfidf.toarray() #打印每類(lèi)文本的tf-idf詞語(yǔ)權(quán)重,第一個(gè)for遍歷所有文本,第二個(gè)for便利某一類(lèi)文本下的詞語(yǔ)權(quán)重 for i in range(len(weight)): print u"-------這里輸出第",i,u"類(lèi)文本的詞語(yǔ)tf-idf權(quán)重------" for j in range(len(word)): print word[j],weight[i][j]
運(yùn)行結(jié)果:
三. 其他錯(cuò)誤解決方法
這里雖然講解幾個(gè)安裝時(shí)遇到的其他錯(cuò)誤及解決方法,但作者更推薦上面的安裝步驟。
在這之前,我反復(fù)的安裝、卸載、升級(jí)包,其中遇到了各種錯(cuò)誤,改了又改,百度了又谷歌。常見(jiàn)PIP用法如下:
* pip install numpy --安裝包numpy* pip uninstall numpy --卸載包numpy* pip show --files PackageName --查看已安裝包* pip list outdated --查看待更新包信息* pip install --upgrade numpy --升級(jí)包* pip install -U PackageName --升級(jí)包* pip search PackageName --搜索包* pip help --顯示幫助信息
ImportError: numpy.core.multiarray failed to import
python安裝numpy時(shí)出現(xiàn)的錯(cuò)誤,這個(gè)通過(guò)stackoverflow和百度也是需要python版本與numpy版本一致,解決的方法包括"pip install -U numpy"升級(jí)或下載指定版本"pip install numpy==1.8"。但這顯然還涉及到更多的包,沒(méi)有前面的卸載下載安裝統(tǒng)一版本的whl靠譜。
Microsoft Visual C++ 9.0 is required(unable to find vcvarsall.bat)
因?yàn)镹umpy內(nèi)部矩陣運(yùn)算是用C語(yǔ)言實(shí)現(xiàn)的,所以需要安裝編譯工具,這和電腦安裝的VC++或VS2012有關(guān),解決方法:如果已安裝Visual Studio則添加環(huán)境變量VS90COMNTOOLS即可,不同的VS版本對(duì)應(yīng)不同的環(huán)境變量值:
Visual Studio 2010 (VS10)設(shè)置 VS90COMNTOOLS=%VS100COMNTOOLS%
Visual Studio 2012 (VS11)設(shè)置 VS90COMNTOOLS=%VS110COMNTOOLS%
Visual Studio 2013 (VS12)設(shè)置 VS90COMNTOOLS=%VS120COMNTOOLS%
但是這并沒(méi)有解決,另一種方法是下載Micorsoft Visual C++ Compiler for Python 2.7的包。
下載地址:http://www.microsoft.com/en-us/download/details.aspx?id=44266
參考文章:http://www.oschina.net/question/2294527_244245
PS:這些問(wèn)題基本解決方法使用pip升級(jí)、版本一致、重新下載相關(guān)版本exe文件再安裝。
總之,最后希望文章對(duì)你有所幫助!尤其是學(xué)習(xí)Python安裝那幾個(gè)包的同學(xué)~寫(xiě)文不易,且看且珍惜!
(By:Eastmount 2015-12-17 早上8點(diǎn)半 http://www.cnblogs.com/eastmount/ )
參考文章:
[Python] Windows7 x64安裝numpy和scipy - delbert
[Python] matplotlib在windows下安裝 - sina
Windows系統(tǒng)下Python與NumPy安裝方法 - bubuko
scikit learn 安裝及注意事項(xiàng) - wbgxx333
Python包numpy、Matplotlib、SciPy在64位Windows上的安裝
windows下安裝scikit learn以及python的各種包
python 機(jī)器學(xué)習(xí)的開(kāi)發(fā)環(huán)境搭建(numpy,scipy,matplotlib)
聯(lián)系客服