溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些

發(fā)布時(shí)間:2021-10-29 09:46:13 來源:億速云 閱讀:99 作者:iii 欄目:web開發(fā)

這篇文章主要介紹“JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些”,在日常操作中,相信很多人在JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

1. 確保Python版本

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

import sys 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è)置中添加以下代碼:

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

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

2. 確保程序包版本

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

import tensorflow as tf tf.__version__'2.0.0'

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

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

3. 避免繪制模糊圖像

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

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

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

JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些

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

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

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

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

JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些

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

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

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)用都會(huì)出現(xiàn)不同的數(shù)字:

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

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

>>> np.random.seed(0) >>> np.random.rand(4) array([0.5488135 , 0.71518937, 0.60276338, 0.54488318]) >>> np.random.seed(0) >>> np.random.rand(4) array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])

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

5. 多單元輸出

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

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

JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些

6. 將圖片保存到文件

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

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指令,來手動(dòng)創(chuàng)建一個(gè)figures文件夾,但是最好創(chuàng)建一個(gè)小函數(shù)來實(shí)現(xiàn)該操作。

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

import os %matplotlib inline import matplotlib.pyplot as plt# Where to save the figures PROJECT_ROOT_DIR = "." SUB_FOLDER = "sub_folder"    #a sub-folder 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):     if not os.path.isdir(images_path):         os.makedirs(images_path)     path = os.path.join(images_path, name+ "." + extension)     print("Saving figure:",name)     if tight_layout:         plt.tight_layout()     plt.savefig(path, format=extension,dpi=resolution)

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

  • tight_layout 能自動(dòng)調(diào)整子圖填充

  • extension 能以多種格式保存圖片

  • resolution 可設(shè)置圖片分辨率

JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些

JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些

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

處理網(wǎng)絡(luò)數(shù)據(jù)對(duì)于數(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è)定期自動(dòng)執(zhí)行的計(jì)劃工作)即可。如果需要在多臺(tái)機(jī)器上安裝數(shù)據(jù)集,自動(dòng)化抓取數(shù)據(jù)流程也十分有用。

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

import os import tarfile import zipfile import urllib   # Where to save the data PROJECT_ROOT_DIR = "." SUB_FOLDER = "group_name" LOCAL_PATH = os.path.join(PROJECT_ROOT_DIR, "datasets", SUB_FOLDER)defdownload(file_url, local_path = LOCAL_PATH):     if not os.path.isdir(local_path):         os.makedirs(local_path)             # Download file     print(">>>downloading")     filename = os.path.basename(file_url)     file_local_path =os.path.join(local_path, filename)     urllib.request.urlretrieve(file_url,file_local_path)         # untar/unzip file     if filename.endswith("tgz")or filename.endswith("tar.gz"):         print(">>>unpacking file:", filename)         tar =tarfile.open(file_local_path, "r:gz")         tar.extractall(path = local_path)         tar.close()     eliffilename.endswith("tar"):         print(">>> unpackingfile:", filename)         tar =tarfile.open(file_local_path, "r:")         tar.extractall(path = local_path)         tar.close()     eliffilename.endwith("zip"):         print(">>>unpacking file:", filename)         zip_file = zipfile.ZipFile(file_local_path)         zip_file.extractall(path =local_path)         zip_file.close()     print("Done")

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

到此,關(guān)于“JuypterNotebook中最有幫助的項(xiàng)目設(shè)置有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI