Commit 5b0c013c authored by Alessio Cosenza's avatar Alessio Cosenza 🐲
Browse files

Testing

parent 67b5fbf0
__pycache__/*
model.*
\ No newline at end of file
......@@ -26,7 +26,6 @@ class DataAnalyzer:
def applyPCA(self, dataframe: pd.DataFrame):
pca = PCA(n_components=self.n_components)
fitted = pca.fit_transform(dataframe[self.metrics])
dataframe[self.axis[:self.n_components]] = pd.DataFrame(fitted, index=dataframe.index)
def applyKMeans(self, dataframe: pd.DataFrame, n_clusters: int) -> KMeans:
......@@ -45,26 +44,30 @@ class DataAnalyzer:
def plot(self, dataframe: pd.DataFrame, labels: List[int], centroids: List[List[float]]):
if self.n_components == 2:
ax = plt.subplot(1, 1, 1)
scat = ax.scatter(dataframe['x'], dataframe['y'], c=labels, cmap=self.cmap, label=labels)
# scat = ax.scatter(dataframe['x'], dataframe['y'], c=labels, cmap=self.cmap, label=labels)
else:
fig = plt.figure()
ax = Axes3D(fig)
scat = ax.scatter(dataframe['x'], dataframe['y'], dataframe['z'], c=labels, cmap=self.cmap, label=labels)
# legend = ax.legend(*scat.legend_elements(), loc='upper right', title='Clusters')
# ax.add_artist(legend)
# scat = ax.scatter(dataframe['x'], dataframe['y'], dataframe['z'], c=labels, cmap=self.cmap, label=labels)
#legend = ax.legend(*scat.legend_elements(), loc='upper right', title='Clusters')
#ax.add_artist(legend)
# , marker='°'
arr = np.arange(len(centroids))
ax.scatter(centroids[:, 0], centroids[:, 1], c=arr, cmap=self.cmap, marker='*', s=500, lw=1.5, edgecolor=(0, 0, 0, 1))
ax.scatter(centroids[:, 0], centroids[:, 1], c=arr, cmap=self.cmap, marker='*', s=400, lw=1.5, edgecolor=(0, 0, 0, 1))
# ax.set_xlim([-1000000, 8000000])
# ax.set_ylim([0, 2600])
# if (self.n_components == 3):
# ax.set_zlim([-80, 80])
ax.set_xticklabels([])
ax.set_yticklabels([])
if (self.n_components == 3):
ax.set_zticklabels([])
return ax, plt
def addPatientPoint(self, dataframe: pd.DataFrame, ax: Axes, processInstanceId: str, lastOrKM: bool):
def addPatientPoint(self, dataframe: pd.DataFrame, ax: Axes, processInstanceId: str, lastOrKM: bool, kmeans: KMeans):
#If lastOrKM is true it plots the cluster of all patient's data
patientData = dataframe[dataframe['processInstanceId'] == processInstanceId]
......@@ -76,15 +79,26 @@ class DataAnalyzer:
pointToPlot = patientData.tail(1)[self.axis[:self.n_components]].to_numpy()[0]
if self.n_components == 2:
ax.scatter(pointToPlot[0], pointToPlot[1], marker='*', c='black', s=450)
ax.scatter(pointToPlot[0], pointToPlot[1], c=kmeans.predict(pointToPlot.reshape(1, -1)), s=250, marker='x')
else:
ax.scatter(pointToPlot[0], pointToPlot[1], pointToPlot[2], marker='*', c='black', s=450)
ax.scatter(pointToPlot[0], pointToPlot[1], pointToPlot[2], c=kmeans.predict(pointToPlot.reshape(1, -1)), s=250, marker='x', cmap=self.cmap)
return pointToPlot
def addAllPatientPoints(self, dataframe: pd.DataFrame, ax: Axes, processInstanceId: str):
patientData = dataframe[dataframe['processInstanceId'] == processInstanceId]
data = patientData[self.axis[:self.n_components]].to_numpy()
if self.n_components == 2:
x = data[:, 0]
y = data[:, 1]
ax.quiver(x[:-1:4], y[:-1:4], x[1::4] - x[:-1:4], y[1::4] - y[:-1:4])
#ax.plot(data[:, 0], data[:, 1], c='green')
else:
x = data[:, 0]
y = data[:, 1]
z = data[:, 2]
ax.quiver(x[:-1], y[:-1], z[:-1], x[1:] - x[:-1], y[1:] - y[:-1], z[1:] - z[:-1], length=10, normalize=True, arrow_length_ratio=0.9)
......@@ -21,8 +21,8 @@ def main(n_dimensions: int, n_clusters: int, processInstanceId: str, method: str
method = True if method == 'cluster' else False
point = da.addPatientPoint(df, ax, processInstanceId, method)
# da.addAllPatientPoints(df, ax, processInstanceId)
# point = da.addPatientPoint(df, ax, processInstanceId, True, kmeans)
da.addAllPatientPoints(df, ax, processInstanceId)
#distance = kmeans.transform(point.reshape(1, -1))
#normalizedDistance = (distance / np.linalg.norm(distance, ord=2, axis=1, keepdims=True))[0]
# out = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment