欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV python sklearn隨機(jī)超參數(shù)搜索的實(shí)現(xiàn)

 更新時(shí)間:2020年01月17日 09:46:42   作者:廷益--飛鳥  
這篇文章主要介紹了OpenCV python sklearn隨機(jī)超參數(shù)搜索的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文介紹了OpenCV python sklearn隨機(jī)超參數(shù)搜索的實(shí)現(xiàn),分享給大家,具體如下:

"""
房?jī)r(jià)預(yù)測(cè)數(shù)據(jù)集 使用sklearn執(zhí)行超參數(shù)搜索
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import tensorflow as tf
from tensorflow_core.python.keras.api._v2 import keras # 不能使用 python
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split, RandomizedSearchCV
from scipy.stats import reciprocal

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
assert tf.__version__.startswith('2.')

# 0.打印導(dǎo)入模塊的版本
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, sklearn, pd, tf, keras:
  print("%s version:%s" % (module.__name__, module.__version__))


# 顯示學(xué)習(xí)曲線
def plot_learning_curves(his):
  pd.DataFrame(his.history).plot(figsize=(8, 5))
  plt.grid(True)
  plt.gca().set_ylim(0, 1)
  plt.show()


# 1.加載數(shù)據(jù)集 california 房?jī)r(jià)
housing = fetch_california_housing()

print(housing.DESCR)
print(housing.data.shape)
print(housing.target.shape)

# 2.拆分?jǐn)?shù)據(jù)集 訓(xùn)練集 驗(yàn)證集 測(cè)試集
x_train_all, x_test, y_train_all, y_test = train_test_split(
  housing.data, housing.target, random_state=7)
x_train, x_valid, y_train, y_valid = train_test_split(
  x_train_all, y_train_all, random_state=11)

print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)

# 3.數(shù)據(jù)集歸一化
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.fit_transform(x_valid)
x_test_scaled = scaler.fit_transform(x_test)


# 創(chuàng)建keras模型
def build_model(hidden_layers=1, # 中間層的參數(shù)
        layer_size=30,
        learning_rate=3e-3):
  # 創(chuàng)建網(wǎng)絡(luò)層
  model = keras.models.Sequential()
  model.add(keras.layers.Dense(layer_size, activation="relu",
                 input_shape=x_train.shape[1:]))
 # 隱藏層設(shè)置
  for _ in range(hidden_layers - 1):
    model.add(keras.layers.Dense(layer_size,
                   activation="relu"))
  model.add(keras.layers.Dense(1))

  # 優(yōu)化器學(xué)習(xí)率
  optimizer = keras.optimizers.SGD(lr=learning_rate)
  model.compile(loss="mse", optimizer=optimizer)

  return model


def main():
  # RandomizedSearchCV

  # 1.轉(zhuǎn)化為sklearn的model
  sk_learn_model = keras.wrappers.scikit_learn.KerasRegressor(build_model)

  callbacks = [keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)]

  history = sk_learn_model.fit(x_train_scaled, y_train, epochs=100,
                 validation_data=(x_valid_scaled, y_valid),
                 callbacks=callbacks)
  # 2.定義超參數(shù)集合
  # f(x) = 1/(x*log(b/a)) a <= x <= b
  param_distribution = {
    "hidden_layers": [1, 2, 3, 4],
    "layer_size": np.arange(1, 100),
    "learning_rate": reciprocal(1e-4, 1e-2),
  }

  # 3.執(zhí)行超搜索參數(shù)
  # cross_validation:訓(xùn)練集分成n份, n-1訓(xùn)練, 最后一份驗(yàn)證.
  random_search_cv = RandomizedSearchCV(sk_learn_model, param_distribution,
                     n_iter=10,
                     cv=3,
                     n_jobs=1)
  random_search_cv.fit(x_train_scaled, y_train, epochs=100,
             validation_data=(x_valid_scaled, y_valid),
             callbacks=callbacks)
  # 4.顯示超參數(shù)
  print(random_search_cv.best_params_)
  print(random_search_cv.best_score_)
  print(random_search_cv.best_estimator_)

  model = random_search_cv.best_estimator_.model
  print(model.evaluate(x_test_scaled, y_test))

  # 5.打印模型訓(xùn)練過程
  plot_learning_curves(history)


if __name__ == '__main__':
  main()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • PyTorch預(yù)訓(xùn)練Bert模型的示例

    PyTorch預(yù)訓(xùn)練Bert模型的示例

    這篇文章主要介紹了PyTorch預(yù)訓(xùn)練Bert模型的示例,幫助大家更好的進(jìn)行機(jī)器學(xué)習(xí),訓(xùn)練模型,感興趣的朋友可以了解下
    2020-11-11
  • 基于TensorBoard中g(shù)raph模塊圖結(jié)構(gòu)分析

    基于TensorBoard中g(shù)raph模塊圖結(jié)構(gòu)分析

    今天小編就為大家分享一篇基于TensorBoard中g(shù)raph模塊圖結(jié)構(gòu)分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python坐標(biāo)軸操作及設(shè)置代碼實(shí)例

    Python坐標(biāo)軸操作及設(shè)置代碼實(shí)例

    這篇文章主要介紹了Python坐標(biāo)軸操作及設(shè)置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Python實(shí)現(xiàn)桌面翻譯工具【新手必學(xué)】

    Python實(shí)現(xiàn)桌面翻譯工具【新手必學(xué)】

    這篇文章主要介紹了Python實(shí)現(xiàn)一個(gè)桌面翻譯工具,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 基于pip install django失敗時(shí)的解決方法

    基于pip install django失敗時(shí)的解決方法

    今天小編就為大家分享一篇基于pip install django失敗時(shí)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python 利用for循環(huán) 保存多個(gè)圖像或者文件的實(shí)例

    python 利用for循環(huán) 保存多個(gè)圖像或者文件的實(shí)例

    今天小編就為大家分享一篇python 利用for循環(huán) 保存多個(gè)圖像或者文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python數(shù)據(jù)分析之文件讀取詳解

    python數(shù)據(jù)分析之文件讀取詳解

    大家好,本篇文章主要講的是python數(shù)據(jù)分析之文件讀取詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)詳解

    python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)詳解

    這篇文章主要給大家介紹了關(guān)于python通過getopt模塊如何獲取執(zhí)行的命令參數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Python實(shí)戰(zhàn)小游戲飛機(jī)大戰(zhàn)詳解

    Python實(shí)戰(zhàn)小游戲飛機(jī)大戰(zhàn)詳解

    飛機(jī)大戰(zhàn)想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫一個(gè)簡(jiǎn)單的飛機(jī)大戰(zhàn)小游戲的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 在win10和linux上分別安裝Python虛擬環(huán)境的方法步驟

    在win10和linux上分別安裝Python虛擬環(huán)境的方法步驟

    這篇文章主要介紹了在win10和linux上分別安裝Python虛擬環(huán)境的方法步驟,虛機(jī)環(huán)境有非常多的優(yōu)點(diǎn),今天我們用的虛擬環(huán)境是virtualenv。感興趣的小伙伴們可以參考一下
    2019-05-05

最新評(píng)論