python 安裝scikit!!!

首先,吐槽一下,真的是折騰好幾天,一會更新這個,一會更新那個,老是各類奇葩問題諸如此類:html

cannot import check-buildjava

pip有新版本,須要更新(黃字)python

其中scipy出錯最多,可是仍是可安裝的git

找不到指定模塊web

no model XXX算法

諸如此類,各類更新就是不行編程

可是下邊的這個文章真的是幫了大忙。。。。。。。。。自己比較懶惰,不想所有卸載後在更新,因此一直各類問題,看了下邊的文章,很是有用,固然有些人的可能真的只是scipy不兼容,那你更新一下就行了,要是還不行,你就全刪了,重新安裝,注意安裝順序數組

http://www.2cto.com/kf/201512/454271.htmlcors

還有版本問題,框架

。。。。。。。。。。。。。。。。。。。。。。。如下是文章內容

這篇文章主要講述Python如何安裝Numpy、Scipy、Matlotlib、Scikit-learn等庫的過程及遇到的問題解決方法。最近安裝這個真是一把淚啊,各類不兼容問題和報錯,但願文章對你有所幫助吧!你可能遇到的問題包括:
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

一. 安裝過程

最先我是使用"pip install scikit-learn"命令安裝的Scikit-Learn程序,並無注意須要安裝Numpy、Scipy、Matlotlib,而後在報錯"No module named Numpy"後,我接着使用PIP或者下載exe程序安裝相應的包,同時也不理解安裝順序和版本的重要性。其中最終都會報錯" ImportError: DLL load failed: 找不到指定的模塊",此時個人解決方法是:

錯誤:sklearn ImportError: DLL load failed: 找不到指定的模塊
重點:安裝python第三方庫時總會出現各類兼容問題,應該是版本問題,版本須要一致。

第一步:卸載原始版本,包括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文件,而是去到官網下載相應版本。
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

安裝過程當中最重要的地方就是版本須要兼容。其中操做系統爲64位,Python爲2.7.8 64位,下載的四個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"相似文件,地址如:
http://sourceforge.net/projects/numpy/files/NumPy
http://sourceforge.net/projects/scipy/files/Scipy

PS(這裏記得下載帶有mkl的)

第三步:去到Python安裝Scripts目錄下,再使用pip install xxx.whl安裝,先裝Numpy\Scipy\Matlotlib包,再安裝Scikit-Learn。

其中個人python安裝路徑"G:\software\Program software\Python\python insert\Scripts",同時四個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\Scripts

G:\software\Program software\Python\python insert\Scripts>pip install G:\numpy+s
cipy+matplotlib\numpy-1.10.2-cp27-none-win_amd64.whl
Processing g:\numpy+scipy+matplotlib\numpy-1.10.2-cp27-none-win_amd64.whl
Installing collected packages: numpy
Successfully installed numpy-1.10.2

G:\software\Program software\Python\python insert\Scripts>pip install G:\numpy+s
cipy+matplotlib\matplotlib-1.5.0-cp27-none-win_amd64.whl
Installing collected packages: matplotlib
Successfully installed matplotlib-1.5.0

G:\software\Program software\Python\python insert\Scripts>pip install G:\numpy+s
cipy+matplotlib\scipy-0.16.1-cp27-none-win_amd64.whl
Processing g:\numpy+scipy+matplotlib\scipy-0.16.1-cp27-none-win_amd64.whl
Installing collected packages: scipy
Successfully 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

 

第四步:此時配置完成,關鍵是Python64位版本兼容問題和Scripts目錄。最後用北郵論壇一個神人的回覆結束這個安裝過程:「傻孩子,用套件啊,給你介紹一個Anaconda或winpython。只能幫你到這裏了! 」

。。。。。。。。。。。。。。。。。。。

因爲時間關係他那個版本你可能找不到了

如下是個人安裝版本僅供參考:

 

。。。。。。。。。。。。。。。。。。。。

 

 

二. 測試運行環境


搞了這麼半天,爲何要裝這些呢?給幾個用例驗證它的正確安裝和強大吧!

Scikit-Learn是基於python的機器學習模塊,基於BSD開源許可。Scikit-learn的基本功能主要被分爲六個部分,分類,迴歸,聚類,數據降維,模型選擇,數據預處理,具體能夠參考官方網站上的文檔。

NumPy(Numeric Python)系統是Python的一種開源的數值計算擴展,一個用python實現的科學計算包。它提供了許多高級的數值編程工具,如:矩陣數據類型、矢量處理,以及精密的運算庫。專爲進行嚴格的數字處理而產生。
內容包括:一、一個強大的N維數組對象Array;二、比較成熟的(廣播)函數庫;三、用於整合C/C++和Fortran代碼的工具包;四、實用的線性代數、傅里葉變換和隨機數生成函數。numpy和稀疏矩陣運算包scipy配合使用更加方便。

SciPy (pronounced "Sigh Pie") 是一個開源的數學、科學和工程計算包。它是一款方便、易於使用、專爲科學和工程設計的Python工具包,包括統計、優化、整合、線性代數模塊、傅里葉變換、信號和圖像處理、常微分方程求解器等等。

Matplotlib是一個Python的圖形框架,相似於MATLAB和R語言。它是python最著名的繪圖庫,它提供了一整套和matlab類似的命令API,十分適合交互式地進行製圖。並且也能夠方便地將它做爲繪圖控件,嵌入GUI應用程序中。


第一個代碼:斜線座標,測試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

運行結果:

\


第五個代碼:計算TF-IDF詞語權重,測試scikit-learn數據分析(以上及個均測試)
參考代碼:http://blog.csdn.net/liuxuejiang158blog/article/details/31360765

# coding:utf-8
__author__ = "liuxuejiang"
import jieba
import jieba.posseg as pseg
import os
import sys
from sklearn import feature_extraction
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer

if __name__ == "__main__":
    corpus=["我 來到 北京 清華大學",     #第一類文本切詞後的結果 詞之間以空格隔開
        "他 來到 了 網易 杭研 大廈",     #第二類文本的切詞結果
        "小明 碩士 畢業 與 中國 科學院",  #第三類文本的切詞結果
        "我 愛 北京 天安門"]            #第四類文本的切詞結果

    #該類會將文本中的詞語轉換爲詞頻矩陣,矩陣元素a[i][j] 表示j詞在i類文本下的詞頻
    vectorizer=CountVectorizer()

    #該類會統計每一個詞語的tf-idf權值
    transformer=TfidfTransformer()

    #第一個fit_transform是計算tf-idf,第二個fit_transform是將文本轉爲詞頻矩陣
    tfidf=transformer.fit_transform(vectorizer.fit_transform(corpus))

    #獲取詞袋模型中的全部詞語
    word=vectorizer.get_feature_names()

    #將tf-idf矩陣抽取出來,元素a[i][j]表示j詞在i類文本中的tf-idf權重
    weight=tfidf.toarray()

    #打印每類文本的tf-idf詞語權重,第一個for遍歷全部文本,第二個for便利某一類文本下的詞語權重
    for i in range(len(weight)):
        print u"-------這裏輸出第",i,u"類文本的詞語tf-idf權重------"
        for j in range(len(word)):
            print word[j],weight[i][j]

運行結果:
 

\

三. 其餘錯誤解決方法


這裏雖然講解幾個安裝時遇到的其餘錯誤及解決方法,但做者更推薦上面的安裝步驟。

在這以前,我反覆的安裝、卸載、升級包,其中遇到了各類錯誤,改了又改,百度了又谷歌。常見PIP用法以下:

 

* pip install numpy             --安裝包numpy
* pip uninstall numpy           --卸載包numpy
* pip show --files PackageName  --查看已安裝包
* pip list outdated             --查看待更新包信息
* pip install --upgrade numpy   --升級包
* pip install -U PackageName    --升級包
* pip search PackageName        --搜索包
* pip help                      --顯示幫助信息


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升級、版本一致、從新下載相關版本exe文件再安裝。

但願對你有幫助

固然有的外文帖子說讓你在cannot import   check-build時候刪掉sklearn裏的import check-build 那幾句,固然的確是不會再報錯了,可是你要把ELSE 那段都刪了,要不下一句又又錯了,並且刪除了只是保證你import sklearn時候沒錯,一旦你要用sklearn裏的算法,仍是會有錯,根本解決不了問題,,,,,不要怕麻煩,從新裝吧

相關文章
相關標籤/搜索