<jupyter_start><jupyter_text>Try Linear regression on same prediction data using Tensorflow<jupyter_code>import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf<jupyter_output><empty_output><jupyter_text>A tensor is a number, vector, matrix or any n-dimensional array. Problem StatementWe'll create a model that predicts crop yeilds for apples (*target variable*) by looking at the average temperature, rainfall and humidity (*input variables or features*) in different regions. Here's the training data:>Temp | Rain | Humidity | Prediction>--- | --- | --- | ---> 73 | 67 | 43 | 56> 91 | 88 | 64 | 81> 87 | 134 | 58 | 119> 102 | 43 | 37 | 22> 69 | 96 | 70 | 103In a **linear regression** model, each target variable is estimated to be a weighted sum of the input variables, offset by some constant, known as a bias :```yeild_apple  = w11 * temp + w12 * rainfall + w13 * humidity + b1```It means that the yield of apples is a linear or planar function of the temperature, rainfall & humidity.**Our objective**: Find a suitable set of *weights* and *biases* using the training data, to make accurate predictions. Training DataThe training data can be represented using 2 matrices (inputs and targets), each with one row per observation and one column for variable.<jupyter_code># Use seaborn for pairplot
import seaborn as sns

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing

print(tf.__version__)
# Input (temp, rainfall, humidity)
inputs = np.array([[73, 67, 43], [91, 88, 64], [87, 134, 58], [102, 43, 37], [69, 96, 70], [73, 67, 43], [91, 88, 64], [87, 134, 58], [102, 43, 37], [69, 96, 70], [73, 67, 43], [91, 88, 64], [87, 134, 58], [102, 43, 37], [69, 96, 70]], dtype='float32')
# Targets (apples)
targets = np.array([[56], [81], [119], [22], [103], 
                    [56], [81], [119], [22], [103], 
                    [56], [81], [119], [22], [103]], dtype='float32')
df_inputs = pd.DataFrame(inputs, columns = ['temp','rainfall','humidity'])
features = df_inputs.copy()
train_features = features[:10]
test_features = features[10:] 

df_targets = pd.DataFrame(targets, columns = ['apples'])
label = df_targets.copy()
train_label =  label[:10]
test_label = label[10:]
#print(train_data)
print(train_features)
print(train_label)
train_features.describe().transpose()[['mean', 'std']]
sns.pairplot(train_features[['temp', 'rainfall', 'humidity']], diag_kind='kde')
# with one variable input "Temp"
from sklearn.preprocessing import Normalizer
import sklearn.preprocessing
temp = np.array(train_features['temp'])

temp_normalizer = preprocessing.Normalization(input_shape=[1, ], axis = None)
temp_normalizer.adapt(temp)
temp_linear_model = tf.keras.Sequential([
    temp_normalizer,
    layers.Dense(units=1)           
])
temp_linear_model.summary()
temp_linear_model.predict(temp[1:6])
temp_linear_model.compile(
    optimizer = tf.optimizers.Adam(learning_rate = 0.1),
    loss='mean_absolute_error')
%%time
history = temp_linear_model.fit(
    train_features['temp'], train_label,
    epochs = 1000,
    # suppress logging
    verbose = 0,
    # Calculate validation results on 10% of the training data
    validation_split = 0.3)
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.tail(10)
test_results = {}

test_results['temp_linear_model'] = temp_linear_model.evaluate(
    test_features['temp'],
    test_label, verbose=0)
test_results['temp_linear_model']
y = temp_linear_model.predict(test_features['temp'])
y
norm_test_features = np.linalg.norm(test_features['temp'])
norm_train_features = np.linalg.norm(train_features['temp'])
# norm_test_label = np.linalg.norm(test_label['temp'])
norm_train_label = np.linalg.norm(train_label['apples'])


normal_array_test_features = test_features['temp']/norm_test_features
normal_array_train_features = train_features['temp']/norm_train_features
normal_array_train_label = train_label['apples']/norm_train_label

print(normal_array_train_features)
print(normal_array_test_features)
print(normal_array_train_label)
def plot_Apples(x, y):
  plt.scatter(train_features['temp'], train_label['apples'], label = 'Data')
  plt.plot(x, y, color='g', label = 'Predictions')
  plt.xlabel('Temp')
  plt.ylabel('Apples')
  plt.legend()
  
plot_Apples(test_features['temp'], y)<jupyter_output><empty_output><jupyter_text>NOW FOR MANY INPUTS<jupyter_code>normalizer = preprocessing.Normalization(axis = -1)
normalizer.adapt(np.array(train_features))
linear_model = tf.keras.Sequential([
    normalizer,
    layers.Dense(units = 1)
])
linear_model.predict(train_features[:9])
linear_model.compile(
    optimizer=tf.optimizers.Adam(learning_rate = 0.1),
    loss='mean_absolute_error')

%%time
history = linear_model.fit(
    train_features, train_label, 
    epochs = 1000,
    # suppress logging
    verbose=0,
    # Calculate validation results on 20% of the training data
    validation_split = 0.2)
test_results['linear_model'] = linear_model.evaluate(
    test_features, test_label, verbose = 0)

test_results['linear_model']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1324298160696999, 'fraction_numerical': 0.04433688286544046, 'mean_word_length': 5.4818067754077795, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, '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': '484835', 'license': 'MIT', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1647, 'n_words': 448, 'path': 'LAB5/lab5_1.ipynb', 'repo_name': 'JG-geek/CE046_Jay_Gorakhiya', 'size': '5165'}