<jupyter_start><jupyter_text>The Data** Source: https://datamarket.com/data/set/22ox/monthly-milk-production-pounds-per-cow-jan-62-dec-75!ds=22ox&display=line ****Monthly milk production: pounds per cow. Jan 62 - Dec 75** ** Import numpy pandas and matplotlib **<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>** Use pandas to read the csv of the monthly-milk-production.csv file and set index_col='Month' **<jupyter_code>from google.colab import drive 
drive.mount('/content/gdrive')
milk = pd.read_csv("gdrive/My Drive/dataML/monthly-milk-production.csv",index_col='Month')

#milk = pd.read_csv("monthly-milk-production.csv",index_col='Month')
milk.head()<jupyter_output><empty_output><jupyter_text>** Check out the head of the dataframe** ** Make the index a time series by using: **    milk.index = pd.to_datetime(milk.index)<jupyter_code>milk.index = pd.to_datetime(milk.index)
milk.head()
milk.keys()<jupyter_output><empty_output><jupyter_text>** Plot out the time series data. **<jupyter_code>milk.plot()<jupyter_output><empty_output><jupyter_text>Train Test Split** Let's attempt to predict a year's worth of data. (12 months or 12 steps into the future) **<jupyter_code>milk.info()
len(milk)
train_set= milk.head((1976-1962-1)*12)
test_set = milk.tail(12)<jupyter_output><empty_output><jupyter_text>Scale the Data** Use sklearn.preprocessing to scale the data using the MinMaxScaler. Remember to only fit_transform on the training data, then transform the test data. You shouldn't fit on the test data as well, otherwise you are assuming you would know about future behavior!**<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
train_scaled = scaler.fit_transform(train_set)
test_scaled = scaler.transform(test_set)<jupyter_output><empty_output><jupyter_text>Build Training Data(many to many)* X_train: Past 12 monthes productions, shape: (-1,12)* Y_train: Future 12 month productions, shape:(-1, 12) * Shift the window to get more trainning data<jupyter_code>def build_train_data(data, past_monthes = 12, future_monthes = 12):
  X_train, Y_train = [],[]
  
  for i in range(data.shape[0] + 1 - past_monthes - future_monthes):
    X_train.append(np.array(data[i:i + past_monthes]))
    Y_train.append(np.array(data[i + past_monthes:i + past_monthes + future_monthes]))
    
  return np.array(X_train).reshape([-1,12]), np.array(Y_train).reshape([-1,12])
   
x, y = build_train_data(train_scaled)
print('x shape;', x.shape, 'y shape: ', y.shape)<jupyter_output>x shape; (132, 12) y shape:  (132, 12)<jupyter_text>Take a look at the data<jupyter_code>plt.plot(x[12])
plt.plot(y[12])<jupyter_output><empty_output><jupyter_text>Keras<jupyter_code>from tensorflow.keras import layers, Sequential,models
RNN_CELLSIZE = 10
SEQLEN = 12
BATCHSIZE = 1<jupyter_output><empty_output><jupyter_text>Build and Train the ModelMany to ManyStateful is True<jupyter_code>model_layers = [
    layers.Reshape((SEQLEN,1),input_shape=(SEQLEN,),batch_size = BATCHSIZE),
    layers.GRU(RNN_CELLSIZE, return_sequences=True, stateful=True),
    layers.GRU(RNN_CELLSIZE, return_sequences=True, stateful=True),
    layers.TimeDistributed(layers.Dense(1)),
    layers.Flatten()
    
]
model = Sequential(model_layers)
model.summary()
model.compile(
       loss = 'mean_squared_error',
       optimizer = 'adam'
)
h = model.fit(x,y, 
              epochs = 100)

#model.save('gdrive/My Drive/dataML/rnn_many_to_many_model.h5')
plt.plot(h.history['loss'])<jupyter_output><empty_output><jupyter_text>Predict Future * Use the last 12 monthes data of the train data set to predict the future 12 monthes production * Compare the predicted data with the real data in test data set<jupyter_code>#model = models.load_model('gdrive/My Drive/dataML/rnn_many_to_many_model.h5')<jupyter_output><empty_output><jupyter_text>Get the last 12 monthes data of the train data<jupyter_code>last_12_month_data = train_scaled[-12:].reshape([1,12])
predicts = model.predict(last_12_month_data).reshape([12,1])
results = scaler.inverse_transform(np.array(predicts).reshape(12,1))
test_set['Generated'] = results
test_set
test_set.plot()<jupyter_output><empty_output><jupyter_text>Predict more Use the first months data to predict the future 13 years' milk productions and compare with the real data<jupyter_code>train_seed = list(train_scaled[:12].flatten())
def get_prediction(data_list):
  train_seed = data_list
  for i in range(13):
    x_train = np.array(train_seed[-12:]).reshape(1,12)
    one_predict = model.predict(x_train)
    train_seed.extend(one_predict.flatten().tolist())
   
  return train_seed
train_seed = get_prediction(train_seed)
plt.plot(train_seed)
results = scaler.inverse_transform(np.array(train_seed).reshape(-1,1))
milk_predict = milk
milk_predict['Generated'] = results
milk_predict.plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12380952380952381, 'fraction_numerical': 0.022492401215805473, 'mean_word_length': 6.501519756838905, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 1, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '276053', 'license': 'MIT', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1761, 'n_words': 372, 'path': '5_Prediction_MilkProdction/3_RNN_Many_to_Many_Stateful_Keras.ipynb', 'repo_name': 'nomad224/tensorflow', 'size': '4935'}