Posts Tagged ‘learn’

Python: Cooking App P-7

Learning : Python, Data-Base, SQlite
Subject: Writing a Cooking Application

RECIPES MENU: Delete Recipe To Delete a recipe we need to point on it, we will do this by it’s ID. So we can show all recipes by names, then we select the one we want to delete and enter it’s ID, the system will ask to confirm this action by typing (y,Y) then it we will call the delete function. In Delete function “def del_recipe()” we need to delete all the data regarding this ID form three DB Tables, recipes, recipes_steps, rec_ingredient and photo. Although we did not use the photo table and and we just create it for future use, but er will write its code.

Future Plan: In coming weeks we will convert this application “Cooking Application” to GUI “Graphical User Interface” application using tkinter library (Tkinter: is a Python binding to the Tk GUI toolkit) .

Coding: So to Delete a recipe we will show all Recipes and there ID’s sorted by it’s Name and ask the user to enter the ID of the Recipe he/she want to delete.

 # Code to Delete a Recipe

def del_recipe():
    print('\n ====== Delete a Recipe =====')

    # Start to list down all the Recipes Name.
    print("\n\n  List of ALL Recipes we have.")
    c.execute("select r_id,r_name from recipes where r_id > 0")
    for each_r_name in c.fetchall():
        print('   ID:',each_r_name[0], 'Name:',each_r_name[1])

    # Now we ask the user to Enter the Recipe ID. 
    rec_del = input('\n\n   Enter an Recipe ID to be Deleted: ')
    # Now we ask the user to Confirm Deleting Recipe.
    sure_remove = input('\n The Recipe will be DELETE and can''t be Rolled-Back.
Are you sure you want to Remove it [Y,N] ') if sure_remove in ['y','Y']: c.execute ("Delete from recipes_steps where r_id = {}".format(rec_del)) db_conn.commit() c.execute ("Delete from recipes where r_id = {}".format(rec_del)) db_conn.commit() c.execute ("Delete from rec_ingredient where r_id = {}".format(rec_del)) db_conn.commit() c.execute ("Delete from photo where r_id = {}".format(rec_del)) db_conn.commit() elif sure_remove in ['n','N']: # If the user decide not to delete and select N print('\n You select NOT to remove the Recipe.') else :# If the user enter anything else than Y or N print('\n You must select (Y or N).') input('\n .. One Recipe Removed .. Press any key .. ')

To Download my Python code (.py) files Click-Here

Follow me on Twitter..

By: Ali Radwani

Python: Machine Learning – Part 3

December 3, 2019 Leave a comment

Learning :Python and Machine Learning Part 3
Subject: Implementation and saving ML-Model

After creating a data-set and use it to train a ML model and make sure that it works fine and give a height accuracy predictions (Click here to read: Python and Machine Learning Part 2 ), we may or say we need to keep this model trained and re-use it on any actual data. In many real-life ML to training the model may take time with huge train data in image recognition or voice recognition models, so we need to keep the model trained even if we exit the application. To do this in sklearn we will use the “Model persistence” document page and use the joblib serialization.

First we need to import joblib , also import so to print out the file name and the path, we will use two functions in joblib (dump and load) in save_trained_model we will use the dump. Her is the code.

 # Function to save a trained ML-Model

  import joblib, os  # To Import  joblib and os
  def save_trained_model(model_name):
      print('\n  You select to save the trained ML model.')
      ml_name = input('  Enter a file name: ')
      joblib.dump(model_name, ml_name)
      print('\n  --> ML Model been saved.\n')
      print('   File Name is :',ml_name)  # To print-out the file name 
      print('   File Path is :',os.path.abspath(ml_name))  # To print-out the file path
      print('\n\n Do you want to save the ML trained Model? (Y,N): ' )
      if input('') in ['y','Y'] :

Now after we save our trained ML-Model we want to load it and use it in our ML program without training our machine. I will use the function new_test_data() from part 2 and pass the ML trained model to it. And to do this, first we need to load the trained ML-Mode. So let’s do it.

 # Function to load trained ML-Model
def load_ML_Model(ML_filename):
    the_trained_model= joblib.load(ML_filename)
    return the_trained_model

# we call the function in the main application code.
ML_model = load_ML_Model(ML_t_model_filename)

And now we will call our new_test_data() function and pass ML_model to see the prediction.

 # Function to load trained ML-Model

def new_test_data(ML_model):
    print('---------  START PREDICTION  for New Data Set ---------')
    print('\n   In this function a new data set will be generated, ')
    print('  and a trained ML-Model for "mouse on the coordinate plane" ')
    print('  will be loaded from the disk. So we will not train the Model.')
    #print('  So we will not train the Model. ')
    #print('  will use the IF loops.')
    new_data_size = 1000 
    new_data_range = 100
    print('\n\n  The new data range is {}, and the new data size is {}.'.format(new_data_range,new_data_size))
    # generate new data 
    new_test_data1= []
    for x in range (new_data_size):
    print('\n  This is the prediction for the New Data set..\n')
    # Do prediction using ML_model.
    prediction = ML_model.predict(new_test_data1)
    cot = 0
    # check the predictions accuracy .
    for i in range (len(prediction)) :
        if prediction[i] =='Up_r':
          if ((new_test_data1[i][0]) > 0 and (new_test_data1[i][1]) > 0) :
            cot = cot + 1
        elif  prediction[i] =='Up_l':
          if ((new_test_data1[i][0])  0) :
            cot = cot + 1
        elif  prediction[i] =='D_r':
          if ((new_test_data1[i][0]) > 0 and (new_test_data1[i][1]) < 0) :
            cot = cot + 1
        elif  prediction[i] =='D_l':
          if ((new_test_data1[i][0]) < 0 and (new_test_data1[i][1]) < 0) :
            cot = cot + 1
    print('\n  We count {} correct prediction out of {} Instances.'.format(cot,(new_data_size)))
    print('\n  The Accuracy is:',round((cot/len(prediction))*100,3),'%')


To Download my Python code (.py) files Click-Here

Follow me on Twitter..

By: Ali Radwani