Different results for linalg.norm in numpy
Question: I am trying to create a feature matrix based on certain features and then
finding distance b/w the items. For testing purpose I am using only 2 points
right now.

data : list of items I have

specs : feature dict of the items (I am using their values of keys as features
of item)

features : list of features

This is my code by using numpy zero matrix :

    
    
    import numpy as np
    matrix = np.zeros((len(data),len(features)),dtype=bool)
    for dataindex,item in enumerate(data):
        if dataindex > 5:
        break
    specs = item['specs']
    values = [value.lower() for value in specs.values()]
    for idx,feature in enumerate(features):
        if(feature in values):
            matrix[dataindex,idx] = 1
            print dataindex,idx
    v1 = matrix[0]
    v2 = matrix[1]
    # print v1.shape
    diff = v2 - v1
    dist = np.linalg.norm(diff)
    print dist
    

The value for dist I am getting is 1.0

This is my code by using python lists :

    
    
    matrix = []
    for dataindex,item in enumerate(data):
        if dataindex > 5:
            f = open("Matrix.txt",'w')
            f.write(str(matrix))
            f.close()
            break
        print "Item" + str(dataindex)
        row = []
        specs = item['specs']
        values = [value.lower() for value in specs.values()]
        for idx,feature in enumerate(features):
            if(feature in values):
                print dataindex,idx
                row.append(1)
            else:
                row.append(0)
        matrix.append(row)
    
    v1 = np.array(matrix[0]);
    v2 = np.array(matrix[1]);
    diff = v2 - v1
    print diff
    dist = np.linalg.norm(diff)
    print dist
    

The value of dist in this case is 4.35889894354

I have checked many time that the value 1 is being set at the same position in
both cases but the answer is different.

May be I am not using numpy properly or there is an issue with the logic. I am
using numpy zero based matrix because of its memory efficiency.

What is the issue?


Answer: It's a type issue :

    
    
    In [9]: norm(ones(3).astype(bool))
    Out[9]: 1.0
    
    In [10]: norm(ones(3).astype(float))
    Out[10]: 1.7320508075688772
    

You must decide what it the good norm for your problem and eventually cast
your data with `astype`.

[`norm(M)`](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.linalg.norm.html)
is `sqrt(dot(M.ravel(),M.ravel()))` , so for a boolean matrix, `norm(M)` is 0.
if `M` is the `False` matrix, 1\. otherwise. use the `ord` parameter of `norm`
to tune the function.

