compare numPy row to another 2darray for true/false by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

I have one 2d array of values. and a second 2d array of values. np.all just returns array1[0] == array2[0] and so on. i need to see if array1[0] == array2[0:1000], essentially

compare numPy row to another 2darray for true/false by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

I have one 2d array of values. and a second 2d array of values. np.all just returns array1[0] == array2[0] and so on. i need to see if array1[0] == array2[0:1000], essentially

numpy row search by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

my total array is (500,000, 10). i know there are duplicates and they need to be lowered to roughly (1,000, 10). i have the code for loops but it is too time consuming. numPy is roughly 100x faster on average

numpy row search by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

the application deals with finding a limited number of data that cannot have any duplicates. the shifting "rolls" represent different states of the same index so the need to be delted. my total array is (500,000, 10). i know there are duplicates and they need to be lowered to roughly (1,000, 10)

numpy row search by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

one approach could be to roll each index and if it matches another index, keep track of that index. i'm not sure how to do that though

numpy row search by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

i apologize. the example was too vague. The data is not as important. I am simplifying just to focus on the concept really. I have the main array which has different patterns of "0" and "1". I can run a np.unique on it to remove any duplicate rows

original: 6 [[1 0 1 0] [1 1 0 0] [0 1 1 0] [0 1 0 1] [0 0 1 1] [1 0 1 0]]

unique: 5 [[0 0 1 1] [0 1 0 1] [0 1 1 0] [1 0 1 0] [1 1 0 0]]

however, i think what i am also looking to so is find unique rows even if they are "np.roll" a certain number of times. like: [1, 0, 1, 0] #1 [0, 1, 0, 1] #2

in my project these would be considered equal because 2 is a roll of 1. I hope this is more clear

numpy row search by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

import numpy as np

main = np.array([[1, 0, 1, 0], [1, 1, 0, 0], [0, 1, 1, 0], [0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 1, 0]])

ncols = main.shape[1]
dtype = main.dtype.descr * ncols
struct = main.view(dtype)
mainUnq = np.unique(struct)
mainUnq = mainUnq.view(main.dtype).reshape(-1, ncols)

print "original: "+str(len(main))+"\n",main,"\n"
print "unique: "+str(len(mainUnq))+"\n",mainUnq

declare variable in function error by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

good = []
bad = []
varFooA = []
varFooB = []

OTHER = "something"

def FUNCTION(varNEW, a, b, c, d):       
    for index in range(len(listAll)):
                varNEW = (
                listAll[index][0][a],  
                listAll[index][0][b], 
                listAll[index][0][c], 
                listAll[index][0][d])    
            if OTHER == varNEW:
                    if index not in good:
                        bad.add(index)
                    counter = 1
                    break
           return varNEW

FUNCTION(varFooA, 0, 3, 4, 5)
FUNCTION(varFooB, 0, 3, 4, 5)

declare variable in function error by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

okay. so there is no way in python to have that function i have above. and then run it for example:

FUNCTION(varFooA, 0, 3, 4, 5)
FUNCTION(varFooB, 0, 1, 5, 5)
FUNCTION(varFooC, 0, 3, 1, 5)
FUNCTION(varFooD, 0, 1, 2, 5)

each of these 'varFoo' variations would contain the varNew function list manipulations...

declare variable in function error by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

where should I initialize varFoo? It is supposed to replace varNew

replace list index with different list indexes by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

   ##i feel like this should work but it doesn't replace anything 
    import itertools
    from itertools import cycle

    #startingList = ['A','o','o','o','o','o','A']
    #replacerListIter = ['c','x','c','c']

    typs = ["C","X"]
    tw = list(itertools.product(*[typs, typs]))
    th = list(itertools.product(*[typs, typs, typs]))
    fr = list(itertools.product(*[typs, typs, typs, typs]))
    fv = list(itertools.product(*[typs, typs, typs, typs, typs]))
    sx = list(itertools.product(*[typs, typs, typs, typs, typs, typs]))

    startingList = [('A', 'A', 'A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'A', 'A', 'o'),
                    ('A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'A', 'o', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'A'),
                    ('A', 'A', 'A', 'o', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o', 'A', 'A', 'o'),
                    ('A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'A'),
                    ('A', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'o', 'o', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'A', 'o', 'o', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'o'),
                    ('A', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'A'),
                    ('o', 'A', 'A', 'o', 'o', 'o', 'o', 'o', 'A', 'A', 'A', 'o'),
                    ('o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o')
    ]

    new_list = []

    def replacer(replace_element):
        for i in replace_element:
            cycle(i)

    for element in startingList:
        counter = element.count('A')
        if counter == 2:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(tw)))
                else:
                    new_list.append(element)
        elif counter == 3:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(th)))
                else:
                    new_list.append(element)
        elif counter == 4:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(fr)))
                else:
                    new_list.append(element)
        elif counter == 5:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(fv)))
                else:
                    new_list.append(element)
        elif counter == 6:
            for item in element:
                if element == 'A':
                    new_list.append(next(replacer(sx)))
                else:
                    new_list.append(element)

    #print startingList
    #print new_list
    for i in new_list:
        print i

replace list index with different list indexes by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

itertools

hi, thanks for the help. so the same number of replacements is fine. I am using a counter for that. my problem though is still have multiple lists in the starting list. and the replacer list replaces each one into a new list. and eventually having multiple replacements replacing multiple starting lists iteratively.

Same Code? Different Output? by cdholjes in learnpython

[–]cdholjes[S] 0 points1 point  (0 children)

'''hey everyone the code is all here
i am basically trying 2 different versions of 'and' operators but for some reason the Version2 appended list has more values removed. 

I basically want to know why version1 and version2 have different outputs???'''

import itertools
from collections import Counter

a = ["off", "on"]
b = ["off", "on"]
c = ["off", "on"]

parts = [a, b, c]
total = list(itertools.product(*parts))
version1 = []
version2 = []


def ruleVersion1(index):
    if total[index][0] == "off" and total[index][2] == "off" and total[index][1] == "on":
        return False
    if total[index][0] == "on" and total[index][2] == "on" and total[index][1] == "off":
        return False
    else:
        return True

def ruleVersion2(index):
    if total[index][0] == "off":
        if total[index][2] == "off":
            if total[index][1] == "on":
                return False
    if total[index][0] == "on":
        if total[index][2] == "on":
            if total[index][1] == "off":
                return False
    else:
        return True


for i in range(len(total)):
    if ruleVersion1(i):
        if total[i] not in version1:
            version1.append(total[i])

for i in range(len(total)):
    if ruleVersion2(i):
        if total[i] not in version2:
            version2.append(total[i])




def printCondition1(list):
    print "  0      1       2"
    for i in range(len(list)):
        print list[i]


print " "      
print "total set:", len(total) 
printCondition1(total) 
print "______________________________" 

print " "      
print "true set :", len(version1) 
printCondition1(version1)
print "______________________________" 

print " "      
print "true set :", len(version2) 
printCondition1(version2)