<jupyter_start><jupyter_text><jupyter_code>!pip install -q sklearn
%tensorflow_version 2.x
from __future__ import absolute_import,division,print_function,unicode_literals
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc
import tensorflow as tf

dftrain=pd.read_csv("https://storage.googleapis.com/tf-datasets/titanic/train.csv")
dftest=pd.read_csv("https://storage.googleapis.com/tf-datasets/titanic/eval.csv")
y_train=dftrain.pop("survived")
y_test=dftest.pop("survived")
dftrain.head()
dftrain.info()
dftrain.age.hist(bins=20)
dftrain["sex"].value_counts().plot(kind='barh')

dftrain["class"].value_counts().plot(kind='barh')
dftrain.head()
male_survived = 0
female_survived = 0
g = list(y_train)
for i in range(len(g)):
  if g[i] == 1:
    if dftrain['sex'][i] =='male':
      male_survived +=1
    else:
      female_survived+=1
print(male_survived,female_survived)
l=[male_survived,female_survived]
labels = ['male survived','female survived']
fig = plt.figure(figsize=(5,5))
plt.bar(labels, l, color ='orange',  
        width = 0.12) 
  
plt.xlabel("\n\nClasses") 
plt.ylabel("Frequency") 
plt.title("Yo") 
plt.show()
dftest.shape
CATEGORRICAL_COLUMNS=['sex','n_siblings_spouses','parch','class','deck','embark_town','alone']
NUMERICAL_COLUMNS=['age','fare']
feature_columns=[]
for feature_name in CATEGORRICAL_COLUMNS:
  vocabulary=dftrain[feature_name].unique()
  feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name,vocabulary))

for feature_name in NUMERICAL_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name,dtype=tf.float32))
def make_input_fn(data_df,label_df,num_epochs=10,shuffle=True,batch_size=32):
  def input_fun():
    ds=tf.data.Dataset.from_tensor_slices((dict(data_df),label_df))
    if shuffle :
      ds=ds.shuffle(1000)
    ds=ds.batch(batch_size).repeat(num_epochs)
    return ds
  return input_fun

train_input_fn=make_input_fn(dftrain,y_train)
eval_input_function = make_input_fn(dftest,y_test,num_epochs=1,shuffle=False)
linear_est=tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn)

result=linear_est.evaluate(eval_input_function)
clear_output()
print(result['accuracy'])
print(result)<jupyter_output>0.7462121
{'accuracy': 0.7462121, 'accuracy_baseline': 0.625, 'auc': 0.8344352, 'auc_precision_recall': 0.795979, 'average_loss': 0.4745508, 'label/mean': 0.375, 'loss': 0.46696007, 'precision': 0.66, 'prediction/mean': 0.40796265, 'recall': 0.6666667, 'global_step': 200}
{'fraction_non_alphanumeric': 0.14274981217129978, 'fraction_numerical': 0.03906836964688205, 'mean_word_length': 7.590322580645161, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '425286', 'license': 'MIT', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1029, 'n_words': 145, 'path': 'Titanic_Linear_Regression.ipynb', 'repo_name': 'soumilbaldota/TensorFlow_Notebooks_101', 'size': '2662'}