Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

Python ?stack overflow” (104 if statements). Is def(x) the only solution to optimise code?

Today have tried to check files with path directory name. Previously it worked, until I tried to create 104 if/else statements.

How to dispose of this overflow error? More specific question: Does def(x) for several checks the only one solution to optimise such cases in Python?enter image description here

import turicreate as turi

url = "images/"
data = turi.image_analysis.load_images(url)

data["moneyType"] = data["path"].apply(lambda path: 
                                                 "ARS_1" if "ARS_1" in path
                                            else("ARS_2" if "ARS_2" in path
                                            else("ARS_5" if "ARS_5" in path
                                            else("ARS_10" if "ARS_10" in path
                                            else("ARS_20" if "ARS_20" in path
                                            else("ARS_50" if "ARS_50" in path
                                            else("ARS_100" if "ARS_100" in path
                                            else("ARS_200" if "ARS_200" in path
                                            else("ARS_500" if "ARS_500" in path
                                            else("ARS_1000" if "ARS_1000" in path

else("AED_5" if "AED_5" in path
else("AED_10" if "AED_10" in path
else("AED_20" if "AED_20" in path
else("AED_50" if "AED_50" in path
else("AED_100" if "AED_100" in path
else("AED_200" if "AED_200" in path
else("AED_500" if "AED_500" in path
else("AED_1000" if "AED_1000" in path

else("BRL_1" if "BRL_1" in path
else("BRL_2" if "BRL_2" in path
else("BRL_5" if "BRL_5" in path
else("BRL_10" if "BRL_10" in path
else("BRL_20" if "BRL_20" in path
else("BRL_50" if "BRL_50" in path
else("BRL_100" if "BRL_100" in path

else("CLP_1000" if "CLP_1000" in path
else("CLP_2000" if "CLP_2000" in path
else("CLP_5000" if "CLP_5000" in path
else("CLP_10000" if "CLP_10000" in path
else("CLP_20000" if "CLP_20000" in path

else("COP_1000" if "COP_1000" in path
else("COP_2000" if "COP_2000" in path
else("COP_5000" if "COP_5000" in path
else("COP_10000" if "COP_10000" in path
else("COP_20000" if "COP_20000" in path
else("COP_50000" if "COP_50000" in path
else("COP_100000" if "COP_100000" in path

else("CUC_1" if "CUC_1" in path
else("CUC_3" if "CUC_3" in path
else("CUC_5" if "CUC_5" in path
else("CUC_10" if "CUC_10" in path
else("CUC_20" if "CUC_20" in path
else("CUC_50" if "CUC_50" in path
else("CUC_100" if "CUC_100" in path
else("CUC_200" if "CUC_200" in path
else("CUC_500" if "CUC_500" in path
else("CUC_1000" if "CUC_1000" in path

else("EGP_1" if "EGP_1" in path
else("EGP_5" if "EGP_5" in path
else("EGP_10" if "EGP_10" in path
else("EGP_20" if "EGP_20" in path
else("EGP_25pt" if "EGP_25pt" in path
else("EGP_50" if "EGP_50" in path
else("EGP_50pt" if "EGP_50pt" in path
else("EGP_100" if "EGP_100" in path
else("EGP_200" if "EGP_200" in path

else("INR_1" if "INR_1" in path
else("INR_2" if "INR_2" in path
else("INR_5" if "INR_5" in path
else("INR_10" if "INR_10" in path
else("INR_20" if "INR_20" in path
else("INR_50" if "INR_50" in path
else("INR_100" if "INR_100" in path
else("INR_200" if "INR_200" in path
else("INR_500" if "INR_500" in path
else("INR_2000" if "INR_2000" in path

else("JOD_1" if "JOD_1" in path
else("JOD_5" if "JOD_5" in path
else("JOD_10" if "JOD_10" in path
else("JOD_20" if "JOD_20" in path
else("JOD_50" if "JOD_50" in path

else("KHR_50" if "KHR_50" in path
else("KHR_100" if "KHR_100" in path
else("KHR_500" if "KHR_500" in path
else("KHR_1000" if "KHR_1000" in path
else("KHR_2000" if "KHR_2000" in path
else("KHR_5000" if "KHR_5000" in path
else("KHR_10000" if "KHR_10000" in path
else("KHR_20000" if "KHR_20000" in path
else("KHR_50000" if "KHR_50000" in path
else("KHR_100000" if "KHR_100000" in path

else("LAK_500" if "LAK_500" in path
else("LAK_1000" if "LAK_1000" in path
else("LAK_2000" if "LAK_2000" in path
else("LAK_5000" if "LAK_5000" in path
else("LAK_10000" if "LAK_10000" in path
else("LAK_20000" if "LAK_20000" in path
else("LAK_50000" if "LAK_50000" in path
else("LAK_100000" if "LAK_100000" in path

else("LKR_10" if "LKR_10" in path
else("LKR_20" if "LKR_20" in path
else("LKR_50" if "LKR_50" in path
else("LKR_100" if "LKR_100" in path
else("LKR_200" if "LKR_200" in path
else("LKR_500" if "LKR_500" in path
else("LKR_1000" if "LKR_1000" in path
else("LKR_2000" if "LKR_2000" in path
else("LKR_5000" if "LKR_5000" in path

else("MAD_20" if "MAD_20" in path
else("MAD_50" if "MAD_50" in path
else("MAD_100" if "MAD_100" in path
else("MAD_200" if "MAD_200" in path

else("MMK_1" if "MMK_1" in path
else("MMK_5" if "MMK_5" in path
else("MMK_10" if "MMK_10" in path
else("MMK_20" if "MMK_20" in path
else("MMK_50" if "MMK_50" in path
else("MMK_100" if "MMK_100" in path
else("MMK_200" if "MMK_200" in path
else("MMK_500" if "MMK_500" in path
else("MMK_1000" if "MMK_1000" in path
else("MMK_5000" if "MMK_5000" in path
else("MMK_10000" if "MMK_10000" in path

else("MNT_1" if "MNT_1" in path
else("MNT_5" if "MNT_5" in path
else("MNT_10" if "MNT_10" in path
else("MNT_20" if "MNT_20" in path
else("MNT_50" if "MNT_50" in path
else("MNT_100" if "MNT_100" in path
else("MNT_500" if "MNT_500" in path
else("MNT_1000" if "MNT_1000" in path
else("MNT_5000" if "MNT_5000" in path
else("MNT_10000" if "MNT_10000" in path
else("MNT_20000" if "MNT_20000" in path

else("MXN_20" if "MXN_20" in path
else("MXN_50" if "MXN_50" in path
else("MXN_100" if "MXN_100" in path
else("MXN_200" if "MXN_200" in path
else("MXN_500" if "MXN_500" in path
else("MXN_1000" if "MXN_1000" in path

else("USD" if "USD" in path

else("NPR_1" if "NPR_1" in path
else("NPR_2" if "NPR_2" in path
else("NPR_5" if "NPR_5" in path
else("NPR_10" if "NPR_10" in path
else("NPR_20" if "NPR_20" in path
else("NPR_25" if "NPR_25" in path
else("NPR_50" if "NPR_50" in path
else("NPR_100" if "NPR_100" in path
else("NPR_200" if "NPR_200" in path
else("NPR_250" if "NPR_250" in path
else("NPR_500" if "NPR_500" in path
else("NPR_1000" if "NPR_1000" in path

else("QAR_1" if "QAR_1" in path
else("QAR_5" if "QAR_5" in path
else("QAR_10" if "QAR_10" in path
else("QAR_50" if "QAR_50" in path
else("QAR_100" if "QAR_100" in path
else("QAR_500" if "QAR_500" in path

else("RUB_5" if "RUB_5" in path
else("RUB_10" if "RUB_10" in path
else("RUB_50" if "RUB_50" in path
else("RUB_100" if "RUB_100" in path
else("RUB_200" if "RUB_200" in path
else("RUB_500" if "RUB_500" in path
else("RUB_1000" if "RUB_1000" in path
else("RUB_2000" if "RUB_2000" in path
else("RUB_5000" if "RUB_5000" in path

else("THB_20" if "THB_20" in path
else("THB_50" if "THB_50" in path
else("THB_100" if "THB_100" in path
else("THB_500" if "THB_500" in path
else("THB_1000" if "THB_1000" in path

else("VND_100" if "VND_100" in path
else("VND_200" if "VND_200" in path
else("VND_500" if "VND_500" in path
else("VND_1000" if "VND_1000" in path
else("VND_2000" if "VND_2000" in path
else("VND_5000" if "VND_5000" in path
else("VND_10000" if "VND_10000" in path
else("VND_20000" if "VND_20000" in path
else("VND_50000" if "VND_50000" in path
else("VND_100000" if "VND_100000" in path
else("VND_200000" if "VND_200000" in path
else("VND_500000" if "VND_500000" in path

else "NONE")))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))) )))))))))) ))))) )))))))))) ))))))))) )))))))))) ))))))) ))))) ))))))) )))))))) ))))))))))

data.save("money.sframe")
data.explore()

dataBuffer = turi.SFrame("money.sframe")
trainingBuffers, testingBuffers = dataBuffer.random_split(0.8)

model = turi.image_classifier.create(trainingBuffers, target="moneyType", model="resnet-50")

evaluations = model.evaluate(testingBuffers)
print evaluations["accuracy"]

model.save("money.model")

model.export_coreml("MoneyClassifier.mlmodel")

Generally saying there is turi machine learning module that helps to train object classification MLmodel. I've collected 15000 images and now I try to make this learn these categories (200+). Previously I worked with 5-10 only. However, now I wonder how to implement 100+ categories, because right now for if/else statement technically it is impossible?

As I understood: the Good solution is a loop, but Internet resources use mostly lists for files with extensions. Moreover, I agree that it should be done with DataFrames, but how? I don't understand their possibilities and usefulness.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have found the optimal solution: To use def(x) it's the best solution now. (loop)

list = ['enfj','enfp','entj','entp','esfj','esfp','estj','estp','infj','infp','intj','intp','isfj','isfp','istj','istp']

def get_label(path, list=list):
  for psychoType in list:
       if psychoType in path:
           return psychoType

data["psychoType"] = data["path"].apply(get_label)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...