Scikit-Learn API Design

Scikit-Learn’s API is remarkably well designed. The main design principles are:

  • Consistency. All objects share a consistent and simple interface:

— Estimators. Any object that can estimate some parameters based on a dataset is called an estimator (e.g., an imputer is an estimator). The estimation itself is performed by the fit() method, and it takes only a dataset as a parameter (or two for supervised learning algorithms; the second dataset contains the labels). Any other parameter needed to guide the estimation process is considered a hyperparameter (such as an imputer’s strategy), and it must be set as an instance variable (generally via a constructor parameter).

— Transformers. Some estimators (such as an imputer) can also transform a dataset; these are called transformers. Once again, the API is quite simple: the transformation is performed by the transform() method with the dataset to transform as a parameter. It returns the transformed dataset. This transformation generally relies on the learned parameters, as is the case for an imputer. All transformers also have a convenience method called fit_transform() that is equivalent to calling fit() and then transform() (but sometimes fit_transform() is optimized and runs much faster).

— Predictors. Finally, some estimators are capable of making predictions given a dataset; they are called predictors. For example, the LinearRegression model in the previous chapter was a predictor: it predicted life satisfaction given a country’s GDP per capita. A predictor has a predict() method that takes a dataset of new instances and returns a dataset of corresponding predictions. It also has a score() method that measures the quality of the predictions given a test set (and the corresponding labels in the case of supervised learning algorithms).

  • Inspection. All the estimator’s hyperparameters are accessible directly via public instance variables (e.g., imputer.strategy), and all the estimator’s learned parameters are also accessible via public instance variables with an underscore suffix (e.g., imputer.statistics_).

  • Nonproliferation of classes. Datasets are represented as NumPy arrays or SciPy sparse matrices, instead of homemade classes. Hyperparameters are just regular Python strings or numbers.

  • Composition. Existing building blocks are reused as much as possible. For example, it is easy to create a Pipeline estimator from an arbitrary sequence of transformers followed by a final estimator, as we will see.

  • Sensible defaults. Scikit-Learn provides reasonable default values for most parameters, making it easy to create a baseline working system quickly.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌峡迷,老刑警劉巖拂共,帶你破解...
    沈念sama閱讀 206,214評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叽赊,死亡現(xiàn)場離奇詭異人芽,居然都是意外死亡冰啃,警方通過查閱死者的電腦和手機碾局,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評論 2 382
  • 文/潘曉璐 我一進店門荆残,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人净当,你說我怎么就攤上這事内斯。” “怎么了像啼?”我有些...
    開封第一講書人閱讀 152,543評論 0 341
  • 文/不壞的土叔 我叫張陵俘闯,是天一觀的道長。 經(jīng)常有香客問我忽冻,道長真朗,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,221評論 1 279
  • 正文 為了忘掉前任僧诚,我火速辦了婚禮遮婶,結(jié)果婚禮上蝗碎,老公的妹妹穿的比我還像新娘。我一直安慰自己旗扑,他們只是感情好蹦骑,可當我...
    茶點故事閱讀 64,224評論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著臀防,像睡著了一般眠菇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上袱衷,一...
    開封第一講書人閱讀 49,007評論 1 284
  • 那天捎废,我揣著相機與錄音,去河邊找鬼致燥。 笑死登疗,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的嫌蚤。 我是一名探鬼主播谜叹,決...
    沈念sama閱讀 38,313評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼搬葬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起艳悔,我...
    開封第一講書人閱讀 36,956評論 0 259
  • 序言:老撾萬榮一對情侶失蹤急凰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后猜年,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抡锈,經(jīng)...
    沈念sama閱讀 43,441評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,925評論 2 323
  • 正文 我和宋清朗相戀三年乔外,在試婚紗的時候發(fā)現(xiàn)自己被綠了床三。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,018評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡杨幼,死狀恐怖撇簿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情差购,我是刑警寧澤四瘫,帶...
    沈念sama閱讀 33,685評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站欲逃,受9級特大地震影響找蜜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜稳析,卻給世界環(huán)境...
    茶點故事閱讀 39,234評論 3 307
  • 文/蒙蒙 一洗做、第九天 我趴在偏房一處隱蔽的房頂上張望弓叛。 院中可真熱鬧,春花似錦诚纸、人聲如沸撰筷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽闭专。三九已至,卻和暖如春旧烧,著一層夾襖步出監(jiān)牢的瞬間影钉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評論 1 261
  • 我被黑心中介騙來泰國打工掘剪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留平委,地道東北人。 一個月前我還...
    沈念sama閱讀 45,467評論 2 352
  • 正文 我出身青樓夺谁,卻偏偏與公主長得像廉赔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子匾鸥,可洞房花燭夜當晚...
    茶點故事閱讀 42,762評論 2 345

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,292評論 0 10
  • 我愛過一個美麗的朋友蜡塌,她如春天的風夏日的荷,她孤獨起來有秋的寂寥勿负,也不失冬的傲雪寒霜馏艾。多希望她生命中有一部...
    鮮研閱讀 283評論 0 4
  • 發(fā)個剛剛好
    后滾翻閱讀 131評論 0 0
  • 2月6日,陰轉(zhuǎn)晴奴愉,依然寒冷琅摩。 閱讀書目:《眠》。 作者:村上春樹锭硼,著有《挪威的森林》《海邊的卡夫卡》等房资。他雖然是諾...
    陳陳_19b4閱讀 220評論 0 1
  • 和一個朋友聊天鳖擒,說起讀到一本書很恐怖溉浙,不在于氣氛的渲染或者是可怕的描寫,而是讀到一種今天看來難以理解以至于詭異的邏...
    南審讀書人閱讀 1,283評論 1 5