<jupyter_start><jupyter_text>> This notebook was created for code illustration of the `ML.now()` course**`Univariate Linear Regression`**[Link to Download the dataset](https://www.kaggle.com/hesh97/titanicdataset-traincsv)**Date Created**: June 8, 2021**Author**:<NAME>[[Course Repository](https://https://github.com/Sakshi1007/MLnow_2.0)]  Setup<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
cd '/content/drive/MyDrive/ColabNotebooks/MLnow()'
ls
import os
import pandas as pd #working with csv or excel files
import numpy as np #working with numbers/ arrays/ tensors
import tensorflow as tf #framework
from tensorflow import keras #API / library 
import os #using os commands between the python language
mathData = pd.DataFrame(pd.read_csv('train.csv'))#load the csv file as dataframe

mathData.head(10) #print the first ten rows of the dataframe
mathData.shape
mathData.isnull().sum()<jupyter_output><empty_output><jupyter_text>**Drop the column which are not necessary during prediction as well as it have ore than 35% Null values**<jupyter_code>drop_column=mathData.isnull().sum()[mathData.isnull().sum()>(35/100*mathData.shape[0])]
drop_column<jupyter_output><empty_output><jupyter_text>**Cabin will doesn't matter to know that whether person will survive or not, So we will drop this column**<jupyter_code>drop_column.index
mathData.drop(drop_column.index,axis=1,inplace=True)
mathData.isnull().sum()
mathData.fillna(mathData.mean(),inplace=True)
mathData.isnull().sum()
mathData['Embarked'].describe()
mathData['Embarked'].fillna('S',inplace=True)
mathData.isnull().sum()
#unique classes/categories in every column 

# variable.column.unique() --> all {column} values from the {variable} dataframe and print out the unique ones. 


print(f'Survived: {mathData.Survived.unique()}')
print(f'SEX: {mathData.Sex.unique()}')
print(f'Pclass: {mathData.Pclass.unique()}')
print(f'SibSP: {mathData.SibSp.unique()}')
print(f'Parch: {mathData.Parch.unique()}')
print(f'Embarked: {mathData.Embarked.unique()}')
mathData.corr()
mathData['FamilySize']=mathData['SibSp']+mathData['Parch']
mathData.drop(['SibSp','Parch'],axis=1,inplace=True)
mathData.corr()
mathData['FamilySize'] = mathData['FamilySize'].apply({1:1, 0:0, 4:1, 2:1, 6:1, 5:1, 3:1, 7:1, 10:1}.get)
mathData.head(10)
mathData['Alone']=[0 if mathData['FamilySize'][i]<=0 else 1 for i in mathData.index]
mathData.head(10)

mathData.groupby(['Alone'])['Survived'].mean()
mathData[['Alone','Fare']].corr()
mathData['Sex'] = mathData['Sex'].apply({'male':1, 'female':0}.get)
mathData['Embarked'] = mathData['Embarked'].apply({'S':1, 'C':2, 'Q':3}.get)
mathData.head(10)
mathData.groupby(['Sex'])['Survived'].mean()
mathData.groupby(['Embarked'])['Survived'].mean()
mathData.head(10)
print(f'Survived: {mathData.Survived.unique()}')
print(f'SEX: {mathData.Sex.unique()}')
print(f'Pclass: {mathData.Pclass.unique()}')
print(f'Alone: {mathData.Alone.unique()}')
print(f'FamilySize: {mathData.FamilySize.unique()}')
print(f'Embarked: {mathData.Embarked.unique()}')<jupyter_output>Survived: [0 1]
SEX: [1 0]
Pclass: [3 1 2]
Alone: [1 0]
FamilySize: [1 0]
Embarked: [1 2 3]<jupyter_text><jupyter_code>#columns to be considered
mathData=mathData[['PassengerId','Survived','Pclass','Name','Sex','Fare','FamilySize','Alone']]
mathData.head(10)
mathData.to_csv('ClaenMathData.csv') #exporting the file
ls<jupyter_output>ClaenMathData.csv  train.csv  univariate_MathData_2.csv<jupyter_text>**VISUALIZATION**<jupyter_code>import seaborn as sns
sns.palplot(sns.color_palette('PuOr')) #Purple to Orange colors
pal = sns.color_palette('PuOr', 6) #print 6 color shades from Purple to Orange
pal.as_hex() #set hex code values for colors

import matplotlib.pyplot as plt
plt.style.use(['seaborn'])
sns_colors = ['#c6690c', '#664697'] #orange Purple hex codes
sns.set_palette(sns_colors) #set the palette as sns_colors 
sns.palplot(sns.color_palette(sns_colors)) #plot the color codes
print(f'''There are {len(mathData.columns)} columns in the dataframe mathData namely,

{mathData.columns}''') #columns in the dataframe<jupyter_output>There are 8 columns in the dataframe mathData namely,

Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Fare',
       'FamilySize', 'Alone'],
      dtype='object')<jupyter_text>A pairplot is a plot where the `x` axis and `y` axis will have all the columns you specify it with<jupyter_code>sns.pairplot(mathData,
             x_vars = [ 'Sex','Alone','Fare', 'Pclass' ],
             y_vars = [ 'Survived','Sex','Alone'],
       diag_kind='kde', hue='Survived'
             )
sns.pairplot(mathData,
             x_vars = [ 'Survived','Sex','Alone','Fare', 'Pclass' ],
             y_vars = [ 'Survived','Sex','Alone','Fare', 'Pclass'],
       diag_kind='kde', hue='Survived'
             )
sns.stripplot(y='Fare', x='Survived', data=mathData)
fig, axarr = plt.subplots(2,2, figsize=(20,20))

#three rows and 2 columns all starting from (0,0)

sns.stripplot(y='Fare', x='Survived', data=mathData, hue=None, ax=axarr[0][0])
sns.stripplot(y='Sex', x='Survived', data=mathData, hue=None, ax=axarr[1][0])
sns.stripplot(y='Alone', x='Survived', data=mathData, hue=None, ax=axarr[0][1])
uniMathData = mathData[['Fare', 'Survived']]

uniMathData.head(5)

sns.pairplot(uniMathData,
             x_vars = ['Fare', 'Survived'],
             y_vars =  ['Fare', 'Survived'],
             
       diag_kind='kde'
             )
uniMathData.to_csv('univariate_MathData_2.csv')
ls<jupyter_output>ClaenMathData.csv  train.csv  univariate_MathData_2.csv<jupyter_text>**DATA SPLIT**<jupyter_code>trainDataset = uniMathData.sample(frac=0.8, random_state=0) 
testDataset = uniMathData.drop(trainDataset.index)

print(trainDataset.head())
print(testDataset.head())
print(trainDataset.shape)
print(testDataset.shape)
trainFeatures = trainDataset.copy()
testFeatures = testDataset.copy()

print(trainFeatures.head())
print(testFeatures.head())

trainLabels = trainFeatures.pop('Fare')
testLabels = testFeatures.pop('Fare')

print(trainLabels.head())
print(testLabels.head())
print(trainFeatures.head())
print(testFeatures.head())
print(testFeatures.shape)
uniMathData.columns
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
model.compile(
    loss='mean_absolute_error',
    optimizer= tf.keras.optimizers.SGD(learning_rate=0.01,momentum=0.1,nesterov=False,name="SGD"),
    metrics=['mae','mse']
)
uniMathData.shape
numEpochs = 600
history = model.fit(x = trainFeatures, y = trainLabels, validation_data = (testFeatures, testLabels), epochs = numEpochs)
print(history)
model.summary()
def curvePlots(tempString):
  plt.plot(history.history[tempString])
  plt.plot(history.history[f'val_{tempString}'])
  plt.xlabel('NumEpochs')
  plt.ylabel(tempString)
  plt.legend([tempString, f'val_{tempString}'])
  plt.show()


curvePlots('mse')
curvePlots('mae')
curvePlots('loss')
model.predict([1])

print(f'Prediction for input value 1: {model.predict([1])}')
tempListforPreds = [1,2,3,4,5]
print(f'''

input List = {tempListforPreds}


List of Predictions:
{model.predict(tempListforPreds)}

List of Predictions (flattened out):
{model.predict(tempListforPreds).flatten()}
''')
print(testFeatures)
testPreds = model.predict(testFeatures).flatten()
print(len(testPreds))

print(testPreds)
def predPlot(labels, predictions):
  plt.scatter(labels, predictions)
  plt.ylabel('Predictions')
  plt.xlabel('True Value or Labels')
  plt.axis('equal')
  plt.axis('square')
  plt.xlim([0, plt.xlim()[1]])
  plt.ylim([0, plt.ylim()[1]])
  plt.show()

predPlot(testLabels, testPreds)
def errorPlot(preds, labels, counts):
  errors = preds - labels
  plt.hist(errors, counts)
  plt.xlabel('Error')
  plt.ylabel('Counts')
  plt.show()

errorPlot(testPreds, testLabels, numEpochs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17030848329048842, 'fraction_numerical': 0.01696658097686375, 'mean_word_length': 6.561710398445093, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '495229', 'license': 'MIT', 'n_tokens_mistral': 2950, 'n_tokens_neox': 2851, 'n_words': 530, 'path': '3_1_EDA&Univariate.ipynb', 'repo_name': 'Sakshi1007/30-DaysOfCode-March-2021', 'size': '7786'}