output of predicted_classes
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 4, 4, 2, 4, 4, 4, 4, 5, 4, 4, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 7, 7, 7, 7, 7, 7, 7, 13, 7, 7, 8, 11, 8, 8, 8,
11, 8, 11, 11, 8, 11, 9, 9, 9, 9, 9, 9, 9, 9, 8, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 8, 11, 11, 11,
11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 3, 13, 3,
3, 13, 13, 13, 14, 14, 14, 14, 14, 14, 2, 14, 14, 14, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 20, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 19, 19, 19, 19, 8, 19, 19, 19, 19, 19, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25])
output of y_true
0 0
1 0
2 0
3 0
4 0
..
255 25
256 25
257 25
258 25
259 25
Name: label, Length: 260, dtype: int64
I want to get the indices with this code, and getting this value error.
predicted_classes = model.predict_classes(X_test)
y_true = data_test.iloc[:, 0]
correct = np.nonzero(predicted_classes==y_true)[0]
incorrect = np.nonzero(predicted_classes!=y_true)[0]
trace of error
ValueError Traceback (most recent call last)
in
4 #get the indices to be plotted
5 y_true = data_test.iloc[:, 0]
----> 6 correct = np.nonzero(predicted_classes!=y_true)[0]
7 incorrect = np.nonzero(predicted_classes==y_true)[0]
in nonzero(*args, **kwargs)
//anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in nonzero(a)
1894
1895 """
-> 1896 return _wrapfunc(a, 'nonzero')
1897
1898
//anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
56 bound = getattr(obj, method, None)
57 if bound is None:
---> 58 return _wrapit(obj, method, *args, **kwds)
59
60 try:
//anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds)
49 if not isinstance(result, mu.ndarray):
50 result = asarray(result)
---> 51 result = wrap(result)
52 return result
53
//anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __array_wrap__(self, result, context)
1916 return result
1917 d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
-> 1918 return self._constructor(result, **d).__finalize__(self)
1919
1920 # ideally we would define this to avoid the getattr checks, but
//anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
290 if len(index) != len(data):
291 raise ValueError(
--> 292 f"Length of passed values is {len(data)}, "
293 f"index implies {len(index)}."
294 )
ValueError: Length of passed values is 1, index implies 260.
Please let me know where I am going wrong.
A quick search reveals that an old version of the documentation advises to use .to_numpy().nonzero() as a replacement for Series.nonzero().
I am trying really hard for my function not to mess my global 'b' value because I wish to re-use that list in another similar function using sets but it seems that even not using the same name (y), they (b and y) are still bound together...
most of the print lines are for debugging only as I was not understanding what was happening.
What am I doing wrong?
import random
a = random.sample(range(1,25),8)
b = random.sample(range(1,25),11)
a.sort()
b.sort()
def list_rdup(x,y):
print('Loop remove duplicates:')
print('x:',x)
print('y:',y)
for i in x:
y.append(i)
y.sort()
print('y modified:',y)
c = []
for i in y:
if i in c:
pass
else:
c.append(i)
return c
print('a:',a)
print('b:',b)
print(list_rdup(a,b))
print('a:',a)
print('b:',b)
Output: we see a and b in their original state.. then I run the function and
print a and b again to show that b was modified in the process...
a: [1, 6, 10, 11, 12, 13, 17, 22]
b: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
Loop remove duplicates:
x: [1, 6, 10, 11, 12, 13, 17, 22]
y: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
y modified: [1, 1, 2, 3, 6, 7, 10, 11, 12, 13, 13, 16, 17, 17, 19, 20, 21, 22, 24]
[1, 2, 3, 6, 7, 10, 11, 12, 13, 16, 17, 19, 20, 21, 22, 24]
a: [1, 6, 10, 11, 12, 13, 17, 22]
b: [1, 1, 2, 3, 6, 7, 10, 11, 12, 13, 13, 16, 17, 17, 19, 20, 21, 22, 24]
The call list_rdup(a,b) simply passes the reference of a and b which are stored in x and y. So, any change in x and y will change a and b. If you do not want a and b to change make a copy by using b_copy = b.copy().
To avoid altering the elements, copy the array.
import random
a=[1, 6, 10, 11, 12, 13, 17, 22]
b=[1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
#a.sort()
#b.sort()
def list_rdup(x,y):
print('Loop remove duplicates:')
print('x:',x)
print('y:',y)
for i in x:
y.append(i)
y.sort()
print('y modified:',y)
c = []
for i in y:
if i in c:
pass
else:
c.append(i)
return c
print('a:',a)
print('b:',b)
print(list_rdup(a[:], b[:]))
print('a:',a)
print('b:',b)
a: [1, 6, 10, 11, 12, 13, 17, 22]
b: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
Loop remove duplicates:
x: [1, 6, 10, 11, 12, 13, 17, 22]
y: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
y modified: [1, 1, 2, 3, 6, 7, 10, 11, 12, 13, 13, 16, 17, 17, 19, 20, 21, 22, 24]
[1, 2, 3, 6, 7, 10, 11, 12, 13, 16, 17, 19, 20, 21, 22, 24]
a: [1, 6, 10, 11, 12, 13, 17, 22]
b: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
Also consider deep copying if you have non-primitive values in the array.
I'm trying to programm the Data Encyption Standard on my own and I'm struggling to Programm the SBoxes. I know there is already a module to encrypt and decrypt with the DES but my teacher asked to programm it myself, so here is what i have:
import random
from re import findall
class DES:
def __init__(self):
self. Eingabe=""
self.Schluessel=""
self.NachrichtBinaer=""
self.Bitslinks=""
self.Bitsrechts=""
self.Teil1=""
self.Teil2=""
self.Subkey=""
self.pcschluessel=""
self.subkeyliste=[]
self.initialpermutation=""
self.liste1=[]
self.Bits48=""
self.ausgabesbox=[]
self.liste2=[]
def EingabeNachricht(self):
self.Eingabe=input("Geben Sie ein Wort ein:")
print ("Eingegebenes Wort: ",self.Eingabe)
def Bitumwandlung(self):
for i in range(0,len(self.Eingabe)):
self.NachrichtBinaer=self.NachrichtBinaer+bin(ord(self.Eingabe[i]))
self.NachrichtBinaer=self.NachrichtBinaer.replace("b","")
if len(self.NachrichtBinaer)<64:
self.NachrichtBinaer=self.NachrichtBinaer.rjust(64,"0")
self.NachrichtBinaer="0000000100100011010001010110011110001001101010111100110111101111"
print ("Nachricht in Binaer: ",self.NachrichtBinaer)
def Teilen(self):
self.Bitslinks=self.initialpermutation[:int(len(self.initialpermutation)/2)]
self.Bitsrechts=self.initialpermutation[int(len(self.initialpermutation)/2):]
print ("Teil links: ",self.Bitslinks)
print ("Teil rechts: ",self.Bitsrechts)
def SchluesselGenerieren(self):
#self.Schluessel= getrandbits(64)
self.Schluessel="0001001100110100010101110111100110011011101111001101111111110001"
def ippermutation(self):
ip = [57, 49, 41, 33, 25, 17, 9,1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7,
56, 48, 40, 32, 24, 16, 8, 0,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6
]
for i in ip:
self.initialpermutation=self.initialpermutation+self.NachrichtBinaer[i]
print ("Erste Permutation: ",self.initialpermutation)
def Expandieren(self):
ExpandierenTabelle = [
31,0,1,2,3,4,
3,4,5,6,7,8,
7,8,9,10,11,12,
11,12,13,14,15,16,
15,16,17,18,19,20,
9,20,21,22,23,24,
3,24,25,26,27,28,
27,28,29,30,31,0]
for Elemente in ExpandierenTabelle:
self.Bits48 = self.Bits48 + self.Bitsrechts[Elemente]
print ("expandiert: ",self.Bits48)
def XOR(self,wert1,wert2):
antwort=wert1^wert2
return antwort
def SBox(self):
self.sbox=[
[[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
[0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
[4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
[15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]],
# S2
[[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
[3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
[0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
[13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]],
# S3
[[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
[13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
[13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
[1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]],
# S4
[[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
[13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
[10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
[3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]],
# S5
[[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
[14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
[4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
[11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]],
# S6
[[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
[10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
[9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
[4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]],
# S7
[[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
[13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
[1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
[6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]],
# S8
[[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
[1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
[7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
[2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]
]
for z in range(len(self.sbox)):
for y in range(len(self.sbox[z])):
for x in range(len(self.sbox[z][y])):
ausserebits=self.liste2[x][z][0]+self.liste2[x][z][-1]
innerebits=self.liste2[x][z][1:5]
print ("innere: ",innerebits)
print ("aussere: ",ausserebits)
def pc1undteilen(self):
pc1 = [56,48,40,32,24,16,8,
0,57,49,41,33,25,17,
9,1,58,50,42,34,26,
18,19,2,59,51,43,35,
62,54,46,38,30,22,14,
6,61,53,45,37,29,21,
13,5,60,52,44,36,28,
20,2,4,27,19,11,3]
#Subkey = ""
for j in pc1:
self.Subkey = self.Subkey+self.Schluessel[j]
self.Teil1=self.Subkey[:int(len(self.Subkey)/2)]
self.Teil2=self.Subkey[int(len(self.Subkey)/2):]
print("Schluessel64 :",self.Subkey)
print("SchluesselTeil1 :",self.Teil1)
print("SchluesselTeil2 :",self.Teil2)
def rotationundpc2(self):
pc2 = [
13, 16, 10, 23, 0, 4,
2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7,
15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54,
29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52,
45, 41, 49, 35, 28, 31]
rotation = [
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
k=0
#schluesselliste
self.Teil1=list(self.Teil1)
self.Teil2=list(self.Teil2)
while k<16:
#rotation
u=0
while u < rotation[k]:
self.Teil1.append(self.Teil1[0])
del self.Teil1[0]
self.Teil2.append(self.Teil2[0])
del self.Teil2[0]
self.Teil1="".join(self.Teil1)
self.Teil2="".join(self.Teil2)
self.subschluessel=self.Teil1+self.Teil2
print("Teil1: ",self.Teil1)
print("Teil2: ",self.Teil2)
print ("Subschluessel: ",self.subschluessel)
self.Teil1=list(self.Teil1)
self.Teil2=list(self.Teil2)
u+=1
#pc2 und erstellung der 16 subkeys
for index2 in pc2:
self.subkeyliste.append(self.subschluessel[index2])
k+=1
while len(self.subkeyliste)>0:
self.liste1.append("".join(self.subkeyliste[0:48]))
del self.subkeyliste[0:48]
print ("Subkeyliste: ",self.liste1)
def sechsbitunterteilung(self):
for l in range(0,16):
self.liste2.append(findall("......",listenachxor[l]))
print ("liste2: ",self.liste2)
#objekt der klasse DES wird erstellt
listenachxor=[]
Krypto=DES()
#Schluesselgenerieren
Krypto.SchluesselGenerieren()
Krypto.pc1undteilen()
Krypto.rotationundpc2()
Krypto.EingabeNachricht()
Krypto.Bitumwandlung()
Krypto.ippermutation()
Krypto.Teilen()
Krypto.Expandieren()
for p in range(0,16):
listenachxor.append(bin(Krypto.XOR(int(Krypto.liste1[p],2),int(Krypto.Bits48,2))))
listenachxor[p]=listenachxor[p].replace("b","")
print ("listenachxor: ",listenachxor)
Krypto.sechsbitunterteilung()
Krypto.SBox()
By the way the problem is on this part of the programm, the rest just works fine:
for z in range(len(self.sbox)):
for y in range(len(self.sbox[z])):
for x in range(len(self.sbox[z][y])):
ausserebits=self.liste2[x][z][0]+self.liste2[x][z][-1]
innerebits=self.liste2[x][z][1:5]
print ("innere: ",innerebits)
print ("aussere: ",ausserebits)