100天搞定機(jī)器學(xué)習(xí):寫(xiě)YAML配置文件
↑↑↑點(diǎn)擊上方藍(lán)字,回復(fù)資料,10個(gè)G的驚喜
100天搞定機(jī)器學(xué)習(xí)|Day1-62 合集
大家好,我是老胡
編程中免不了要寫(xiě)配置文件,今天我們繼續(xù)100天搞定機(jī)器學(xué)習(xí)的番外,學(xué)習(xí)一個(gè)比 JSON 更簡(jiǎn)潔和強(qiáng)大的語(yǔ)言————YAML 。本文簡(jiǎn)單介紹 YAML 的語(yǔ)法和用法,以及 YAML 在機(jī)器學(xué)習(xí)項(xiàng)目中的應(yīng)用實(shí)例。歡迎大家一起學(xué)習(xí),也歡迎點(diǎn)贊、在看、分享!
另:yaml的妙用不限于本篇,我會(huì)在后續(xù)的Python網(wǎng)絡(luò)編程專(zhuān)欄再做介紹。
YAML
YAML 是 "YAML Ain't a Markup Language"(YAML 不是一種標(biāo)記語(yǔ)言)的遞歸縮寫(xiě)。YAML 的語(yǔ)法和其他高級(jí)語(yǔ)言類(lèi)似,并且可以簡(jiǎn)單表達(dá)清單、散列表,標(biāo)量等數(shù)據(jù)形態(tài)。它使用空白符號(hào)縮進(jìn)和大量依賴(lài)外觀的特色,特別適合用來(lái)表達(dá)或編輯數(shù)據(jù)結(jié)構(gòu)、各種配置文件、傾印調(diào)試內(nèi)容、文件大綱。YAML 的配置文件后綴為 .yaml
YAML 它的基本語(yǔ)法規(guī)則如下:
大小寫(xiě)敏感 使用縮進(jìn)表示層級(jí)關(guān)系 縮進(jìn)時(shí)不允許使用Tab鍵,只允許使用空格。 縮進(jìn)的空格數(shù)目不重要,只要相同層級(jí)的元素左側(cè)對(duì)齊即可 #號(hào) 表示注釋
YAML 支持的數(shù)據(jù)結(jié)構(gòu)有三種:
對(duì)象:鍵值對(duì)的集合,對(duì)象鍵值對(duì)使用冒號(hào)結(jié)構(gòu)表示 key: value,冒號(hào)后面要加一個(gè)空格。 數(shù)組:一組按次序排列的值,又稱(chēng)為序列/ 列表,用 - 表示。 純量(scalars):?jiǎn)蝹€(gè)的、不可再分的值
YAML 用法
安裝
pip install pyyaml
yaml 文件格式很簡(jiǎn)單,比如:
# categories.yaml file
sports: #注意,冒號(hào)后面要加空格
- soccer # 數(shù)組
- football
- basketball
- cricket
- hockey
- table tennis
countries:
- Pakistan
- USA
- India
- China
- Germany
- France
- Spain
python 讀取 yaml 文件
# read_categories.py file
import yaml
with open(r'categories.yaml') as file:
documents = yaml.full_load(file)
for item, doc in documents.items():
print(item, ":", doc)
運(yùn)行結(jié)果:
sports : ['soccer', 'football', 'basketball', 'cricket', 'hockey', 'table tennis']
countries : ['Pakistan', 'USA', 'India', 'China', 'Germany', 'France', 'Spain']
以上便是 YAML 最基礎(chǔ)的應(yīng)用了,可能大家還是有點(diǎn)一頭霧水,咱們更進(jìn)一步,看看在機(jī)器學(xué)習(xí)項(xiàng)目中如何寫(xiě) YAML 配置文件。
YAML & Machine Learning
我們直接改寫(xiě)100天搞定機(jī)器學(xué)習(xí)|Day62 隨機(jī)森林調(diào)參實(shí)戰(zhàn)中的代碼。
寫(xiě)配置文件rf_config.yaml
#INITIAL SETTINGS
data_directory: ./data/
data_name: creditcard.csv
target_name: Class
test_size: 0.3
model_directory: ./models/
model_name: RF_classifier.pkl
#RF parameters
n_estimators: 50
max_depth: 6
min_samples_split: 5
oob_score: True
random_state: 666
n_jobs: 2
完整代碼,可以對(duì)比源代碼看看區(qū)別:
# rf_with_yaml_file.py
import os
import yaml
import joblib
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
CONFIG_PATH = "./config/"
def load_config(config_name):
with open(os.path.join(CONFIG_PATH, config_name)) as file:
config = yaml.safe_load(file)
return config
config = load_config("rf_config.yaml")
df = pd.read_csv(os.path.join(config["data_directory"], config["data_name"]))
data = df.iloc[:, 1:31]
X = data.loc[:, data.columns != config["target_name"]]
y = data.loc[:, data.columns == config["target_name"]]
number_records_fraud = len(data[data.Class == 1])
fraud_indices = np.array(data[data.Class == 1].index)
normal_indices = data[data.Class == 0].index
random_normal_indices = np.random.choice(
normal_indices, number_records_fraud, replace=False)
random_normal_indices = np.array(random_normal_indices)
under_sample_indices = np.concatenate(
[fraud_indices, random_normal_indices])
under_sample_data = data.iloc[under_sample_indices, :]
X_undersample = under_sample_data.loc[:,
under_sample_data.columns != config["target_name"]]
y_undersample = under_sample_data.loc[:,
under_sample_data.columns == config["target_name"]]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=config["test_size"], random_state=42
)
rf1 = RandomForestClassifier(
n_estimators=config["n_estimators"],
max_depth=config["max_depth"],
min_samples_split=config["min_samples_split"],
oob_score=config["oob_score"],
random_state=config["random_state"],
n_jobs=config["n_jobs"]
)
rf1.fit(X_train, y_train)
print(rf1.oob_score_)
y_predprob1 = rf1.predict_proba(X_test)[:, 1]
print("AUC Score (Train): %f" % roc_auc_score(y_test, y_predprob1))
joblib.dump(rf1, os.path.join(config["model_directory"], config["model_name"]))
reference
https://www.runoob.com/w3cnote/yaml-intro.html
https://www.ruanyifeng.com/blog/2016/07/yaml.html
也可以加一下老胡的微信 圍觀朋友圈~~~
推薦閱讀
(點(diǎn)擊標(biāo)題可跳轉(zhuǎn)閱讀)
論機(jī)器學(xué)習(xí)領(lǐng)域的內(nèi)卷
老鐵,三連支持一下,好嗎?↓↓↓

