<jupyter_start><jupyter_text>Data Mining HW1 - DBSCAN  <NAME>) Ridaoui  1950044013 Algorithm mentioned in the book (Kumar):<jupyter_code>from sklearn import datasets
from sklearn.preprocessing import scale
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
import queue
iris_t =datasets.load_iris() #For information!!
iris =datasets.load_iris(True) #data,target
iris_t.columns=['Sepal length','Sepal Width','petal Length','Petal Width']
X = pd.DataFrame(iris_t.data)
X
y = pd.DataFrame(iris_t.target)
y
list(iris_t.target_names) #names of classes which means species of each plant<jupyter_output><empty_output><jupyter_text>This is more detailed version of algorithm from Kaufmann's book:<jupyter_code>#Mark all objects as unvisited
UNVISITED=0
VISITED =1
clusters=[]   
def neighborhood(eps,index,minpts,data):
    n_points=[]
    for i in range(len(data)):
        if np.linalg.norm(data[i]-data[index])<= eps:
            n_points.append(i)
    return n_points if len(n_points)>minpts else 0
def dbscan(data,eps,minpts):
    #1 mark all objects as unvisited
    vLabel= [UNVISITED] * len(data)
    #print("hi1")
    while (0 in vLabel):#(16)
        #print("hi2")
        #randomly select an unvisited object p
        randomP = -1
        rand_ind=-1
        while  rand_ind==-1:
            rand_ind = random.randint(0, len(data)-1)
            randomP = data[rand_ind]
         
        
        vLabel[rand_ind]=VISITED
        N=[]
        N= neighborhood(eps,rand_ind,minpts,data)
        if N !=0:
            q = []
            q.append(randomP)
            for eachp_ind in range(len(N)): # eachp is p'
                if vLabel[eachp_ind]==0:
                    vLabel[eachp_ind]=1
                    neig_P =neighborhood(eps,rand_ind,minpts,data)  
                    if neig_P!=0:
                        N.append(neig_P)
                if vLabel[eachp_ind]!=1 or vLabel[eachp_ind]!=-1:
                    q.append(data[eachp_ind])
                    
            #if not(q in clusters):
            clusters.append(q)#if not already in a cluster!!!             
        else:
            vLabel[rand_ind]=-1 #mark as noise
    return clusters<jupyter_output><empty_output><jupyter_text>Parameters v1<jupyter_code>iris_tuple=iris[:4]
eps=0.4
minpts=19
a.clear()
a=dbscan(iris_tuple[0],eps,minpts)
for i in range(len(a)):
    print("Cluster ",i,a[i])
len(a)#number of clusters<jupyter_output><empty_output><jupyter_text>parameters v2<jupyter_code>eps=0.7
minpts=10
a.clear()
a=dbscan(iris_tuple[0],eps,minpts)
len(a)#number of clusters<jupyter_output><empty_output><jupyter_text>parameters v3<jupyter_code>eps=0.2
minpts=40
a.clear()
a=dbscan(iris_tuple[0],eps,minpts)
len(a)#number of clusters<jupyter_output><empty_output><jupyter_text>Note1: when eps is low and minpts is high it is expected that one cluster will be found at least! My solution... Note: I need to rewrite controls of clusters :/   Library Results I will use library to compare my results with actual results.<jupyter_code>from sklearn.cluster import DBSCAN
from collections import Counter
iris =datasets.load_iris()
X = pd.DataFrame((iris.data))
y = pd.DataFrame(iris.target)
X.columns=['Sepal length','Sepal Width','petal Length','Petal Width']
model = DBSCAN(eps=0.8, min_samples=19).fit(X)
model.labels_
print(Counter(model.labels_))

outliers = X[model.labels_==-1]
print(outliers)
colors=model.labels_
plt.scatter( X.values[:,2] , X.values[:,1],c=colors )
plt.ylabel("Sepal Width")
plt.xlabel("Petal Length")
plt.show()


from mpl_toolkits.mplot3d import axes3d 
import matplotlib.pyplot as plt 
from matplotlib import style 
import numpy as np 
  
# setting a custom style to use 
style.use('ggplot') 
  
# create a new figure for plotting 
fig = plt.figure(figsize=(20,10)) 
  
# create a new subplot on our figure 
# and set projection as 3d 
ax1 = fig.add_subplot(111, projection='3d') 
  
# defining x, y, z co-ordinates 
x = X.values[:,2] 
y = X.values[:,1] 
z = X.values[:,0]
t= y
# plotting the points on subplot 
pl =ax1.scatter(x, y, z, c = t, marker = 'o', cmap=plt.hot())
fig.colorbar(pl)  
# setting labels for the axes 
ax1.set_xlabel('x-axis') 
ax1.set_ylabel('y-axis') 
ax1.set_zlabel('z-axis') 
  
# function to show the plot 
plt.show()
model = DBSCAN(eps=0.2, min_samples=40).fit(X)
colors=model.labels_
plt.scatter( X.values[:,2] , X.values[:,1],c=colors )
plt.ylabel("Sepal Width")
plt.xlabel("Petal Length")
plt.show()
model3 = DBSCAN(eps=0.2, min_samples=5).fit(X)
colors=model3.labels_
plt.scatter( X.values[:,2] , X.values[:,1],c=colors )
plt.ylabel("Sepal Width")
plt.xlabel("Petal Length")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12953478446436192, 'fraction_numerical': 0.019419547588561674, 'mean_word_length': 3.701103309929789, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '465804', 'license': 'MIT', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1716, 'n_words': 394, 'path': 'dbscan_.ipynb', 'repo_name': 'rmham93/DBSCAN-Clustering', 'size': '4692'}