Can't convert a string into a list of integers - python-3.x

I am trying to make a program in python that identifies whether a square is a magic square or not and i am having trouble getting the user input into a list. I understand that my code could be more efficient but I am very new to python.
column_1 = (0,3)
column_2 = (0,3)
column_3 = (0,3)
column_4 = (0,3)
row_1 = [int(i) for i in input('input row 1 with spaces inbetween numbers: ').split(' ')]
row_2 = [int(i) for i in input('input row 2 with spaces inbetween numbers: ').split(' ')]
row_3 = [int(i) for i in input('input row 3 with spaces inbetween numbers: ').split(' ')]
row_4 = [int(i) for i in input('input row 4 with spaces inbetween numbers: ').split(' ')]
column_1[0].append(row_1[0])
column_1[1].append(row_2[0])
column_1[2].append(row_3[0])
column_1[3].append(row_4[0])
column_2[0] = row_1[1]
column_2[1] = row_2[1]
column_2[2] = row_3[1]
column_2[3] = row_4[1]
column_3[0] = row_1[2]
column_3[1] = row_2[2]
column_3[2] = row_3[2]
column_3[3] = row_4[2]
column_4[0] = row_1[3]
column_4[1] = row_2[3]
column_4[2] = row_3[3]
column_4[3] = row_4[3]
diagonal_left_to_right[0] = column_1[0]
diagonal_left_to_right[1] = column_2[1]
diagonal_left_to_right[2] = column_3[2]
diagonal_left_to_right[3] = column_4[3]
diagonal_right_to_left[0] = column_4[0]
diagonal_right_to_left[1] = column_3[1]
diagonal_right_to_left[2] = column_2[2]
diagonal_right_to_left[3] = column_1[3]
sum_row_1 = sum(row_1)
sum_row_2 = sum(row_2)
sum_row_3 = sum(row_3)
sum_row_4 = sum(row_4)
sum_col_1 = sum(column_1)
sum_col_2 = sum(column_2)
sum_col_3 = sum(column_3)
sum_col_4 = sum(column_4)
sum_dag_l2r = sum(diagonal_left_to_right)
sum_dag_r2l = sum(diagonal_right_to_left)
if sum_row_1 == sum_row_2 == sum_row_3 == sum_row_4 == sum_col_1 == sum_col_2 == sum_col_3 == sum_col_4 == sum_dag_r2l == sum_dag_l2r:
print('magic')
else:
print('not magic')
I keep getting error messages that 'int' object has no attribute 'append'
I have tried a lot of different methods that I found on this website and none of them have worked for various reasons.
I am open to all suggestions, anything will help me.
Thanks

You first define column_1 as tuple (with 2 integer values, one at index 0 and one at index 1). The append method cannot work on column_1[0], which is like doing 0.append(). You probably did not intend to create a tuple, but a list with certain dimensions.
You can assign the values to columns and diagonals with this list notation:
column_1 = [row_1[0], row_2[0], row_3[0], row_4[0]]
column_2 = [row_1[1], row_2[1], row_3[1], row_4[1]]
column_3 = [row_1[2], row_2[2], row_3[2], row_4[2]]
column_4 = [row_1[3], row_2[3], row_3[3], row_4[3]]
diagonal_left_to_right = [column_1[0],column_2[1],column_3[2],column_4[3]]
diagonal_right_to_left = [column_4[0],column_3[1],column_2[2], column_1[3]]

Related

why I have output like this for make pair in siamese network?

This is my code, and I take the output that illustrates the below. Len of idx is 1000. I want to make image pairs and label pairs, but I take errors like this:
Code:
pair_images = []
pair_labels = []
new_labels = []
for k in labels:
new_labels.append(int(k))
numClasses = len(np.unique(new_labels))
new_labels = np.array(new_labels)
idx = [np.where(new_labels == i)[0] for i in range(0,numClasses)]
for idxA in range(len(images)):
# Make Posetive Images
currentImage = images[idxA]
label = new_labels[idxA]
idxB = np.random.choice(idx[label])
posImage = images[idxB]
output:
idxB = np.random.choice(idx[label])
IndexError: list index out of range

Print REGEX using USER DEFINED FUNCTION

I'm trying to print the variables ccb_3, nome, data, taxa and parcela using the function I defined as "ext_ccb", but when I run the code it returns 3 times (because I defined q as 3) the variable ccb_3.
I tried splitting it into 2 functions (one with the variable ccb_3 e one with the rest that uses REGEX) but it didn't worked to.
'''
from PyPDF2 import PdfFileReader, PdfFileWriter
import re
x = 1
q = 3
def ext_ccb():
nome_ccb = str("Vazio (" + y + ").pdf")
ccb = PdfFileReader(nome_ccb)
ccb_obj_1 = ccb.getPage(0)
ccb_text_1 = ccb_obj_1.extractText()
ccb_obj_2 = ccb.getPage(1)
ccb_text_2 = ccb_obj_2.extractText()
ccb_3 = ccb_text_1[1:8]
print(ccb_3)
pattern_nome = re.compile(r'''[^\n][^CPF][A-Z](|\.)\w*\s*.*$
Nome Completo
''', re.M)
matches_nome = pattern_nome.finditer(ccb_text_1)
for match in matches_nome:
nome = str(match)
nome = nome[40:].replace(r"\n\nNome Completo\n'>", "")
print(nome)
pattern_data = re.compile(r'''5\.2\. Modalidade
\d{2}/\d{2}/\d{4}
''')
matches_data = pattern_data.findall(ccb_text_1)
for match in matches_data:
data = match[17:27]
print(data)
pattern_taxa = re.compile(r'''Taxa de Juros a\.m\. \(%\)
\d*,\d*''')
matches_taxa = pattern_taxa.findall(ccb_text_2)
for match in matches_taxa:
taxa = match[24:]
print(taxa)
pattern_vparcela = re.compile(r'''Valor das Parcelas
R\$ \d*,\d*''')
matches_vparcela = pattern_vparcela.findall(ccb_text_2)
for match in matches_vparcela:
parcela = match[23:]
print(parcela)
while x <= q:
y = str(x)
x += 1
ext_ccb()
'''
What I really need is to insert it into an csv, multiple times from different PDF's, which I already have the code for:
'''
from csv import writer
x = 5
q = 0
while q < x:
q += 1
ccb_3 += 1
nome += 2
data += 4
taxa += 4
parcela += 5
list_data = [ccb_3, nome, data, taxa, parcela]
with open('csv_teste.csv', 'a', newline = '') as f_object:
writer_object = writer(f_object)
writer_object.writerow(list_data)
f_object.close()
'''
How can I save each data from each PDF and put it into the CSV?

Pandas Group By Weird Behaviour

def quant(X,col_):
print('X\n',X.head(5))
q25 = np.quantile(X[col_],0.15)
q75 = np.quantile(X[col_],0.85)
total = X[col_].tolist()
ltq = []
mtq = []
iqr = []
for i in total:
if i < q25:
ltq.append(i)
elif i > q75:
mtq.append(i)
else:
iqr.append(i)
p_l_q = 100*(len(ltq))/len(total)
p_l_m = 100*(len(mtq))/len(total)
percent_iqr = 100*len((iqr))/len(total)
X['p_l_q'] = p_l_q
X['p_l_m'] = p_l_m
X['p_l_i'] = percent_iqr
X['count'] = len(total)
X_short = X[['p_l_q','p_l_m','p_l_i','count']].copy(deep = True)
print(X_short[:1])
new = X_short[:1]
return new
X = pd.DataFrame()
X['G'] = ''
X['H'] = ''
X['M'] = ''
lst1 = ['a','a','a','a','a','b','b','b','b','b','b','c','c','c','c']
lst2 = [10,12,13,45,52,34,78,34,56,79,90,65,56,43,11]
lst3 = [1,2,3,4,5,1,2,3,4,5,6,1,2,3,4]
X['G'] = lst1
X['H'] = lst2
X['M'] = lst3
X_q = X.groupby('G').apply(quant,'H').reset_index()
I have used a print statement to give me head of dataframe block for each unique 'G' but I get weird print like in image.
There should be exactly three print outputs.(for each unqiue G) but it is showing 5 on top of that second print output (G='b') has H values as that of G ='a'.
Try replacing this:
def quant(X,col_):
print('X\n',X.head(5))
With this:
def quant(XX,col_):
X = XX.copy()
print('X\n',XX.head(5))
del XX # Delete 'XX' because 'X' copy is available
Output

Calculating entropy in ID3 log2(0) in formula

import numpy as np
udacity_set = np.array(
[[1,1,1,0],
[1,0,1,0],
[0,1,0,1],
[1,0,0,1]])
label = udacity_set[:,udacity_set.shape[1]-1]
fx = label.size
positive = label[label == 1].shape[0]
positive_probability = positive/fx
negative = label[label == 0].shape[0]
negative_probability = negative/fx
entropy = -negative_probability*np.log2(negative_probability) - positive_probability*np.log2(positive_probability)
atribute = 0
V = 1
attribute_set = udacity_set[np.where(udacity_set[:,atribute] == 1)] #selecting positive instance of occurance in attribute 14
instances = attribute_set.shape[0]
negative_labels = attribute_set[np.where(attribute_set[:,attribute_set.shape[1]-1]== 0)].shape[0]
positive_labels = attribute_set[np.where(attribute_set[:,attribute_set.shape[1]-1]== 1)].shape[0]
p0 = negative_labels/instances
p1 = positive_labels/instances
entropy2 = - p0*np.log2(p0) - p1*np.log2(p1)
attribute_set2 = udacity_set[np.where(udacity_set[:,atribute] == 0)] #selecting positive instance of occurance in attribute 14
instances2 = attribute_set2.shape[0]
negative_labels2 = attribute_set[np.where(attribute_set2[:,attribute_set2.shape[1]-1]== 0)].shape[0]
positive_labels2 = attribute_set[np.where(attribute_set2[:,attribute_set2.shape[1]-1]== 1)].shape[0]
p02 = negative_labels2/instances2
p12 = positive_labels2/instances2
entropy22 = - p02*np.log2(p02) - p12*np.log2(p12)
Problem is when attribute is pure and entropy is meant to be 0. But when i put this into a formula i get NaN. I know how to code workaround, but why is this formula rigged?

Sklearn DecisionTreeClassifier.tree_.value output floats

I am using sklearn DecisionTreeClassifier to predict between two classes.
clf = DecisionTreeClassifier(class_weight='balanced', random_state=SEED)
params = {'criterion':['gini','entropy'],
'max_leaf_nodes':[100,1000]
}
grid = GridSearchCV(estimator=clf,param_grid=params, cv=SKF,
scoring=scorer,
n_jobs=-1, verbose=5)
trans_df = pipe.fit_transform(df.drop(["out"], axis=1))
grid.fit(trans_df, df['out'].fillna(0))
I need to output the tree for analysis.
No problem until there, I am going through all nodes and get the rules following more or less this answer.
def tree_to_flat(tree, feature_names):
tree_ = tree.tree_
feature_name = [
feature_names[i] if i != _tree.TREE_UNDEFINED else "undefined!"
for i in tree_.feature
]
positions = []
def recurse(node, depth, position=OrderedDict()):
indent = " " * depth
if tree_.feature[node] != _tree.TREE_UNDEFINED:
name = feature_name[node]
threshold = tree_.threshold[node]
lname = name
ldict = {key:value for (key,value) in position.items()}
ldict[lname] = '<=' + str(threshold)
rname = name
rdict = {key:value for (key,value) in position.items()}
rdict[rname] = '>' + str(threshold)
recurse(tree_.children_left[node], depth + 1, ldict)
recurse(tree_.children_right[node], depth + 1, rdict)
else:
position['value'] = tree_.value[node]
positions.append(position)
return position
recurse(0, 1)
return positions
If I look at the different values, they are all non integer, like [[296.727705967, 104.03070761]]. The 104.03 is close to the number of instances in the node in total (104).
My understanding was that tree_.value[node] gives the number of instances in the two classes. How can I end up with non integer numbers?
Thanks in advance

Resources