Khipu Analysis

Loading the Khipu Summary

In this page, we will perform high-level analysis, comparing khipus to each other, as a whole.

As part of the building of the python class object OODB, we now have a summary table of useful metrics of Khipu. A list of sample metrics includes, how many pendant cords, how many S-Twist cords, how many subsidiary cords for each pendant cord, etc..

One interesting field is the document field which is a textual representation of the khipus “features”, such as cord attachments, values, colors, knot types, etc. This textual representation becomes a “proxy” for the Khipu, and allows NLP algorithms to work on the khipu. The long term goal then becomes translating that text document into something meaningful to us.

I will admit it is an odd process. First humans convert thoughts to cloth. Then humans measure the cloth, and convert it back to numbers (the database). Then I come along and convert the numbers back to pixel-cloth and I convert the numbers to text. Which I will convert back to numbers in code, so that I can hopefully convert those numbers back to thoughts. Hmmmmn….

The long prologue of imports and setup has been extracted to a startup file for brevity. The first task is to complete initialization by doing final setup and importing the Python Khipu OODB (Object Oriented Database) and summary files.

# Initialize plotly
plotly.offline.init_notebook_mode(connected = True);

# Khipu Imports
import khipu_kamayuq as kamayuq  # A Khipu Maker is known (in Quechua) as a Khipu Kamayuq
import khipu_qollqa as kq
all_khipus = [aKhipu for aKhipu in kamayuq.fetch_all_khipus().values()]
khipu_summary_df = kq.fetch_khipu_summary()

The KhipuKamayuq class (a Khipu scribe is a KhipuKamayuq) fetches khipus using the data accessed using the KhipuQollqa (a Khipu Warehouse) class. The KhipuKamayuq class can fetch all the khipus stored(pickled) as python object-oriented class objects. Additionally it has a khipu summary dataframe made by the KhipuQollqa. Let’s load the OODB, and the summary dataframe. A short list of summary columns is useful, as well.

Index(['Unnamed: 0', 'khipu_id', 'kfg_name', 'okr_name', 'original_name',
       'legal_filename', 'nickname', 'museum_name', 'museum_num',
       'museum_description', 'region', 'provenance', 'num_cord_clusters',
       'mean_cords_per_cluster', 'max_subsidiary_branches',
       'num_top_cord_double_sum_clusters', 'num_top_cord_sum_clusters',
       'num_banded_clusters', 'num_seriated_clusters',
       'num_decreasing_clusters', 'benford_match', 'hierarchical_cluster_pos',
       'num_canuto_cords', 'num_cords', 'recto_cords', 'verso_cords',
       'r_ratio', 'v_ratio', 'rv_cords', 'num_pendant_cords',
       'num_nonzero_top_pendant_cords', 'num_top_cords',
       'num_subsidiary_cords', 'num_s_cords', 'num_z_cords', 'mean_cord_value',
       'fanout_ratio', 'num_total_cords', 'num_ascher_colors',
       'knot_cluster_count', 'knot_count', 'mean_knots_per_cord',
       'num_s_knots', 'num_z_knots', 'percent_s_knots', 'percent_z_knots',
       'notes', 'pcord_notes'],

Benford’s Law

Beginning programmers are taught to learn how to write “Hello World” as their first programming exercise. For khipu scholars, who are researching khipus statistically, Benford’s Law is their “Hello World.” Benford’s law, simply put, says that the first digits of numbers used to measure human activity tend to have a higher probability that they are 1. For example, 10,000, 1,000, etc. We can use Benford’s law to predict which khipu are of an “accounting nature” - ie. khipu that have sums and accounts of things.

>Benford’s Law by Dan Ma

The first digit (or leading digit) of a number is the leftmost digit (e.g. the first digit of 567 is 5). The first digit of a number can only be 1, 2, 3, 4, 5, 6, 7, 8, and 9 since we do not usually write a number such as 567 as 0567. Some fraudsters may think that the first digits in numbers in financial documents appear with equal frequency (i.e. each digit appears about 11% of the time). In fact, this is not the case. It was discovered by Simon Newcomb in 1881 and rediscovered by physicist Frank Benford in 1938 that the first digits in many data sets occur according to the probability distribution indicated in Figure 1 below:

The above probability distribution is now known as the Benford’s law. It is a powerful and yet relatively simple tool for detecting financial and accounting frauds. For example, according to the Benford’s law, about 30% of numbers in legitimate data have 1 as a first digit. Fraudsters who do not know this will tend to have much fewer ones as first digits in their faked data.

Data for which the Benford’s law is applicable are data that tend to distribute across multiple orders of magnitude. Examples include income data of a large population, census data such as populations of cities and counties. In addition to demographic data and scientific data, the Benford’s law is also applicable to many types of financial data, including income tax data, stock exchange data, corporate disbursement and sales data. The author of I’ve Got Your Number How a Mathematical Phenomenon can help CPAs Uncover Fraud and other Irregularities., Mark Nigrini, also discussed data analysis methods (based on the Benford’s law) that are used in forensic accounting and auditing.

first_digit_probability = [0.301, 0.176, 0.125, 0.097, 0.079, 0.067, 0.058, 0.051, 0.046]
df = pd.DataFrame({'first_digit':list(range(1,10)), 'proportion':first_digit_probability})
fig =, x='first_digit', y='proportion', text='proportion', width=1200, height=300, title="Benford\'s Law").show()

An Explanation of “Cosine Distance”

Using the distribution of “first digits” in a cord (the value of the first knot) we can use a simple cosine distance similarity metric of Benford’s distribution and cord distribution. Cosine distance similarity is a simple concept that is used a lot in this study … If you’re unfamiliar here’s an intuitive explanation.

Suppose we want to see how close two sets of numbers are to each other. For example house A has 3 dogs and 2 cats, house B has 5 dogs and 1 cat and house C has 1 dog and 7 cats. We can represent each of these as a vector:

A = [3,2] B = [5,1] C = [1,7]

Each of these vectors can be “normalized” to a unit vector (a vector of length 1) by dividing by the vector length. Then you can take advantage of math. A vector dot-product of two vectors measures the angle between those two vectors. If we want to know if A is closer to B or C we take A . B and A . C and see which has the smallest angle between them. The dot -product generates the cosine of the angle, which is why this technique is known as the cosine distance metric.

We can do the same for khipus by creating a vector of first digit counts, for a given khipu and then dot producting it with the Benford probabilities above.

f(angle_theta) = first_digit_probability . given_khipu_digit_distribution. If the angle theta is 0, the vectors lie on top of each other (an exact match) so cos(theta) = 1, with no match = 0.

Another approach to understanding which khipus are accounting in nature, and which aren’t is to sum up cord values and see if that sum occurs elsewhere on another cord in the khipu. Dr. Urton, shows an example of this kind, khipu UR028, in his youtube video on accounting khipu. Here I examine how many “sum matches” occur in a khipu (normalized by the number of total cords…). This gives us a secondary statistical check, in addition to Benford’s Law.

Note that the metric calculated below is not exact. Here, only two cord sums are measured. The first decoded khipu, in the 1920s by Leland Locke, had one up-cord sum for four down cord values and would not score well on this metric.

There is another metric similar to the above. Some cord clusters (ie. top cords) are the sum, or approximate sum of the cluster adjacent (ie. on either side) of them. The khipu class tags these “sum_clusters” and allows us another metric.

## Benford Law Similarity
# Note that 0 index's are ignored, by setting first_digit_probability[0] to 0
first_digit_probability = [0.0, 0.301, 0.176, 0.125, 0.097, 0.079, 0.067, 0.058, 0.051, 0.046]
def benford_match(aKhipu):
    digit_count = [0]*10
    for cord in aKhipu.attached_cords():
        first_digit = int(str(cord.decimal_value)[0])
        digit_count[first_digit] += 1
    cosine_similarity = 1 - scipy.spatial.distance.cosine(first_digit_probability, digit_count)
    return cosine_similarity
khipu_benford_match = [benford_match(aKhipu) for aKhipu in all_khipus]
# Cords whose sum occurs as another cord in a khipu
def sum_matches(numbers, verbose = False):
    numbers = list(filter(lambda a: a != 0, numbers))
    num_matches= 0
    max_match = 0
    all_matches = [0]
    for i in range(len(numbers)):
        for j in range(i):
            match_sum = numbers[i] + numbers[j]
            if (match_sum in numbers) and (match_sum != 0):
                if verbose: print(f"[{i}]{numbers[i]}, [{j}]{numbers[j]} sums to [{numbers.index(match_sum)}]{match_sum}")
                num_matches += 1
    return ({'num_matches':num_matches, 'max_match': max(all_matches), 'mean_match': mean(all_matches) })

def khipu_sum_matches(aKhipu):
    cord_values = [aCord.decimal_value for aCord in aKhipu.attached_cords() if aCord.decimal_value > 0]
    return sum_matches(cord_values)

sum_match_dicts = [khipu_sum_matches(aKhipu) for aKhipu in all_khipus]
sum_match_counts = [aDict['num_matches']for aDict in sum_match_dicts]
sum_match_max = [aDict['max_match']for aDict in sum_match_dicts]
sum_match_mean = [aDict['mean_match']for aDict in sum_match_dicts]
benford_df = pd.DataFrame({'khipu_id': [aKhipu.khipu_id for aKhipu in all_khipus],
                           'name': [ for aKhipu in all_khipus],
                           'num_pendants': [aKhipu.num_pendant_cords() for aKhipu in all_khipus],
                           'num_subsidiary_cords': [aKhipu.num_subsidiary_cords() for aKhipu in all_khipus],
                           'num_total_cords': [aKhipu.num_attached_cords()for aKhipu in all_khipus],
                           'fanout_ratio': [aKhipu.num_pendant_cords() / aKhipu.num_attached_cords() for aKhipu in all_khipus],
                           'benford_match': khipu_benford_match,
                           'sum_match_count': sum_match_counts,
                           'sum_match_max': sum_match_max,
                           'sum_match_mean': sum_match_mean})

benford_df['normalized_sum_match_count'] = benford_df['sum_match_count']/benford_df['num_total_cords']
benford_df = benford_df.sort_values(by='benford_match', ascending=False)
benford_df.to_csv(kq.qollqa_data_directory() + "benford.csv") # Save for later when we cross correlate with other statistical tests
khipu_id name num_pendants num_subsidiary_cords num_total_cords fanout_ratio benford_match sum_match_count sum_match_max sum_match_mean normalized_sum_match_count
252 1000269 UR053E 70 35 105 0.666667 0.992311 3811 81 14 36.295238
645 6000088 UR193 99 127 226 0.438053 0.985153 15906 130 54 70.380531
577 6000019 KH0058 80 98 178 0.449438 0.984313 10570 115 22 59.382022
426 1000404 UR151 12 8 20 0.600000 0.978858 64 120 61 3.200000
612 6000054 QU005 57 25 82 0.695122 0.978147 425 1107 112 5.182927
... ... ... ... ... ... ... ... ... ... ... ...
449 1000489 UR179 18 0 18 1.000000 0.000000 0 0 0 0.000000
432 1000417 UR158 24 1 25 0.960000 0.000000 0 0 0 0.000000
14 1000196 AS025 2 0 2 1.000000 0.000000 0 0 0 0.000000
161 1000447 HP028 10 0 10 1.000000 0.000000 0 0 0 0.000000
159 1000445 HP026 17 0 17 1.000000 0.000000 0 0 0 0.000000
hist_data = [khipu_benford_match]
group_labels = ['benford_match']
colors = ['#835AF1']

ff.create_distplot(hist_data, group_labels,  bin_size=.01, 
    .update_layout(title_text='Distribution Plot of Khipu -> Benford Matches', # title of plot
                     xaxis_title_text='Benford Match Value (cosine_similarity)', 
                     yaxis_title_text='Percentage of Khipus', # yaxis label
                     bargap=0.05, # gap between bars of adjacent location coordinates
                     #bargroupgap=0.1 # gap between bars of the same location coordinates

Here’s a clear statistical/graphical narrative as to which khipus are “accounting” in nature (from a little above .6 to 1.0!), somewhat accounting in nature (.4 to .6), and possibly narrative in nature (< 0.4). How many khipus in each category?

is_likely_accounting = benford_df[benford_df.benford_match > .625]
print(f"Khipus with benford_match > .625 = {is_likely_accounting.shape[0]} (is_likely_accounting)")
is_likely_narrative = benford_df[benford_df.benford_match < .425]
print(f"Khipus with benford_match < .425 = {is_likely_narrative.shape[0]} (is_likely_narrative)")
Khipus with benford_match > .625 = 428 (is_likely_accounting)
Khipus with benford_match < .425 = 103 (is_likely_narrative)

We see that there’s probably 352 khipu that are likely accounting, and 67 khipu that are likely narrative.

### Benford Match vs Sum Clusters

Let’s do a scatter plot of Benford Matching vs (normalized) cords whose sums match another cord. The clustering of accounting khipus is revealed even more clearly.

fig = px.scatter(benford_df, x="benford_match", y="normalized_sum_match_count", log_y=True,
                 size="sum_match_max", color="sum_match_mean", 
                 labels={"benford_match": "#Benford Match", "normalized_sum_match_count": "#Number of Matching Cord Sums (Normalized by # Total Cords)"},
                 hover_data=['name'], title="<b>Benford Match vs Normalized Sum Match Counts</b> - <i>Hover Over Circles to View Khipu Info</i>",
                 width=950, height=950);

Again, we see the lumpiness and confirmation of the “accounting khipus” whose Benford match is > 0.6 or so….

Sanity Check - Marcia Ascher’s “Mathematical” Khipus versus their Corresponding Benford Match

As another checkpoint on Benford Law matching approach, let’s review the khipus that Marcia Ascher’s annotated with mathematical relationships and compare them with their corresponding Benford match.

# These are the khipus that Marcia Ascher annotated as having interesting mathematical relationships
ascher_mathy_khipus = ['AS010', 'AS020', 'AS026B', 'AS029', 'AS038', 'AS041', 'AS048', 'AS066', 'AS068', 
                       'AS079', 'AS080', 'AS082', 'AS092', 'AS115', 'AS128', 'AS129', 'AS164', 'AS168', 'AS192', 
                       'AS193', 'AS197', 'AS198', 'AS199', 'AS201', 'AS206', 'AS207A', 'AS208', 'AS209', 'AS210', 
                       'AS211', 'AS212', 'AS213', 'AS214', 'AS215', 'UR1034', 'UR1096', 'UR1097', 'UR1098', 
                       'UR1100', 'UR1103', 'UR1104', 'UR1114', 'UR1116', 'UR1117', 'UR1120', 'UR1126', 'UR1131', 
                       'UR1135', 'UR1136', 'UR1138', 'UR1140', 'UR1141', 'UR1143', 'UR1145', 'UR1148', 'UR1149', 
                       'UR1151', 'UR1152', 'UR1163', 'UR1175', 'UR1180']
ascher_khipu_benford_match = [benford_match(aKhipu) for aKhipu in all_khipus if in ascher_mathy_khipus]

def is_an_ascher_khipu(aKhipu):
    return True if str(aKhipu.original_name)[0:2] == "AS" or[0:2] == "AS" else False

ascher_khipus = [aKhipu for aKhipu in all_khipus if is_an_ascher_khipu(aKhipu)]
percent_mathy_khipus = 100.0 * float(len(ascher_mathy_khipus))/float(len(ascher_khipus))
percent_string = "{:4.1f}".format(percent_mathy_khipus)

hist_data = [ascher_khipu_benford_match]
group_labels = ['benford_match']
colors = ['#835AF1']

ff.create_distplot(hist_data, group_labels,  bin_size=.01, 
    .update_layout(title_text='<b>Benford Match for Ascher Khipus with Mathematical Relationships</b> - <i>Hover Over Circles to View Khipu Info</i>', # title of plot
                     xaxis_title_text='Benford Match Value (cosine_similarity)', 
                     yaxis_title_text='Percentage of Khipus', # yaxis label
                     bargap=0.05, # gap between bars of adjacent location coordinates
                     #bargroupgap=0.1 # gap between bars of the same location coordinates
print (f"Percent of ascher khipus having mathematical relationships ({len(ascher_mathy_khipus)} out of {len(ascher_khipus)}) is {percent_string}%")
Percent of ascher khipus having mathematical relationships (61 out of 144) is 42.4%

Benford Match vs Fanout Ratio (Primaries vs Subsidiaries)

Let’s conclude this section with an interesting “triangular” graph - Benford match vs Fanout Ratio (the ratio of Pendant cords vs Subsidiary cords)

fig = px.scatter(benford_df, x="benford_match", y="fanout_ratio", 
                 size="num_total_cords", color="num_subsidiary_cords", 
                 labels={"num_total_cords": "#Total Cords", "num_subsidiary_cords": "#Subsidiary Cords"},
                 hover_data=['name'], title="<b>Benford Match vs Fanout Ratio</b> - <i>Hover Over Circles to View Khipu Info</i>",
                 width=950, height=950);

Grouping Khipus into Families - What Constitutes “Similarity”

As a useful first step, it will help to have the ability to sort khipus not just by meta-features like benford_match, but also by other forms of similarity.

One of my favorite similarity techniques is the text-clustering approaches of NLP. A driving need in NLP is to sort topics into various categories; for example, is this a news document or fiction. So how do we text mine khipus? A simple solution is to convert the khipu to words. Each Python Khipu class object (knot, cord, cluster, etc.) is given a “writer” function that documents itself as text. Below is the “document” for a simple 3 cord khipu - AS055. It’s a pretty sparse and simple description. We’ll go about making this more sophisticated as the project becomes more developed. The point is that we now have a set of words that roughly describe a khipu with categorical words such as U_knot_twist, or single_knot and numerical words such as 1421_decimal_value for the cord value. Now we use that set of words as the input to NLP algorithms.

khipu_docstrings_df = kq.fetch_khipu_docstrings()
khipu_names = list(khipu_docstrings_df.kfg_name.values)
khipu_documents_orig = list(khipu_docstrings_df.document.values)

# To assist clustering Ascher khipus, where many khipu metrics such as cord attachment, twist, etc are unknown, 
# We can impute values for attachment, cord twist and knot twist
khipu_documents = [aDocument.replace("Unknown_attachment_type", "Recto_attachment_type").replace("U_twist", "S_twist").replace("U_knot_twist", "S_knot_twist") for aDocument in khipu_documents_orig]
 primary_cord_start YB cord_cluster_start 1_cluster_level 3_cc_cords cord_start U_attachment_type U_twist 1_cord_level 1_cord_ordinal 1421_decimal_value 1_cluster_ordinal KB knot_cluster_start single_knot U_knot_twist ʘ_knot_value knot_cluster_end knot_cluster_start single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value knot_cluster_end knot_cluster_start single_knot U_knot_twist •_knot_value single_knot U_knot_twist •_knot_value knot_cluster_end knot_cluster_start figure_eight_knot U_knot_twist ※_knot_value knot_cluster_end cord_end cord_start U_attachment_type U_twist 1_cord_level 2_cord_ordinal 2427_decimal_value 2_cluster_ordinal YB knot_cluster_start single_knot U_knot_twist ʘ_knot_value single_knot U_knot_twist ʘ_knot_value knot_cluster_end knot_cluster_start single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value knot_cluster_end knot_cluster_start single_knot U_knot_twist •_knot_value single_knot U_knot_twist •_knot_value knot_cluster_end knot_cluster_start long_knot U_knot_twist 7_knot_value knot_cluster_end cord_end cord_start U_attachment_type U_twist 1_cord_level 3_cord_ordinal 734_decimal_value 3_cluster_ordinal KB knot_cluster_start single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value single_knot U_knot_twist o_knot_value knot_cluster_end knot_cluster_start single_knot U_knot_twist •_knot_value single_knot U_knot_twist •_knot_value single_knot U_knot_twist •_knot_value knot_cluster_end knot_cluster_start long_knot U_knot_twist 4_knot_value knot_cluster_end cord_end cord_cluster_end primary_cord_end 

Then we can pass the document to modern NLP text mining algorithms which can cluster the text by similarity along the n-dimensions of all the different words and their frequencies in each document.

The usual first step in NLP document analysis is to build a term-frequency, inverse-document-frequency matrix.

Tf-Idf matrices for NLP have a long history. An explanation:

Term Frequency, Inverse Document Frequency and Tf°Idf

As part of training our machine model, we want to give it information about how words are distributed across our set of data. This is commonly done using Term Frequency/Inverse Document Frequency techniques.

The first step, then, is to calculate the term frequency (Tf), the inverse document frequency (Idf), and their product (Tf°Idf) Apologies: There’s an occasional Google Chrome bug with latex/equation rendering - see equations in Safari if you want to understand them

How probable is that this particular word appears in a SINGLE document.

\(Tf(\omega) = \left( \frac{ \# occurences\ of\ \omega\ in\ document}{\# words\ in\ document} \right)\)

How common or rare a word is across ALL documents. If it’s a common word across all documents, it’s regarded as a non-differentiating word, and we want to weight it lower.

Alternatively, it can be thought of as the specificity of a term - quantified as an inverse function of the number of documents in which it occurs. The most common function used is log(natural) of the number of docs divided by the number of docs in which this term appears.

\(Idf(\omega) = ln \left( \frac{ \# docs}{\# docs.containing(\omega) } \right)\)

Tf°Idf, the product of Tf and Idf, is used to weight words according to how “important” they are to the machine learning algorithm ☺. The higher the TF°IDF score (weight), the rarer the term and vice versa. The intuition for this measure is this:

If a word appears frequently in a document, then it should be important and we should give that word a high score. But if a word appears in too many other documents, it’s probably not a unique identifier, therefore we should assign a lower score to that word.

Tf°idf is one of the most popular term-weighting schemes today. Over 80% of text-based recommender systems, including Google’s search engine, use Tf°Idf. Introduced, as “term specificity” by Karen Spärck Jones in a 1972 paper, it has worked well as a heuristic. However, its theoretical foundations have been troublesome for at least three decades afterward, with many researchers trying to find information theoretic justifications for it.

Ashok Khosla - Detecting Deceptive Hotel Reviews

# Calculate TF-IDF Matrix

def simple_tokenizer(text):
    # first tokenize by sentence, then by word to ensure that punctuation is caught as it's own token
    tokens = [word.lower() for word in nltk.word_tokenize(text)]
    return tokens

tfidf_vectorizer = TfidfVectorizer(max_df=.9, max_features=200000,
                                   min_df=.1, stop_words=None,
                                   use_idf=True, tokenizer=simple_tokenizer, ngram_range=(1,10))

tfidf_matrix = tfidf_vectorizer.fit_transform(khipu_documents)

# terms = tfidf_vectorizer.get_feature_names_out() # Newer version problem....?
terms = tfidf_vectorizer.get_feature_names()
cosine_distance = 1 - cosine_similarity(tfidf_matrix)

K-Means Clustering

Traditionally the next step is to use a variety of clustering methods. Here I will only use two. The first, K-means clustering, groups the “documents” based on n-dimensional clustering of the bag of words contained in the documents. I’ve used K-means in the past. It’s not a great clustering algorithm, but it has the advantage that it outputs discrete groupings which can be handy. So I always do it as a first pass.

num_clusters = 9

km = KMeans(n_clusters=num_clusters)
clusters = km.labels_.tolist()
khipu_cluster_dict = { 'khipu_name': khipu_names, 'document': khipu_documents, 'cluster': clusters  }
khipu_cluster_df = pd.DataFrame(khipu_cluster_dict, index = [clusters] , columns = ['khipu_name', 'document', 'cluster'])

kMeansClusters = []
for cluster_id in range(num_clusters):
    khipus_in_cluster = khipu_cluster_df[khipu_cluster_df.cluster == cluster_id].khipu_name.values.tolist()
    print(cluster_id, ":\t", khipus_in_cluster,",")
0 :  ['UR272', 'UR252', 'UR250', 'UR244', 'UR243', 'UR242', 'UR239', 'UR235', 'UR231', 'UR214', 'UR211', 'UR210', 'UR203', 'UR198', 'UR195', 'UR191', 'UR190', 'UR189', 'UR179', 'UR173', 'UR170', 'UR160', 'UR155', 'UR140', 'UR139', 'UR135', 'UR134', 'UR128', 'UR124', 'UR117D', 'UR117B', 'UR114', 'UR1096', 'UR1095', 'UR1091', 'UR106', 'UR1058', 'UR1053', 'UR1033F', 'UR1033C', 'UR1032', 'UR102', 'UR093', 'UR091', 'UR089', 'UR082', 'UR079', 'UR078', 'UR069', 'UR067', 'UR066', 'UR059', 'UR056B', 'UR053C', 'UR046', 'UR027', 'UR021', 'UR019', 'UR006', 'QU021', 'QU017', 'QU013', 'QU009', 'QU007', 'QU004', 'MM021', 'MM013', 'MM011', 'MM003', 'MM001', 'KH0227', 'HP057', 'HP051 B', 'HP051 A', 'HP050', 'HP048', 'HP046 A', 'HP045', 'HP044', 'HP040', 'HP033', 'HP032', 'HP020', 'HP015', 'AS191', 'AS080', 'AS076'] ,
1 :  ['UR241', 'UR238', 'UR215', 'UR205', 'UR196', 'UR177', 'UR175', 'UR169', 'UR156', 'UR148', 'UR132', 'UR117C', 'UR116A', 'UR1166', 'UR1151', 'UR1130', 'UR1108', 'UR109', 'UR1052', 'UR097', 'UR096', 'UR084', 'UR057', 'UR029', 'UR024', 'UR023', 'UR016', 'UR007', 'UR003', 'KH0350', 'KH0267', 'HP016', 'AS215F', 'AS213', 'AS212', 'AS211', 'AS207B', 'AS205', 'AS204', 'AS201', 'AS189', 'AS186', 'AS174', 'AS157', 'AS155', 'AS153', 'AS142', 'AS094', 'AS071', 'AS062', 'AS061/MA036', 'AS048', 'AS027', 'AS026B', 'AS026A', 'AS017', 'AS005'] ,
2 :  ['UR294', 'UR293', 'UR280', 'UR279', 'UR277', 'UR270', 'UR267A', 'UR264', 'UR262', 'UR261', 'UR260', 'UR258', 'UR255', 'UR246', 'UR240', 'UR237', 'UR233', 'UR232', 'UR229', 'UR228', 'UR227', 'UR225', 'UR222', 'UR221', 'UR220', 'UR219', 'UR217', 'UR213', 'UR212', 'UR208', 'UR207', 'UR206', 'UR204', 'UR202', 'UR197', 'UR193', 'UR192', 'UR174', 'UR154', 'UR152', 'UR151', 'UR150', 'UR149', 'UR146', 'UR141', 'UR138', 'UR127', 'UR120', 'UR1175', 'UR1167', 'UR1165', 'UR1162B', 'UR1161', 'UR1154', 'UR1147', 'UR1144', 'UR1140', 'UR1138', 'UR1136', 'UR1135', 'UR1131', 'UR113', 'UR1127', 'UR1126', 'UR1124', 'UR1114', 'UR1109', 'UR1107', 'UR1103', 'UR1102', 'UR1100', 'UR1087', 'UR1084', 'UR107', 'UR1051', 'UR105', 'UR1033G', 'UR103', 'UR090', 'UR088', 'UR086', 'UR062', 'UR061', 'UR034', 'UR033', 'UR022', 'UR018', 'UR017', 'UR010', 'UR008', 'UR005', 'UR004', 'QU016', 'MM1086', 'MM020', 'MM006/AN001', 'KH0033', 'KH0032', 'KH0001', 'JC017', 'JC001', 'HP055', 'HP034', 'HP023', 'HP022', 'HP021', 'HP018', 'HP017', 'HP013', 'HP012', 'HP009', 'HP005', 'HP004', 'HP003', 'AS199', 'AS196', 'AS195', 'AS194'] ,
3 :  ['UR1163', 'UR1162A', 'UR1146', 'UR1121', 'UR1117', 'UR1098', 'UR1097', 'UR1034', 'UR1033A', 'UR051', 'UR042', 'UR041', 'UR040', 'KH0080', 'AS35A', 'AS215', 'AS214', 'AS210', 'AS207C', 'AS207A', 'AS203', 'AS202', 'AS185', 'AS182', 'AS178', 'AS169', 'AS160', 'AS159', 'AS158', 'AS139', 'AS132', 'AS128', 'AS112', 'AS110', 'AS093', 'AS092', 'AS090/N2', 'AS089', 'AS085', 'AS082', 'AS081', 'AS077', 'AS069', 'AS065B', 'AS064', 'AS060', 'AS059', 'AS045', 'AS044', 'AS043', 'AS042', 'AS039', 'AS035D', 'AS028', 'AS025', 'AS024', 'AS023', 'AS021', 'AS019', 'AS016', 'AS015', 'AS013', 'AS012', 'AS010'] ,
4 :  ['UR253', 'UR230', 'UR223', 'UR216', 'UR194', 'UR184', 'UR145', 'UR144', 'UR137', 'UR130', 'UR125', 'UR1141', 'UR112', 'UR1105', 'UR110', 'UR104', 'UR1033E', 'UR100', 'UR098', 'UR095', 'UR058A', 'UR055', 'UR054', 'UR053E', 'UR053B', 'UR052', 'UR050', 'UR039', 'UR014', 'UR013', 'UR012', 'QU019', 'QU012', 'MM017', 'MM015', 'MM010', 'MM009', 'MM008', 'KH0058', 'KH0057', 'HP056', 'AS35B', 'AS192', 'AS184', 'AS177', 'AS173', 'AS171', 'AS137', 'AS083', 'AS066', 'AS063', 'AS007', 'AS006', 'AS004', 'AS003', 'AS002', 'AS001'] ,
5 :  ['UR278', 'UR275', 'UR274B', 'UR274A', 'UR273B', 'UR273A', 'UR271', 'UR269', 'UR268', 'UR267B', 'UR266', 'UR257', 'UR256', 'UR249', 'UR248', 'UR234', 'UR226', 'UR218', 'UR201', 'UR200', 'UR188', 'UR185', 'UR183', 'UR165', 'UR164', 'UR162', 'UR153', 'UR147', 'UR129', 'UR1180', 'UR1179', 'UR1152', 'UR1149', 'UR1148', 'UR1145', 'UR1143', 'UR1124 Detail 1', 'UR1123', 'UR1120', 'UR1119', 'UR1118', 'UR1106', 'UR1104', 'UR038', 'UR026', 'UR020', 'UR001', 'QU015', 'QU010', 'MM018', 'KH0197', 'KH0067', 'JC021', 'JC020', 'JC018', 'JC007', 'HP030', 'HP029', 'HP027', 'HP024', 'HP011', 'HP010', 'HP008', 'HP007', 'HP006', 'HP002', 'HP001'] ,
6 :  ['UR292A', 'UR291A', 'UR290', 'UR289', 'UR288', 'UR287', 'UR286', 'UR285', 'UR284', 'UR283', 'UR282', 'UR251', 'UR180', 'UR178', 'JC023', 'JC022', 'JC019', 'JC016', 'JC015', 'JC014', 'JC013', 'JC012', 'JC011', 'JC010', 'JC009', 'JC008', 'JC006', 'JC005', 'JC004', 'JC003', 'JC002'] ,
7 :  ['UR276', 'UR265', 'UR263', 'UR259', 'UR254', 'UR245', 'UR209', 'UR199', 'UR172', 'UR171', 'UR168', 'UR167', 'UR159', 'UR158', 'UR157', 'UR123', 'UR117A', 'UR116B', 'UR111', 'UR1088', 'UR1057', 'UR1049', 'UR1033D', 'UR1033B', 'UR092', 'UR087', 'UR081', 'UR080', 'UR077', 'UR076', 'UR075', 'UR074', 'UR073', 'UR072', 'UR071', 'UR070', 'UR068', 'UR065', 'UR064', 'UR063', 'UR060', 'UR056C', 'UR056A', 'UR048', 'UR047', 'UR045', 'UR037', 'UR032', 'UR028', 'UR009', 'QU022', 'QU020', 'QU018', 'QU008', 'QU006', 'QU002', 'QU001', 'MM019', 'MM007/AN002', 'MM005', 'HP054', 'HP049', 'HP046 B', 'HP043', 'HP042', 'HP038', 'HP031', 'HP028', 'HP026', 'HP025', 'HP019', 'HP014', 'AS198', 'AS193', 'AS079', 'AS078', 'AS075', 'AS074'] ,
8 :  ['UR247', 'UR187', 'UR186', 'UR182', 'UR166', 'UR161', 'UR143', 'UR142', 'UR136', 'UR131C', 'UR131B', 'UR131A', 'UR122', 'UR121', 'UR119', 'UR118', 'UR1176', 'UR1150', 'UR1116', 'UR1113', 'UR1099', 'UR108', 'UR1040', 'UR101', 'UR099', 'UR094', 'UR085', 'UR058B', 'UR053D', 'UR053A', 'UR049', 'UR015', 'UR011', 'UR002', 'QU014', 'QU011', 'QU005', 'QU003', 'MM016', 'MM014', 'MM012', 'MM004', 'MM002', 'KH0083', 'KH0081', 'KH0049', 'HP053', 'HP052', 'HP047', 'HP039', 'HP037', 'AS209', 'AS206', 'AS200', 'AS197', 'AS188', 'AS187', 'AS183', 'AS182B', 'AS172', 'AS170', 'AS168', 'AS164', 'AS156', 'AS134', 'AS133', 'AS129', 'AS125', 'AS122', 'AS115', 'AS111', 'AS101 - Part 2', 'AS101 - Part 1', 'AS073', 'AS072', 'AS065', 'AS063B', 'AS055', 'AS054', 'AS050', 'AS041', 'AS037', 'AS036', 'AS035C', 'AS029', 'AS020', 'AS018', 'AS014', 'AS011', 'AS009', 'AS008'] ,

One simple test of the clustering - where do known khipu clusters cluster? For example, where does UR1033[A-F] group? How about UR058[A-B]? Examine these and similar clusters in the list above and below in the hierarchical dendogram. It is also useful to browse the results visually using the Khipu Field Guide Image Browser

Hierarchical Clustering

Hierarchical clustering, uses “cosine similarity” (i.e. how close together are two normalized vectors in an n-dimensional bag-of-words frequency matrix) to group together and cluster documents. In my experience, it generally performs better than Kmeans or Principal Component Analysis. Although I prefer the R package for clustering, Python provides graphics adequate for the job at hand. In Phase 3 we will likely move to R.

Z = ward(cosine_distance) #define the linkage_matrix using ward clustering pre-computed distances

fig, ax = plt.subplots(figsize=(15, 40)) # set size
ax = dendrogram(Z, orientation="left", labels=khipu_names);

    axis= 'x',          # changes apply to the x-axis
    which='both',       # both major and minor ticks are affected
    bottom='off',       # ticks along the bottom edge are off
    top='off',          # ticks along the top edge are off

plt.tight_layout() #show plot with tight layout

#uncomment below to save figure
plt.savefig('./images/khipu_clusters.png', dpi=200) #save figure as ward_clusters

# Clustered 
sorted_by_similarity = ax['ivl']
['UR162', 'UR020', 'JC007', 'UR185', 'JC021', 'JC020', 'UR026', 'UR273A', 'UR271', 'HP029', 'UR274A', 'UR256', 'QU010', 'UR154', 'UR038', 'UR226', 'UR151', 'JC018', 'UR257', 'UR269', 'UR266', 'UR201', 'UR234', 'UR200', 'HP024', 'HP006', 'UR153', 'KH0067', 'HP002', 'UR188', 'HP010', 'UR164', 'UR268', 'UR249', 'UR1179', 'UR1124 Detail 1', 'UR1119', 'UR1148', 'UR1143', 'HP001', 'UR1149', 'UR1120', 'UR1118', 'UR165', 'HP027', 'HP030', 'UR275', 'UR248', 'UR267B', 'HP011', 'QU015', 'HP007', 'UR147', 'UR273B', 'UR218', 'HP008', 'UR274B', 'UR183', 'MM018', 'UR217', 'UR213', 'UR001', 'UR1087', 'UR022', 'UR005', 'UR146', 'KH0197', 'UR1051', 'UR202', 'HP023', 'UR018', 'AS199', 'UR240', 'UR237', 'HP034', 'UR262', 'UR062', 'UR278', 'MM006/AN001', 'UR220', 'HP012', 'MM020', 'UR261', 'UR255', 'HP055', 'HP003', 'UR197', 'UR233', 'UR208', 'UR193', 'UR033', 'UR279', 'UR219', 'UR1145', 'UR1144', 'UR1135', 'UR1107', 'UR1152', 'UR129', 'UR1180', 'UR1106', 'UR1123', 'UR1103', 'KH0001', 'UR1167', 'UR1138', 'UR1147', 'UR1162B', 'UR1127', 'UR1175', 'UR1102', 'UR1124', 'UR1109', 'UR105', 'MM1086', 'JC001', 'UR294', 'UR293', 'JC017', 'UR138', 'UR206', 'UR149', 'UR088', 'UR207', 'UR152', 'UR120', 'UR086', 'UR061', 'UR141', 'HP021', 'UR232', 'UR267A', 'UR258', 'UR228', 'UR227', 'UR246', 'UR174', 'HP017', 'HP013', 'AS196', 'UR221', 'UR270', 'UR008', 'UR264', 'UR277', 'UR212', 'UR192', 'UR260', 'UR199', 'HP018', 'HP005', 'HP014', 'UR068', 'UR064', 'HP022', 'UR229', 'UR225', 'UR1033D', 'UR1033G', 'KH0033', 'UR074', 'UR209', 'UR056A', 'HP042', 'UR056C', 'UR073', 'UR063', 'UR254', 'UR172', 'UR276', 'HP054', 'QU019', 'MM008', 'UR049', 'MM004', 'UR053C', 'MM003', 'UR1104', 'UR1084', 'MM002', 'UR1166', 'UR1114', 'UR127', 'UR1098', 'UR1136', 'UR1126', 'UR1165', 'UR1140', 'UR1100', 'UR1161', 'AS195', 'AS194', 'UR280', 'UR1131', 'UR155', 'MM001', 'UR239', 'UR069', 'UR231', 'UR067', 'UR203', 'UR235', 'UR091', 'UR089', 'UR189', 'UR160', 'KH0032', 'UR242', 'QU016', 'UR004', 'UR090', 'UR010', 'UR107', 'HP051 A', 'UR222', 'UR017', 'UR150', 'UR048', 'UR113', 'HP004', 'HP009', 'UR204', 'UR034', 'UR1105', 'AS164', 'AS041', 'AS011', 'UR042', 'AS066', 'AS063', 'AS110', 'AS092', 'AS185', 'UR1176', 'AS063B', 'AS050', 'AS037', 'AS178', 'AS036', 'AS174', 'AS153', 'AS159', 'AS212', 'AS189', 'KH0080', 'AS210', 'UR1034', 'AS044', 'AS128', 'AS043', 'AS173', 'AS132', 'AS045', 'UR119', 'UR118', 'AS200', 'UR011', 'UR131C', 'UR131B', 'UR131A', 'AS170', 'AS029', 'UR143', 'UR142', 'UR094', 'UR002', 'UR108', 'KH0081', 'AS008', 'AS054', 'AS209', 'AS125', 'AS115', 'UR161', 'HP047', 'UR247', 'UR053A', 'AS197', 'UR015', 'KH0049', 'UR187', 'UR122', 'AS134', 'AS073', 'AS188', 'AS020', 'AS206', 'AS133', 'AS183', 'AS014', 'UR1150', 'UR1040', 'AS111', 'AS101 - Part 1', 'AS101 - Part 2', 'AS072', 'AS168', 'AS156', 'UR1116', 'AS187', 'AS065', 'AS122', 'AS035C', 'UR101', 'UR053D', 'AS182B', 'AS018', 'HP052', 'AS009', 'UR136', 'MM014', 'UR099', 'MM012', 'MM017', 'MM016', 'QU005', 'HP053', 'UR186', 'HP039', 'AS172', 'AS055', 'UR1099', 'AS129', 'AS139', 'AS082', 'UR1117', 'AS042', 'UR084', 'KH0267', 'UR137', 'UR095', 'QU012', 'MM015', 'AS192', 'UR223', 'UR125', 'UR166', 'HP037', 'UR1113', 'UR085', 'QU011', 'UR100', 'MM010', 'MM009', 'UR039', 'UR1141', 'AS083', 'UR098', 'AS007', 'AS001', 'KH0058', 'AS006', 'AS177', 'AS137', 'AS35B', 'AS171', 'AS184', 'AS003', 'AS002', 'HP056', 'QU014', 'QU003', 'UR145', 'KH0083', 'UR130', 'UR053B', 'UR121', 'UR1033E', 'UR013', 'UR058B', 'UR216', 'UR058A', 'UR215', 'UR1130', 'UR1146', 'UR1162A', 'UR1108', 'AS155', 'AS215F', 'AS204', 'AS061/MA036', 'AS017', 'AS094', 'AS048', 'AS027', 'AS205', 'AS071', 'UR040', 'AS012', 'AS211', 'AS026A', 'UR1151', 'AS186', 'UR132', 'AS157', 'AS062', 'AS207B', 'AS026B', 'AS142', 'AS213', 'AS201', 'UR1097', 'AS015', 'AS203', 'AS202', 'UR1121', 'AS112', 'AS024', 'AS35A', 'AS065B', 'AS064', 'AS039', 'AS077', 'AS060', 'AS207C', 'AS016', 'AS019', 'AS169', 'AS093', 'UR051', 'AS090/N2', 'AS010', 'AS160', 'AS158', 'AS089', 'AS085', 'AS069', 'AS215', 'AS207A', 'AS021', 'UR1033A', 'AS013', 'AS028', 'AS035D', 'UR1163', 'UR041', 'AS214', 'AS059', 'AS182', 'AS023', 'UR190', 'UR059', 'UR079', 'UR056B', 'UR173', 'HP051 B', 'HP032', 'UR1095', 'UR170', 'UR243', 'UR117D', 'UR1091', 'HP046 A', 'UR252', 'UR214', 'HP033', 'UR244', 'UR272', 'UR139', 'UR128', 'UR210', 'UR124', 'UR1096', 'UR1033F', 'UR191', 'UR195', 'MM021', 'UR102', 'QU021', 'UR066', 'HP045', 'QU009', 'KH0227', 'UR009', 'UR045', 'UR006', 'UR263', 'HP057', 'QU017', 'QU013', 'UR093', 'QU001', 'UR1053', 'UR032', 'HP044', 'AS191', 'UR250', 'QU004', 'HP048', 'UR211', 'UR082', 'UR1032', 'UR198', 'UR114', 'UR021', 'QU007', 'UR135', 'HP015', 'UR140', 'HP020', 'UR1058', 'UR078', 'AS076', 'UR1033C', 'HP040', 'UR134', 'AS080', 'UR117B', 'UR027', 'UR196', 'UR238', 'UR116B', 'UR019', 'UR205', 'UR116A', 'UR148', 'UR177', 'UR175', 'UR109', 'UR096', 'UR117C', 'UR097', 'UR003', 'UR024', 'UR007', 'KH0350', 'UR169', 'UR057', 'UR029', 'UR023', 'UR077', 'UR156', 'UR1052', 'HP016', 'UR123', 'UR168', 'QU018', 'UR1088', 'MM019', 'UR111', 'QU002', 'UR230', 'UR241', 'UR016', 'UR014', 'UR012', 'UR053E', 'UR184', 'UR104', 'UR182', 'UR194', 'UR253', 'KH0057', 'UR065', 'JC016', 'UR292A', 'JC012', 'UR158', 'UR080', 'QU006', 'UR117A', 'UR081', 'QU022', 'UR075', 'HP038', 'UR171', 'MM005', 'UR071', 'HP026', 'HP025', 'HP049', 'UR070', 'HP028', 'AS198', 'UR259', 'UR157', 'AS193', 'UR265', 'UR037', 'UR087', 'MM007/AN002', 'UR072', 'HP046 B', 'QU008', 'QU020', 'UR245', 'UR1033B', 'UR092', 'HP019', 'UR167', 'HP031', 'UR159', 'HP043', 'UR060', 'UR076', 'AS078', 'AS079', 'UR1057', 'AS075', 'AS074', 'UR1049', 'UR028', 'MM013', 'MM011', 'UR180', 'UR179', 'AS005', 'AS004', 'AS025', 'UR144', 'UR112', 'UR110', 'UR050', 'AS081', 'UR055', 'UR054', 'UR052', 'UR285', 'UR289', 'UR288', 'UR178', 'UR290', 'JC003', 'JC008', 'JC004', 'JC002', 'JC015', 'JC014', 'UR282', 'JC023', 'JC022', 'JC011', 'UR251', 'UR047', 'UR046', 'HP050', 'UR106', 'UR1154', 'UR103', 'JC010', 'JC009', 'JC013', 'UR286', 'JC005', 'UR287', 'JC006', 'UR291A', 'UR283', 'UR284', 'JC019']

Sanity Check - Calendar Khipus

Again, you can examine the results visually using the Khipu Field Guide Image Browser. As a sample of how well this hierarchical clustering occurs, let’s look at how close UR006, UR009, UR021, and UR1084/AS084 cluster. These 4 khipus are all considered to be “astronomical” or calendar type khipu, based on their cluster frequencies and cords per cluster. You can see that the clustering algorithm correctly places UR006, UR009 and UR021 close together, but it places UR1084 much farther away. Why? I’ll have to analyze Juliana Martin’s work on UR1084 more in phase 2 or 3.

calendar_khipus = ['UR021', 'UR006', 'UR009', 'UR1084']
for i, aKhipu in enumerate(calendar_khipus):
    previous_khipu_name = calendar_khipus[i-1] if i > 0 else calendar_khipus[0]
    cur_khipu_name = calendar_khipus[i]
    previous_khipu_index = sorted_by_similarity.index(previous_khipu_name)
    cur_khipu_index = sorted_by_similarity.index(cur_khipu_name)
    delta_previous = abs(cur_khipu_index - previous_khipu_index)
    print(f"{aKhipu} is at position: {cur_khipu_index} - {delta_previous} away from previous khipu")
UR021 is at position: 494 - 0 away from previous khipu
UR006 is at position: 475 - 19 away from previous khipu
UR009 is at position: 473 - 2 away from previous khipu
UR1084 is at position: 178 - 295 away from previous khipu

What exactly are we measuring when we cluster? That’s a difficult question to answer. How good is the clustering? Again a difficult question to answer. Later, in phase 3, we can look at the eigenvectors of a khipu, the mathematical equivalent of archetypal khipus that are the basis of all khipus. Until then, a simple plot of our hierarchical clustering with respect to benford_match and color shows that hierarchical clustering is occuring i.e. lots of clusters of little hills, but not as we expect, i.e. montonically decreasing.

The khipus simply defy simple explanation. As Piet Hein says:

Problems worthy
Of attack,
Prove their worth,
By hitting back.

benford_match = [benford_df[ == khipu_name].benford_match.values[0] for khipu_name in sorted_by_similarity]
match_df = pd.DataFrame({'khipu_name':sorted_by_similarity, 'benford_match':benford_match})
fig =, x='khipu_name', y='benford_match', width=2400, height=300, title="Similarity by Benford\'s Law Match").show()
color_similarity = [khipu_summary_df[khipu_summary_df.kfg_name == khipu_name].num_ascher_colors.values[0] for khipu_name in sorted_by_similarity]
match_df = pd.DataFrame({'khipu_name':sorted_by_similarity, 'num_ascher_colors':color_similarity})
fig =, x='khipu_name', y='num_ascher_colors', width=2400, height=300, title="Similarity by Number of Ascher Colors").show()