tensorflow2------迴歸問題california_housing_dataset

import matplotlib as mpl #畫圖用的庫
import matplotlib.pyplot as plt
#下面這一句是爲了可以在notebook中畫圖
%matplotlib inline
import numpy as np
import sklearn   #機器學習算法庫
import pandas as pd #處理數據的庫   
import os
import sys
import time
import tensorflow as tf

from tensorflow import keras   #使用tensorflow中的keras
#import keras #單純的使用keras

print(tf.__version__)
print(sys.version_info)
for module in mpl, np, sklearn, pd, tf, keras:
    print(module.__name__, module.__version__)



2.0.0
sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)
matplotlib 3.1.2
numpy 1.18.0
sklearn 0.22
pandas 0.25.3
tensorflow 2.0.0
tensorflow_core.keras 2.2.4-tf
#引用位於sklearn數據集中的房價預測數據集
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()
print(housing.DESCR) #數據集的描述
print(housing.data.shape) #相當於 x
print(housing.target.shape) #相當於 y




Downloading Cal. housing from https://ndownloader.figshare.com/files/5976036 to /home/galaxy/scikit_learn_data
.. _california_housing_dataset:

California Housing dataset
--------------------------

**Data Set Characteristics:**

    :Number of Instances: 20640 #當前數據集中有20640個樣本

    :Number of Attributes: 8 numeric, predictive attributes and the target #每個樣本有8個特徵

    :Attribute Information: #8個特徵如下
        - MedInc        median income in block #收入
        - HouseAge      median house age in block #房屋年齡
        - AveRooms      average number of rooms #每套房屋的房間數
        - AveBedrms     average number of bedrooms # 。。。
        - Population    block population
        - AveOccup      average house occupancy
        - Latitude      house block latitude
        - Longitude     house block longitude

    :Missing Attribute Values: None

This dataset was obtained from the StatLib repository.
http://lib.stat.cmu.edu/datasets/

The target variable is the median house value for California districts.

This dataset was derived from the 1990 U.S. census, using one row per census
block group. A block group is the smallest geographical unit for which the U.S.
Census Bureau publishes sample data (a block group typically has a population
of 600 to 3,000 people).

It can be downloaded/loaded using the
:func:`sklearn.datasets.fetch_california_housing` function.

.. topic:: References

    - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,
      Statistics and Probability Letters, 33 (1997) 291-297

(20640, 8) # data是20640*8的一個矩陣,8的意思是每個樣本有8個特徵
(20640,) # target就是表示有20640個目標值
#查看當前x、y的數據類型

import pprint #pprint()模塊打印出來的數據結構更加完整,對於數據結構比較複雜、數據長度較長的數據,適合採用pprint()

pprint.pprint(housing.data[0:5])
pprint.pprint(housing.target[0:5])



array([[ 8.32520000e+00,  4.10000000e+01,  6.98412698e+00,
         1.02380952e+00,  3.22000000e+02,  2.55555556e+00,
         3.78800000e+01, -1.22230000e+02],
       [ 8.30140000e+00,  2.10000000e+01,  6.23813708e+00,
         9.71880492e-01,  2.40100000e+03,  2.10984183e+00,
         3.78600000e+01, -1.22220000e+02],
       [ 7.25740000e+00,  5.20000000e+01,  8.28813559e+00,
         1.07344633e+00,  4.96000000e+02,  2.80225989e+00,
         3.78500000e+01, -1.22240000e+02],
       [ 5.64310000e+00,  5.20000000e+01,  5.81735160e+00,
         1.07305936e+00,  5.58000000e+02,  2.54794521e+00,
         3.78500000e+01, -1.22250000e+02],
       [ 3.84620000e+00,  5.20000000e+01,  6.28185328e+00,
         1.08108108e+00,  5.65000000e+02,  2.18146718e+00,
         3.78500000e+01, -1.22250000e+02]])
array([4.526, 3.585, 3.521, 3.413, 3.422])
#用sklearn中專門用於劃分訓練集和測試集的方法
from sklearn.model_selection import train_test_split

#train_test_split默認將數據劃分爲3:1,我們可以通過修改test_size值來改變數據劃分比例(默認0.25,即3:1)
#將總數乘以test_size就表示test測試集、valid驗證集數量
#將數據集整體拆分爲train_all和test數據集
x_train_all,x_test, y_train_all,y_test = train_test_split(housing.data, housing.target, random_state=7)
#將train_all數據集拆分爲train訓練集和valid驗證集
x_train,x_valid, y_train,y_valid = train_test_split(x_train_all, y_train_all, random_state=11)

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



(15480, 8) (15480,)
(5160, 8) (5160,)
(11610, 8) (11610,)
(3870, 8) (3870,)
#訓練數據歸一化處理
# x = (x - u)/std  u爲均值,std爲方差
from sklearn.preprocessing import StandardScaler #使用sklearn中的StandardScaler實現訓練數據歸一化

scaler = StandardScaler()#初始化一個scaler對象
x_train_scaler = scaler.fit_transform(x_train)#x_train已經是二維數據了,無需astype轉換
x_valid_scaler = scaler.transform(x_valid)
x_test_scaler  = scaler.transform(x_test)
#tf.keras.models.Sequential()建立模型

model = keras.models.Sequential([
    keras.layers.Dense(30, activation="relu",input_shape=x_train.shape[1:]),
    keras.layers.Dense(1),
])
#編譯model。 loss目標函數爲均方差,這裏表面上是字符串,實際上tensorflow中會映射到對應的算法函數,我們也可以自定義
model.compile(loss="mean_squared_error", optimizer="adam")
#查看model有多少層
model.layers



[<tensorflow.python.keras.layers.core.Dense at 0x7f07b754f2b0>,
 <tensorflow.python.keras.layers.core.Dense at 0x7f0744dd82b0>]
#查看model的架構
model.summary()



Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 30)                270       
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 31        
=================================================================
Total params: 301
Trainable params: 301
Non-trainable params: 0
_________________________________________________________________
#使用監聽模型訓練過程中的callbacks

logdir='./callbacks_regression'
if not os.path.exists(logdir):
    os.mkdir(logdir)
output_model_file = os.path.join(logdir,"regression_california_housing.h5")

#首先定義一個callback數組
callbacks = [
    keras.callbacks.TensorBoard(logdir),
    keras.callbacks.ModelCheckpoint(output_model_file,save_best_only=True),
    keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)
]

history=model.fit(x_train_scaler,y_train,epochs=100,
                 validation_data=(x_valid_scaler,y_valid),
                 callbacks=callbacks)



Train on 11610 samples, validate on 3870 samples
Epoch 1/100
11610/11610 [==============================] - 2s 164us/sample - loss: 1.9563 - val_loss: 0.8091
Epoch 2/100
11610/11610 [==============================] - 1s 59us/sample - loss: 0.6244 - val_loss: 0.5893
Epoch 3/100
11610/11610 [==============================] - 1s 58us/sample - loss: 0.4935 - val_loss: 0.4895
Epoch 4/100
11610/11610 [==============================] - 1s 58us/sample - loss: 0.4372 - val_loss: 0.4474
。。。
Epoch 44/100
11610/11610 [==============================] - 1s 58us/sample - loss: 0.3136 - val_loss: 0.3218
Epoch 45/100
11610/11610 [==============================] - 1s 56us/sample - loss: 0.3084 - val_loss: 0.3294
Epoch 46/100
11610/11610 [==============================] - 1s 55us/sample - loss: 0.3098 - val_loss: 0.3243
#上面迭代訓練了46次即停止,使用了EarlyStopping,發現後面再也無法進行優化,即停止迭代訓練
#打印模型訓練過程中的相關曲線
def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8,5))
    plt.grid(True)
    plt.gca().set_ylim(0,1)
    plt.show()
plot_learning_curves(history)

model.evaluate(x_test_scaler,y_test)



5160/1 [===========。。。=======] - 0s 24us/sample - loss: 0.4707
0.33584680608076645

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章