# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/Simran-13/simran.18cse064/blob/main/DMDW_5.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="rPwHmLbhkxMe"
# # Lab-5(Dissimilarity Matrix for Binary Attributes)

# + id="yrEs4wgQklW1"
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sbs

# + colab={"base_uri": "https://localhost:8080/", "height": 224} id="nWHD6rQDl4mI" outputId="2289996c-57f7-47d2-cf22-fd2e97dd61d8"
url="https://raw.githubusercontent.com/Anasuya-Sahoo/DMDW-Lab/main/student-mat.csv"
df=pd.read_csv(url)
df.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="hbS-hoKCmGTE" outputId="f36c9ca8-736c-4e90-eec8-19f253f2b63a"
#extract the dataset from the original dataset
dfs=df[['schoolsup','famsup','paid','activities','nursery','romantic','internet','higher']]
dfs.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="B3826yQJnfic" outputId="2f72a01c-9bb8-4360-c03d-fd29812425c3"
#convert binary into 0,1 format
dfs=dfs.replace('no',0)
dfs=dfs.replace(to_replace='yes',value=1)
dfs.head()

# + colab={"base_uri": "https://localhost:8080/"} id="SZ3Dg272oO70" outputId="5582e10f-fd52-4428-a9c2-1807e5810d96"
# create obj and find the distance or the dissimilarity matrix using scipy 
n=np.array(dfs[['schoolsup','famsup']])
n=n.reshape(-1,2)# -1 => numpy will calculate whatever will be the no. and 2 =>
n.shape

# + colab={"base_uri": "https://localhost:8080/"} id="7hS_Z6xLpAA8" outputId="ce200b6e-e868-4aac-a541-ace13d159e9d"
m=np.array(dfs[['romantic','internet']])
m=m.reshape(-1,2)
m.shape

# + id="vLGzxNcjpKBx"
from scipy.spatial import distance

# + colab={"base_uri": "https://localhost:8080/"} id="cYoJiS2BpkcL" outputId="70a869c0-bc32-4371-b76c-23cbba0e30e6"
dist_matrix=distance.cdist(n,m)
dist_matrix.shape


# + colab={"base_uri": "https://localhost:8080/"} id="_mYSZjzup9Df" outputId="56172d15-a885-4120-9647-182c924cb55c"
print(dist_matrix)

# + colab={"base_uri": "https://localhost:8080/", "height": 278} id="EtyuHTMxqfVe" outputId="892b1256-2105-47b4-eb02-14c3c64a3ae7"
sbs.heatmap(dist_matrix)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 131} id="-BCZvRYbqrMI" outputId="cca1f26e-2f77-4796-a26d-40bd68e61d4d"
#numerical attribute
#extract
df.head(2)

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="mG9rleUwrW4C" outputId="d3db3583-f51f-4bf8-dde2-630f73ae6c6c"
numeric=df[['age','Medu','Fedu','studytime','failures']]
numeric.head()

# + colab={"base_uri": "https://localhost:8080/"} id="mI0j8-Hbrwu1" outputId="c946d204-aedf-4cee-ca28-348489542ed6"
num1=np.array(numeric[['age','failures']])
num1.reshape(-1,2)
num1.shape

# + colab={"base_uri": "https://localhost:8080/"} id="9jHvN3sdsK9x" outputId="866555c4-777c-4e2c-cc17-38db9affb134"
num2=np.array(numeric[['Fedu','Medu']])
num2.reshape(-1,2)
num2.shape

# + colab={"base_uri": "https://localhost:8080/"} id="qqJnJa_SsW7G" outputId="9b572f08-8d50-476a-ac95-66d149735bce"
#Euclidean distance
dist_matrix=distance.cdist(num1,num2)
print(dist_matrix)

# + colab={"base_uri": "https://localhost:8080/", "height": 278} id="ehpalU-esokb" outputId="cbb4d306-f90e-467d-d168-ed24234fb118"
sbs.heatmap(dist_matrix)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="eLjkKQxzsumI" outputId="385928a9-de6e-42a0-b367-f43ead86ef4e"
#Nominal Attributes(name or chars or string)
nomi=df[['Mjob','Fjob','reason','guardian']]
nomi.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="7_sX_woDtdU8" outputId="832c1912-55a0-4a5a-e710-ff2ad111983e"
nomi=nomi.replace('at_home','home')
nomi.head()

# + id="YoFh1hn3t22j"
# 1st convert into categorical/ ordinal 
nomi=nomi.astype('category')

# + id="so4t45IBuvRo"
# labelencoder gives a unique and normalised nalue like from 0,1,2 etc
from sklearn.preprocessing import LabelEncoder
lb=LabelEncoder()

# + id="fZCm_GJZvAzV"
#fit the labelencoder and return the label value
nomi['guardian']=lb.fit_transform(nomi['guardian'])
nomi['Mjob']=lb.fit_transform(nomi['Mjob'])
nomi['Fjob']=lb.fit_transform(nomi['Fjob'])
nomi['reason']=lb.fit_transform(nomi['reason'])


# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="8fHpe0QTvofj" outputId="84c6966f-d8b6-431e-d4ce-15133602ca45"
nomi.head()

# + colab={"base_uri": "https://localhost:8080/"} id="YlFeXeEtwAYr" outputId="eef2dd01-9cec-40ff-ee94-50fb6e623e89"
nom1=np.array(nomi)
nom1.reshape(-1,2)
nom1.shape

# + colab={"base_uri": "https://localhost:8080/"} id="w9jIzQEuwoxr" outputId="36fa4db0-3899-45ae-8e5f-8518ffcf804f"
nom2=np.array(nomi)
nom2.reshape(-1,2)
nom2.shape

# + colab={"base_uri": "https://localhost:8080/"} id="0XCWXrJZwuHA" outputId="91b6ac56-2ca1-42d0-d3e6-ed446269afae"
dist_matrix2=distance.cdist(nom1,nom2)
dist_matrix2.shape

# + colab={"base_uri": "https://localhost:8080/"} id="f7a4uH_wxltG" outputId="abc7ad0e-bf97-4458-a102-d92d01d216aa"
print(dist_matrix2)

# + colab={"base_uri": "https://localhost:8080/", "height": 278} id="rBFCBPbxxutI" outputId="98da7018-1794-45ed-857d-43ad71955988"
sbs.heatmap(dist_matrix2)
plt.show()

StarcoderdataJupyterScriptsDedupFiltered