自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項目開始時應(yīng)該包括的7個設(shè)置

開發(fā) 前端
在開始一項數(shù)據(jù)科學(xué)項目時,我們通常需要進行設(shè)置或配置,以確保所需的依賴關(guān)系,保持輸出穩(wěn)定,準(zhǔn)備通用函數(shù)。本文將介紹JuypterNotebook中最有幫助的一些項目設(shè)置。

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

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

穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項目開始時應(yīng)該包括的7個設(shè)置

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

本文將介紹JuypterNotebook中最有幫助的一些項目設(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)]' 

為確保項目由Python解釋器的最低及以上要求版本運行,可在項目設(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' 

確保項目是由TensorFlow2.0及以上版本運行的,否則會拋出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'

穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項目開始時應(yīng)該包括的7個設(shè)置

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

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

要解決這個問題,可在%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'

穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項目開始時應(yīng)該包括的7個設(shè)置

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

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

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

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

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

若未重置隨機種子,則每次調(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)使隨機數(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]) 

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

5. 多單元輸出

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

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

穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項目開始時應(yīng)該包括的7個設(shè)置

6. 將圖片保存到文件

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

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

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

當(dāng)需要一些自定義圖形設(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_PATHtight_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)建一個images/sub_folder目錄,圖片以“figure_name.png”名稱被保存到目錄中。此外,還提供了三個最常用的設(shè)置:

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

穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項目開始時應(yīng)該包括的7個設(shè)置

穩(wěn)定輸出加速開發(fā):數(shù)據(jù)科學(xué)項目開始時應(yīng)該包括的7個設(shè)置

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

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

編寫一個小腳本,在獲取最新數(shù)據(jù)時運行(也可以設(shè)置一個定期自動執(zhí)行的計劃工作)即可。如果需要在多臺機器上安裝數(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)建一個datasets/group_name目錄,下載housing.tgz,并從該目錄中提取出housing.csv ,這個小函數(shù)也能用于CSV和文本文件。

[[337548]]

圖源:unsplash

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

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

 

責(zé)任編輯:趙寧寧 來源: 今日頭條
相關(guān)推薦

2019-12-03 09:11:57

數(shù)據(jù)科學(xué)編程算法

2012-11-13 10:34:03

PythonWeb

2021-01-29 14:38:36

數(shù)據(jù)科學(xué)數(shù)據(jù)科學(xué)家統(tǒng)計學(xué)

2019-10-14 15:41:37

數(shù)據(jù)科學(xué)GitHub機器學(xué)習(xí)

2021-04-06 08:00:00

數(shù)據(jù)湖存儲技術(shù)

2021-02-20 21:29:40

GitHub代碼開發(fā)者

2024-12-04 08:00:00

數(shù)據(jù)科學(xué)數(shù)據(jù)ETL管道

2019-12-19 14:42:40

開源數(shù)據(jù)科學(xué)項目

2012-09-10 10:26:22

工作工作習(xí)慣調(diào)整心態(tài)

2021-06-29 10:03:45

數(shù)據(jù)科學(xué)機器學(xué)習(xí)算法

2020-09-17 14:20:24

數(shù)據(jù)科學(xué)簡歷崗位

2019-07-03 15:21:47

數(shù)據(jù)科學(xué)統(tǒng)計數(shù)據(jù)數(shù)據(jù)結(jié)構(gòu)

2018-04-09 11:20:40

數(shù)據(jù)科學(xué)項目數(shù)據(jù)

2017-09-11 15:46:36

數(shù)據(jù)科學(xué)語言Java

2017-09-18 10:36:35

Python類庫開發(fā)者

2019-08-07 18:52:40

GPU數(shù)據(jù)科學(xué)CPU

2013-07-04 13:19:24

Java開發(fā)速度

2015-09-01 16:27:31

薪資錯誤

2021-09-13 13:43:43

圖數(shù)據(jù)科學(xué)

2019-10-22 08:00:22

數(shù)據(jù)科學(xué)AWSDC
點贊
收藏

51CTO技術(shù)棧公眾號