-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.py
79 lines (62 loc) · 2.22 KB
/
kmeans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 13 22:24:30 2023
@author: Hamza
"""
import cv2
import numpy as np
import os
from glob import glob
import matplotlib.pyplot as plt
#lib for lazy
from lazypredict.Supervised import LazyClassifier
#from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
import matplotlib.pyplot as plt
import numpy
from sklearn import metrics
from sklearn.cluster import KMeans
from sklearn.linear_model import LogisticRegression
#-------
folder_path = 'C:/Users/Hamza/Desktop/dataset50'
folder_images = glob(folder_path + '/*/*.jpg')
#hog = cv2.HOGDescriptor()
from skimage.feature import hog
image_features = []
label_features=[]
total_images=len(folder_images)
for i,image_path in enumerate(folder_images):
ir_=os.path.basename(os.path.dirname(image_path))
image = cv2.imread(image_path)
image1=image[...,2]
#imagea=np.expand_dims(image1,-1)
fd, hog_image = hog(image1, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(2, 2), visualize=True, multichannel=False)
#plt.imshow(hog_image,cmap='gray')
#break
#features = hog.compute(image)
image_features.append(fd)
label_features.append(ir_)
print(i+1, '/' , total_images,'-->',round((i+1)/total_images*100,4),'%')
X=np.array(image_features)
y=np.array(label_features)
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=.2,random_state =123)
model=RandomForestClassifier()
model.fit(X_train,y_train)
accuracy=model.score(X_test,y_test)
print("Accuracy: {:.2f}%".format(accuracy * 100))
kmeans = KMeans(n_clusters=2)
# Fit the model on the training data
kmeans.fit(X_train)
# Get the cluster assignments for the training data
train_clusters = kmeans.predict(X_train)
# Get the cluster assignments for the test data
test_clusters = kmeans.predict(X_test)
# Use the cluster assignments as features in a logistic regression model
model = LogisticRegression()
model.fit(train_clusters.reshape(-1,1), y_train)
accuracy = model.score(test_clusters.reshape(-1,1), y_test)
print("Accuracy: {:.2f}%".format(accuracy * 100))