<jupyter_start><jupyter_text>Parent Satisfaction Classification problem Based on the students details we predict the parent satisfaction classification yes or no. In here we handle class imbalance problem. Its binary classification problem. In this notebook we did these following steps.   1. Importing Libraries<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline<jupyter_output><empty_output><jupyter_text>2. Loading Datasets<jupyter_code># Load the datasets
file = "datasets/xAPI-Edu-Data.csv"
df = pd.read_csv(file)<jupyter_output><empty_output><jupyter_text>3. Explore Datasets<jupyter_code>df.head()
df.columns
df.shape
# Checking missing values
df.isna().sum().sum()<jupyter_output><empty_output><jupyter_text>4. Exploratory Data Analysis<jupyter_code>plt.title('Gender of the student', fontsize=22)
sns.countplot(x=df.gender);



bar = df['NationalITy'].value_counts().sort_values(ascending=False)
ax = bar.plot(kind='barh', figsize=(15,8), color="blue", fontsize=13);
ax.set_alpha(0.5)
ax.set_title("Nationality of the students", fontsize=22)
ax.set_ylabel("Nationality", fontsize=15);
for i, v in enumerate(bar):
    ax.text(v + 3, i + .10, str(v))
plt.show()
bar = df['PlaceofBirth'].value_counts().sort_values(ascending=False)
ax = bar.plot(kind='barh', figsize=(15,8), color="blue", fontsize=13);
ax.set_alpha(0.5)
ax.set_title("Place of Birth", fontsize=22)
ax.set_ylabel("Birt place", fontsize=15);
for i, v in enumerate(bar):
    ax.text(v + 3, i + .10, str(v))
plt.show()
df['Discussion'].hist();
df['raisedhands'].hist();
df['VisITedResources'].hist();
df['AnnouncementsView'].hist();
df['ParentschoolSatisfaction'].value_counts(normalize=True)
df['ParentschoolSatisfaction'].value_counts()
# label 0 and 1
df['ParentschoolSatisfaction'].replace(['Bad','Good'],[1, 0],inplace=True)<jupyter_output><empty_output><jupyter_text>5. Input and Target Columns<jupyter_code># Sperate the data, features and label
features_df = df.drop('ParentschoolSatisfaction', axis='columns')
labels_df = df.ParentschoolSatisfaction
#sample data for our pridict purpose
sample_data = features_df.sample(1)<jupyter_output><empty_output><jupyter_text>6. Encode categorical columns to one-hot vectors<jupyter_code># Encoder
numeric_cols = features_df.columns[features_df.dtypes != "object"].values
print(numeric_cols)
categori_cols = features_df.columns[features_df.dtypes == "object"].values
print(categori_cols)
features_df[categori_cols].nunique()
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(sparse=False, handle_unknown='ignore')
encoder.fit(features_df[categori_cols])
encoder.categories_
encoded_cols = list(encoder.get_feature_names(categori_cols))
print(encoded_cols)
features_df[encoded_cols] = encoder.transform(features_df[categori_cols])
features_df.head()
features_df.shape<jupyter_output><empty_output><jupyter_text>7. Train Test Split<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features_df, labels_df, stratify = labels_df, test_size=0.2, random_state=42)
print(X_train.shape)
print(X_test.shape)
y_train.value_counts(normalize=True)
y_test.value_counts(normalize=True)
numeric_cols = X_train.select_dtypes(include=np.number).columns.tolist()
categorical_cols = X_train.select_dtypes('category').columns.tolist()
X_train = X_train[numeric_cols]
X_test = X_test[numeric_cols]<jupyter_output><empty_output><jupyter_text>8. Model training<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import plot_confusion_matrix
# Random forrest
model = RandomForestClassifier(random_state = 22, max_depth = 5, class_weight='balanced')
model.fit(X_train, y_train)
train_pred = model.predict(X_train)
test_pred = model.predict(X_test)
train_acc = accuracy_score(y_train, train_pred)
test_acc = accuracy_score(y_test, test_pred)


print(f"Train Accuracy = {train_acc} ")
print(f"Test Accuracy = {test_acc} ")
print(classification_report(y_train, train_pred, labels=[0, 1]))
print(classification_report(y_test, test_pred, labels=[0, 1]))
from sklearn.metrics import confusion_matrix, accuracy_score

cm = confusion_matrix(y_test, test_pred)
print(cm)
accuracy_score(y_test, test_pred)
plot_confusion_matrix(model, X_test, y_test)  
plt.show()<jupyter_output><empty_output><jupyter_text>9. Model Store<jupyter_code>import pickle
filename = 'saved_model/parent_satisfiction_model.pkl'
pickle.dump(model, open(filename, 'wb'))<jupyter_output><empty_output><jupyter_text>10. Model load and pridict the value<jupyter_code># load the model from disk
train_model = pickle.load(open(filename, 'rb'))
from sklearn.preprocessing import OneHotEncoder

def predict_the_result(input_data, model_name):
    
    """ Predict the result based on the input values"""
    numeric_cols = input_data.columns[input_data.dtypes != "object"].values
    categori_cols = input_data.columns[input_data.dtypes == "object"].values
    
    
    encoder = OneHotEncoder(sparse=False, handle_unknown='ignore')
    encoder.fit(features_df[categori_cols])
    
    encoded_cols = list(encoder.get_feature_names(categori_cols))
    input_data[encoded_cols] = encoder.transform(input_data[categori_cols])
    
    numeric_cols = input_data.select_dtypes(include=np.number).columns.tolist()
    categorical_cols = input_data.select_dtypes('category').columns.tolist()
    
    input_data = input_data[numeric_cols]
    
    return model_name.predict(input_data)
sample_data
predict_the_result(sample_data, train_model)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11695407205053782, 'fraction_numerical': 0.0097319446815776, 'mean_word_length': 8.138845553822152, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '148548', 'license': 'MIT', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1977, 'n_words': 368, 'path': 'parent_satisfaction_classification.ipynb', 'repo_name': 'norochalise/Student-well-beings', 'size': '5857'}