<jupyter_start><jupyter_text>Data Preprocessing<jupyter_code>print( 'Columns:-', df.columns, '\n')
df.dtypes.value_counts()
df.head()
df.describe()
df.isna()<jupyter_output><empty_output><jupyter_text>Understanding Columns RI: refractive index Na: Sodium (unit measurement: weight percent in corresponding oxide, as are attributes 4-10) Mg: Magnesium Al: Aluminum Si: Silicon K: Potassium Ca: Calcium Ba: Barium Fe: Iron Type of glass: (class attribute) -- 1 buildingwindowsfloatprocessed  -- 2 buildingwindowsnonfloatprocessed  -- 3 vehiclewindowsfloatprocessed -- 4 vehiclewindowsnonfloatprocessed (none in this database) -- 5 containers -- 6 tableware -- 7 headlamps  Data Vizualization<jupyter_code>sns.distplot( df['RI'])
sns.lmplot(x= 'Type', y= 'RI', data= df)
type_1= df[df['Type']==1]
type_1.describe()<jupyter_output><empty_output><jupyter_text>The relationship between elements that pertain to a similar group in the PERIODIC TABLE.  Group 1 Elements (ALKALI METALS)<jupyter_code>sns.regplot(x='Na', y='K', data= type_1)<jupyter_output><empty_output><jupyter_text>Group 2 Elements (ALKALINE EARTH METALS)<jupyter_code>plt.subplot(1,3,1)
sns.regplot(x='Mg', y='Ca', data=df)
plt.grid()

plt.subplot(1,3,2)
sns.regplot(y='Mg', x='Ba', data=df)
plt.grid()

plt.subplot(1,3,3)
sns.regplot(x='K', y='Ba', data=df)
plt.grid()

plt.gcf().set_size_inches(15,8)

plt.show()<jupyter_output><empty_output><jupyter_text>A slightly inverse proportionality can be observed in the first two plots i.e. Ca vs Mg, Mg vs Ba.   Refractive Index Vs The Elements<jupyter_code>df_1=df.drop(columns= ['Type'])
plot_1= pd.melt(df_1, id_vars=['RI']) 
ggplot(plot_1, aes('value', 'RI'))+ geom_density(color='blue')+facet_wrap('variable', scales='free')
plot_2= pd.melt(df, id_vars=['Type']) 
ggplot(plot_2, aes('value', 'Type'))+ geom_density(color='teal')+facet_wrap('variable', scales='free')
sns.lineplot(y=df['RI'], x=df['Type'])
plt.grid()
# Individual correlations of the elements with type of glass

type_dum= pd.get_dummies(df['Type'])
df_dum= df.drop(columns= ['Type'])
df_dum= pd.concat([df_dum, type_dum], axis=1)
sns.heatmap(df_dum.corr(), annot=True)
plt.gcf().set_size_inches(15,8)
sns.heatmap(df.corr(), annot=True)
plt.gcf().set_size_inches(15,8)<jupyter_output><empty_output><jupyter_text>Model Building<jupyter_code>import itertools
from numpy import sort
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import train_test_split, KFold, cross_val_score, StratifiedKFold
from sklearn.metrics import accuracy_score, confusion_matrix
from xgboost import XGBClassifier, plot_tree, plot_importance
# Train test split
x= df.drop(columns= ['Type'])
y= df['Type']
x_train, x_test, y_train, y_test= train_test_split(x,y, test_size=0.3, random_state= 0)
xg= XGBClassifier()
xg.fit(x_train, y_train)
plot_tree(xg, num_trees=0, rankdir='LR')
fig=plt.gcf()
fig.set_size_inches(100,50)
plot_tree(xg, num_trees=5, rankdir='LR')
fig=plt.gcf()
fig.set_size_inches(100,50)
y_pred= xg.predict(x_test)
accuracy= accuracy_score(y_test, y_pred)
print('Accuracy:-', accuracy*100)
cm=confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True)
plot_importance(xg)
import warnings
warnings.filterwarnings("ignore")
feature_importance= sort(xg.feature_importances_)

for each_thresh in feature_importance:
    selection= SelectFromModel(xg, threshold= each_thresh, prefit= True)
    modified_x_train= selection.transform(x_train)
    selection_model= XGBClassifier()
    selection_model.fit(modified_x_train, y_train)
    modified_x_test= selection.transform(x_test)
    prediction= selection_model.predict(modified_x_test)
    accuracy= accuracy_score(y_test, prediction)
    print('Threshold:- ', each_thresh, 'Number of Features:-', modified_x_train.shape[1], 'Model Accuracy:-', accuracy*100 )
strat= StratifiedKFold(n_splits=40, random_state=0)
xg_strat= XGBClassifier()
sf_results= cross_val_score(xg_strat, x_train, y_train, cv= strat, verbose=True)
print('Accuracy:-', sf_results.mean()*100)<jupyter_output>[23:08:47] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.3.0/src/learner.cc:1061: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'multi:softprob' was changed from 'merror' to 'mlogloss'. Explicitly set eval_metric if you'd like to restore the old behavior.
{'fraction_non_alphanumeric': 0.13972792252709246, 'fraction_numerical': 0.02005994927369149, 'mean_word_length': 7.817073170731708, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '479982', 'license': 'MIT', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1606, 'n_words': 337, 'path': 'glass_identification.ipynb', 'repo_name': 'anirudhrvs/Ensemble-approach-for-glass-identification', 'size': '4337'}