The function returns the time data of the location when:
- data capture
- sunrise
- sunset
Function Definition: time(capture_time = "sunrise")
Name | Type | Description | Expected Values | Default Value |
---|---|---|---|---|
capture_time | string | The event for which time is required. | "sunrise", "sunset", or "data capture" | "sunrise" |
The function returns the time data of the location when:
A humanoid is a type of robot or artificial system that is designed to resemble a human being in its form and functionality. Humanoid robots are typically equipped with sensors, actuators, and artificial intelligence capabilities that enable them to interact with their environment and perform tasks that are similar to those of a human being. Humanoid robots are used in various fields, including robotics research, entertainment, healthcare, education, and customer service.
The Humanoid will move according to the following logic:
The program uses the up, down, left, and right arrows to control the robot and make it move forward, backward, left, and right. Every time you press one of the arrows, Humanoid will move in the direction you choose.
sprite = Sprite('Tobi')
quarky = Quarky()
import time
humanoid = Humanoid(7, 2, 6, 3, 8, 1)
while True:
if sprite.iskeypressed("up arrow"):
humanoid.move("forward", 1000, 1)
time.sleep(1)
if sprite.iskeypressed("down arrow"):
humanoid.move("backward", 1000, 1)
if sprite.iskeypressed("left arrow"):
humanoid.move("left", 1000, 1)
if sprite.iskeypressed("right arrow"):
humanoid.move("right", 1000, 1)
In this example project, we are going to create a Machine Learning Model where shark run by the user and fish automatically feed on randomly generated food while escaping from sharks.
sprite = Sprite('Fish')
sprite1 = Sprite('Orange')
sprite2 = Sprite('Shark 2')
sprite3 = Sprite('Button3')
import random
import time
import tensorflow as tf
import pandas as pd
import os
import math
curr_x = 25
curr_y = 108
shark_x=-177
shark_y=116
score=0
chance=5
fish_d=20
fish_m=25
shark_m=4
angle_f=90
angle_s=90
sprite.setx(curr_x)
sprite.sety(curr_y)
sprite2.setx(shark_x)
sprite2.sety(shark_y)
sprite.setdirection(DIRECTION=angle_f)
sprite2.setdirection(DIRECTION=angle_s)
def settarget(t):
x = random.randrange(-200, 200, t)
y = random.randrange(-155, 155, t)
x1 = random.randrange(-200, 200, t)
y1 = random.randrange(-155, 155, t)
x2 = random.randrange(-200, 200, t)
y2 = random.randrange(-155, 155, t)
time.sleep(0.1)
sprite1.setx(x1)
sprite1.sety(y1)
sprite.setx(x)
sprite.sety(y)
sprite2.setx(x2)
sprite2.sety(y2)
return x, y, x1, y1, x2, y2
def settarget1(m):
x = random.randrange(-200, 200, m)
y = random.randrange(-155, 155, m)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget(40)
target_x, target_y = settarget1(40)
if(os.path.isfile('Chase_Data.csv')):
data=pd.read_csv('Chase_Data.csv')
else:
data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": angle_f, "direction_s": angle_s, "Action": "RIGHT"}, index=[0])
while True:
sprite2.spriteRequest.requestCommand("motion_pointtowards", {"TOWARDS": "Fish"})
sprite2.move(shark_m)
angle_f=sprite.direction()
angle_s=sprite2.direction()
anglef=math.floor(angle_f)
angles=math.floor(angle_s)
if sprite.iskeypressed("up arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "UP"}, ignore_index=True)
sprite.move(fish_m)
if sprite.iskeypressed("left arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "LEFT"}, ignore_index=True)
angle = anglef - fish_d
sprite.setdirection(DIRECTION=angle)
if sprite.iskeypressed("right arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "RIGHT"}, ignore_index=True)
angle = anglef + fish_d
sprite.setdirection(DIRECTION=angle)
if(score>0 and score%2==0):
data.to_csv('Chase_Data.csv',index=False)
if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
chance= chance-1
if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
chance= chance-1
curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
sprite3.say(("score: ",score ," and chance: ",chance,""))
if (chance == 0):
data.to_csv('Chase_Data.csv',index=False)
curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
chance=5
if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
score = score + 1
sprite.say(("your score is: {}".format(score)))
if (score >= 40):
print(data)
data.to_csv('Chase Data.csv')
break
target_x, target_y = settarget1()
curr_x=math.floor(sprite.x())
curr_y=math.floor(sprite.y())
shark_x=math.floor(sprite2.x())
shark_y=math.floor(sprite2.y())
time.sleep(0.02)
sprite = Sprite('Fish')
sprite1 = Sprite('Orange')
sprite2 = Sprite('Shark 2')
sprite3 = Sprite('Button3')
import random
import time
import numpy as np
import tensorflow as tf
import pandas as pd
import os
import math
curr_x = 25
curr_y = 108
shark_x=-177
shark_y=116
score=0
chance=5
fish_d=20
fish_m=25
shark_m=4
angle_f=90
angle_s=90
sprite3.say(("score: ",score ," and chance: ",chance,""))
sprite.setx(curr_x)
sprite.sety(curr_y)
sprite2.setx(shark_x)
sprite2.sety(shark_y)
sprite.setdirection(DIRECTION=angle_f)
sprite2.setdirection(DIRECTION=angle_s)
def settarget(t):
x = random.randrange(-200, 200, t)
y = random.randrange(-155, 155, t)
x1 = random.randrange(-200, 200, t)
y1 = random.randrange(-155, 155, t)
x2 = random.randrange(-200, 200, t)
y2 = random.randrange(-155, 155, t)
time.sleep(0.1)
sprite1.setx(x1)
sprite1.sety(y1)
sprite.setx(x)
sprite.sety(y)
sprite2.setx(x2)
sprite2.sety(y2)
return x, y, x1, y1, x2, y2
def settarget1(m):
x = random.randrange(-200, 200, m)
y = random.randrange(-155, 155, m)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget1(40)
if(os.path.isfile('Chase_Data.csv')):
data=pd.read_csv('Chase_Data.csv')
else:
data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": angle_f, "direction_s": angle_s, "Action": "RIGHT"}, index=[0])
while True:
# sprite2.pointto()
sprite2.spriteRequest.requestCommand("motion_pointtowards", {"TOWARDS": "Fish"})
sprite2.move(shark_m)
angle_f=sprite.direction()
angle_s=sprite2.direction()
anglef=math.floor(angle_f)
angles=math.floor(angle_s)
if sprite.iskeypressed("up arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "UP"}, ignore_index=True)
sprite.move(fish_m)
if sprite.iskeypressed("left arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "LEFT"}, ignore_index=True)
angle = anglef - fish_d
sprite.setdirection(DIRECTION=angle)
if sprite.iskeypressed("right arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "RIGHT"}, ignore_index=True)
angle = anglef + fish_d
sprite.setdirection(DIRECTION=angle)
if(score>0 and score%2==0):
data.to_csv('Chase_Data.csv',index=False)
if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
chance= chance-1
curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
sprite3.say(("score: ",score ," and chance: ",chance,""))
if (chance == 0):
data.to_csv('Chase_Data.csv',index=False)
curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
chance=5
if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
score = score + 1
sprite3.say(("score: ",score ," and chance: ",chance,""))
if (score >= 50):
data.to_csv('Chase_Data.csv',index=False)
break
target_x, target_y = settarget1(40)
curr_x=math.floor(sprite.x())
curr_y=math.floor(sprite.y())
shark_x=math.floor(sprite2.x())
shark_y=math.floor(sprite2.y())
time.sleep(0.02)
Datasets on the internet are hardly ever fit to directly train on. Programmers often have to take care of unnecessary columns, text data, target columns, correlations, etc. Thankfully, PictoBlox’s ML Environment is packed with features to help us pre-process the data as per our liking.
Let’s create the ML model.
Alert: The Machine Learning Environment for model creation is available in the only desktop version of PictoBlox for Windows, macOS, or Linux. It is not available in Web, Android, and iOS versions.
Follow the steps below:
Datasets can either be uploaded or created on the ML Environment. Lets see how it is done.
Notes:
After data is pre-processed and optimized, it’s fit to be used in model training. To train the model, simply click the “Train Model” button found in the “Training” panel.
By training the model, meaningful information is extracted from the numbers, and that in turn updates the weights. Once these weights are saved, the model can be used to make predictions on data previously unseen.
The model’s function is to use the input data and predict the output. The target column must always contain numbers.
However, before training the model, there are a few hyperparameters that need to be understood. Click on the “Advanced” tab to view them.
There are three hyperparameters that can be altered in the Numbers(C/R) Extension:
It’s a good idea to train a numeric classification model for a high number of epochs. The model can be trained in both JavaScript and Python. In order to choose between the two, click on the switch on top of the Training panel.
The accuracy of the model should increase over time. The x-axis of the graph shows the epochs, and the y-axis represents the accuracy at the corresponding epoch.
A window will open. Type in a project name of your choice and select the “Numbers(C/R)” extension. Click the “Create Project” button to open the Numbers(C/R) window.
To test the model, simply enter the input values in the “Testing” panel and click on the “Predict” button.
The model will return the probability of the input belonging to the classes.
Click on the “PictoBlox” button, and PictoBlox will load your model into the Python Coding Environment if you have opened the ML Environment in Python Coding.
sprite = Sprite('Fish')
sprite1 = Sprite('Orange')
sprite2 = Sprite('Shark 2')
sprite3 = Sprite('Button3')
import random
import time
import tensorflow as tf
import pandas as pd
import os
import math
curr_x = 25
curr_y = 108
shark_x=-177
shark_y=116
score=0
chance=5
fish_d=20
fish_m=35
shark_m=25
shark_d= 20
angle_f=90
angle_s=90
sprite.setx(curr_x)
sprite.sety(curr_y)
sprite2.setx(shark_x)
sprite2.sety(shark_y)
sprite.setdirection(DIRECTION=angle_f) sprite2.setdirection(DIRECTION=angle_s)
def settarget1(t):
x = random.randrange(-200, 200, t)
y = random.randrange(-155, 155, t)
x1 = random.randrange(-200, 200, t)
y1 = random.randrange(-155, 155, t)
x2 = random.randrange(-200, 200, t)
y2 = random.randrange(-155, 155, t)
time.sleep(0.1)
sprite1.setx(x1)
sprite1.sety(y1)
sprite.setx(x)
sprite.sety(y)
sprite2.setx(x2)
sprite2.sety(y2)
return x, y, x1, y1, x2, y2
def settarget(m):
x = random.randrange(-200, 200, m)
y = random.randrange(-155, 155, m)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget(40)
def runprediction(diff_x, diff_y, diff_x1, diff_y1, ang1, ang2):
inputValue=[diff_x, diff_y, diff_x1, diff_y1, ang1, ang2]
#Input Tensor
inputTensor = tf.expand_dims(inputValue, 0)
#Predict
predict = model.predict(inputTensor)
predict_index = np.argmax(predict[0], axis=0)
#Output
predicted_class = class_list[predict_index]
return predicted_class
While True :
if sprite.iskeypressed("up arrow"):
sprite2.move(shark_m)
shark_x=sprite2.x()
shark_y=sprite2.y()
if sprite.iskeypressed("left arrow"):
angles = angle_s - shark_d
sprite2.setdirection(DIRECTION=angles)
if sprite.iskeypressed("right arrow"):
angles = angle_s + shark_d
sprite2.setdirection(DIRECTION=angles)
angle_f=sprite.direction()
angle_s=sprite2.direction()
move = runprediction(curr_x- target_x, curr_y-target_y, shark_x-curr_x, shark_y-curr_y, angle_f, angle_s)
if move == "UP":
sprite.move(fish_m)
curr_x=sprite.x()
curr_y=sprite.y()
if move == "LEFT":
angle = angle_f - fish_d
sprite.setdirection(DIRECTION=angle)
if move == "RIGHT":
angle = angle_f + fish_d
sprite.setdirection(DIRECTION=angle)
if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
chance= chance-1
if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
chance= chance-1
curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget1(40)
sprite3.say(("score: ",score ," and chance: ",chance,""))
if (chance == 0):
chance=5
if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
score = score + 1
sprite.say(("your score is: {}".format(score)))
target_x, target_y = settarget(4)
sprite = Sprite('Fish')
sprite1 = Sprite('Orange')
sprite2 = Sprite('Shark 2')
sprite3 = Sprite('Button3')
import random
import time
import numpy as np
import tensorflow as tf
import pandas as pd
import os
import math
#Load Number Model
model= tf.keras.models.load_model(
"num_model.h5",
custom_objects=None,
compile=True,
options=None)
#List of classes
class_list = ['UP','RIGHT','LEFT',]
curr_x = 25
curr_y = 108
shark_x=-177
shark_y=116
score=0
chance=5
fish_d=20
fish_m=35
shark_m=25
shark_d=20
angle_f=90
angle_s=90
sprite3.say(("score: ",score ," and chance: ",chance,""))
sprite.setx(curr_x)
sprite.sety(curr_y)
sprite2.setx(shark_x)
sprite2.sety(shark_y)
sprite.setdirection(DIRECTION=angle_f)
sprite2.setdirection(DIRECTION=angle_s)
def settarget1(t):
x = random.randrange(-200, 200, t)
y = random.randrange(-155, 155, t)
x1 = random.randrange(-200, 200, t)
y1 = random.randrange(-155, 155, t)
x2 = random.randrange(-200, 200, t)
y2 = random.randrange(-155, 155, t)
time.sleep(0.1)
sprite1.setx(x1)
sprite1.sety(y1)
sprite.setx(x)
sprite.sety(y)
sprite2.setx(x2)
sprite2.sety(y2)
return x, y, x1, y1, x2, y2
def settarget(m):
x = random.randrange(-200, 200, m)
y = random.randrange(-155, 155, m)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget(40)
def runprediction(diff_x, diff_y, diff_x1, diff_y1, ang1, ang2):
inputValue=[diff_x, diff_y, diff_x1, diff_y1, ang1, ang2]
#Input Tensor
inputTensor = tf.expand_dims(inputValue, 0)
#Predict
predict = model.predict(inputTensor)
predict_index = np.argmax(predict[0], axis=0)
#Output
predicted_class = class_list[predict_index]
return predicted_class
while True:
if sprite.iskeypressed("up arrow"):
sprite2.move(shark_m)
shark_x=sprite2.x()
shark_y=sprite2.y()
if sprite.iskeypressed("left arrow"):
angles = angle_s - shark_d
sprite2.setdirection(DIRECTION=angles)
if sprite.iskeypressed("right arrow"):
angles = angle_s + shark_d
sprite2.setdirection(DIRECTION=angles)
angle_f=sprite.direction()
angle_s=sprite2.direction()
move = runprediction(curr_x- target_x, curr_y-target_y, shark_x-curr_x, shark_y-curr_y, angle_f, angle_s)
if move == "UP":
sprite.move(fish_m)
curr_x=sprite.x()
curr_y=sprite.y()
if move == "LEFT":
angle = angle_f - fish_d
sprite.setdirection(DIRECTION=angle)
if move == "RIGHT":
angle = angle_f + fish_d
sprite.setdirection(DIRECTION=angle)
if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
chance= chance-1
curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget1(40)
sprite3.say(("score: ",score ," and chance: ",chance,""))
if (chance == 0):
chance=5
if abs(curr_x-target_x)<35 and abs(curr_y-target_y)<35:
score = score + 1
sprite3.say(("score: ",score ," and chance: ",chance,""))
target_x, target_y = settarget(4)
time.sleep(0.2)
Creating a Machine Learning Model of “Shark Attack: Hungry for Fish” game can be both complex and time-consuming. Through the steps demonstrated in this project, you can create your own Machine Learning Model of automated game. Once trained, you can export the model into the Python Coding Environment, where you can tweak it further to give you the desired output. Try creating a Machine Learning Model of your own today and explore the possibilities of Number Classifier in PictoBlox!
In this example project, we are going to create a Machine Learning Model where beetle automatically feed on randomly generated food in space.
sprite = Sprite('Beetle')
sprite1 = Sprite('Strawberry')
import random
import time
import tensorflow as tf
import pandas as pd
import os
curr_x = -170
curr_y = 138
mov_f= 5
score = 0
angle = 90
sprite.setx(curr_x)
sprite.sety(curr_y)
sprite.setdirection(DIRECTION=angle)
def settarget(t):
x = random.randrange(-200, 200, t)
y = random.randrange(-155, 155, t)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget(40)
if(os.path.isfile('Chase_Data.csv')):
data=pd.read_csv('Chase_Data.csv')
else:
data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "RIGHT"}, index=[0])
while True:
if sprite.iskeypressed("up arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "UP"}, ignore_index=True)
curr_y = curr_y + beetle_m
sprite.setdirection(DIRECTION=0)
sprite.setx(curr_x)
sprite.sety(curr_y)
if sprite.iskeypressed("down arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "DOWN"}, ignore_index=True)
curr_y = curr_y - beetle_m
sprite.setdirection(DIRECTION=-180)
sprite.setx(curr_x)
sprite.sety(curr_y)
if sprite.iskeypressed("left arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "LEFT"}, ignore_index=True)
curr_x = curr_x - beetle_m
sprite.setdirection(DIRECTION=-90)
sprite.setx(curr_x)
sprite.sety(curr_y)
if sprite.iskeypressed("right arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "RIGHT"}, ignore_index=True)
curr_x = curr_x + beetle_m
sprite.setdirection(DIRECTION=90)
sprite.setx(curr_x)
sprite.sety(curr_y)
if(score>0 and score%10==0):
data.to_csv('Chase_Data.csv',index=False)
if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
score = score + 1
sprite.say(("your score is: {}".format(score)))
if (score >= 20):
print(data)
data.to_csv('Chase Data.csv')
break
target_x, target_y = settarget()
sprite = Sprite('Fish')
sprite1 = Sprite('Orange')
import random
import time
import numpy as np
import tensorflow as tf
import pandas as pd
import os
curr_x = -170
curr_y = 138
score=0
angle=90
beetle_m=5
sprite.say(("your score is: {}".format(score)))
sprite.setx(-170)
sprite.sety(138)
sprite.setdirection(DIRECTION=angle)
def settarget(t):
x = random.randrange(-200, 200, t)
y = random.randrange(-155, 155, t)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget(40)
if(os.path.isfile('Chase_Data.csv')):
data=pd.read_csv('Chase_Data.csv')
else:
data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "RIGHT"}, index=[0])
while True:
if sprite.iskeypressed("up arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "UP"}, ignore_index=True)
curr_y = curr_y + beetle_m
sprite.setdirection(DIRECTION=0)
sprite.setx(curr_x)
sprite.sety(curr_y)
if sprite.iskeypressed("down arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "DOWN"}, ignore_index=True)
curr_y = curr_y - beetle_m
sprite.setdirection(DIRECTION=-180)
sprite.setx(curr_x)
sprite.sety(curr_y)
if sprite.iskeypressed("left arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "LEFT"}, ignore_index=True)
curr_x = curr_x - beetle_m
sprite.setdirection(DIRECTION=-90)
sprite.setx(curr_x)
sprite.sety(curr_y)
if sprite.iskeypressed("right arrow"):
data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "RIGHT"}, ignore_index=True)
curr_x = curr_x + beetle_m
sprite.setdirection(DIRECTION=90)
sprite.setx(curr_x)
sprite.sety(curr_y)
if(score>0 and score%10==0):
data.to_csv('Chase_Data.csv',index=False)
if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
score = score + 1
sprite.say(("your score is: {}".format(score)))
if (score >= 20):
data.to_csv('Chase_Data.csv',index=False)
break
target_x, target_y = settarget(40)
Datasets on the internet are hardly ever fit to directly train on. Programmers often have to take care of unnecessary columns, text data, target columns, correlations, etc. Thankfully, PictoBlox’s ML Environment is packed with features to help us pre-process the data as per our liking.
Let’s create the ML model.
Alert: The Machine Learning Environment for model creation is available in the only desktop version of PictoBlox for Windows, macOS, or Linux. It is not available in Web, Android, and iOS versions.
Follow the steps below:
Datasets can either be uploaded or created on the ML Environment. Lets see how it is done.
Notes:
After data is pre-processed and optimized, it’s fit to be used in model training. To train the model, simply click the “Train Model” button found in the “Training” panel.
By training the model, meaningful information is extracted from the numbers, and that in turn updates the weights. Once these weights are saved, the model can be used to make predictions on data previously unseen.
The model’s function is to use the input data and predict the output. The target column must always contain numbers.
However, before training the model, there are a few hyperparameters that need to be understood. Click on the “Advanced” tab to view them.
There are three hyperparameters that can be altered in the Numbers(C/R) Extension:
It’s a good idea to train a numeric classification model for a high number of epochs. The model can be trained in both JavaScript and Python. In order to choose between the two, click on the switch on top of the Training panel.
The accuracy of the model should increase over time. The x-axis of the graph shows the epochs, and the y-axis represents the accuracy at the corresponding epoch.
A window will open. Type in a project name of your choice and select the “Numbers(C/R)” extension. Click the “Create Project” button to open the Numbers(C/R) window.
To test the model, simply enter the input values in the “Testing” panel and click on the “Predict” button.
The model will return the probability of the input belonging to the classes.
Click on the “PictoBlox” button, and PictoBlox will load your model into the Python Coding Environment if you have opened the ML Environment in Python Coding.
sprite = Sprite('Beetle')
sprite1 = Sprite('Strawberry')
import random
import time
import tensorflow as tf
import pandas as pd
curr_x = -170
curr_y = 138
beetle_m= 15
angle = 90
score = 0
sprite.setx(curr_x)
sprite.sety(curr_y)
sprite.setdirection(DIRECTION=90)
def settarget(t):
x = random.randrange(-200, 200, t)
y = random.randrange(-155, 155, t)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget(40)
def runprediction(diff_x, diff_y):
inputValue=[diff_x, diff_y]
#Input Tensor
inputTensor = tf.expand_dims(inputValue, 0)
#Predict
predict = model.predict(inputTensor)
predict_index = np.argmax(predict[0], axis=0)
#Output
predicted_class = class_list[predict_index]
return predicted_class
while True:
angle=sprite.direction()
move = runprediction(curr_x- target_x, curr_y-target_y, angle)
if move == "UP": curr_y = curr_y + beetle_m sprite.setdirection(DIRECTION=0) sprite.setx(curr_x) sprite.sety(curr_y) if move == "LEFT": curr_x = curr_x - beetle_m sprite.setdirection(DIRECTION=-90) sprite.setx(curr_x) sprite.sety(curr_y) if move == "RIGHT": curr_x = curr_x + beetle_m sprite.setdirection(DIRECTION=90) sprite.setx(curr_x) sprite.sety(curr_y)
if move == "DOWN": curr_y = curr_y - beetle_m sprite.setdirection(DIRECTION=-180) sprite.setx(curr_x) sprite.sety(curr_y)
if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
score = score + 1
sprite.say(("your score is: {}".format(score)))
target_x, target_y = settarget()
time.sleep(0.02)
sprite = Sprite('Beetle')
sprite1 = Sprite('Strawberry')
import random
import time
import numpy as np
import tensorflow as tf
#Load Number Model
model= tf.keras.models.load_model(
"num_model.h5",
custom_objects=None,
compile=True,
options=None)
#List of classes
class_list = ['RIGHT','LEFT','DOWN','UP',]
curr_x = -170
curr_y = 138
score=0
beetle_m=30
angle=90
sprite.say(("your score is: {}".format(score)))
sprite.setx(-170)
sprite.sety(138)
sprite.setdirection(DIRECTION=angle)
def settarget():
x = random.randrange(-200, 200, 1)
y = random.randrange(-155, 155, 1)
time.sleep(0.1)
sprite1.setx(x)
sprite1.sety(y)
return x, y
target_x, target_y = settarget()
def runprediction(diff_x, diff_y):
inputValue=[diff_x, diff_y]
#Input Tensor
inputTensor = tf.expand_dims(inputValue, 0)
#Predict
predict = model.predict(inputTensor)
predict_index = np.argmax(predict[0], axis=0)
#Output
predicted_class = class_list[predict_index]
return predicted_class
while True:
move = runprediction(curr_x- target_x, curr_y-target_y)
if move == "UP":
curr_y = curr_y + beetle_m
sprite.setdirection(DIRECTION=0)
sprite.setx(curr_x)
sprite.sety(curr_y)
if move == "DOWN":
curr_y = curr_y - beetle_m
sprite.setdirection(DIRECTION=-180)
sprite.setx(curr_x)
sprite.sety(curr_y)
if move == "LEFT":
curr_x = curr_x - beetle_m
sprite.setdirection(DIRECTION=-90)
sprite.setx(curr_x)
sprite.sety(curr_y)
if move == "RIGHT":
curr_x = curr_x + beetle_m
sprite.setdirection(DIRECTION=90)
sprite.setx(curr_x)
sprite.sety(curr_y)
if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
score = score + 1
sprite.say(("your score is: {}".format(score)))
target_x, target_y = settarget()
time.sleep(0.2)
Creating a Machine Learning Model of “Beetle’s Cosmic Feast: Strawberry Space Adventure” can be both complex and time-consuming. Through the steps demonstrated in this project, you can create your own Machine Learning Model of automated game. Once trained, you can export the model into the Python Coding Environment, where you can tweak it further to give you the desired output. Try creating a Machine Learning Model of your own today and explore the possibilities of Number Classifier in PictoBlox!
Copyright 2024 – Agilo Research Pvt. Ltd. All rights reserved – Terms & Condition | Privacy Policy