Using Keras depth learning to implement regression problem examples _ depth learning

Source: Internet
Author: User
Tags random seed wrappers keras

Usually, we use deep learning to classify, but sometimes it is used to do regression.
Original source: Regression Tutorial with the Keras Deep Learning Library in Python
1. Here the author uses keras and Python's Scikit-learn machine learning Library To achieve the return of housing prices forecast. About Scikit-learn and Keras Federated Reference Scikit-learn interface wrapper
2. In the first routine, the author introduces how to import data and then constructs a single hidden layer neural network. Because of the time regression problem, the author does not use the activation function at the output layer. Then the sequential model is used as a part of the Scikit-learn workflow using the Scikit-learn interface wrapper, and the cross validation method is used to evaluate the model.
3. In the second routine, the author normalizes the data and evaluates its effect
4. In the third routine, the author changes the depth and width of the network and looks at the effect.
5. The code and dataset are all in the original text, after the source is sorted and then posted here:
housing_price.py

Import numpy Import pandas from keras.models import sequential to keras.layers import dense from keras.wrappers.scikit_l
Earn import Kerasregressor from sklearn.model_selection import cross_val_score from sklearn.model_selection import KFold From sklearn.preprocessing import Standardscaler from sklearn.pipeline import pipeline # load DataSet Dataframe = Pandas. Read_csv ("Housing.csv", Delim_whitespace=true, header=none) DataSet = dataframe.values # split into input (X) and output (  Y) Variables X = dataset[:, 0:13] Y = dataset[:, # define base mode Def baseline_model (): # Create Model = Sequential () model.add (Dense (input_dim=13, init= ' normal ', activation= ' Relu ')) Model.add (dense (1, init= ' norm Al ') # Compile model Model.compile (loss= ' mean_squared_error ', optimizer= ' Adam ') return Model # fix random SE ed for reproducibility seed = 7 Numpy.random.seed (SEED) # Evaluate model with standardized DataSet estimator = Kerasregres Sor (Build_fn=baseline_modEl, nb_epoch=100, Batch_size=5, verbose=0) # Use 10-fold Cross validation to evaluate this baseline model Kfold = Kfold (n_ splits=10, random_state=seed) results = Cross_val_score (estimator, X, Y, cv=kfold) print ("Results:%.2f (%.2f) MSE" (Res Ults.mean (), RESULTS.STD ())

housing_price2.py

Import numpy Import pandas from keras.models import sequential to keras.layers import dense from keras.wrappers.scikit_l
Earn import Kerasregressor from sklearn.model_selection import cross_val_score from sklearn.model_selection import KFold From sklearn.preprocessing import Standardscaler from sklearn.pipeline import pipeline # load DataSet Dataframe = Pandas. Read_csv ("Housing.csv", Delim_whitespace=true, header=none) DataSet = dataframe.values # split into input (X) and output (  Y) Variables X = dataset[:, 0:13] Y = dataset[:, # define base mode Def baseline_model (): # Create Model = Sequential () model.add (Dense (input_dim=13, init= ' normal ', activation= ' Relu ')) Model.add (dense (1, init= ' norm Al ') # Compile model Model.compile (loss= ' mean_squared_error ', optimizer= ' Adam ') return Model # fix random SE ed for reproducibility seed = 7 Numpy.random.seed (SEED) # Evaluate model with standardized dataset estimators = [] Estimat Ors.append (' Standardize ', Standardscaler ()) estimators.append (' MLP ', Kerasregressor (Build_fn=baseline_model, nb_epoch=50, Batch_size=5, verbose=0)) pipeline = Pipeline (estimators) # Use 10-fold Cross validation to evaluate this baseline model Kfold = Kfold (  n_splits=10, random_state=seed) results = Cross_val_score (pipeline, X, Y, cv=kfold) print ("Standardized:%.2f (%.2f) MSE" % (Results.mean (), RESULTS.STD ())

housing_price3.py

Import numpy Import pandas from keras.models import sequential to keras.layers import dense from keras.wrappers.scikit_l
Earn import Kerasregressor from sklearn.model_selection import cross_val_score from sklearn.model_selection import KFold From sklearn.preprocessing import Standardscaler from sklearn.pipeline import pipeline # load DataSet Dataframe = Pandas. Read_csv ("Housing.csv", Delim_whitespace=true, header=none) DataSet = dataframe.values # split into input (X) and output (  Y) Variables X = dataset[:, 0:13] Y = dataset[:, # define base mode Def baseline_model (): # Create Model = Sequential () model.add (Dense (input_dim=13, init= ' normal ', activation= ' Relu ')) Model.add (Dense (6, init= ' norm Al ', activation= ' Relu ')) Model.add (dense (1, init= ' normal ')) # Compile model Model.compile (loss= ' mean_squared_e Rror ', optimizer= ' Adam ') return Model # fix random seed for reproducibility seed = 7 Numpy.random.seed (seed) # Evalua Te model with standardized DataSet estimators = [] estimators.append (' Standardize ', Standardscaler ()) estimators.append (' MLP ', Kerasregressor (Build_fn=baseline_model, nb_epoch=50, Batch_size=5, verbose=0)) pipeline = Pipeline (estimators) # Use 10-fold Cross validation to evaluate this baseline model Kfold = Kfold (n_splits=10, random_state=seed) results = Cross_val _score (Pipeline, X, Y, cv=kfold) print ("Standardized:%.2f (%.2f) MSE"% (Results.mean (), RESULTS.STD ())

housing_price4.py

Import numpy Import pandas from keras.models import sequential to keras.layers import dense from keras.wrappers.scikit_l
Earn import Kerasregressor from sklearn.model_selection import cross_val_score from sklearn.model_selection import KFold From sklearn.preprocessing import Standardscaler from sklearn.pipeline import pipeline # load DataSet Dataframe = Pandas. Read_csv ("Housing.csv", Delim_whitespace=true, header=none) DataSet = dataframe.values # split into input (X) and output (  Y) Variables X = dataset[:, 0:13] Y = dataset[:, # define base mode Def baseline_model (): # Create Model = Sequential () model.add (Dense (input_dim=13, init= ' normal ', activation= ' Relu ')) Model.add (dense (1, init= ' norm Al ') # Compile model Model.compile (loss= ' mean_squared_error ', optimizer= ' Adam ') return Model # fix random SE ed for reproducibility seed = 7 Numpy.random.seed (SEED) # Evaluate model with standardized dataset estimators = [] Estimat Ors.append (' Standardize ', Standardscaler ()) estimators.append (' MLP ', Kerasregressor (Build_fn=baseline_model, nb_epoch=50, Batch_size=5, verbose=0)) pipeline = Pipeline (estimators) # Use 10-fold Cross validation to evaluate this baseline model Kfold = Kfold (  n_splits=10, random_state=seed) results = Cross_val_score (pipeline, X, Y, cv=kfold) print ("Standardized:%.2f (%.2f) MSE" % (Results.mean (), RESULTS.STD ())

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.