<jupyter_start><jupyter_text>imports - numpy just to read data<jupyter_code>import numpy as np
import pandas as pd
from keras.layers import Dense
from keras.models import Sequential
from keras.callbacks import EarlyStopping<jupyter_output><empty_output><jupyter_text>parameter 'patience' is the number of epochs to proceed without improvement<jupyter_code>early_stopping_monitor = EarlyStopping(patience=3)<jupyter_output><empty_output><jupyter_text>read the data so we can find the number of nodes in the input layer (n_cols)<jupyter_code>data_file = 'hourly_wages.csv'<jupyter_output><empty_output><jupyter_text>use pandas to examine the file since we had a problem loading into numpy<jupyter_code>df = pd.read_csv(data_file)<jupyter_output><empty_output><jupyter_text>first row was a text headers which numpy could not handle. had to add "skiprows=1" parameter to get the loadtxt method to work<jupyter_code>predictors = np.loadtxt(data_file, delimiter=',', skiprows=1)
df.info()
df.union = df.union.astype('bool')
df.female = df.female.astype('bool')
df.marr = df.marr.astype('bool')
df.south = df.south.astype('bool')
df.manufacturing = df.manufacturing.astype('bool')
df.construction = df.construction.astype('bool')
predictors = df.drop(['wage_per_hour'], axis=1)
from sklearn.preprocessing import Normalizer
import matplotlib.pyplot as plt
df.plot()
df20 = df.head(20)
target = df.wage_per_hour
transformer = Normalizer().fit(df)
df_norm = transformer.transform(df)
df_norm[1:3,1:5]
df_target = df_norm[0:,0:1]
df_target
df_my = pd.DataFrame(df_norm)
target2 = df_my
df_my.columns
target2([1:],[1:])
df20['wage_per_hour'].plot(kind='bar')
target.mean()
target.std()
target
df.head()
n_cols = predictors.shape[1]
n_cols<jupyter_output><empty_output><jupyter_text>Sequential model - each layer connected only to the previous layer<jupyter_code>model = Sequential()<jupyter_output><empty_output><jupyter_text>build up model layer at a time - 'Dense' layers are fully connected<jupyter_code>model.add(Dense(100, activation='relu', input_shape = (n_cols,)))
model.add(Dense(100, activation='relu'))
model.add(Dense(1))<jupyter_output><empty_output><jupyter_text>dump out the model configuration<jupyter_code>model.get_config()<jupyter_output><empty_output><jupyter_text>'adam' is a good, general purpose optimizer that adjusts the learning rate as it goes<jupyter_code>model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>need to split out the target column from the data<jupyter_code>model.fit(predictors, target, validation_split=0.3, epochs=20)
model.fit(predictors, target, validation_split=0.3, epochs=20, 
          callbacks = [early_stopping_monitor])<jupyter_output><empty_output><jupyter_text>this model is not doing very well. Maybe need to look at scaling the data. Find the mean of each feature and divide by standard deviation.<jupyter_code>from keras.models import load_model
model.save('model_file.h5')
my_model = load_model('model_file.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11466840901666123, 'fraction_numerical': 0.01274093433518458, 'mean_word_length': 8.140298507462687, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '57204', 'license': 'Apache-2.0', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1055, 'n_words': 238, 'path': 'keras1.ipynb', 'repo_name': 'greemer/misc', 'size': '3061'}