<jupyter_start><jupyter_text>Artificial Neural Network  Example 1: Housing Data<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv('../data/kc_house_data.csv')
df.head(n=3)
df.drop(['id', 'date'], axis=1, inplace=True)
df.describe()
selected_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'sqft_basement', 'sqft_above', 'floors', 'yr_built']

X = df[selected_features]
y = df['price']
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
from sklearn.preprocessing import MinMaxScaler

sc = MinMaxScaler().fit(X_train)
X_train_scaled = sc.transform(X_train)
X_test_scaled = sc.transform(X_test)
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(50, input_dim=X_train_scaled.shape[1], activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='linear'))
model.summary()
model.compile(optimizer='Adam', loss='mean_squared_error')
epochs_hist = model.fit(X_train_scaled, y_train, epochs=100, batch_size=50, validation_split=0.2)
plt.plot(epochs_hist.history['loss'])
plt.plot(epochs_hist.history['val_loss'])
plt.title('Training Neural Network');
plt.xlabel('epoch');
plt.ylabel('loss');
plt.legend(['Training Loss', 'Validation Loss']);
predictions = model.predict(X_test_scaled)
delta = y_test - predictions.reshape(-1)

plt.scatter(range(len(delta)), delta);
plt.xlabel('Test Sample Index');
plt.ylabel('Truth - Model Prediction');
plt.title('Prediction Delta');
from sklearn.metrics import r2_score

r2 = r2_score(y_test, predictions)
print('R2:', r2)<jupyter_output>R2: 0.5131462015151431<jupyter_text>Ways to improve/change model:- Use dropout layers- Change selected features- Measure correlation between features and target- Make model larger- Change activations<jupyter_code>from keras.layers import Dropout
X = df.drop(['price'], axis=1)
y = df['price']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

sc = MinMaxScaler().fit(X_train)
X_train_scaled = sc.transform(X_train)
X_test_scaled = sc.transform(X_test)
model = Sequential()
model.add(Dense(100, input_dim=X_train_scaled.shape[1], activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='linear'))
model.summary()
model.compile(optimizer='Adam', loss='mean_squared_error')
epochs_hist = model.fit(X_train_scaled, y_train, epochs=100, batch_size=50, validation_split=0.2)
plt.plot(epochs_hist.history['loss'])
plt.plot(epochs_hist.history['val_loss'])
plt.title('Training Neural Network');
plt.xlabel('epoch');
plt.ylabel('loss');
plt.legend(['Training Loss', 'Validation Loss']);
predictions = model.predict(X_test_scaled)
delta = y_test - predictions.reshape(-1)

plt.scatter(range(len(delta)), delta);
plt.xlabel('Test Sample Index');
plt.ylabel('Truth - Model Prediction');
plt.title('Prediction Delta');
r2 = r2_score(y_test, predictions)
print('R2:', r2)<jupyter_output>R2: 0.6835791040821051
{'fraction_non_alphanumeric': 0.15285219035612985, 'fraction_numerical': 0.028679483138985187, 'mean_word_length': 8.146974063400576, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '665400', 'license': 'MIT', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1154, 'n_words': 223, 'path': 'notebooks/artificial_neural_network.ipynb', 'repo_name': 'Joshua-Robison/MLRegression', 'size': '3173'}