<jupyter_start><jupyter_text>Assignment: Ionosphere Data Problem Dataset Description: This radar data was collected by a system in Goose Bay, Labrador. This system consists of a phased array of 16 high-frequency antennas with a total transmitted power on the order of 6.4 kilowatts. See the paper for more details. The targets were free electrons in the ionosphere. "Good" radar returns are those showing evidence of some type of structure in the ionosphere. "Bad" returns are those that do not; their signals pass through the ionosphere.Received signals were processed using an autocorrelation function whose arguments are the time of a pulse and the pulse number. There were 17 pulse numbers for the Goose Bay system. Instances in this databse are described by 2 attributes per pulse number, corresponding to the complex values returned by the function resulting from the complex electromagnetic signal. Attribute Information:- All 34 are continuous- The 35th attribute is either "good" or "bad" according to the definition summarized above. This is a binary classification task. 	        		Data Set Characteristics:&nbsp;&nbsp;		Multivariate		Number of Instances:		351		Area:		Physical                                         Attribute Characteristics:            Integer,Real            Number of Attributes:            34            Date Donated            N/A                 	        		Associated Tasks:		Classification		Missing Values?		N/A		Number of Web Hits:		N/A	          WORKFLOW :- Load Data .<jupyter_code>import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
pd.__version__
np.__version__<jupyter_output><empty_output><jupyter_text>Load Data:[Click Here to Download DataSet](https://github.com/ramsha275/ML_Datasets/blob/main/ionosphere_data.csv) Loading data...<jupyter_code># Load the dataset.
df = pd.read_csv('ionosphere_data.csv', delimiter=',')
# Find the shape of the dataset 
df.shape<jupyter_output><empty_output><jupyter_text>It's clear from the shape of the data that dataset is not a huge one. Only 351 records are available with 34 features/columns.<jupyter_code>df.head()
df.describe().T
for feature in df:
    print(feature)
    print(len(df[feature].unique()))
df['feature2'].unique()
df.drop(df.columns[1], inplace=True, axis=1)

df.head()
df.ndim
df.info()
'''for feature in df:
    print(feature)
    df[feature].hist()
    plt.show()'''

# df.hist()
# plt.show()
# Check summary statistics
df.describe()<jupyter_output><empty_output><jupyter_text>- Check Missing Values ( If Exist ; Fill each record with mean of its feature ) or any usless column.<jupyter_code># Find missing values
df.isnull().sum()
df['label'] = [1 if lbl == 'g' else 0 for lbl in df['label']]
train_data = df.sample(frac= 0.6, random_state=125)
test_data = df.drop(train_data.index)
train_label = train_data.iloc[:,-1]
train_data = train_data.iloc[:,0:-1]
test_label = test_data.iloc[:,-1]
test_data = test_data.iloc[:,0:-1]
# df.drop(columns= 'label', inplace = True)
train_data.head()
train_label<jupyter_output><empty_output><jupyter_text>- Standardized the Input Variables. **Hint**: Centeralized the data<jupyter_code># Normalize the data
# train_mean = train_data.mean()
# train_data -= train_mean
# train_std = train_data.std()
# train_data /= train_std
# test_data -= train_mean
# test_data /= train_std<jupyter_output><empty_output><jupyter_text>- Encode labels. - Shuffle the data if needed.- Split into 60 and 40 ratio.<jupyter_code># Now sample the dataframe

train_data.shape
test_data.shape
train_label.shape
test_label.shape
train_label.sum()
len(train_label)
# train_label.sum()/len(train_label)<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>train_data = train_data.to_numpy()
train_label = train_label.to_numpy().astype('float32')
test_data = test_data.to_numpy()
test_label = test_label.to_numpy().astype('float32')
#train_set = np.array(train_set.as_matrix())
#train_label = np.array(pd.DataFrame(train_label).as_matrix())
print(type(train_data))
print(type(train_label))
print(type(test_data))
print(type(test_label))

print(train_data.dtype)
print(train_label.dtype)
print(test_label.dtype)
print(test_data.dtype)<jupyter_output>float64
float32
float32
float64<jupyter_text>Model Architecture - Model : 1 hidden layers including 16 unit.<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout

# Define model
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(train_data.shape[1],)))
model.add(Dropout(0.3))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1,  activation='sigmoid'))
model.summary()<jupyter_output>Model: "sequential_34"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_102 (Dense)            (None, 128)               4352      
_________________________________________________________________
dropout_68 (Dropout)         (None, 128)               0         
_________________________________________________________________
dense_103 (Dense)            (None, 64)                8256      
_________________________________________________________________
dropout_69 (Dropout)         (None, 64)                0         
_________________________________________________________________
dense_104 (Dense)            (None, 1)                 65        
=================================================================
Total params: 12,673
Trainable params: 12,673
Non-trainable params: 0
_________________________________________________[...]<jupyter_text>- Compilation Step (Note : Its a Binary problem , select loss , metrics according to it)<jupyter_code>from tensorflow.keras import optimizers

model.compile(optimizer = 'RMSprop', loss='binary_crossentropy', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>- Train the Model with Epochs (100).<jupyter_code>history = model.fit(train_data, train_label, validation_split=0.25, epochs= 75, batch_size = 32)
history.history.keys()
history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']

epochs = range(75)
plt.plot(epochs, loss_values, 'bo', label='Training loss')
plt.plot(epochs, val_loss_values, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
acc_values = history_dict['accuracy']
val_acc_values = history_dict['val_accuracy']
plt.plot(epochs, acc_values, 'bo', label='Training acc')
plt.plot(epochs, val_acc_values, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>- Evaluation Step- Prediction<jupyter_code>score = model.evaluate(test_data, test_label)
score<jupyter_output><empty_output><jupyter_text>- If the model gets overfit tune your model by changing the units , No. of layers , epochs , add dropout layer or add Regularizer according to the need .- Prediction should be > **92%**<jupyter_code>predictions=model.predict(test_data)
y_pred = (predictions > 0.5)
tf.math.confusion_matrix(
    test_label, y_pred, num_classes=2, weights=None, dtype=tf.dtypes.int32,
    name=None
)
 # It will evaluate the logical expression y_predict>0.25 and return True or False 
np.count_nonzero(y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11919566080169335, 'fraction_numerical': 0.018653261013361556, 'mean_word_length': 5.067415730337078, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '623091', 'license': 'MIT', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2253, 'n_words': 619, 'path': 'PIAIC Assignments/Deep Learning Assignments Set/Ionosphere Assignment.ipynb', 'repo_name': 'fazeel15/PIAIC125459_PIAIC_Assignments', 'size': '7559'}