日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項(xiàng)目開始時(shí)應(yīng)該包括的7個(gè)設(shè)置

本文轉(zhuǎn)載自公眾號“讀芯術(shù)”(ID:AI_Discovery)。

創(chuàng)新互聯(lián)是專業(yè)的豐鎮(zhèn)網(wǎng)站建設(shè)公司,豐鎮(zhèn)接單;提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行豐鎮(zhèn)網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

在開始一項(xiàng)數(shù)據(jù)科學(xué)項(xiàng)目時(shí),我們通常需要進(jìn)行設(shè)置或配置,以確保所需的依賴關(guān)系,保持輸出穩(wěn)定,準(zhǔn)備通用函數(shù)。

項(xiàng)目設(shè)置的一個(gè)案例(來自Handson-ML2)

本文將介紹JuypterNotebook中最有幫助的一些項(xiàng)目設(shè)置。

1. 確保Python版本

檢查JupyterNotebook中的Python解釋器版本:

 
 
 
  1. import sys 
  2. sys.version'3.7.6 (default, Jan 8 2020, 13:42:34) \n[Clang 4.0.1 (tags/RELEASE_401/final)]' 

為確保項(xiàng)目由Python解釋器的最低及以上要求版本運(yùn)行,可在項(xiàng)目設(shè)置中添加以下代碼:

 
 
 
  1. # Python ≥3.7 is required 
  2. import sys 
  3. assert sys.version_info >= (3, 7) 

Python需要為3.7及以上版本,否則會拋出AssertionError。

2. 確保程序包版本

檢查安裝的程序包版本,如TensorFlow。

 
 
 
  1. import tensorflow as tf 
  2. tf.__version__'2.0.0' 

確保項(xiàng)目是由TensorFlow2.0及以上版本運(yùn)行的,否則會拋出AssertionError。

 
 
 
  1. # TensorFlow ≥2.0 is required 
  2. import tensorflow as tf 
  3. assert tf.__version__ >= "2.0" 

3. 避免繪制模糊圖像

JuypterNotebook中的默認(rèn)繪圖看起來有些模糊。例如,一張查找缺失值的簡單熱圖。

(https://towardsdatascience.com/using-pandas-pipe-function-to-improve-code-readability-96d66abfaf8)

 
 
 
  1. import seaborn as sns 
  2. import matplotlib.pyplot as plt 
  3. %matplotlib inline# Default figure format png 
  4. sns.heatmap(df.isnull(), 
  5.             yticklabels=False, 
  6.             cbar=False, 
  7.             cmap='viridis') 

默認(rèn)圖像看起來很模糊

由上圖可以看出,文本很模糊,Cabin欄中的缺失值過于擁擠,Embarked欄中的缺失值無法識別。

要解決這個(gè)問題,可在%matplotlib inline之后使用%config InlineBackend.figure_format='retina'或 %configInlineBackend.figure_format = 'svg',即:

 
 
 
  1. %matplotlib inline 
  2. %config InlineBackend.figure_format = 'retina'         # or 'svg'sns.heatmap(df.isnull(), 
  3.             yticklabels=False, 
  4.             cbar=False, 
  5.             cmap='viridis') 

圖片格式設(shè)置為retina或svg

與先前的圖片比較,上圖更加清晰,Embarked欄中的缺失值也能成功識別。

4. 在不同運(yùn)行中保持輸出穩(wěn)定

數(shù)據(jù)科學(xué)項(xiàng)目中很多地方都在使用隨機(jī)數(shù)字。例如:

  • 來自Scikit-Learn的 train_test_split()
  • 用于初始化權(quán)重的np.random.rand()

若未重置隨機(jī)種子,則每次調(diào)用都會出現(xiàn)不同的數(shù)字:

 
 
 
  1. >>> np.random.rand(4) 
  2. array([0.83209492, 0.10917076, 0.15798519, 0.99356723]) 
  3. >>> np.random.rand(4) 
  4. array([0.46183001, 0.7523687 , 0.96599624, 0.32349079]) 

np.random.seed(0)使隨機(jī)數(shù)字可預(yù)測:

 
 
 
  1. >>> np.random.seed(0) 
  2. >>> np.random.rand(4) 
  3. array([0.5488135 , 0.71518937, 0.60276338, 0.54488318]) 
  4. >>> np.random.seed(0) 
  5. >>> np.random.rand(4) 
  6. array([0.5488135 , 0.71518937, 0.60276338, 0.54488318]) 

如果(每次)都重置隨機(jī)種子,那么每次都會出現(xiàn)相同的數(shù)據(jù)組。因此,項(xiàng)目能在不同運(yùn)行中保持輸出穩(wěn)定。

5. 多單元輸出

默認(rèn)情況下,JupyterNotebook不能在同一單元中輸出多種結(jié)果。要輸出多種結(jié)果,可使用IPython重新配置shell。

 
 
 
  1. from IPython.core.interactiveshell import InteractiveShell 
  2. InteractiveShell.ast_node_interactivity = "all" 

6. 將圖片保存到文件

Matplotlib能通過savefig()方法保存圖片,但如果給定路徑不存在則會引發(fā)錯(cuò)誤。

 
 
 
  1. plt.savefig('./figures/my_plot.png')FileNotFoundError: [Errno 2] Nosuch file or directory: './figures/my_plot.png' 

最好的做法是將所有圖片都放到一個(gè)地方,如工作區(qū)的figures文件夾??墒褂肙S GUI(操作系統(tǒng)界面)或是在JupyterNotebook中運(yùn)行l(wèi)ogic指令,來手動創(chuàng)建一個(gè)figures文件夾,但是最好創(chuàng)建一個(gè)小函數(shù)來實(shí)現(xiàn)該操作。

當(dāng)需要一些自定義圖形設(shè)置或附加子文件夾來分組圖形時(shí),這種方法尤其適用。以下是將圖片保存到文件的函數(shù):

 
 
 
  1. import os 
  2. %matplotlib inline 
  3. import matplotlib.pyplot as plt# Where to save the figures 
  4. PROJECT_ROOT_DIR = "." 
  5. SUB_FOLDER = "sub_folder"    #a sub-folder 
  6. IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images", SUB_FOLDER)defsave_fig(name, images_path=IMAGES_PATH, tight_layout=True,extension="png", resolution=300): 
  7.     if not os.path.isdir(images_path): 
  8.         os.makedirs(images_path) 
  9.     path = os.path.join(images_path, name+ "." + extension) 
  10.     print("Saving figure:",name) 
  11.     if tight_layout: 
  12.         plt.tight_layout() 
  13.     plt.savefig(path, format=extension,dpi=resolution) 

現(xiàn)在調(diào)用save_fig('figure_name'),會在工作區(qū)中創(chuàng)建一個(gè)images/sub_folder目錄,圖片以“figure_name.png”名稱被保存到目錄中。此外,還提供了三個(gè)最常用的設(shè)置:

  • tight_layout 能自動調(diào)整子圖填充
  • extension 能以多種格式保存圖片
  • resolution 可設(shè)置圖片分辨率

7. 下載數(shù)據(jù)(并解壓)

處理網(wǎng)絡(luò)數(shù)據(jù)對于數(shù)據(jù)科學(xué)工作者是常事??梢允褂脼g覽器下載數(shù)據(jù),并運(yùn)行指令來解壓文件,但最好的是創(chuàng)建一個(gè)小函數(shù)來執(zhí)行該操作。當(dāng)數(shù)據(jù)需要定期更改時(shí),這一點(diǎn)尤其重要。

編寫一個(gè)小腳本,在獲取最新數(shù)據(jù)時(shí)運(yùn)行(也可以設(shè)置一個(gè)定期自動執(zhí)行的計(jì)劃工作)即可。如果需要在多臺機(jī)器上安裝數(shù)據(jù)集,自動化抓取數(shù)據(jù)流程也十分有用。

以下是下載并解壓數(shù)據(jù)的函數(shù):

 
 
 
  1. import os 
  2. import tarfile 
  3. import zipfile 
  4. import urllib 
  5.   
  6. # Where to save the data 
  7. PROJECT_ROOT_DIR = "." 
  8. SUB_FOLDER = "group_name" 
  9. LOCAL_PATH = os.path.join(PROJECT_ROOT_DIR, "datasets", SUB_FOLDER)defdownload(file_url, local_path = LOCAL_PATH): 
  10.     if not os.path.isdir(local_path): 
  11.         os.makedirs(local_path) 
  12.         
  13.     # Download file 
  14.     print(">>>downloading") 
  15.     filename = os.path.basename(file_url) 
  16.     file_local_path =os.path.join(local_path, filename) 
  17.     urllib.request.urlretrieve(file_url,file_local_path) 
  18.     
  19.     # untar/unzip file 
  20.     if filename.endswith("tgz")or filename.endswith("tar.gz"): 
  21.         print(">>>unpacking file:", filename) 
  22.         tar =tarfile.open(file_local_path, "r:gz") 
  23.         tar.extractall(path = local_path) 
  24.         tar.close() 
  25.     eliffilename.endswith("tar"): 
  26.         print(">>> unpackingfile:", filename) 
  27.         tar =tarfile.open(file_local_path, "r:") 
  28.         tar.extractall(path = local_path) 
  29.         tar.close() 
  30.     eliffilename.endwith("zip"): 
  31.         print(">>>unpacking file:", filename) 
  32.         zip_file = zipfile.ZipFile(file_local_path) 
  33.         zip_file.extractall(path =local_path) 
  34.         zip_file.close() 
  35.     print("Done") 

現(xiàn)在調(diào)用download("http://a_valid_url/housing.tgz"),會在工作區(qū)創(chuàng)建一個(gè)datasets/group_name目錄,下載housing.tgz,并從該目錄中提取出housing.csv ,這個(gè)小函數(shù)也能用于CSV和文本文件。

圖源:unsplash

請查看筆者Github庫中的源代碼:

https://github.com/BindiChen/machine-learning/blob/master/data-analysis/004-7-setups-for-a-data-science-project/7-setups.ipynb


分享題目:穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項(xiàng)目開始時(shí)應(yīng)該包括的7個(gè)設(shè)置
轉(zhuǎn)載來源:http://www.dlmjj.cn/article/cdechde.html