Import libraries to use


In [1]:
#import sys
#sys.path.append("/home/svanhmic/workspace/Python/Erhvervs/src/cvr")

SQLContext.newSession(sqlContext)

from pyspark.sql import functions as F
from pyspark.accumulators import AccumulatorParam
from pyspark.ml.feature import VectorAssembler, StandardScaler
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from pyspark.sql.types import StringType,StructField,StructType,ArrayType,DoubleType
from pyspark.ml.linalg import Vectors, VectorUDT,Matrix,MatrixUDT,DenseMatrix
from pyspark.ml.clustering import KMeans
from pyspark.sql import Row
from pyspark.sql import Window
from spark_sklearn import GridSearchCV,Converter
from sklearn.cluster import KMeans as skKmeans
from sklearn.linear_model import LogisticRegression as skLogistic
from pyspark.mllib.linalg import Vectors as oVector, VectorUDT as oVectorUDT


import pandas as pd
from IPython.display import display
import re
import random
from prettytable import PrettyTable
import sys
from datetime import datetime
from operator import add
import numpy as np
import matplotlib.pyplot as plt
#from spark_sklearn import GridSearchCV,Converter
PATH = "/home/svanhmic/workspace/Python/Erhvervs/data/cdata/"
sc.addPyFile("/home/svanhmic/workspace/Python/Erhvervs/src/cvr/GridSearchLogRegAndKmeans.py")

import GridSearchLogRegAndKmeans


/usr/local/lib/python3.5/dist-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
/usr/local/lib/python3.5/dist-packages/sklearn/grid_search.py:43: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. This module will be removed in 0.20.
  DeprecationWarning)

Cleaned Data from cvr is imported, rank column name is changed to vaerdiSlope


In [2]:
#import data and rename bad name rank into vaerdiSlope
df = (sqlContext
      .read
      .parquet(PATH+"featureDataCvr")
     )

rankCols = [re.sub(pattern="rank_",repl="vaerdiSlope_",string=i) for i in df.columns ]
renamedDf = (df
             .withColumn(colName="reklamebeskyttet",col=F.col("reklamebeskyttet").cast("integer"))
             .select([F.col(val).alias(rankCols[idx]) for idx,val in enumerate(df.columns)])
            )
renamedDf.show()


+---------+--------------------+-----+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+------------+------------+------------+------------+------------+------------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+---------+---------+---------+---------+---------+-----------+-----------------+------------------+------------------+------------------+------------------+-------------+-------------+-------------+-------------+----------------+---------------+
|cvrNummer|              status|label|AarsVaerk_1|AarsVaerk_2|AarsVaerk_3|AarsVaerk_4|AarsVaerk_5|AarsVaerk_6|AarsVaerk_7|AarsVaerk_8|AarsVaerk_9|AarsVaerk_10|AarsVaerk_11|AarsVaerk_12|AarsVaerk_13|AarsVaerk_14|AarsVaerk_15|medArb_1|medArb_2|medArb_3|medArb_4|medArb_5|medArb_6|medArb_7|medArb_8|medArb_9|medArb_10|medArb_11|medArb_12|medArb_13|medArb_14|medArb_15|avgVarighed|totalAabneEnheder|totalLukketEnheder|     vaerdiSlope_1|     vaerdiSlope_2|     vaerdiSlope_3|vaerdiSlope_4|vaerdiSlope_5|vaerdiSlope_6|vaerdiSlope_7|reklamebeskyttet|kortBeskrivelse|
+---------+--------------------+-----+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+------------+------------+------------+------------+------------+------------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+---------+---------+---------+---------+---------+-----------+-----------------+------------------+------------------+------------------+------------------+-------------+-------------+-------------+-------------+----------------+---------------+
| 10029961|            [NORMAL]|  0.0|        2.0|        2.0|        2.0|        2.0|        2.0|        2.0|        2.0|        2.0|        2.0|         2.0|         2.0|         2.0|        null|        null|        null|     2.0|     2.0|     5.0|     2.0|     2.0|     2.0|     2.0|     2.0|     5.0|      5.0|      5.0|      5.0|     null|     null|     null|       null|                0|                 1|              null|              null|              null|         null|         null|         null|         null|               0|            A/S|
| 10043271|            [NORMAL]|  0.0|        2.0|        5.0|        5.0|        5.0|        5.0|        5.0|        5.0|        5.0|        5.0|         5.0|         5.0|         5.0|         5.0|         2.0|         5.0|     2.0|     5.0|     5.0|     5.0|     5.0|     5.0|     5.0|     5.0|     5.0|      5.0|      5.0|      5.0|      5.0|      5.0|      5.0|       null|                0|                 1|  36.8437212158428|              null|              null|         null|         null|         null|         null|               0|            APS|
| 10058511|[OPLØST EFTER ERK...|  0.0|        1.0|        2.0|        2.0|        2.0|        1.0|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     1.0|     5.0|     5.0|    10.0|     2.0|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|    1281.25|                4|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            APS|
| 10058511|[OPLØST EFTER ERK...|  0.0|        1.0|        2.0|        2.0|        2.0|        1.0|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     1.0|     5.0|     5.0|    10.0|     2.0|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|    1281.25|                4|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            APS|
| 10063027|[TVANGSOPLØST, UN...|  0.0|        1.0|        1.0|       null|       null|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     1.0|     1.0|    null|    null|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|     1356.0|                1|                 0|105.02173913043478|301.92857142857144|              null|         null|         null|         null|         null|               0|            APS|
| 10063027|[TVANGSOPLØST, UN...|  0.0|        1.0|        1.0|       null|       null|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     1.0|     1.0|    null|    null|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|     1356.0|                1|                 0|105.02173913043478|301.92857142857144|              null|         null|         null|         null|         null|               0|            APS|
| 10063027|[TVANGSOPLØST, UN...|  0.0|        1.0|        1.0|       null|       null|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     1.0|     1.0|    null|    null|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|     1356.0|                1|                 0|105.02173913043478|301.92857142857144|              null|         null|         null|         null|         null|               0|            APS|
| 10063310|[TVANGSOPLØST, UN...|  0.0|        1.0|       null|       null|       null|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    null|    null|    null|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|      712.0|                2|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            APS|
| 10063310|[TVANGSOPLØST, UN...|  0.0|        1.0|       null|       null|       null|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    null|    null|    null|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|      712.0|                2|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            APS|
| 10063310|[TVANGSOPLØST, UN...|  0.0|        1.0|       null|       null|       null|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    null|    null|    null|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|      712.0|                2|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            APS|
| 10065615|            [NORMAL]|  0.0|        1.0|        1.0|        1.0|        1.0|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     1.0|     1.0|     1.0|     2.0|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|       null|                0|                 1|-612.4527363184079|               0.0|              null|         null|         null|         null|         null|               0|            A/S|
| 10065615|            [NORMAL]|  0.0|        1.0|        1.0|        1.0|        1.0|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     1.0|     1.0|     1.0|     2.0|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|       null|                0|                 1|-612.4527363184079|               0.0|              null|         null|         null|         null|         null|               0|            APS|
| 10080568|            [NORMAL]|  0.0|        1.0|        1.0|        1.0|       null|       null|       null|       null|       null|       null|        null|        null|        null|        null|        null|        null|     2.0|     0.0|     0.0|    null|    null|    null|    null|    null|    null|     null|     null|     null|     null|     null|     null|       null|                0|                 1|20161.290322580644| 2927.400468384075|11842.105263157895|         null|         null|         null|         null|               0|            A/S|
| 10083869|[OPLØST EFTER FRI...|  0.0|        1.0|       10.0|       10.0|        5.0|        5.0|        5.0|        5.0|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    20.0|    20.0|    20.0|    20.0|    20.0|    10.0|    null|    null|     null|     null|     null|     null|     null|     null|     3023.0|                1|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            EUO|
| 10083869|[OPLØST EFTER FRI...|  0.0|        1.0|       10.0|       10.0|        5.0|        5.0|        5.0|        5.0|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    20.0|    20.0|    20.0|    20.0|    20.0|    10.0|    null|    null|     null|     null|     null|     null|     null|     null|     3023.0|                1|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            A/S|
| 10083869|[OPLØST EFTER FRI...|  0.0|        1.0|       10.0|       10.0|        5.0|        5.0|        5.0|        5.0|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    20.0|    20.0|    20.0|    20.0|    20.0|    10.0|    null|    null|     null|     null|     null|     null|     null|     null|     3023.0|                1|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            APS|
| 10083869|[OPLØST EFTER FRI...|  0.0|        1.0|       10.0|       10.0|        5.0|        5.0|        5.0|        5.0|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    20.0|    20.0|    20.0|    20.0|    20.0|    10.0|    null|    null|     null|     null|     null|     null|     null|     null|     3023.0|                1|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            EUO|
| 10083869|[OPLØST EFTER FRI...|  0.0|        1.0|       10.0|       10.0|        5.0|        5.0|        5.0|        5.0|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    20.0|    20.0|    20.0|    20.0|    20.0|    10.0|    null|    null|     null|     null|     null|     null|     null|     null|     3023.0|                1|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            A/S|
| 10083869|[OPLØST EFTER FRI...|  0.0|        1.0|       10.0|       10.0|        5.0|        5.0|        5.0|        5.0|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    20.0|    20.0|    20.0|    20.0|    20.0|    10.0|    null|    null|     null|     null|     null|     null|     null|     null|     3023.0|                1|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            APS|
| 10083869|[OPLØST EFTER FRI...|  0.0|        1.0|       10.0|       10.0|        5.0|        5.0|        5.0|        5.0|       null|       null|        null|        null|        null|        null|        null|        null|     0.0|    20.0|    20.0|    20.0|    20.0|    20.0|    10.0|    null|    null|     null|     null|     null|     null|     null|     null|     3023.0|                1|                 0|              null|              null|              null|         null|         null|         null|         null|               0|            EUO|
+---------+--------------------+-----+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+------------+------------+------------+------------+------------+------------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+---------+---------+---------+---------+---------+-----------+-----------------+------------------+------------------+------------------+------------------+-------------+-------------+-------------+-------------+----------------+---------------+
only showing top 20 rows


In [3]:
def getAllDistances(matrix1,matrix2):
    return [[np.linalg.norm(v-w) for v in matrix1 ] for w in matrix2]

def createPrettyTable(matrix,labels,names=None):
    #print(type(matrix))
    try:
        x = PrettyTable(["v"]+labels)
    except:
        x = PrettyTable(["v"]+list(labels))
    if names is not None:
        zippedMatrix = zip(names,matrix)
    else:
        zippedMatrix = enumerate(matrix)
    
    
    for  i in labels:
        x.align[str(i)] = "l"
    for idx,v in zippedMatrix:
        #print(type(v))
        x.add_row([str(idx)]+['{:.7}'.format(str(i)) for i in v])
    return x

In [5]:
#import name data frame

windowSpecRank =(Window.partitionBy(F.col("cvrNummer"))).orderBy(F.col("periode_gyldigFra").desc())

groupCols = ["cvrNummer","vaerdi"]

companyNameDf = (sqlContext
                 .read
                 .parquet(PATH+"companyCvrData")
                 .withColumn(colName="rank",col=F.rank().over(windowSpecRank))
                 .filter((F.col("rank")==1) & (F.col("sekvensnr")==0))
                 .select([F.col(i) for i in groupCols])
                 .withColumnRenamed(existing="vaerdi",new="navn")
                 .orderBy(F.col("cvrNummer"))
                 .cache()
                )
#companyNameDf.show()

In [5]:
#take ln(x+1) of features
#Depricated!
labelCols = ["navn","cvrNummer","label","status"]
featCols = [i for i in companyNameDf.columns+renamedDf.columns if i not in labelCols]

minCols = [F.min(i).alias(i) for i in featCols]
minValsRdd = renamedDf.groupby().agg(*minCols).rdd
broadcastedmin = sc.broadcast(minValsRdd.first().asDict())

logColsSelected = [F.col(i).alias(i) for i in labelCols]+[(F.col(i)-F.lit(broadcastedmin.value[i])).alias(i) for i in featCols]
logDf = (renamedDf
         .join(companyNameDf,(companyNameDf["cvrNummer"]==renamedDf["cvrNummer"]),"inner")
         .drop(companyNameDf["cvrNummer"])
         .select(*logColsSelected)
         .select([F.col(i).alias(i) for i in labelCols]+[F.log1p(F.col(i)).alias(i) for i in featCols])
         .distinct()
         .na
         .fill(0.0,featCols) # yes i know we set null to zero here. But a company has zero in kapital if it is not opened
         .cache()
        )

In [6]:
#First convert features to vetor and scale it with mean and stddev
toDenseUDf = F.udf(lambda x: Vectors.dense(x.toArray()),VectorUDT())
vectorizer = VectorAssembler(inputCols=featCols,outputCol="features")

rawVectorDataDf = (vectorizer.transform(renamedDf                                                                               
                                        .join(companyNameDf,(companyNameDf["cvrNummer"]==renamedDf["cvrNummer"]),"inner")
                                        .drop(companyNameDf["cvrNummer"]) 
                                        .na
                                        .fill(0.0,featCols)
                                        .distinct()
                                       )
                   .select(labelCols+[toDenseUDf(vectorizer.getOutputCol()).alias(vectorizer.getOutputCol())])
                  )

standardScale = StandardScaler(withMean=True,withStd=True,inputCol=vectorizer.getOutputCol(),outputCol="scaledFeatures")
standardScaleModel = standardScale.fit(rawVectorDataDf)
scaledFeaturesDf = (standardScaleModel
                    .transform(rawVectorDataDf)
                    .drop("features")
                    .withColumnRenamed(existing="scaledFeatures",new="features")
                    .filter(F.col("label") != 2)
                   )
scaledFeaturesDf.show()


+--------------------+---------+-----+--------------------+--------------------+
|                navn|cvrNummer|label|              status|            features|
+--------------------+---------+-----+--------------------+--------------------+
|   EMPAKA KARTONNAGE| 10016533|    1|            [NORMAL]|[0.12721405809989...|
|      DET GAMLE GODS| 10016606|    1|            [NORMAL]|[-0.1483813281958...|
|               DIXEN| 10018064|    0|[OPLØST EFTER KON...|[-0.1483813281958...|
|        TRELBORG VVS| 10063760|    1|            [NORMAL]|[-0.1483813281958...|
|            CYBERSUN| 10065917|    0|[OPLØST EFTER KON...|[-0.0258944898421...|
|                HME2| 10080207|    1|            [NORMAL]|[-0.1483813281958...|
|TØMRERFIRMAET HER...| 10082528|    1|            [NORMAL]|[-0.1177596186074...|
|    MØRKHOLT VINDUER| 10096227|    1|            [NORMAL]|[-0.1483813281958...|
|                KISØ| 10108993|    1|            [NORMAL]|[-0.1177596186074...|
|               C-CUT| 10117267|    1|[OPLØST EFTER ERK...|[-0.1483813281958...|
|          HLS INVEST| 10120829|    0|[OPLØST EFTER KON...|[-0.1483813281958...|
|       ELKA RAINWEAR| 10138698|    1|            [NORMAL]|[0.12721405809989...|
|              BØMLER| 10142539|    0|[OPLØST EFTER KON...|[-0.1483813281958...|
|     NORDISKE MEDIER| 10150825|    1|            [NORMAL]|[-0.1177596186074...|
|  SBR POUL-ERIK KJÆR| 10178509|    0|[OPLØST EFTER KON...|[-0.1483813281958...|
|         SCAN ORIENT| 10228476|    1|[TVANGSOPLØST, UN...|[-0.1483813281958...|
|           AQUA-NORD| 10310989|    1|[OPLØST EFTER FRI...|[-0.1177596186074...|
|        IKAST ETIKET| 10402980|    1|            [NORMAL]|[-0.1177596186074...|
|BRDR. JØRGENSEN C...| 10518431|    1|            [NORMAL]|[0.43343115398404...|
|         AXEL MADSEN| 10553490|    1|            [NORMAL]|[-0.0258944898421...|
+--------------------+---------+-----+--------------------+--------------------+
only showing top 20 rows


In [7]:
#show new summery statistics
stats = [standardScaleModel.mean,standardScaleModel.std]
statsShow = createPrettyTable(matrix=stats,labels=featCols,names=["mean","std"])
print(statsShow)


+------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+--------------+--------------+--------------+--------------+--------------+--------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+-----------+-----------+-----------+-----------+-----------+-------------+-------------------+--------------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+------------------+
|  v   | AarsVaerk_1 | AarsVaerk_2 | AarsVaerk_3 | AarsVaerk_4 | AarsVaerk_5 | AarsVaerk_6 | AarsVaerk_7 | AarsVaerk_8 | AarsVaerk_9 | AarsVaerk_10 | AarsVaerk_11 | AarsVaerk_12 | AarsVaerk_13 | AarsVaerk_14 | AarsVaerk_15 | medArb_1 | medArb_2 | medArb_3 | medArb_4 | medArb_5 | medArb_6 | medArb_7 | medArb_8 | medArb_9 | medArb_10 | medArb_11 | medArb_12 | medArb_13 | medArb_14 | medArb_15 | avgVarighed | totalAabneEnheder | totalLukketEnheder | vaerdiSlope_1 | vaerdiSlope_2 | vaerdiSlope_3 | vaerdiSlope_4 | vaerdiSlope_5 | vaerdiSlope_6 | vaerdiSlope_7 | reklamebeskyttet |
+------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+--------------+--------------+--------------+--------------+--------------+--------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+-----------+-----------+-----------+-----------+-----------+-------------+-------------------+--------------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+------------------+
| mean | 5.84562     | 6.19433     | 5.83882     | 5.77540     | 5.24740     | 4.66470     | 4.18111     | 3.79860     | 3.41813     | 3.13869      | 2.85917      | 2.61095      | 2.30529      | 2.03331      | 1.76706      | 6.66283  | 7.62812  | 7.09079  | 6.93549  | 6.54344  | 5.81079  | 5.18615  | 4.62285  | 4.16564  | 3.80297   | 3.44311   | 3.15938   | 2.80299   | 2.44629   | 2.11903   | 1704.17     | 0.97203           | 0.69349            | 5156.18       | 2752.85       | 2728.82       | 32104.2       | 1720.06       | 1138.45       | 1777.23       | 0.08140          |
| std  | 32.6565     | 32.8589     | 33.7437     | 34.8135     | 33.8392     | 31.6859     | 30.3409     | 29.1166     | 27.8495     | 27.0790      | 25.8592      | 24.7475      | 23.1631      | 22.7200      | 21.3953      | 36.2179  | 37.1435  | 37.2833  | 38.3626  | 37.8808  | 35.7942  | 34.0816  | 32.2753  | 31.2939  | 30.0494   | 28.2444   | 27.3115   | 25.9211   | 24.8145   | 23.0689   | 2671.43     | 4.99587           | 3.07674            | 882936.       | 231044.       | 294379.       | 1127331       | 218631.       | 112231.       | 304935.       | 0.27345          |
+------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+--------------+--------------+--------------+--------------+--------------+--------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+-----------+-----------+-----------+-----------+-----------+-------------+-------------------+--------------------+---------------+---------------+---------------+---------------+---------------+---------------+---------------+------------------+

In [8]:
#dfDescriptionDf = renamedDf.describe()

In [9]:
def printDf(df):
    #this method is very narrow aimed towards this particular problem.
    ##TODO: create a general method for printing summery statistics. 
    df.select([F.col("summary")]+[F.col("AarsVaerk_"+str(i)) for i in range(1,6)]).show()
    df.select([F.col("summary")]+[F.col("AarsVaerk_"+str(i)) for i in range(6,11)]).show()
    df.select([F.col("summary")]+[F.col("AarsVaerk_"+str(i)) for i in range(10,16)]).show()
    df.select([F.col("summary")]+[F.col("medArb_"+str(i)) for i in range(1,7)]).show()
    df.select([F.col("summary")]+[F.col("medArb_"+str(i)) for i in range(7,12)]).show()
    df.select([F.col("summary")]+[F.col("medArb_"+str(i)) for i in range(11,16)]).show()
    df.select([F.col("summary")]+[F.col("vaerdiSlope_"+str(i)) for i in range(1,6)]).show()
    df.select([F.col("summary")]+[F.col("vaerdiSlope_"+str(i)) for i in range(6,8)]).show()
    cols = ["avgVarighed","totalAabneEnheder","totalLukketEnheder","reklamebeskyttet"]
    df.select([F.col("summary")]+[F.col(i) for i in cols]).show()

In [ ]:


In [ ]:

Initial analysis completed now it's time for using machine learning!


In [ ]:


In [10]:
def getConfusion(label,prediction,outlier):
    diff = abs(label-prediction)
    if (diff == 0) and (label == 0):
        return "TN"
    elif (diff == 0) and (label == 1):
        return "TP"
    elif (diff == 1) and (label == 0):
        return "FP"
    elif (diff == 1) and (label == 1):
        return "FN"
    elif outlier > 0:
        return "Excluded"

subUdf = F.udf(lambda x,y,z: getConfusion(x,y,z),StringType())


def computeConfusion(df):
    cols = [F.col(i) for i in ("cvrNummer","label","predictionLogReg")]
    return (df
            .select(*cols,subUdf(df["label"],df["predictionLogReg"],df["isOutlier"]).alias("diff"))
            .groupby().pivot("diff",["TP","TN","FN","FP","Excluded"])
            .count()
            .withColumn(col=(F.col("TP")+F.col("TN"))/(F.col("TP")+F.col("TN")+F.col("FN")+F.col("FP")),colName="Accuracy")
           )


def showStats(df):
    accuracyDf = computeConfusion(df)
    accuracyDf.show()

In [11]:
def runner(df,**kvargs):
    '''
        This method computes the accuracy for each of iteration of the combined k-means logistic regression method. 
        It outputs a spark dataframe that has been subjected to the iterations and contains clusters that have been
        segmented into k clusters, and has been 
    '''
    if "isOutlier" not in df.columns: 
        df = df.withColumn(colName="isOutlier",col=F.lit(0))
    dfs = GridSearchLogRegAndKmeans.createPandasDf(sc,df,"features","cvrNummer","label","isOutlier")
    iteration = kvargs.get("i",2)
    labelCol = kvargs.get("labelCol","label")
    idCol = kvargs.get("idCol","cvrNummer")
    featuresCol = kvargs.get("featuresCol","features")
    threshold = kvargs.get("threshold",0.005)
    dfsI = dfs
    
    
    for i in range(1,iteration):
        dfsI = (GridSearchLogRegAndKmeans
                                       .onePass(sc,dfsI,{'n_clusters':(8,10),},featuresCol,idCol,labelCol,i,threshold)
                                      )
        #display(trainPdf)
        #display(testPdf)
        #display(OutlierPdf)
        sparkDf = sqlContext.createDataFrame(dfsI)
        if i == 1:
            totalDf = (computeConfusion(sparkDf)
                       .withColumn(col=F.lit(i),colName="iteration") 
                      )
        else:
            totalDf = (totalDf
                       .unionAll(computeConfusion(sparkDf).withColumn(col=F.lit(i),colName="iteration"))
                      )
    return (sparkDf,totalDf)

In [12]:
#print(dfs)

segmentedDf,statsDf = runner(scaledFeaturesDf,i=5)


no extra cols
Index(['cvrNummer', 'features', 'label', 'isOutlier'], dtype='object')
Remaining data points: 127527
Total data points: 159771
Excluded data points: 362
Remaining data points: 126883
Total data points: 159409
Excluded data points: 1167
Remaining data points: 126459
Total data points: 158604
Excluded data points: 1697
Remaining data points: 126444
Total data points: 158074
Excluded data points: 1715

In [ ]:


In [13]:
statsDf.show()


+-----+---+---+----+--------+------------------+---------+
|   TP| TN| FN|  FP|Excluded|          Accuracy|iteration|
+-----+---+---+----+--------+------------------+---------+
|25703| 96| 73|6010|     362|0.8092026849005709|        1|
|25519|149|107|5946|    1167|0.8091800384603259|        2|
|25379|204|150|5882|    1697|0.8092044915388265|        3|
|25378|199|148|5887|    1715|0.8090914842464887|        4|
+-----+---+---+----+--------+------------------+---------+


In [14]:
segmentedDf.show()


+---------+--------------------+---------+-----+----------------+----------------+
|cvrNummer|            features|isOutlier|label|predictionKmeans|predictionLogReg|
+---------+--------------------+---------+-----+----------------+----------------+
| 10016533|[0.12721405809989...|        0|    1|             1.0|             1.0|
| 10016606|[-0.1483813281958...|        0|    1|             2.0|             1.0|
| 10018064|[-0.1483813281958...|        0|    0|             7.0|             1.0|
| 10063760|[-0.1483813281958...|        0|    1|             8.0|             1.0|
| 10065917|[-0.0258944898421...|        0|    0|             0.0|             1.0|
| 10080207|[-0.1483813281958...|        0|    1|             8.0|             1.0|
| 10082528|[-0.1177596186074...|        0|    1|             8.0|             1.0|
| 10096227|[-0.1483813281958...|        0|    1|             8.0|             1.0|
| 10108993|[-0.1177596186074...|        0|    1|             8.0|             1.0|
| 10117267|[-0.1483813281958...|        0|    1|             8.0|             1.0|
| 10120829|[-0.1483813281958...|        0|    0|             0.0|             1.0|
| 10138698|[0.12721405809989...|        0|    1|             5.0|             1.0|
| 10142539|[-0.1483813281958...|        0|    0|             6.0|             1.0|
| 10150825|[-0.1177596186074...|        0|    1|             8.0|             1.0|
| 10178509|[-0.1483813281958...|        0|    0|             7.0|             1.0|
| 10228476|[-0.1483813281958...|        0|    1|             0.0|             1.0|
| 10310989|[-0.1177596186074...|        0|    1|             7.0|             1.0|
| 10402980|[-0.1177596186074...|        0|    1|             8.0|             1.0|
| 10518431|[0.43343115398404...|        0|    1|             5.0|             1.0|
| 10553490|[-0.0258944898421...|        0|    1|             8.0|             1.0|
+---------+--------------------+---------+-----+----------------+----------------+
only showing top 20 rows


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [4]:
vec = [Row(cluster=i,center=Vectors.dense(v)) for i,v in enumerate(bModel.clusterCenters())]
#print(type(vec))
SpDf = sqlContext.createDataFrame(data=vec)
#SpDf.show(truncate=False)

#compute the squared distance beetween the cluster-center and its data points.

featureContributionUdf = F.udf(lambda x,y: (x-y)*(x-y),VectorUDT() )
sqrtUdf = F.udf(lambda x,y: float(Vectors.norm(vector=x-y,p=2)),DoubleType())
printUdf = F.udf(lambda x: type(x),StringType())
#print(np.sum(vec[0]["vec"]))
joinedDf = (predictDf
            .withColumn(colName="features",col=toDenseUDf(F.col("features")))
            .join(SpDf,on=(predictDf["prediction"]==SpDf["cluster"]),how="left")
            .drop(SpDf["cluster"])
            .withColumn(colName="contribution",col=featureContributionUdf(F.col("features"),F.col("center")))
            .withColumn(colName="distance",col=sqrtUdf(F.col("features"),F.col("center")))
            .drop("centers")
            .drop("features")
            
            .orderBy(F.col("distance").desc())
           )

joinedDf.show(truncate=True)
#joinedDf.printSchema()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-6a8ddec13d04> in <module>()
----> 1 vec = [Row(cluster=i,center=Vectors.dense(v)) for i,v in enumerate(bModel.clusterCenters())]
      2 #print(type(vec))
      3 SpDf = sqlContext.createDataFrame(data=vec)
      4 #SpDf.show(truncate=False)
      5 

NameError: name 'bModel' is not defined

In [2]:
joinedDf.groupBy("label").count()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-8ad2af8ee58d> in <module>()
----> 1 joinedDf.groupBy("label").count()

NameError: name 'joinedDf' is not defined

In [ ]:


In [112]:
#show elements in joinedDf
#joinedDf.filter(F.col("prediction")==7).show(truncate=True)

#windowDistToAll  = (Window.partitionBy(F.col("prediction")))

class VectorAccumulatorParam(AccumulatorParam):
    def zero(self, value):
        return [0.0] * len(value)
    def addInPlace(self, val1, val2):
        for i in xrange(len(val1)):
            val1[i] += val2[i]
        return val1
    
va = sc.accumulator([1.0, 2.0, 3.0], VectorAccumulatorParam())
va.value
def g(x):
    global va
    va += [x] * 3
rdd.foreach(g)
va.value


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-112-75590c96ea10> in <module>()
      8 clustMatricesRDD = scaledFeatRDD.map(lambda x: ([x["label"]],[x["features"]]))
      9 combined = clustMatricesRDD.reduce(lambda x,y:(x[0]+y[0],x[1]+y[1]))
---> 10 print(combined.take(1))
     11 labelCount = (scaledFeaturesDf
     12               .select(F.col("label"),F.col("features"))

AttributeError: 'tuple' object has no attribute 'take'

In [32]:
centroids = bModel.clusterCenters()

cols = ["prediction","label"]
funcs = [F.count, F.sum]
funcCols = [f(i) for f in funcs for i in cols]
#list the number of elements in each cluster
groupedByClusters = (joinedDf
                     .filter(F.col("label") <= 1)
                     .groupBy(F.col("prediction"))
                     .agg(*funcCols)
                     .drop(F.col("sum(prediction)"))
                     .drop(F.col("count(label)"))
                     .withColumn(colName="bancruptcy ratio",col=F.col("sum(label)")/F.col("count(prediction)"))
                    )
groupedByClusters.show()

#show distances for all cluster centers
allDists = getAllDistances(centroids,centroids)
pred = createPrettyTable(allDists,range(0,len(allDists)))
print(pred)


+----------+-----------------+----------+------------------+
|prediction|count(prediction)|sum(label)|  bancruptcy ratio|
+----------+-----------------+----------+------------------+
|         1|               68|        68|               1.0|
|         6|                2|         2|               1.0|
|         3|                1|         1|               1.0|
|         5|            12753|     12130|0.9511487493138869|
|         9|              186|       176| 0.946236559139785|
|         4|                1|         1|               1.0|
|         8|            22407|     18118|0.8085866024010354|
|         7|                8|         8|               1.0|
|         2|             1243|      1172|0.9428801287208367|
|         0|           123102|     97552|0.7924485386102582|
+----------+-----------------+----------+------------------+

+---+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| v | 0       | 1       | 2       | 3       | 4       | 5       | 6       | 7       | 8       | 9       |
+---+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| 0 | 0.0     | 155.549 | 20.9818 | 348.200 | 381.822 | 3.66834 | 253.506 | 157.620 | 2.46387 | 74.2798 |
| 1 | 155.549 | 0.0     | 135.188 | 367.406 | 411.479 | 155.356 | 296.242 | 140.818 | 154.298 | 92.2888 |
| 2 | 20.9818 | 135.188 | 0.0     | 346.317 | 382.383 | 21.0123 | 254.229 | 144.539 | 19.6770 | 53.9313 |
| 3 | 348.200 | 367.406 | 346.317 | 0.0     | 516.550 | 348.215 | 421.893 | 358.742 | 347.994 | 346.407 |
| 4 | 381.822 | 411.479 | 382.383 | 516.550 | 0.0     | 381.839 | 453.234 | 413.052 | 381.823 | 388.313 |
| 5 | 3.66834 | 155.356 | 21.0123 | 348.215 | 381.839 | 0.0     | 253.531 | 157.468 | 4.38304 | 74.1320 |
| 6 | 253.506 | 296.242 | 254.229 | 421.893 | 453.234 | 253.531 | 0.0     | 298.229 | 253.508 | 263.663 |
| 7 | 157.620 | 140.818 | 144.539 | 358.742 | 413.052 | 157.468 | 298.229 | 0.0     | 156.724 | 116.546 |
| 8 | 2.46387 | 154.298 | 19.6770 | 347.994 | 381.823 | 4.38304 | 253.508 | 156.724 | 0.0     | 72.9385 |
| 9 | 74.2798 | 92.2888 | 53.9313 | 346.407 | 388.313 | 74.1320 | 263.663 | 116.546 | 72.9385 | 0.0     |
+---+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+

In [48]:
#take out cluster two 



clusterTwo = (joinedDf
              .filter((F.col("prediction") == 6) | 
                      (F.col("prediction") == 1) | 
                      (F.col("prediction") == 4) | 
                      (F.col("prediction") == 3) | 
                      (F.col("prediction") == 7) |
                      (F.col("prediction") == 9))
              .drop(F.col("center"))
              .orderBy(F.col("prediction").desc(),F.col("distance").desc())
             )
clusterTwo.show(300,truncate=True)


+--------------------+---------+-----+--------------------+----------+--------------------+------------------+
|                navn|cvrNummer|label|              status|prediction|        contribution|          distance|
+--------------------+---------+-----+--------------------+----------+--------------------+------------------+
| AAK DENMARK HOLDING| 45954919|    1|            [NORMAL]|         9|[175.272675248476...| 262.0141663914305|
|        COOP TRADING| 20406194|    1|            [NORMAL]|         9|[144.340768947889...|107.36946714293636|
|       GROUP 4 FALCK| 24792110|    1|[OPLØST EFTER SPA...|         9|[144.340768947889...|104.31912237790463|
|  TDC TOTALLØSNINGER| 25767535|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...|101.49116950212107|
|               NETTO| 24603717|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...| 97.54914169205776|
|      BIOMAR HOLDING| 41951818|    1|[OPLØST EFTER FUS...|         9|[179.350143583126...| 97.18591338425341|
|      HKSCAN DENMARK| 25177509|    1|            [NORMAL]|         9|[3.11726920112986...| 84.38935360748032|
|DANFOSS POWER SOL...| 25814363|    1|            [NORMAL]|         9|[291.604591088187...| 82.73623915904814|
|   CARLSBERG DANMARK| 25508386|    1|            [NORMAL]|         9|[291.604591088187...| 82.52372119111422|
|DELOITTE STATSAUT...| 24213714|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...| 82.29873096368908|
|             BG BANK| 20420987|    1|[OPLØST EFTER FUS...|         9|[109.895487013682...| 81.44370192873463|
|    ERICSSON DANMARK| 62580828|    1|            [NORMAL]|         9|[55.0702596170279...| 81.33347334569542|
|          MHOLDING 5| 30345363|    1|            [NORMAL]|         9|[291.604591088187...|  80.6904741504929|
|  FYENS DISTRIBUTION| 10110130|    1|[OPLØST EFTER FRI...|         9|[109.895487013682...| 79.65617774095452|
|JYLLANDS-POSTEN H...| 35947817|    1|            [NORMAL]|         9|[182.645875058378...| 77.97832818011601|
|              TVILUM| 19562786|    1|            [NORMAL]|         9|[3.11726920112986...| 77.40179497698674|
|       LM WIND POWER| 76490511|    1|            [NORMAL]|         9|[3.11726920112986...| 76.62945393377119|
|ALM. BRAND FORSIK...| 10526949|    1|            [NORMAL]|         9|[291.604591088187...| 76.06125997198127|
|SELSKABET AF 14. ...| 18537176|    0|[OPLØST EFTER KON...|         9|[291.604591088187...| 74.05361074912342|
|      STATOIL DETAIL| 55661111|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...| 73.68444343361912|
|            MANPOWER| 24903818|    1|[OPLØST EFTER FUS...|         9|[109.895487013682...| 73.15983776615488|
|        ABB ELECTRIC| 30445414|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...| 71.09371531484554|
|  HØJGAARD & SCHULTZ| 17173928|    1|[OPLØST EFTER SPA...|         9|[291.604591088187...| 71.09007229331912|
|   JP/POLITIKENS HUS| 26933676|    1|            [NORMAL]|         9|[291.604591088187...| 70.59290423852892|
|DONG ENERGY POWER...| 25460715|    1|[OPLØST EFTER FUS...|         9|[167.258391943894...| 70.56906378222628|
|BANG & OLUFSEN AU...| 14344403|    1|[OPLØST EFTER SPA...|         9|[183.474496372681...| 70.53522719684321|
|BANG & OLUFSEN OP...| 26035406|    1|            [NORMAL]|         9|[3.11726920112986...|  70.2281950037233|
|        SHELL CENTER| 43032917|    1|[OPLØST EFTER FRI...|         9|[167.258391943894...| 70.15574923901436|
|    ROYAL COPENHAGEN| 62671114|    1|[OPLØST EFTER SPA...|         9|[291.604591088187...| 68.48921998751348|
|ALM. BRAND AF 179...| 21257613|    2|   [SLETTET, NORMAL]|         9|[183.474496372681...| 67.90352709884773|
|PRICEWATERHOUSECO...| 16994294|    1|[OPLØST EFTER SPA...|         9|[3.11726920112986...| 67.40859348420179|
| FAZER FOOD SERVICES| 44139812|    1|            [NORMAL]|         9|[3.11726920112986...| 67.24736358964222|
|               FØTEX| 89492416|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...| 67.21549561287377|
|        TDC SERVICES| 25767586|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...| 67.14738954589784|
|DONG ENERGY OIL &...| 27446485|    1|            [NORMAL]|         9|[291.604591088187...| 66.99415011796152|
|SAS GROUND SERVIC...| 27723454|    1|[OPLØST EFTER FUS...|         9|[182.645875058378...| 66.63913378380326|
|BILKA LAVPRISVAREHUS| 89492513|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...| 66.61151318907977|
|VESTAS MANUFACTURING| 27988210|    1|            [NORMAL]|         9|[291.604591088187...| 66.31687000098233|
|SAS SCANDINAVIAN ...| 25598113|    1|[OPLØST EFTER FUS...|         9|[182.645875058378...| 66.19147021915869|
|              EUREST| 14108718|    1|[OPLØST EFTER FUS...|         9|[181.819129122270...| 65.76110546935189|
|    CAVERION DANMARK| 10112354|    1|            [NORMAL]|         9|[291.604591088187...| 64.81290060369093|
|             SIEMENS| 16993085|    1|            [NORMAL]|         9|[291.604591088187...| 64.78674389549032|
|         DONG ENERGY| 36213728|    1|            [NORMAL]|         9|[175.272675248476...| 63.76643739830597|
|     VESTAS NACELLES| 28118589|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...| 63.34014023874098|
|DET DANSKE STÅLVA...| 70099314|    0|[OPLØST EFTER KON...|         9|[182.645875058378...|63.048431474817164|
|        BUSDAN 32. 1| 12885075|    1|            [NORMAL]|         9|[182.645875058378...| 62.19312252195294|
|          NECKELMANN| 44997215|    1|[OPLØST EFTER FRI...|         9|[175.272675248476...|60.225566351220586|
|G4S SECURITY SERV...| 26891280|    1|            [NORMAL]|         9|[291.604591088187...|59.962188510282054|
|           TDC MOBIL| 25837061|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...| 59.91884782379156|
|PFA PENSION, FORS...| 13594376|    1|            [NORMAL]|         9|[291.604591088187...|59.095712956006196|
|                 ABB| 31371716|    1|            [NORMAL]|         9|[3.11726920112986...| 58.83624141580985|
|       NYKREDIT BANK| 10519608|    1|            [NORMAL]|         9|[3.11726920112986...|58.513083984469176|
|             SIEMENS| 11660118|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...| 57.73212389691171|
|NORDJYSKE DISTRIB...| 19189384|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...| 57.70446133660485|
|      LEMVIGH-MULLER| 56973311|    1|            [NORMAL]|         9|[291.604591088187...| 57.54274638851076|
|            SYNOPTIK| 62756217|    1|            [NORMAL]|         9|[3.11726920112986...| 57.25962361168245|
|THOMAS COOK AIRLI...| 13896909|    1|            [NORMAL]|         9|[3.11726920112986...| 57.19120256874193|
|DS SMITH PACKAGIN...| 21153702|    1|            [NORMAL]|         9|[3.11726920112986...|56.737033596742926|
|AVIATOR AIRPORT S...| 62714018|    1|            [NORMAL]|         9|[3.11726920112986...|56.541168462107024|
|            ASC 2001| 25852613|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...|56.466533012072794|
|   TOPNORDIC DANMARK| 71130711|    1|[OPLØST EFTER FUS...|         9|[167.258391943894...| 56.30704780621544|
|SCA PACKAGING DEN...| 87408310|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...| 56.08613203403762|
|              ORANGE| 19979733|    1|[OPLØST EFTER ERK...|         9|[181.819129122270...| 55.26781329999641|
|        BEUMER GROUP| 12655681|    1|            [NORMAL]|         9|[3.11726920112986...| 55.21938723242756|
|            DSV ROAD| 26366224|    1|            [NORMAL]|         9|[291.604591088187...| 54.28305091173123|
|       ROYAL UNIBREW| 41956712|    1|            [NORMAL]|         9|[3.11726920112986...| 53.91214735911058|
|GLOBAL SERVICES C...| 31988713|    1|[OPLØST EFTER FUS...|         9|[291.604591088187...|  53.8900637602999|
| C&D FOODS (DENMARK)| 78804912|    1|            [NORMAL]|         9|[55.0702596170279...| 53.65573886304535|
|              MOMENT| 25328949|    1|            [NORMAL]|         9|[55.0702596170279...| 53.25368236255008|
|               ARKIL| 15070544|    1|            [NORMAL]|         9|[291.604591088187...|53.232331014217614|
|             TOP-TOY| 10839238|    1|            [NORMAL]|         9|[3.11726920112986...|53.159116442193685|
|             NISSENS| 40212116|    1|            [NORMAL]|         9|[55.0702596170279...| 52.87320621063871|
|         TV2/DANMARK| 10413494|    1|            [NORMAL]|         9|[291.604591088187...| 52.60080788440677|
|  SIEMENS WIND POWER| 76486212|    1|            [NORMAL]|         9|[291.604591088187...| 52.09156402630144|
|             TELENOR| 19433692|    1|            [NORMAL]|         9|[291.604591088187...|51.946896871113026|
|DANSK SUPERMARKED...| 89492912|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...| 51.82025662848772|
|            BODILSEN| 41368918|    0|[OPLØST EFTER KON...|         9|[109.895487013682...|51.518899382715176|
|AMCOR FLEXIBLES D...| 21409839|    1|            [NORMAL]|         9|[55.0702596170279...|51.442055114939485|
|  F. SALLING HOLDING| 41940115|    1|            [NORMAL]|         9|[3.11726920112986...| 51.23576419946869|
|LSG SKY CHEFS DAN...| 21146242|    1|            [NORMAL]|         9|[144.340768947889...| 51.21036267358236|
|               DANPO| 31241316|    1|            [NORMAL]|         9|[3.11726920112986...| 50.67521159776472|
|              UNICON| 16064939|    1|            [NORMAL]|         9|[55.0702596170279...|50.670444066843295|
|         MOLS-LINIEN| 17881248|    1|            [NORMAL]|         9|[55.0702596170279...|50.574796379831696|
|    RATIONEL VINDUER| 40371818|    1|            [NORMAL]|         9|[144.340768947889...| 50.53474318816158|
|               TERMA| 41881828|    1|            [NORMAL]|         9|[3.11726920112986...|50.460250863886195|
|BRUGER - HJÆLPER ...| 25477154|    1|            [NORMAL]|         9|[291.604591088187...| 50.17308131041875|
|             FLUGGER| 32788718|    1|            [NORMAL]|         9|[55.0702596170279...| 49.90807379803703|
|       ARCUS DENMARK| 21409677|    1|            [NORMAL]|         9|[144.340768947889...| 49.82941168587175|
|       E. PIHL & SØN| 33037112|    0|[UNDER KONKURS, N...|         9|[3.11726920112986...| 49.81555693072686|
|             LINDPRO| 82675418|    1|            [NORMAL]|         9|[3.11726920112986...|49.574192737248346|
|       NOBIA DENMARK| 89645417|    1|            [NORMAL]|         9|[3.11726920112986...|49.127184038963684|
|           ACTIVCARE| 19344444|    1|            [NORMAL]|         9|[3.11726920112986...| 49.04582553828058|
|         PFA HOLDING| 22438018|    1|            [NORMAL]|         9|[181.819129122270...| 48.48902994750925|
|          BESTSELLER| 88216512|    1|            [NORMAL]|         9|[291.604591088187...|48.406814110320404|
|     ISO SUPERMARKED| 25675886|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...| 47.96576936561682|
|AALBORG PORTLAND ...| 14244441|    1|            [NORMAL]|         9|[55.0702596170279...| 47.87661692337444|
|            ECCO SKO| 45349918|    1|            [NORMAL]|         9|[3.11726920112986...|  47.8138453266597|
|SCHNEIDER ELECTRI...| 70698714|    1|            [NORMAL]|         9|[55.0702596170279...| 47.79969867252878|
|CSC AIRLINE SOLUT...| 14278176|    1|            [NORMAL]|         9|[55.0702596170279...| 47.76457105568166|
|    KEMP & LAURITZEN| 57471719|    1|            [NORMAL]|         9|[291.604591088187...| 47.54661491528758|
|GATE GOURMET DENMARK| 23370212|    1|            [NORMAL]|         9|[3.11726920112986...| 47.45329911600851|
|         KELSENBISCA| 21295310|    1|[OPLØST EFTER SPA...|         9|[3.11726920112986...|47.145332196738025|
|SELSKABET AF 1. N...| 13638632|    0|[OPLØST EFTER KON...|         9|[55.0702596170279...| 46.68885002778056|
|           JEM & FIX| 10360641|    1|            [NORMAL]|         9|[3.11726920112986...| 46.66297512467227|
|            RANDSTAD| 10724236|    1|            [NORMAL]|         9|[3.11726920112986...|46.626306726913086|
|   BRØDRENE HARTMANN| 63049611|    1|            [NORMAL]|         9|[55.0702596170279...|46.597898312290354|
|             SPÆNCOM| 26271010|    1|            [NORMAL]|         9|[55.0702596170279...| 46.29076307733621|
|ØSTBIRK BYGNINGSI...| 45557219|    1|            [NORMAL]|         9|[55.0702596170279...|46.279757999019495|
|          CCI EUROPE| 41915811|    1|            [NORMAL]|         9|[55.0702596170279...| 46.18133831639826|
|         DANSK SHELL| 10373816|    1|            [NORMAL]|         9|[55.0702596170279...|46.089199269063464|
|     HOUSE OF PRINCE| 13801134|    1|            [NORMAL]|         9|[109.895487013682...| 45.93912482236453|
|  ALFA LAVAL AALBORG| 17830635|    1|            [NORMAL]|         9|[55.0702596170279...|45.801883450660476|
|FTZ AUTODELE & VÆ...| 73648718|    1|            [NORMAL]|         9|[291.604591088187...|  45.6341572991747|
|        ALLIANCEPLUS| 12643047|    1|            [NORMAL]|         9|[291.604591088187...| 45.46133964407545|
| HARDI INTERNATIONAL| 55274517|    1|            [NORMAL]|         9|[55.0702596170279...| 45.36958779255953|
|IBM SERVICE DELIV...| 13446083|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...| 44.95783632566664|
|VESTAS CONTROL SY...| 27988202|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...|44.602504443158224|
|        KIWI DANMARK| 87470113|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...|44.499600461812726|
|      FIONIA HOLDING| 14669000|    0|[OPLØST EFTER KON...|         9|[182.645875058378...| 44.39233133488803|
|                NNIT| 21093106|    1|            [NORMAL]|         9|[291.604591088187...| 44.14630693945354|
|              LINDAB| 33124228|    1|            [NORMAL]|         9|[55.0702596170279...| 43.95291897365383|
|      SEMCO MARITIME| 25490762|    1|            [NORMAL]|         9|[291.604591088187...|43.827655788823954|
|     JENSEN'S BØFHUS| 13212538|    1|            [NORMAL]|         9|[3.11726920112986...|43.519346392309686|
|    DAGROFA LOGISTIK| 78895918|    1|            [NORMAL]|         9|[3.11726920112986...| 43.10948790350756|
|  SJÆLLANDSKE MEDIER| 68435617|    1|            [NORMAL]|         9|[55.0702596170279...|42.924512225494524|
|           SAXO BANK| 15731249|    1|            [NORMAL]|         9|[3.11726920112986...| 42.58629824811183|
|LANTMANNEN SCHULSTAD| 10245613|    1|            [NORMAL]|         9|[55.0702596170279...|  42.3635469002308|
|         HEDEDANMARK| 27623549|    1|            [NORMAL]|         9|[3.11726920112986...|41.985881619235094|
|NOVO NORDISK INVE...| 21093084|    1|            [NORMAL]|         9|[182.645875058378...| 41.63199449250626|
|    DREISLER STORKØB| 26089115|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...|41.454174013025266|
|  VALD. BIRN HOLDING| 44651017|    1|            [NORMAL]|         9|[55.0702596170279...| 41.22781869080159|
|            ROCKWOOL| 42391719|    1|            [NORMAL]|         9|[3.11726920112986...|41.187862773302214|
|           CHEMINOVA| 12760043|    1|            [NORMAL]|         9|[3.11726920112986...| 41.10907819901243|
|   STERLING AIRLINES| 18235404|    0|[UNDER KONKURS, N...|         9|[144.340768947889...|41.073238609344244|
|           BRFKREDIT| 13409838|    1|            [NORMAL]|         9|[3.11726920112986...| 41.04028272131484|
|         CHR. HANSEN| 12516479|    1|            [NORMAL]|         9|[3.11726920112986...| 41.02161304384598|
|    BILLUND LUFTHAVN| 23343118|    1|            [NORMAL]|         9|[3.11726920112986...|40.883591873360814|
|     BAUHAUS DANMARK| 19555305|    1|            [NORMAL]|         9|[3.11726920112986...| 40.87766201424604|
|JOHNSON CONTROLS ...| 19056171|    1|            [NORMAL]|         9|[3.11726920112986...| 40.86680283998658|
|  KEOLIS BUS DANMARK| 12690800|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...|  40.6948381753638|
|AFVIKLINGSSELSKAB...| 54612311|    0|[OPLØST EFTER KON...|         9|[55.0702596170279...| 39.60338691086002|
|LØVBJERG SUPERMARKED| 17888749|    1|            [NORMAL]|         9|[55.0702596170279...|39.267920524158534|
|               LINAK| 66365328|    1|            [NORMAL]|         9|[3.11726920112986...| 38.52129111491292|
| MARTIN PROFESSIONAL| 11805744|    1|            [NORMAL]|         9|[55.0702596170279...| 38.18819638840865|
|         CGI DANMARK| 63890812|    1|            [NORMAL]|         9|[3.11726920112986...| 37.95771844841334|
|NORDEA LIV & PENS...| 24260577|    1|            [NORMAL]|         9|[3.11726920112986...| 37.62567120522292|
|          ELGIGANTEN| 17237977|    1|            [NORMAL]|         9|[291.604591088187...|   36.792274821965|
|LANTMANNEN UNIBAK...| 37249211|    1|            [NORMAL]|         9|[167.258391943894...| 36.72633119695868|
|BRUEL & KJÆR SOUN...| 23958414|    1|            [NORMAL]|         9|[55.0702596170279...| 36.41998149090111|
|             VELSERV| 15512172|    1|            [NORMAL]|         9|[291.604591088187...| 36.29781341512472|
|ERNST & YOUNG DAN...| 73317428|    1|            [NORMAL]|         9|[55.0702596170279...| 36.19989681932292|
|NORDISK FILM BIOG...| 28344910|    1|            [NORMAL]|         9|[55.0702596170279...|35.560663073287316|
|       AMAGERBANKEN.| 15773928|    0|[UNDER KONKURS, N...|         9|[144.340768947889...|35.354119057028825|
|GÅSDAL BYGNINGSIN...| 46911512|    1|            [NORMAL]|         9|[55.0702596170279...| 35.14788584154752|
|        NETS HOLDING| 27225993|    1|            [NORMAL]|         9|[3.11726920112986...| 35.13812807251579|
|  ALFA LAVAL KOLDING| 30938011|    1|            [NORMAL]|         9|[3.11726920112986...|34.942374196073814|
|         IDA SERVICE| 16227501|    1|[OPLØST EFTER FUS...|         9|[167.258391943894...| 34.81562750557545|
|   DFDS ROLAND MUNCH| 56423818|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...| 34.79832905814489|
|NORDISK SOLAR COM...| 37784710|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...| 34.70585268174967|
|       HARALD NYBORG| 37783315|    1|            [NORMAL]|         9|[3.11726920112986...|34.661260638766606|
|                  EG| 84667811|    1|            [NORMAL]|         9|[3.11726920112986...|34.336345145293656|
|HØJBJERG MASKINFA...| 43969315|    1|            [NORMAL]|         9|[109.895487013682...|34.056851096768185|
|              VELFAC| 24347214|    1|            [NORMAL]|         9|[109.895487013682...|33.669788972203136|
|SELSKABET DGS AF ...| 12497040|    0|[UNDER KONKURS, N...|         9|[167.258391943894...|32.451556516357854|
|      BANG & OLUFSEN| 41257911|    1|            [NORMAL]|         9|[3.11726920112986...| 31.85002216600261|
|              TIVOLI| 10404916|    1|            [NORMAL]|         9|[3.11726920112986...| 31.76318421410322|
|LOUIS POULSEN EL-...| 61023518|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...|31.219420920353226|
|   FORSTÆDERNES BANK| 52969816|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...|31.200915007263514|
|KOHBERG BAKERY GROUP| 11326943|    1|            [NORMAL]|         9|[55.0702596170279...|30.961960559720623|
|SELECT SERVICE PA...| 71750515|    1|            [NORMAL]|         9|[55.0702596170279...|30.705090164282126|
|        CRH CONCRETE| 21474878|    1|            [NORMAL]|         9|[3.11726920112986...|30.617555597439363|
|       COLAS DANMARK| 10246415|    1|            [NORMAL]|         9|[55.0702596170279...| 30.23577312826998|
|        BETONELEMENT| 15523409|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...|29.760609429329577|
|       TAKEDA PHARMA| 16406899|    1|            [NORMAL]|         9|[3.11726920112986...|29.692253287228063|
|MICROSOFT DEVELOP...| 76247218|    1|            [NORMAL]|         9|[55.0702596170279...|28.927634998598656|
|              YOUSEE| 25767578|    1|[OPLØST EFTER FUS...|         9|[3.11726920112986...| 28.50372673106097|
|             ALECTIA| 22278916|    1|            [NORMAL]|         9|[3.11726920112986...| 27.56071759767465|
|SAPA PRECISION TU...| 37488410|    1|            [NORMAL]|         9|[55.0702596170279...|  24.2708271527488|
|            SPARBANK| 12523580|    1|[OPLØST EFTER FUS...|         9|[55.0702596170279...|24.127592313142838|
|    JELD-WEN DANMARK| 15134739|    1|            [NORMAL]|         9|[3.11726920112986...| 23.55429970877803|
|     THRANE & THRANE| 65724618|    1|            [NORMAL]|         9|[3.11726920112986...| 22.77711988344509|
|      NNE PHARMAPLAN| 13246009|    1|            [NORMAL]|         9|[3.11726920112986...| 22.63584895303109|
|        EJNER HESSEL| 58811211|    1|            [NORMAL]|         9|[3.11726920112986...|22.315813781161168|
| WILLIAM COOK EUROPE| 83742313|    1|            [NORMAL]|         9|[3.11726920112986...|21.342519580362936|
|BRØDRENE A. & O. ...| 58210617|    1|            [NORMAL]|         9|[3.11726920112986...|20.424586947086905|
|       FRODE LAURSEN| 29482713|    1|            [NORMAL]|         9|[3.11726920112986...|20.149508116946226|
|               NIRAS| 37295728|    1|            [NORMAL]|         9|[3.11726920112986...|19.205873932707853|
| NORDEA BANK DANMARK| 13522197|    1|            [NORMAL]|         7|[3.66284803952036...|184.23268676513774|
|   DANSK SUPERMARKED| 35954716|    1|            [NORMAL]|         7|[3.66284803952036...|118.27650200664819|
|        POST DANMARK| 26663903|    1|            [NORMAL]|         7|[3.66284803952036...|100.78459896835855|
|                DEAS| 20283416|    1|            [NORMAL]|         7|[179.479553936499...| 84.37634120420579|
|               FAKTA| 11517498|    1|            [NORMAL]|         7|[3.66284803952036...| 69.76079566519337|
|COMPASS GROUP DAN...| 81093628|    1|            [NORMAL]|         7|[3.66284803952036...| 63.05737719678359|
|        COOP DANMARK| 26259495|    1|            [NORMAL]|         7|[3.66284803952036...| 55.90772257910196|
|       FALCK DANMARK| 16271241|    1|            [NORMAL]|         7|[3.66284803952036...|53.410175458594566|
|           PBU BOLIG| 12124139|    1|            [NORMAL]|         6|[0.01898820423687...| 67.89547514635058|
|    DAN AGRO HOLDING| 31374618|    1|            [NORMAL]|         6|[0.01898820423687...| 67.89547514635055|
|        LYNX HOLDING| 29447683|    1|[OPLØST EFTER FRI...|         4|[0.0,0.0,0.0,0.0,...|               0.0|
|NORDEA COMPANIES ...| 13824746|    1|[OPLØST EFTER SPA...|         3|[0.0,0.0,0.0,0.0,...|               0.0|
|         DANSKE BANK| 61126228|    1|            [NORMAL]|         1|[65.2169186410488...|149.89104973907632|
|                 TDC| 14773908|    1|            [NORMAL]|         1|[65.2169186410488...|148.35204912806756|
|   POLITIKEN HOLDING| 57001615|    1|            [NORMAL]|         1|[506.942724369376...|109.32737016885801|
|              KOMBIT| 19435075|    1|            [NORMAL]|         1|[441.627066140620...|106.92441346251186|
|     TRYG FORSIKRING| 24260666|    1|            [NORMAL]|         1|[65.2169186410488...|106.30098830814357|
|MAERSK AVIATION H...| 22765310|    1|            [NORMAL]|         1|[505.564742675753...| 90.46409951710699|
|                DFDS| 14194711|    1|            [NORMAL]|         1|[65.2169186410488...| 90.28886515217702|
|DONG ENERGY WIND ...| 18936674|    1|            [NORMAL]|         1|[52.3474755118174...| 88.25795643292028|
|          MAN DIESEL| 39661314|    1|[OPLØST EFTER FUS...|         1|[65.2169186410488...| 88.12676052611673|
|           NOVOZYMES| 10007127|    1|            [NORMAL]|         1|[65.2169186410488...| 85.73797282737496|
|     BRAVIDA DANMARK| 14769005|    1|            [NORMAL]|         1|[65.2169186410488...| 84.74871266298852|
|         KORT & GODT| 10882230|    1|            [NORMAL]|         1|[52.3474755118174...| 84.53242423226563|
|           DSB S-TOG| 21827738|    1|[OPLØST EFTER ERK...|         1|[52.3474755118174...| 82.48233990634657|
|     SKANSKA DANMARK| 24257150|    1|            [NORMAL]|         1|[501.442049864061...| 79.14233870328916|
|F. JUNCKERS INDUS...| 66920216|    1|            [NORMAL]|         1|[269.671193182411...| 74.64299164378767|
|PER AARSLEFF HOLDING| 24257797|    1|            [NORMAL]|         1|[65.2169186410488...| 73.55861677701519|
|          Q8 DANMARK| 61082913|    1|            [NORMAL]|         1|[52.3474755118174...| 71.80438743092428|
|           VESTFROST| 37101117|    1|            [NORMAL]|         1|[269.671193182411...|  70.9785976598655|
|    CIRCLE K DANMARK| 28142412|    1|            [NORMAL]|         1|[65.2169186410488...| 70.23321416618315|
|DONG ENERGY SALES...| 20214414|    1|            [NORMAL]|         1|[65.2169186410488...| 69.47694871498786|
|       NOKIA DANMARK| 10104580|    1|[OPLØST EFTER FUS...|         1|[52.3474755118174...| 66.84580428651839|
|         NORTH MEDIA| 66590119|    1|            [NORMAL]|         1|[481.081761862095...| 66.78705170031235|
|                IRMA| 43264710|    1|[OPLØST EFTER FUS...|         1|[52.3474755118174...| 65.03607118838683|
|          TOPDANMARK| 78040017|    1|            [NORMAL]|         1|[501.442049864061...| 63.78471446796613|
|         ELITE MILJØ| 21018392|    1|            [NORMAL]|         1|[65.2169186410488...| 63.11796695551122|
|  REALKREDIT DANMARK| 13399174|    1|            [NORMAL]|         1|[269.671193182411...| 62.72532566898807|
|            HOFFMANN| 63030228|    1|            [NORMAL]|         1|[269.671193182411...| 61.60345262820565|
|SAINT-GOBAIN DIST...| 81822514|    1|            [NORMAL]|         1|[65.2169186410488...| 59.28996354982999|
|DANFOSS POWER ELE...| 19883876|    1|            [NORMAL]|         1|[65.2169186410488...|58.530380017570835|
|        TOMS GRUPPEN| 56759328|    1|            [NORMAL]|         1|[52.3474755118174...|58.453777341347546|
|         SAS DANMARK| 56994912|    1|            [NORMAL]|         1|[65.2169186410488...| 58.03392565080907|
|            FLSMIDTH| 15028882|    1|            [NORMAL]|         1|[65.2169186410488...| 57.12839213036113|
|ISS FACILITY SERV...| 14406042|    1|            [NORMAL]|         1|[65.2169186410488...|  56.6814681320167|
|          UNOMEDICAL| 64153315|    1|            [NORMAL]|         1|[269.671193182411...| 54.63493362497381|
|              OTICON| 42334219|    1|            [NORMAL]|         1|[65.2169186410488...|54.375857743338166|
|        NOVO NORDISK| 24256790|    1|            [NORMAL]|         1|[65.2169186410488...|54.096620959147366|
|                JYSK| 13590400|    1|            [NORMAL]|         1|[65.2169186410488...| 52.94741761976489|
|           TEMP TEAM| 85232312|    1|            [NORMAL]|         1|[269.671193182411...|  51.5975778807893|
|SPX FLOW TECHNOLO...| 24730018|    1|            [NORMAL]|         1|[52.3474755118174...| 51.14676410336739|
|ARBEJDERNES LANDS...| 31467012|    1|            [NORMAL]|         1|[65.2169186410488...| 50.35762561713206|
|H & M HENNES & MA...| 33345828|    1|            [NORMAL]|         1|[65.2169186410488...| 49.86168016496017|
| VESTAS WIND SYSTEMS| 10403782|    1|            [NORMAL]|         1|[65.2169186410488...| 49.83298080467588|
|       SCANDIC HOTEL| 12596774|    1|            [NORMAL]|         1|[52.3474755118174...|48.812073446485435|
|    DT GROUP DANMARK| 55828415|    1|            [NORMAL]|         1|[65.2169186410488...| 48.73013603561907|
|ODENSE STAALSKIBS...| 45739910|    1|            [NORMAL]|         1|[481.081761862095...|48.412994002240225|
|             F GROUP| 14247238|    2|[UNDER REKONSTRUK...|         1|[52.3474755118174...| 45.87823677563077|
|    GLUD & MARSTRAND| 67287118|    1|            [NORMAL]|         1|[52.3474755118174...|  43.9080245567322|
|          JYSKE BANK| 17616617|    1|            [NORMAL]|         1|[65.2169186410488...| 42.51567051812012|
|TH. WESSEL & VETT...| 58191213|    1|            [NORMAL]|         1|[52.3474755118174...|40.191077130037066|
|              ADECCO| 12932375|    1|            [NORMAL]|         1|[52.3474755118174...| 39.73647185073192|
|      SPAR NORD BANK| 13737584|    1|            [NORMAL]|         1|[65.2169186410488...|39.550574084668185|
|  TULIP FOOD COMPANY| 14003606|    1|            [NORMAL]|         1|[65.2169186410488...| 38.84554873081279|
|          LEO PHARMA| 56759514|    1|            [NORMAL]|         1|[65.2169186410488...| 38.53127483933437|
|         H. LUNDBECK| 56759913|    1|            [NORMAL]|         1|[65.2169186410488...| 38.45109726160443|
|KØBENHAVNS LUFTHAVNE| 14707204|    1|            [NORMAL]|         1|[65.2169186410488...| 38.43371083410574|
|       HALDOR TOPSØE| 41853816|    1|            [NORMAL]|         1|[65.2169186410488...|38.318958590765206|
|       SWECO DANMARK| 48233511|    1|            [NORMAL]|         1|[65.2169186410488...| 38.17187394964192|
|           COLOPLAST| 69749917|    1|            [NORMAL]|         1|[65.2169186410488...|37.685508924504454|
|         LEGO SYSTEM| 47458714|    1|            [NORMAL]|         1|[65.2169186410488...|  37.6662856741215|
| NYKREDIT REALKREDIT| 12719280|    1|            [NORMAL]|         1|[65.2169186410488...| 37.50524887627694|
|             DANFOSS| 20165715|    1|            [NORMAL]|         1|[65.2169186410488...|37.257445580515316|
|         MT HØJGAARD| 12562233|    1|            [NORMAL]|         1|[65.2169186410488...| 37.22040058417957|
|         CSC DANMARK| 15231599|    1|            [NORMAL]|         1|[65.2169186410488...|37.198265990759666|
|DUPONT NUTRITION ...| 11350356|    1|            [NORMAL]|         1|[65.2169186410488...| 37.14258911446865|
|            GRUNDFOS| 37499919|    1|            [NORMAL]|         1|[65.2169186410488...| 36.94452937664717|
|REDERIET A. P. MØ...| 37048119|    1|            [NORMAL]|         1|[65.2169186410488...| 36.90551163100469|
|                COWI| 44623528|    1|            [NORMAL]|         1|[65.2169186410488...| 36.87434414876918|
|      ARRIVA DANMARK| 18429101|    1|            [NORMAL]|         1|[65.2169186410488...|36.295585168915736|
|            SANISTÅL| 42997811|    1|            [NORMAL]|         1|[65.2169186410488...| 34.29908328596764|
+--------------------+---------+-----+--------------------+----------+--------------------+------------------+


In [34]:
def printTotalAndAvgFeatContribution(df,cluster=0,toPrint=False):
    avgFeatureContributionRDD = (df
                                 .filter(F.col("prediction") == cluster)
                                 .select("cvrNummer","navn","contribution")
                                 .rdd)
    
    length = len(avgFeatureContributionRDD.first()["contribution"])

    totalContribution = avgFeatureContributionRDD.map(lambda x: x[2]).reduce(add)
    avgContribution = totalContribution/avgFeatureContributionRDD.count()
    columns = [totalContribution.toArray(),avgContribution.toArray(),]
    contributions = createPrettyTable(columns,featCols,["total","avg"])
    if toPrint:
        print(contributions)
    return avgContribution

In [35]:
cluster6 = printTotalAndAvgFeatContribution(joinedDf,6)

In [36]:
cluster1 = printTotalAndAvgFeatContribution(joinedDf,1)

In [37]:
cluster8 = printTotalAndAvgFeatContribution(joinedDf,8)

In [38]:
cluster0 = printTotalAndAvgFeatContribution(joinedDf,0)

In [39]:
cluster2 = printTotalAndAvgFeatContribution(joinedDf,2)
cluster3 = printTotalAndAvgFeatContribution(joinedDf,3)
cluster4 = printTotalAndAvgFeatContribution(joinedDf,4)
cluster5 = printTotalAndAvgFeatContribution(joinedDf,5)
cluster7 = printTotalAndAvgFeatContribution(joinedDf,7)
cluster9 = printTotalAndAvgFeatContribution(joinedDf,9)

In [40]:
clusters = np.array([cluster0,cluster1,cluster2,cluster3,cluster4,cluster5,cluster6,cluster7,cluster8,cluster9])
transposedCluster = np.log1p(clusters.transpose())
N = 10

import colorsys
HSV_tuples = [(x*1.0/len(transposedCluster), 0.5, 0.5) for x in range(len(transposedCluster))]
RGB_tuples = list(map(lambda x: colorsys.hsv_to_rgb(*x), HSV_tuples))

ind = np.arange(N)    # the x locations for the groups
width = 0.35 
plots = [plt.bar(ind, transposedCluster[1], width, color='#d62728')]  
former = transposedCluster[1]
for i,v in enumerate(transposedCluster[1:]):
    plots.append(plt.bar(ind, v, width, color=RGB_tuples[i],bottom=former))
    former += v
plt.ylabel('Scores')
plt.title('Scores by Cluster and features')
plt.xticks(ind, ('C0', 'C1','C2','C3','C4','C5', 'C6','C7', 'C8','C9'))
plt.legend([p[0] for p in plots], featCols,bbox_to_anchor=(1.05, 1),loc=2,borderaxespad=0.)
plt.show()



In [ ]:
#how does it look in each of the clusters