<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
import tensorflow as tf
import keras
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
# Get the data
!wget "https://he-s3.s3.amazonaws.com/media/hackathon/airplane-accident-severity-hackerearth-machine-learning-challenge/how-severe-can-an-airplane-accident-be-03e7a3f1/3c055e822d5b11ea.zip"
# Unzip it
!unzip 3c055e822d5b11ea.zip
raw_train = pd.read_csv("train.csv")
def get_category_names(df, column_name):
    '''
    Column passed must be categorical
    '''
    unique_names_dict = dict(df[column_name].value_counts())
    unique_names = list(unique_names_dict.keys())
    
    _length = len(unique_names)
    return (_length, unique_names)

def replace_small_categorical_data(df, column_name, categorical_names):
    copy_frame = df.copy(deep=True)
    
    copy_frame[column_name].replace(categorical_names, [x for x in range(len(categorical_names))], inplace=True)
    
    return copy_frame  

cat_names = ["Minor_Damage_And_Injuries", "Significant_Damage_And_Fatalities", "Significant_Damage_And_Serious_Injuries", "Highly_Fatal_And_Damaging"]
train_data = replace_small_categorical_data(raw_train, 'Severity', cat_names)
y_dat = train_data['Severity']
x_dat = train_data.drop('Severity', axis=1)
sc = StandardScaler()
x_final = sc.fit_transform(x_dat)
y_dat = np.array(y_dat)
y_dat = y_dat.reshape((-1,1))
y_final = to_categorical(y_dat, num_classes=4)
trainX, testX, trainY, testY = train_test_split(x_final, y_final, test_size=0.15)
def train_model(X, y, epochs, verbose=True):
  """
  X: [10000, 11]
  y: [10000, 1]
  """
  weights={
      "hidden_1": tf.Variable(tf.random_normal([11, 11]) * tf.sqrt(1./11)),
      "hidden_2": tf.Variable(tf.random_normal([11, 11]) * tf.sqrt(1./11)),
      "output": tf.Variable(tf.random_normal([11, 4]) * tf.sqrt(1./4)),
  }

  biases = {
      "bias_1": tf.Variable(tf.random_normal([11])),
      "bias_2": tf.Variable(tf.random_normal([11])),
      "output": tf.Variable(tf.random_normal([4]))
  }

  x_pl = tf.placeholder(tf.float32, shape=(None, 11))
  y_pl = tf.placeholder(tf.float32, shape=(None, 4))

  h1 = tf.nn.relu(tf.add(tf.matmul(x_pl, weights["hidden_1"]), biases["bias_1"]))
  h2 = tf.nn.relu(tf.add(tf.matmul(h1, weights["hidden_2"]), biases["bias_2"]))
  out = tf.add(tf.matmul(h1, weights["output"]), biases["output"])

  cost = tf.nn.softmax_cross_entropy_with_logits_v2(logits=out, labels=y_pl)
  optim = tf.train.AdamOptimizer().minimize(cost)

  init = tf.global_variables_initializer()
  costs, accs = list(), list()

  # Train the model
  with tf.Session() as sess:
    sess.run(init)

    for epoch in range(epochs):
      opt, cst = sess.run([optim, cost], feed_dict={x_pl:X, y_pl:y})
      costs.append(cst)
      accuracy = tf.equal(tf.argmax(out, 1), tf.argmax(y_pl, 1))
      accuracy = tf.reduce_mean(tf.cast(accuracy, 'float'))
      accuracy = accuracy.eval({x_pl: testX, y_pl: testY})
      accs.append(accuracy)
      if verbose:
        if epoch % 100 == 0:
          print("EPOCH: {} / {} | ACCURACY: {}".format(epoch, epochs, accuracy))
  
  return costs, accs
losses,accs = train_model(trainX, trainY, epochs=2000)
# lss=[]
# for i in range(len(losses)):
#   c_lst = np.array(losses[i])
#   lss.append(c_lst.sum())
acc = [100*x for x in accs]
plt.plot(acc)
plt.xlabel("Epochs")
plt.ylabel("Accuracy (%)")
plt.title("Accuracy v/s Epochs")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14653570480488454, 'fraction_numerical': 0.02760817626758694, 'mean_word_length': 4.8875, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '26281', 'license': 'MIT', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1443, 'n_words': 258, 'path': 'Air_Disaster_Prediction_Models.ipynb', 'repo_name': 'heytanay/air-disaster-prediction', 'size': '3767'}