OpenCV python sklearn implementation of randomized hyperparameter search
This article describes the implementation of OpenCV python sklearn random hyperparameter search, which is shared as follows:
""" House price prediction dataset Performing hyperparametric searches with sklearn """ import matplotlib as mpl import as plt import numpy as np import sklearn import pandas as pd import os import sys import tensorflow as tf from tensorflow_core.._v2 import keras # Can't use python from import StandardScaler from import fetch_california_housing from sklearn.model_selection import train_test_split, RandomizedSearchCV from import reciprocal ['TF_CPP_MIN_LOG_LEVEL'] = '2' assert tf.__version__.startswith('2.') # 0. Print the version of the imported module print(tf.__version__) print(sys.version_info) for module in mpl, np, sklearn, pd, tf, keras: print("%s version:%s" % (module.__name__, module.__version__)) # Show learning curve def plot_learning_curves(his): ().plot(figsize=(8, 5)) (True) ().set_ylim(0, 1) () # 1. load dataset california house prices housing = fetch_california_housing() print() print() print() # 2. Split dataset Training set Validation set Test set x_train_all, x_test, y_train_all, y_test = train_test_split( , , 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. Data set normalization 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) # Create keras models def build_model(hidden_layers=1, # Parameters of the middle tier layer_size=30, learning_rate=3e-3): # Create the network layer model = () ((layer_size, activation="relu", input_shape=x_train.shape[1:])) # Hidden Layer Settings for _ in range(hidden_layers - 1): ((layer_size, activation="relu")) ((1)) # Optimizer learning rate optimizer = (lr=learning_rate) (loss="mse", optimizer=optimizer) return model def main(): # RandomizedSearchCV # 1. Models converted to sklearn sk_learn_model = .scikit_learn.KerasRegressor(build_model) callbacks = [(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. Define the set of hyperparameters # f(x) = 1/(x*log(b/a)) a <= x <= b param_distribution = { "hidden_layers": [1, 2, 3, 4], "layer_size": (1, 100), "learning_rate": reciprocal(1e-4, 1e-2), } # 3. Execute hypersearch parameters # cross_validation: training set divided into n parts, n-1 training, last validation. 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. Display hyperparameters 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((x_test_scaled, y_test)) # 5. Print the model training process plot_learning_curves(history) if __name__ == '__main__': main()
This is the whole content of this article, I hope it will help you to learn more.
Related articles
python3 use QQ mailbox to send mail
This article is mainly for you to introduce in detail the use of python3 QQ mailbox email, the sample code in the text is very detailed, with certain reference value, interested partners can refer to it2019-01-01python socket module to create and use sockets example details
This article introduces the python socket module to create and use the socket example, there is a need for friends can learn from reference, I hope to be able to help, I wish you more progress, early promotion and salary increase!2023-06-06Write python code to implement a simple raffle drawer
This article mainly introduces the preparation of python code to achieve a simple raffle, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, you can refer to the following friends2020-10-10Summary of python socket stream redirection examples
A socket is a computing network data structure with the concept of a "communication endpoint" as previously described. It is the equivalent of a telephone jack, without which communication is impossible, which is a very good analogy. Today we will summarize the socket stream redirection example2016-03-03Preliminary introduction to the pydoc and distutils modules in Python
This article introduces the Python pydoc module and distutils module , this article is from the official IBM developer technical documents , need friends can refer to the following2015-04-04python implementation of the svn operation and information retrieval
This article mainly introduces the python implementation of the operation of the svn and information to obtain sample processes, there is a need for friends can learn from the reference, I hope to be able to help, I wish you more progress!2021-10-10How to Install Selenium for Python (Recommended)
Selenium is a Web automation testing tools, initially developed for Web site automation testing, Selenium can be called directly to the browser, which supports all major browsers, this article introduces you to Python how to install Selenium, interested parties to take a look at it together!2021-05-05Some programming tips for Python beginners
This article introduces some programming skills to Python beginners, are based on the basis of some programming habits suggested that the need for friends can refer to the following2020-02-02OpenCV learning of image noise and filtering implementation details
This article is mainly for you to introduce in detail the OpenCV image noise and filtering operations related information, the sample code in the text is concise and easy to understand, has a certain reference value, you can refer to it2023-02-02Ways to Specify View Caching in URLconf under Django Framework
This article mainly introduces the Django framework in the URLconf specified in the view cache method , in Python colorful web framework , Django is one of the most popular , you can refer to the friends need .2015-07-07