<jupyter_start><jupyter_text>Load Libraries<jupyter_code>import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
from sklearn.preprocessing import StandardScaler
from scipy import stats
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
%matplotlib inline
from sklearn import linear_model
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.naive_bayes import GaussianNB<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>df = pd.read_csv('pulsar_data_train.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code>#let's check for missing/ unique values
def dfaux (df):
    cant = df.isnull().sum()
    df_aux = pd.DataFrame(index = df.columns, data =
                         {'type': df.dtypes,
                          'unique_values': df.nunique(),
                          'have_null?': df.isnull().any(),
                          'how many?' : cant,
                          'per' : cant/df.shape[0]*100 })
    return df_aux
dfaux(df)<jupyter_output><empty_output><jupyter_text>**There are quite many null values in three of the total features. Let's replace the null values with mean**<jupyter_code>df.columns
#replacing null values
df[" Excess kurtosis of the integrated profile"] = df[" Excess kurtosis of the integrated profile"].replace(np.NaN, df[" Excess kurtosis of the integrated profile"].mean())
#replacing null values
df[" Standard deviation of the DM-SNR curve"] = df[" Standard deviation of the DM-SNR curve"].replace(np.NaN, df[" Standard deviation of the DM-SNR curve"].mean())
df[" Skewness of the DM-SNR curve"] = df[" Skewness of the DM-SNR curve"].replace(np.NaN, df[" Skewness of the DM-SNR curve"].mean())
#lets check again
dfaux(df)<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>df.shape<jupyter_output><empty_output><jupyter_text>**12528 -> rows****9 -> columns(features)**<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 12528 entries, 0 to 12527
Data columns (total 9 columns):
 #   Column                                         Non-Null Count  Dtype  
---  ------                                         --------------  -----  
 0    Mean of the integrated profile                12528 non-null  float64
 1    Standard deviation of the integrated profile  12528 non-null  float64
 2    Excess kurtosis of the integrated profile     12528 non-null  float64
 3    Skewness of the integrated profile            12528 non-null  float64
 4    Mean of the DM-SNR curve                      12528 non-null  float64
 5    Standard deviation of the DM-SNR curve        12528 non-null  float64
 6    Excess kurtosis of the DM-SNR curve           12528 non-null  float64
 7    Skewness of the DM-SNR curve                  12528 non-null  float64
 8   target_class                                   12528 non-null  float64
dtypes: float64(9)
memory usage: 881.0 KB<jupyter_text>**All the features are of float type, even the target class**<jupyter_code>df.describe()<jupyter_output><empty_output><jupyter_text>**The mean of the target variable is 9.2%, hence we can say that only 9.2% stars are pulsar stars**<jupyter_code>#correlation
corr = df.corr()
sns.heatmap(data=df.corr(),annot=True,cmap="coolwarm",linewidths=1,fmt=".2f",linecolor="gray")<jupyter_output><empty_output><jupyter_text>**A lot of the columns shows positive correlation**<jupyter_code>#Correlation with target variable
cor_target = abs(corr["target_class"])#Selecting highly correlated features
relevant_features = cor_target[cor_target>0]
relevant_features.nlargest(n=12)
sns.pairplot(data=df,
             palette="husl",
             hue="target_class",
             vars=[" Mean of the integrated profile",
                   " Excess kurtosis of the integrated profile",
                   " Skewness of the integrated profile",
                   " Mean of the DM-SNR curve",
                   " Excess kurtosis of the DM-SNR curve",
                   " Skewness of the DM-SNR curve"])

plt.suptitle("PairPlot of Data Without Std. Dev. Fields",fontsize=18)

plt.tight_layout()
plt.show()   # pairplot without standard deviaton fields of data<jupyter_output><empty_output><jupyter_text>**Data is quite separable on most of the columns. Since I do not have the description of these features, I won't drop any of these columns and carry on.**<jupyter_code>#feature scaling
features = df.drop("target_class", axis=1)
scaler = MinMaxScaler(feature_range=(0,1))
fscaled = scaler.fit_transform(features)<jupyter_output><empty_output><jupyter_text>Modeling<jupyter_code>#separating input and target variable
X = df.drop("target_class", axis=1)
Y = df["target_class"]
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.25, random_state = 0 )
#1. Random Forest
rforest = RandomForestClassifier(n_estimators = 100)
rforest.fit(x_train, y_train)
y_pred2 = rforest.predict(x_test)

acc_rf = round(rforest.score(x_train, y_train)* 100, 2)
print(acc_rf)
#2. KNN
knn = KNeighborsClassifier(n_neighbors = 4)
knn.fit(x_train, y_train)
y_pred4 = knn.predict(x_test)
acc_knn = round(knn.score(x_train, y_train)*100 ,2)
print(acc_knn)
#3. GNB
gauss = GaussianNB()
gauss.fit(x_train, y_train)
y_pred5 = gauss.predict(x_test)
acc_gauss = round(gauss.score(x_train, y_train)*100,2)
print(acc_gauss)
#4. Decision Tree
dt = DecisionTreeClassifier()
dt.fit(x_train, y_train)
y_pred6 = dt.predict(x_test)
acc_dt = round(dt.score(x_train, y_train)*100,2)
print(acc_dt)<jupyter_output>100.0<jupyter_text>***The decision tree classifier gave an accuracy of 100%.***  Feature Importance<jupyter_code>importances = pd.DataFrame({'feature':x_train.columns, 'importance':np.round(dt.feature_importances_, 3)})
importances = importances.sort_values('importance', ascending=False).set_index('feature')
importances.head(15)
importances.plot.bar()<jupyter_output><empty_output><jupyter_text>Skewness of integrated profile plays a humongous role to predict the target variable.  Optimization<jupyter_code>#confusion matrix
from sklearn.metrics import precision_score, recall_score

from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
predictions = cross_val_predict(dt, x_train, y_train, cv=3)
confusion_matrix(y_train, predictions)

print("Precision:", precision_score(y_train, predictions))
print("Recall:",recall_score(y_train, predictions))<jupyter_output>Precision: 0.7918552036199095
Recall: 0.8<jupyter_text>**The model predicts 79% of the time, a star's identification as pulsar correctly.****It tells us that it predicted the survival of 81% of the the stars who were actually pulsar stars.**<jupyter_code>#f1 score
from sklearn.metrics import f1_score
f1_score(y_train, predictions)
#roc-auc
y_scores = dt.predict_proba(x_train)
y_scores= y_scores[:,1]
from sklearn.metrics import roc_auc_score
rascore  = roc_auc_score(y_train, y_scores)
print(rascore)<jupyter_output>1.0
{'fraction_non_alphanumeric': 0.10185690879300928, 'fraction_numerical': 0.025669033315128344, 'mean_word_length': 4.335032774945375, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 57, 'https://': 0, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '387196', 'license': 'MIT', 'n_tokens_mistral': 2548, 'n_tokens_neox': 2366, 'n_words': 621, 'path': 'Predicting Pulsar Star/Model/predicting-pulsar-star.ipynb', 'repo_name': 'aka9ksha/ML-Crate', 'size': '7324'}