<jupyter_start><jupyter_text>The Boston Housing Price dataset**We will be attempting to predict the median price of homes in a given Boston suburb in the mid-1970s, given a few data points about the suburb at the time, such as the crime rate, the local property tax rate, etc.****The dataset we will be using has another interesting difference from our two previous examples: it has very few data points, only 506 in total, split between 404 training samples and 102 test samples, and each "feature" in the input data (e.g. the crime rate is a feature) has a different scale. For instance some values are proportions, which take a values between 0 and 1, others take values between 1 and 12, others between 0 and 100...**<jupyter_code>from keras.datasets import boston_housing

(train_data, train_targets), (test_data, test_targets) =  boston_housing.load_data()
train_data.shape
test_data.shape<jupyter_output><empty_output><jupyter_text>The 13 features in the input data are as follow:- Per capita crime rate.- Proportion of residential land zoned for lots over 25,000 square feet.- Proportion of non-retail business acres per town.- Charles River dummy variable (= 1 if tract bounds river; 0 otherwise).- Ni tric oxides concentration (parts per 10 million).- Average number of rooms per dwelling.- Proportion of owner-occupied units built prior to 1940.- Weighted distances to five Boston employment centres.- Index of accessibility to radial highways.- Full-value property-tax rate per $10,000.- Pupil-teacher ratio by town.- 1000 * (Bk - 0.63) ** 2 where Bk is the proportion of Black people by town.- % lower status of the population.**The targets are the median values of owner-occupied homes, in thousands of dollars:**<jupyter_code>train_targets

#Min and Max Price in thousand dollars.
print(train_targets.min())
print(train_targets.max())
train_data<jupyter_output><empty_output><jupyter_text>**Data Normalization by extracting the mean and standard deviation then subtract the mean from every value and divide by the std. This can be performed easily using numpy as follows: **<jupyter_code>mean = train_data.mean(axis=0)
train_data -= mean
std = train_data.std(axis=0)
train_data /= std

test_data -= mean
test_data /= std
train_data.mean()
from keras  import models
from keras  import layers

def  build_model():
    # Because we will need to instantiate
    # the same model multiple times,
    # we use a function to construct it.
    model = models.Sequential()
    model.add(layers.Dense(64, activation='relu',
                           input_shape=(train_data.shape[1],)))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(1))
    model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
    return model
fnn = build_model()
fnn.summary()
import numpy as np

k = 4
num_val_samples = len(train_data) // k
num_epochs = 100
all_scores = []
for i in range(k):
    print('processing fold #', i)
    # Prepare the validation data: data from partition # k
    val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]
    val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]

    # Prepare the training data: data from all other partitions
    partial_train_data = np.concatenate(
        [train_data[:i * num_val_samples],
         train_data[(i + 1) * num_val_samples:]],
        axis=0)
    partial_train_targets = np.concatenate(
        [train_targets[:i * num_val_samples],
         train_targets[(i + 1) * num_val_samples:]],
        axis=0)

    # Build the Keras model (already compiled)
    model = build_model()
    # Train the model (in silent mode, verbose=0)
    model.fit(partial_train_data, partial_train_targets,
              epochs=num_epochs, batch_size=1, verbose=1)
    # Evaluate the model on the validation data
    val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
    all_scores.append(val_mae)

# check scores
all_scores

# average score

np.mean(all_scores)

import matplotlib.pyplot as plt
% matplotlib inline

plt.plot(range(1, len(all_scores) + 1), all_scores)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09060482907004543, 'fraction_numerical': 0.017690652641644752, 'mean_word_length': 4.269521410579345, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '242797', 'license': 'MIT', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1239, 'n_words': 462, 'path': 'Regression with Fully connected layers.ipynb', 'repo_name': 'farisology/Intro_to_Neural_nets_DL', 'size': '4183'}