-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclassification.py
More file actions
137 lines (100 loc) · 4.11 KB
/
classification.py
File metadata and controls
137 lines (100 loc) · 4.11 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from PyQt5.QtCore import QVariant
from qgis.core import *
import matplotlib.pyplot as plt
import numpy as np
import os
from sklearn import datasets
from sklearn import preprocessing
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn.metrics import pairwise_distances_argmin
from sklearn.utils import Bunch
"""
Classification code only k-means for the moment
"""
# Transforing classification to vector layer (layer : input layer, attributes : attribute used for classification, layerName ; name of the layer, attributeClasse : attribute of the classification, attributeID : name of attribute ID) , copyAtt indicate if other attributes are copied in output
def kmeans(layer, attributes, nbClasses, layerName, attributeClass, attributeID, copyAtt):
# Load in the `digits` data
dataset = prepareDataset(layer, attributes)
# Defining k-means
k_means = KMeans(init='k-means++', n_clusters=nbClasses, n_init=10)
# Applying k-means
k_means.fit(dataset.data)
# Determining centers
k_means_cluster_centers = np.sort(k_means.cluster_centers_, axis=0)
# Association between points and nearest centers
k_means_labels = pairwise_distances_argmin(dataset.data, k_means_cluster_centers)
# Transforming classification to VectorLayer
return export(layer, attributes, layerName, attributeClass, k_means_labels, attributeID, copyAtt)
"""
DataManagement
"""
"""
Preparing dataset for sklearn from a layer and a set of selected numeric attributes
"""
def prepareDataset(layer, attributes):
n_samples = layer.featureCount()
n_features = len(attributes)
valueArray = []
attributeArray = []
for a in attributes:
attributeArray.append(a)
columns = np.array(attributeArray)
for f in layer.getFeatures():
data_temp = []
for a in attributes:
data_temp.append(f.attribute(a))
valueArray.append(data_temp)
data = np.array(valueArray)
# print(data)
data = preprocessing.scale(data)
# print(data)
dataset = Bunch(data=data, columns=columns)
return dataset
"""
Exporting a vector layer from input dataset and classification
"""
# Transforing classification to vector layer (layer : input layer, attributes : attribute used for classification, layerName ; nae of the layer, attributeClasse : attribute of the classification, vectorClass : the vector with corresponding class, attributeID : name of attribute ID)
def export(layer, attributes, layerName, attributeClass, vectorClass, attributeID, copyAtt):
vl = QgsVectorLayer("Polygon", layerName, "memory")
pr = vl.dataProvider()
vl.startEditing()
fields = [QgsField(attributeClass, QVariant.Int)]
if copyAtt :
#If copy is activated we copy all attributes
for fieldTemp in layer.fields():
fields.append(fieldTemp)
else :
#if not we only keeps the fild id and necessary to classification
fields.append(layer.fields().field(attributeID))
for a in attributes:
fields.append(QgsField(a, QVariant.Double, "Real", 10, 3))
pr.addAttributes(fields)
vl.updateFields()
featureList = []
count = 0;
for f in layer.getFeatures():
geom = f.geometry()
feat = QgsFeature()
feat.setGeometry(geom)
feat.initAttributes(len(fields))
#print(vectorClass[count])
feat.setAttribute(0, vectorClass.item(count))
countAtt = 1
if copyAtt:
countTemp = 0;
for field in layer.fields() :
feat.setAttribute( countAtt, f.attribute(countTemp))
countTemp+=1
countAtt+=1
else:
feat.setAttribute(1, f.attribute(attributeID))
countAtt+=1
for a in attributes:
feat.setAttribute(countAtt, f.attribute(a))
countAtt = countAtt + 1
count = count + 1
featureList.append(feat)
pr.addFeatures(featureList)
vl.commitChanges()
return vl