<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rupal on Medium]]></title>
        <description><![CDATA[Stories by Rupal on Medium]]></description>
        <link>https://medium.com/@techiscoop?source=rss-5ef0e724ea03------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cEJwGm3TrozTbJoz</url>
            <title>Stories by Rupal on Medium</title>
            <link>https://medium.com/@techiscoop?source=rss-5ef0e724ea03------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 23:18:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@techiscoop/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[FTC Java Programming: Encoders, Drivetrain and Arm]]></title>
            <link>https://techiscoop.medium.com/robotics-coding-for-ftc-dc1458304688?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc1458304688</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[ftc]]></category>
            <category><![CDATA[robotics]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Thu, 16 Jan 2025 21:57:20 GMT</pubDate>
            <atom:updated>2025-01-17T17:43:18.481Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Robot programming involves driving motor at set velocity and power. Sometime we require to control arm movement. You can greatly improve the precision and control of your robot on the game field by using encoders. These fundamental tools enable you to create reliable, repeatable motions, whether you’re driving or manipulating components, which is key to success in robotics competitions.</p><h3>Encoders</h3><p>Encoders measures the number of revolutions (or “ticks”) of a motor, helping track its movement. A quadrature encoders cannot determine the exact position, they can still guide the motor to a specific target.</p><p>Every motor comes with varying numbers of ticks per revolution and different gear ratios. When the Control Hub is powered on, all encoder ports are reset to <strong>0 ticks</strong>. As the motor moves forward, the encoder value <strong>increases</strong>; when it moves backward, the value <strong>decreases</strong>.</p><h3>Programming the drivetrain</h3><h4>Using Encoder Modes</h4><p><strong>RUN_USING_ENCODER</strong>: [ For manual control ] Controls motor speed by maintaining a specific velocity.</p><p><strong>RUN_TO_POSITION</strong>: [ Used with presets — if you know target position ] Moves the motor to a defined position in ticks. To use this mode, follow these steps:</p><ul><li>Set a <strong>target position</strong> (measured in ticks)</li><li>Switch to <strong>RUN_TO_POSITION</strong> mode.</li><li>Set the <strong>maximum velocity</strong> (if not controlled by a gamepad input).</li></ul><h4>Calculating COUNTS_PER_MM (counts per millimeter)</h4><p>Find outCOUNTS_PER_WHEEL_REV from motor documentation, it can be used to calculate how many encoder counts correspond to <strong>one millimeter of movement</strong>. The formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*PbQIkkgW49jHWZK0tyXi0g.png" /></figure><p>Where:</p><p><strong>a</strong> = COUNTS_PER_MOTOR_REV (encoder ticks per motor revolution)</p><p><strong>b</strong> = DRIVE_GEAR_REDUCTION (gear ratio)</p><p><strong>c</strong> = WHEEL_CIRCUMFERENCE_MM (wheel circumference in mm)</p><p><strong>y</strong> = COUNTS_PER_WHEEL_REV (encoder ticks per full wheel rotation)</p><p><strong>x</strong> = COUNTS_PER_MM (encoder ticks per mm moved)</p><p>For a <strong>target distance of 2 feet</strong>:</p><p>target_distance_ticks = 2 * 304.8 * x</p><h4>Calculating Velocity (Ticks Per Second — TPS)</h4><p>To ensure accurate motion, motors should run at approximately <strong>60% of their maximum velocity</strong>. For a <strong>Rev Hex Motor</strong>, which has a max speed of <strong>300 RPM</strong>, a safer value is <strong>175 RPM</strong>.</p><p>Convert RPM to <strong>ticks per second (TPS)</strong> using:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rSEdrjbF7x68AzBfNGnSrQ.png" /></figure><p>This ensures smoother and more reliable drivetrain movement.</p><p>Sample code</p><pre>package org.firstinspires.ftc.teamcode;<br><br>import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;<br>import com.qualcomm.robotcore.eventloop.opmode.Autonomous;<br>import com.qualcomm.robotcore.eventloop.opmode.TeleOp;<br>import com.qualcomm.robotcore.eventloop.opmode.Disabled;<br>import com.qualcomm.robotcore.hardware.DcMotorEx;<br>import com.qualcomm.robotcore.hardware.DcMotor;<br>import com.qualcomm.robotcore.hardware.DcMotorSimple;<br><br>@Autonomous<br>public class TestRobot_Encoder extends LinearOpMode {<br>    private DcMotorEx leftmotor;<br>    private DcMotorEx rightmotor;<br>    <br>    static final double     COUNTS_PER_MOTOR_REV    = 28.0; <br>    static final double     DRIVE_GEAR_REDUCTION    = 30.24;   <br>    static final double     WHEEL_CIRCUMFERENCE_MM  = 90.0 * 3.14;<br>    <br>    static final double     COUNTS_PER_WHEEL_REV    = COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION;<br>    static final double     COUNTS_PER_MM           = COUNTS_PER_WHEEL_REV / WHEEL_CIRCUMFERENCE_MM;<br>    <br>    @Override<br>    public void runOpMode() {<br>        leftmotor = hardwareMap.get(DcMotorEx.class, &quot;leftmotor&quot;);<br>        rightmotor = hardwareMap.get(DcMotorEx.class, &quot;rightmotor&quot;);<br>        <br>        leftmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);<br>        rightmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);<br>        <br>        // moves 2 ft forward<br>        // for teleops, instead of 610, we&#39;ll put inside whileloop and<br>        // move based on input received from gamepad<br>        int leftTarget = (int)(610 * COUNTS_PER_MM); <br>        int rightTarget = (int)(610 * COUNTS_PER_MM); <br>        double TPS = (175/ 60) * COUNTS_PER_WHEEL_REV;<br>        <br>        waitForStart();<br>        <br>        leftmotor.setTargetPosition(leftTarget);<br>        rightmotor.setTargetPosition(rightTarget);<br>        <br>        leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);<br>        rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);<br>        <br>        leftmotor.setVelocity(TPS);<br>        rightmotor.setVelocity(TPS);<br>        <br>        while (opModeIsActive() &amp;&amp; (leftmotor.isBusy() &amp;&amp; rightmotor.isBusy())) {<br>        }<br>    }<br>}</pre><h3>Programming the arm</h3><p>the arm does not follow a linear path. This means rather than converting to a linear distance it makes more sense to convert the encoder ticks into an angle measured in degrees!</p><p>Option1: Using telemetry</p><pre>while(opModeIsActive){<br>    if(gamepad1.dpad_up){<br>       arm.setPower(0.2);         <br>            }<br>     else if (gamepad1.dpad_down){<br>       arm.setPower(-0.2); <br>            }   <br>     else { <br>       arm.setPower(0); <br>            } <br>     telemetry.addData(&quot;Arm Test&quot;, arm.getCurrentPosition());<br>     telemetry.update();<br>          <br> }</pre><p>When controller is turned on, the arm position is 0. When the dpad_up / dpad_down commands are received from gamepad, the arm position will change, after testing once you know the desired position, you can set in your program</p><pre>if(gamepad1.dpad_up){<br>     arm.setTargetPosition(83); // 83 came from telemetry output<br>     arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);<br>     arm.setPower(0.5);<br>            }<br>else if (gamepad1.dpad_down){<br>      arm.setTargetPosition(0);<br>      arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);<br>      arm.setPower(0.5);<br>            } </pre><p>Option2: Using Math</p><p>Tickes per revolution = 288 (Core Hex motor from rev)</p><p>Gear ratio = 125/45 = 2.7778 (gears connected to motor)</p><p>We have to calculate counts per degree = ( 288 * 2.7778 ) / 360</p><p>Translated into code:</p><pre>@TeleOp<br>public class TestRobot_ArmEncoder extends LinearOpMode {<br>    private DcMotor arm;<br>    private DcMotor leftmotor;<br>    private DcMotor rightmotor;<br><br>    static final double COUNTS_PER_MOTOR_REV = 288; <br>    static final double GEAR_REDUCTION = 2.7778;   <br>    static final double COUNTS_PER_GEAR_REV = COUNTS_PER_MOTOR_REV * GEAR_REDUCTION;<br>    static final double COUNTS_PER_DEGREE = COUNTS_PER_GEAR_REV / 360;<br><br>    @Override<br>    public void runOpMode() {<br>        arm = hardwareMap.get(DcMotor.class, &quot;arm&quot;);<br>        leftmotor = hardwareMap.get(DcMotor.class, &quot;leftmotor&quot;);<br>        rightmotor = hardwareMap.get(DcMotor.class, &quot;rightmotor&quot;);<br><br>        // Reset encoder and set mode to RUN_TO_POSITION<br>        arm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);<br>        arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);<br>        arm.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);<br><br>        int minPosition = 0;<br>        int maxPosition = (int)(COUNTS_PER_DEGREE * 45);<br>        int targetPosition = 0;  // Start at zero<br><br>        waitForStart();<br>        <br>        while (opModeIsActive()) {<br>            if (gamepad1.dpad_up &amp;&amp; targetPosition &lt; maxPosition) {<br>                targetPosition += (int) (COUNTS_PER_DEGREE * 5);  // Move up 5 degrees<br>            } <br>            else if (gamepad1.dpad_down &amp;&amp; targetPosition &gt; minPosition) {<br>                targetPosition -= (int) (COUNTS_PER_DEGREE * 5);  // Move down 5 degrees<br>            } <br>            else if (gamepad1.a) {<br>                targetPosition = minPosition;  // Move to home position<br>            }<br><br>            // Set new target position and run<br>            arm.setTargetPosition(targetPosition);<br>            arm.setPower(0.5);  // Power must be set for `RUN_TO_POSITION` to work<br><br>            // Telemetry updates<br>            telemetry.addData(&quot;Arm Position&quot;, arm.getCurrentPosition());<br>            telemetry.addData(&quot;Target Position&quot;, targetPosition);<br>            telemetry.update();<br>        }<br>    }<br>}</pre><h3>Summary</h3><h4>Encoders</h4><ul><li>Count motor revolutions (ticks)</li><li>Quadrature encoders estimate position</li><li>Different motors have different ticks per revolution</li><li>RUN_USING_ENCODER targets velocity</li><li>RUN_TO_POSITION moves motor to a set position</li></ul><p><strong>Code Example:</strong></p><pre>arm.setTargetPosition(1000);<br>arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);<br>arm.setPower(0.5);</pre><h4>Programming the Drivetrain</h4><ul><li>Calculate COUNTS_PER_MM for precise movement</li><li>Convert target distance (e.g., 2ft) into encoder ticks</li><li>Use velocity control (TPS = Ticks Per Second) for smooth motion</li></ul><p><strong>Code Example:</strong></p><pre>int target = (int)(610 * COUNTS_PER_MM);<br>leftmotor.setTargetPosition(target);<br>rightmotor.setTargetPosition(target);<br>leftmotor.setVelocity(TPS);<br>rightmotor.setVelocity(TPS);</pre><h4>Programming the Arm</h4><ul><li>Arm moves in <strong>angles</strong>, not linear distance</li><li>Use <strong>telemetry</strong> to determine the desired encoder value or use <strong>math</strong> to convert encoder ticks into degrees</li><li>RUN_TO_POSITION mode helps achieve precise arm movement</li></ul><p><strong>Code Example:</strong></p><pre>int maxPosition = (int)(COUNTS_PER_DEGREE * 45); //move 45 degrees<br>arm.setTargetPosition(maxPosition);<br>arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);<br>arm.setPower(0.5);</pre><h4>References</h4><p><a href="https://docs.revrobotics.com/duo-control/hello-robot-java/part-3">https://docs.revrobotics.com/duo-control/hello-robot-java/part-3</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc1458304688" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Securing your applications]]></title>
            <link>https://techiscoop.medium.com/securing-your-applications-99d56953068c?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/99d56953068c</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[authorization]]></category>
            <category><![CDATA[oauth2]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Fri, 13 Sep 2024 00:37:04 GMT</pubDate>
            <atom:updated>2024-09-13T00:37:04.266Z</atom:updated>
            <content:encoded><![CDATA[<h3>Topics</h3><ul><li>Authentication and authorization concepts.</li><li>Authorization flow deep dive</li><li>JWT</li><li>Case Study</li><li>oauth and openid connect</li></ul><h3>Authentication</h3><p>What is Authentication? It is mechanism to make sure the logged in users are genuine in other wards process of verifying identity of a user or a process.</p><p>There are two common ways to authenticate</p><p>First, Session based authentication- I am not going into too much detail but at high level, when user logs-in server creates and stores session information of logged in user in db and sends the session information back to the user, the browser set cookie with session information and re-uses in subsequent request.</p><p>Second, Token based authentication. Here we’ll talk about JSON Web Token. Pronounced as “jots”. It is lightweight and stateless authentication mechanism. JWT can be used for <strong>authentication</strong> and <strong>authorization</strong> both.</p><h3><strong>Authorization</strong></h3><p><strong>What is authorization?</strong> Authorization in a system security is the process of giving the user permission to access a specific resource or function. This term is often used interchangeably with access control or client privilege. In simple words what the user can see or do</p><p>Going back to JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access<strong> routes, services, and resources </strong>that are permitted with that token. Single Sign On uses JWT under the hood, because of its small overhead</p><p>JWTs are a good way of <strong>securely</strong> <strong>transmitting</strong> information between parties because they can be <strong>signed</strong>, which allows you to verify that the content hasn’t been tampered with in flight.</p><h3>Authorization flow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*JaJfyfRIeWY87tMVzuH4sA.png" /></figure><p>Step 1. User logs in. Once user logs in server generates JWT token to the user. The user can use jwt on subsequent request to access resources permitted with the token.</p><p>Step2, App (browser or mobile app) sends token + request to service.</p><p>Step3, service verifies the token.</p><p>Step4 service sends response back to the app if token is valid. Basically, the user receives the response.</p><p>Reference: <a href="https://dzone.com/articles/securing-spring-boot-microservices-with-json-web-t">https://dzone.com/articles/securing-spring-boot-microservices-with-json-web-t</a></p><h3>JWT</h3><ul><li>JWT It has 3 parts: <strong>Header</strong>, <strong>payload</strong> and <strong>signature</strong>.</li><li>Client sends token in authorization header</li><li>Service validates the token, verifies the signature and verifies the claims. We’ll look into details in upcoming slides.</li><li>JWT are base64 encoded and signed strings <strong>used as tokens</strong> in microservices.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*khaf22xzwp62I0yfXj_ozA.png" /></figure><pre>//Create a secret key<br> Key secretKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);<br>//Let&#39;s set the JWT Claims<br>    JwtBuilder builder = Jwts.builder().setId(id)<br>                                .setIssuedAt(now)<br>                                .setSubject(subject)<br>                                .setIssuer(issuer)<br>                                .signWith(signatureAlgorithm, secretKey);;<br>  <br>    //Parse the token<br>    Claims claims = Jwts.parser()         <br>       .setSigningKey(secretKey)<br>       .parseClaimsJws(jwt).getBody();<br>    System.out.println(&quot;ID: &quot; + claims.getId());<br>    System.out.println(&quot;Subject: &quot; + claims.getSubject());<br>    System.out.println(&quot;Issuer: &quot; + claims.getIssuer());<br>    System.out.println(&quot;Expiration: &quot; + claims.getExpiration());<br><br>Reference: https://github.com/jwtk/jjwt</pre><h4>Shared Secret</h4><pre># Example using Symmetric Key <br># Generate Secret Key<br>secret_key = &quot;***&quot;<br>token = jwt.encode({&#39;user_id&#39;: 101}, secret_key, algorithm=&#39;HS256&#39;)<br>token<br><br># Generate Token<br>from time import time<br><br>token = jwt.encode({<br>    &#39;aud&#39;: &#39;target&#39;,<br>    &#39;iss&#39;: &#39;myapp&#39;,<br>    &#39;sub&#39;: &#39;subject&#39;,<br>    &#39;exp&#39; : time() + 300<br>    }, secret_key, algorithm=&#39;HS256&#39;)<br><br># Verify Token<br>payload = jwt.decode(token, secret_key, algorithms=[&#39;HS256&#39;], audience=&#39;target&#39;)<br><br># Example using PCI (public key / private key)<br># Generate Private Key run in shell<br># ssh-keygen -t rsa -b 4096<br><br><br># Generate Token<br>from jwt.contrib.algorithms.pycrypto import RSAAlgorithm<br># Uncomment and run for first time only<br># jwt.register_algorithm(‘RS256&#39;, RSAAlgorithm(RSAAlgorithm.SHA256))<br>private_key = open(&#39;./jwt-key&#39;).read()<br>token = jwt.encode({&#39;user_id&#39;: 123}, private_key, algorithm=&#39;RS256&#39;).decode(&#39;utf-8&#39;)<br><br><br># Verify Token<br>public_key = open(&#39;jwt-key.pub&#39;).read()<br>payload = jwt.decode(token, public_key, algorithms=[&#39;RS256&#39;])<br></pre><h3>Case Study</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*rrg21cL7fMqCsYs9p5laCw.png" /></figure><h3>Oauth 2.0</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*vxe3dchSygEHnF9e6aWdrQ.png" /></figure><p>Oauth 2.0 with OpenIDConnect</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cgolTawBLlyXYwn__QOsQA.png" /></figure><h3>Microservices Security best practices</h3><p><strong>Don’t -</strong></p><ul><li>Store key as a plain text in code</li><li>Store in Dockerfile or environment variables</li></ul><p><strong>How to store key securely -</strong></p><ul><li>Vault (<a href="http://www.vaultproject.io/">www.vaultproject.io</a>)</li><li>AWS Key Management Service (KMS)</li></ul><h3><strong>References</strong></h3><ul><li>RFC7517: <a href="https://tools.ietf.org/html/rfc7517">https://tools.ietf.org/html/rfc7517</a></li></ul><p><strong>JWT</strong></p><ul><li><a href="https://jwt.io/">https://jwt.io/</a></li><li>RFC7517: <a href="https://tools.ietf.org/html/rfc7517">https://tools.ietf.org/html/rfc7517</a></li><li><a href="https://owasp.org/www-pdf-archive/Microservice_Security.pdf">https://owasp.org/www-pdf-archive/Microservice_Security.pdf</a></li></ul><p><strong>Articles</strong></p><ul><li><a href="https://developer.okta.com/blog/2019/10/21/illustrated-guide-to-oauth-and-oidc">https://developer.okta.com/blog/2019/10/21/illustrated-guide-to-oauth-and-oidc</a></li><li><a href="https://dzone.com/articles/securing-spring-boot-microservices-with-json-web-t">https://dzone.com/articles/securing-spring-boot-microservices-with-json-web-t</a></li><li><a href="https://medium.facilelogin.com/securing-microservices-with-oauth-2-0-jwt-and-xacml-d03770a9a838">https://medium.facilelogin.com/securing-microservices-with-oauth-2-0-jwt-and-xacml-d03770a9a838</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99d56953068c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FTC Java Programming with REV Robotics: A Step-by-Step Guide]]></title>
            <link>https://techiscoop.medium.com/ftc-java-programming-with-rev-robotics-a-step-by-step-guide-ffcdae73adf7?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffcdae73adf7</guid>
            <category><![CDATA[first-tech-challenge]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Wed, 11 Sep 2024 21:45:07 GMT</pubDate>
            <atom:updated>2025-01-17T17:28:34.996Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Setting Up the Development Environment</h3><p>Before coding, you need to install and set up the necessary tools for programming in Java.</p><h4>Step 1.1: Install Android Studio</h4><p>FTC Java development uses Android Studio to write and upload code to your robot.</p><ul><li>Go to the <a href="https://developer.android.com/studio">Android Studio download page</a>.</li><li>Download and install the latest stable version for your operating system.</li><li>Follow the instructions for installation, then open Android Studio once installed.</li></ul><h4>Step 1.2: Install the FTC SDK</h4><p>FTC provides a Software Development Kit (SDK) that contains all the necessary libraries and examples.</p><ul><li>Visit the <a href="https://github.com/FIRST-Tech-Challenge/FtcRobotController">FTC SDK GitHub page</a>.</li><li>Download the SDK as a ZIP file or clone the repository using Git:</li></ul><pre>git clone https://github.com/FIRST-Tech-Challenge/FtcRobotController.git</pre><ul><li>Open the SDK in Android Studio: File &gt; Open, and select the folder where the SDK is located.</li></ul><h3>2. Understanding REV Robotics Hardware</h3><h4>Step 2.1: REV Control Hub and Expansion Hub</h4><ul><li>The Control Hub is the “brain” of the robot, managing communication between the software and hardware.</li><li>The Expansion Hub connects additional motors, servos, and sensors to your robot.</li><li>Ensure you’ve connected the REV hubs and the power module according to the REV Robotics hardware guide.</li></ul><h3>3. Configuring Your Robot in FTC Dashboard</h3><h4>Step 3.1: Access the FTC Dashboard</h4><ul><li>Turn on your robot and connect it to the same Wi-Fi network as your development computer.</li><li>Open a browser and go to the robot’s IP address (usually 192.168.43.1) to access the robot&#39;s configuration dashboard.</li><li>Set up the motor controllers, sensors, and other peripherals. For example:</li><li>Name your motors, such as “left_drive” and “right_drive”.</li><li>Name your servos, like “arm_servo”.</li></ul><h3>4. Writing Your First FTC Java Program</h3><p>Now that your environment is ready, it’s time to write a basic Java program.</p><h4>Step 4.1: Create an OpMode</h4><ul><li>In the SDK, go to the TeamCode folder.</li><li>Create a new Java class for your OpMode (the main program to control the robot):</li></ul><ol><li>Right-click on the opmode package in TeamCode.</li><li>Select New &gt; Java Class, and name it MyFirstOpMode.</li></ol><h4>Step 4.2: Basic OpMode Structure</h4><p>Every OpMode needs to extend the LinearOpMode or OpMode class. Here’s a basic structure:</p><pre>package org.firstinspires.ftc.teamcode;<br><br>import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;<br>import com.qualcomm.robotcore.hardware.DcMotor;<br><br>@com.qualcomm.robotcore.eventloop.opmode.TeleOp<br>public class MyFirstOpMode extends LinearOpMode {<br>    private DcMotor leftDrive;<br>    private DcMotor rightDrive;<br>    @Override<br>    public void runOpMode() {<br>        // Initialize motors<br>        leftDrive = hardwareMap.get(DcMotor.class, &quot;left_drive&quot;);<br>        rightDrive = hardwareMap.get(DcMotor.class, &quot;right_drive&quot;);<br>        // Wait for the game to start (driver presses PLAY)<br>        waitForStart();<br>        while (opModeIsActive()) {<br>            // Control motors using gamepad joysticks<br>            double drive = -gamepad1.left_stick_y;<br>            double turn = gamepad1.right_stick_x;<br>            double leftPower = drive + turn;<br>            double rightPower = drive - turn;<br>            leftDrive.setPower(leftPower);<br>            rightDrive.setPower(rightPower);<br>            // Send telemetry data to the Driver Station<br>            telemetry.addData(&quot;Left Power&quot;, leftPower);<br>            telemetry.addData(&quot;Right Power&quot;, rightPower);<br>            telemetry.update();<br>        }<br>    }<br>}</pre><h4>Step 4.3: Upload and Test</h4><ol><li>Connect your computer to the Control Hub via Wi-Fi.</li><li>In Android Studio, click the green “Run” button. This will compile your code and push it to the robot.</li><li>Open the FTC Driver Station app on your phone, select the MyFirstOpMode, and run it.</li></ol><h3>5. Understanding the Code</h3><ul><li>hardwareMap: This connects the motor names in the code to the actual hardware configured on the robot.</li><li>gamepad1.left_stick_y and gamepad1.right_stick_x: These capture input from the gamepad’s joysticks to control your robot.</li><li>setPower(): This sets the speed for the motors. Positive values make the motor move forward, while negative values move it in reverse.</li></ul><h3>6. Expanding the Code</h3><h4>Step 6.1: Adding More Hardware</h4><p>To control more hardware, like servos or sensors, follow the same process:</p><pre>Servo armServo = hardwareMap.get(Servo.class, &quot;arm_servo&quot;);<br>armServo.setPosition(1.0); // Set to a certain position</pre><h4>Step 6.2: Autonomous Mode</h4><p>For autonomous programs, you can add time-based movement:</p><pre>leftDrive.setPower(0.5);<br>rightDrive.setPower(0.5);<br>sleep(1000); // Move forward for 1 second</pre><h3>Next Steps</h3><p>Once you’re comfortable with basic teleop (manual control) programming, you can start working on more advanced topics like:</p><ul><li><strong>Autonomous programming</strong>: Program the robot to run pre-defined tasks without driver input.</li><li><strong>Sensors</strong>: Use REV sensors like IMUs, encoders, and color sensors for more precise control.</li></ul><p>Good luck with your FTC season! The more you experiment with your robot, the better you’ll become at programming and controlling it.</p><h4>Don’t forget to read</h4><p>how to use encoders to program drivetrain and arm from next in the series: <a href="https://techiscoop.medium.com/robotics-coding-for-ftc-dc1458304688">https://techiscoop.medium.com/robotics-coding-for-ftc-dc1458304688</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffcdae73adf7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GPT or Transformer architecture]]></title>
            <link>https://techiscoop.medium.com/gpt-or-transformer-architecture-8ae0bada06f6?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ae0bada06f6</guid>
            <category><![CDATA[gpt]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[genai]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Tue, 19 Mar 2024 22:51:18 GMT</pubDate>
            <atom:updated>2024-03-19T22:51:18.313Z</atom:updated>
            <content:encoded><![CDATA[<p>GPT-3.5 is built using GPT (Generative Pre-trained Transformer) architecture, a transformer-based model for natural language processing tasks. Transformers revolutionized the field by introducing attention mechanisms, enabling the model to process information in parallel and capture intricate contextual relationships within vast amounts of data. Transformers play a central role in GPT-3.5’s ability to comprehend, generate, and interact with text in a remarkably human-like manner.</p><p>Let’s look at the transformer architecture</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9T-5dE6P3tR5vmw55LIy_A.png" /></figure><p>Encoder:</p><ul><li>Input sentence is converted into vectors (embeddings) — numerical matrix</li><li>Positional encoding applies information about word in conjunction with position of other words in sentence</li><li>In multihead attention, query, keys and values are multiplied with weights and applied softmax function. The output is normalized to 0–1 range and multiplied with multipliers to get final output, which will amplify words which are relevant to the input. Query is input embeddings, key and values are model training data. Animation shows single head attention, in reality the q, k v are split into smaller chunks and processed in parallel, making it multihead.</li></ul><pre>Attention(Q, K, V) = softmax(QKT/√dk)V</pre><ul><li>Next step is feed forward and normalization. Feed forward applies linear transformations</li></ul><pre>FFN(x) = max(0, xW1 + b1)W2 + b2 (2)</pre><ul><li>This process will produce intermediate output (context) for decoder.</li></ul><p>Decoder:</p><ul><li>Decoder applies positional encoding to output from decoder at time (t-1) and applies mask-multi-head-attention (same as encoder but some values are masked)</li><li>Decoder applies transformations (multihead attention, feedforward and normalization same as encoder but with different weight and bias) to output of encoder (key and values) and output from previous decoder step.</li><li>In linearization, transforms embeddings to positions into our vocabulary (inverse process of converting text to embeddings)</li><li>Softmax, helps select word with max score (most relevant)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_a4Z7zz7RyMLKom7t9boWg.gif" /><figcaption>Transformer data flow: Hello world (English) to Bonjour monde (French)</figcaption></figure><p>References: <a href="https://research.google/pubs/attention-is-all-you-need/">https://research.google/pubs/attention-is-all-you-need/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ae0bada06f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unraveling the Threads: A Comprehensive Guide to Java Concurrency with Akka, CompletableFuture, and…]]></title>
            <link>https://techiscoop.medium.com/unraveling-the-threads-a-comprehensive-guide-to-java-concurrency-with-akka-completablefuture-and-5f89cbfcb045?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f89cbfcb045</guid>
            <category><![CDATA[event-driven-programming]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[akka]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Tue, 09 Jan 2024 19:30:36 GMT</pubDate>
            <atom:updated>2025-07-13T04:06:55.685Z</atom:updated>
            <content:encoded><![CDATA[<h3>Unraveling the Threads: A Comprehensive Guide to Java Concurrency with Akka, CompletableFuture, and Reactive Programming</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*2PXGRDDK8-oKYEe11uHe3A.png" /></figure><h3>Introduction:</h3><p>Java’s prowess in handling concurrency has been a cornerstone of its success in the software development landscape. As applications become more complex and demand for scalability rises, the need for efficient concurrency management becomes paramount. In this article, we will delve into three powerful approaches for handling concurrency in Java: Akka framework, CompletableFuture, and Reactive Programming. Through real-world examples, we’ll explore the strengths, weaknesses, and trade-offs of each approach, comparing them based on efficiency, speed, and complexity.</p><h3>Akka Framework: Actor-based Concurrency</h3><p>Akka is an open-source toolkit and runtime for building highly concurrent, distributed, and fault-tolerant systems. At its core is the actor model, which allows developers to write concurrent code without the complexity of traditional multithreading.</p><pre>public class AkkaExample {<br>    public static void main(String[] args) {<br>        ActorSystem system = ActorSystem.create(&quot;MySystem&quot;);<br>        ActorRef worker = system.actorOf(Props.create(WorkerActor.class), &quot;worker&quot;);<br><br>        // Send a message to the actor<br>        worker.tell(&quot;Hello, Akka!&quot;, ActorRef.noSender());<br>    }<br>}</pre><p>In this example, WorkerActor could perform some tasks concurrently without explicit thread management.</p><h4>Akka Actors:</h4><p>Actor Basics:</p><ul><li>Actor: In Akka, an actor is a lightweight, isolated unit of computation that communicates with other actors through message passing. Actors encapsulate state and behavior, and they execute concurrently.</li><li>Actor System: Actors run within an actor system, which manages their lifecycle, supervision, and message routing. An actor system provides the runtime environment for actors.</li></ul><p>Actor Lifecycle:</p><ul><li>Creation: Actors are created within an actor system using the Props and ActorRef constructs. The Props define an actor&#39;s behavior, and the ActorRef is a reference to the actor.</li><li>Message Processing: Actors respond to messages sent to them. When an actor receives a message, it processes the message using its behavior defined by the receive method. Actors can spawn new actors, change behavior, or send messages to other actors.</li><li>Termination: Actors can be stopped or terminated explicitly or due to an exception. The actor system manages the lifecycle of actors, including supervision and termination.</li></ul><h4>Message Passing in Akka:</h4><p>Asynchronous Communication:</p><ul><li>Sender and Receiver: Actors communicate exclusively through asynchronous message passing. When one actor wants to communicate with another, it sends a message. The sender does not wait for a response, allowing both actors to continue their tasks concurrently.</li><li>Immutable Messages: Messages in Akka are immutable, meaning their content cannot be changed after creation. Immutability ensures that actors can safely share messages without the risk of unexpected modification.</li></ul><p>Message Processing:</p><ul><li>Actor’s receive Method: The core of an actor&#39;s behavior is defined by its receive method. This method is called whenever a message is received. It matches incoming messages against a set of defined patterns and executes the corresponding code block.</li><li>Pattern Matching: Actors use pattern matching to determine how to handle different types of messages. This approach makes it easy to express complex logic based on the content of received messages.</li></ul><p>Under the Hood:</p><ul><li>Mailbox: Each actor has an associated mailbox that holds its incoming messages. Messages are enqueued in the mailbox in the order they are received. The actor processes messages from its mailbox one at a time, ensuring a sequential, non-blocking execution.</li><li>Thread Pool: Akka uses a thread pool to manage the execution of actors. Actors are executed on different threads, allowing them to run concurrently. The actor system intelligently schedules actors based on their availability and workload.</li><li>Concurrency and Isolation: Actors provide a high level of concurrency by running independently and asynchronously. The actor model enforces isolation between actors, preventing direct access to the state of other actors. Communication is only possible through message passing.</li></ul><p>In summary, Akka’s actor model simplifies concurrent programming by encapsulating state and behavior within lightweight actors. Message passing allows actors to communicate asynchronously, promoting loose coupling and scalability. The underlying mechanisms, including mailboxes and thread pools, ensure efficient and concurrent execution of actors in an actor system.</p><h3>CompletableFuture</h3><p>Introduced in Java 8, CompletableFuture provides a powerful mechanism for handling asynchronous tasks with ease. The supplyAsync method allows developers to execute a task asynchronously and continue with other work.</p><pre>public class CompletableFutureExample {<br>    public static void main(String[] args) {<br>        CompletableFuture&lt;String&gt; future = CompletableFuture.supplyAsync(() -&gt; &quot;Hello, CompletableFuture!&quot;);<br><br>        // Attach a callback for when the computation completes<br>        future.thenAccept(result -&gt; System.out.println(result));<br>    }<br>}</pre><p>CompletableFuture simplifies asynchronous programming by allowing developers to chain operations on the completion of tasks.</p><h4>CompletableFuture Chaining and ForkJoinPool:</h4><p>CompletableFuture in Java is a powerful class introduced in Java 8 that provides a way to perform asynchronous operations and compose them in a clean and readable manner. It allows you to chain promises and define how to handle the result of an asynchronous computation.</p><h4>Chaining Promises with CompletableFuture:</h4><pre>public class CompletableFutureChainingExample {<br>    public static void main(String[] args) {<br>        CompletableFuture&lt;String&gt; future = CompletableFuture<br>            .supplyAsync(() -&gt; &quot;Hello&quot;)<br>            .thenApplyAsync(result -&gt; result + &quot; CompletableFuture&quot;)<br>            .thenApplyAsync(result -&gt; result + &quot; Chaining&quot;);<br>        future.thenAcceptAsync(result -&gt; System.out.println(result));<br>        // Ensure the main thread waits for the asynchronous computation to complete<br>        future.join();<br>    }<br>}</pre><p>In this example, supplyAsync initiates an asynchronous computation that produces the string &quot;Hello.&quot; Then, thenApplyAsync and subsequent thenApplyAsync operations are used to chain promises, each transforming the result further. Finally, thenAcceptAsync is used to print the final result.</p><h4>ForkJoinPool Under the Hood:</h4><p>CompletableFuture uses the ForkJoinPool to manage the execution of asynchronous tasks. The ForkJoinPool is a special-purpose implementation of the ExecutorService interface, designed for parallel programming.</p><h4>How ForkJoinPool Works:</h4><p>Work Stealing:</p><ul><li>ForkJoinPool uses a work-stealing algorithm. Each worker thread has its own deque (double-ended queue) of tasks. When a thread finishes its tasks, it can “steal” tasks from the back of another thread’s deque, ensuring a more balanced workload distribution.</li></ul><p>Task Splitting:</p><ul><li>When a task is submitted to the ForkJoinPool, it may be split into subtasks if it’s large enough. These subtasks can be executed in parallel by different worker threads.</li></ul><p>CompletableFuture’s Async Methods:</p><ul><li>Methods like supplyAsync and thenApplyAsync submit tasks to the ForkJoinPool for asynchronous execution. The ForkJoinPool decides how to allocate these tasks to available worker threads.</li></ul><p>Combining Results:</p><ul><li>When tasks are split and executed asynchronously, the ForkJoinPool combines the results to provide the final outcome. This mechanism ensures efficient parallelism and utilization of available resources.</li></ul><p>Parallel Execution with CompletableFuture:</p><p>CompletableFuture leverages the ForkJoinPool to achieve parallel execution of tasks, especially when combining promises through methods like thenApplyAsync. This parallelism can lead to improved performance, particularly in scenarios where computations can be executed concurrently.</p><p>It’s important to note that while CompletableFuture and ForkJoinPool provide a convenient way to express and execute asynchronous computations, understanding the underlying mechanics helps developers make informed decisions when designing and optimizing concurrent systems.</p><h3>Reactive Programming: Project Reactor</h3><p>Reactive programming is an event-driven paradigm that deals with asynchronous data streams. Project Reactor, a reactive library developed by the Spring team, is a powerful alternative to RxJava for implementing reactive programming in Java.</p><pre>public class ProjectReactorExample {<br>    public static void main(String[] args) {<br>        Mono&lt;String&gt; mono = Mono.just(&quot;Hello, Project Reactor!&quot;);<br><br>        // Subscribe to the mono and define how to handle the emitted item<br>        mono.subscribe(item -&gt; System.out.println(item));<br>    }<br>}</pre><p>Project Reactor builds on the reactive paradigm, providing powerful operators similar to RxJava while integrating seamlessly with the Spring ecosystem.</p><p>In this example, the map operator transforms the value emitted by the original Mono by appending &quot; Reactor&quot; to it. The final result is then printed when the subscribe method is called.</p><h4>FlatMap Example:</h4><pre>import reactor.core.publisher.Mono;<br>import reactor.core.publisher.Flux;<br><br>public class ReactorFlatMapExample {<br>    public static void main(String[] args) {<br>        Mono&lt;String&gt; originalMono = Mono.just(&quot;Hello&quot;);<br>        Flux&lt;String&gt; flatMappedFlux = originalMono.flatMap(result -&gt; Flux.just(result, result + &quot; Reactor&quot;));<br>        flatMappedFlux.subscribe(result -&gt; System.out.println(result));<br>    }<br>}</pre><p>In this example, the flatMap operator allows the transformation of a Mono into a Flux. It emits the original value and a modified value, and the result is printed when subscribing.</p><h4>Chaining Multiple Tasks and Callbacks:</h4><pre>import reactor.core.publisher.Mono;<br>import reactor.core.scheduler.Schedulers;<br><br>public class ReactorChainingExample {<br>    public static void main(String[] args) {<br>        Mono.just(&quot;Hello&quot;)<br>            .subscribeOn(Schedulers.parallel()) // Switch to parallel scheduler for the next task<br>            .map(result -&gt; result + &quot; World&quot;)<br>            .flatMap(result -&gt; Mono.just(result + &quot; from Reactor&quot;))<br>            .doOnNext(result -&gt; System.out.println(&quot;Result: &quot; + result))<br>            .subscribe();<br>    }<br>}</pre><h3>Comparison</h3><h4>Ease of Use</h4><ul><li>Akka: The actor model can introduce a learning curve, especially for developers unfamiliar with the paradigm. However, once understood, it provides a clear and expressive way to handle concurrency without low-level thread management.</li><li>CompletableFuture: Known for its simplicity, CompletableFuture is easy to grasp, and the fluent API allows developers to chain asynchronous operations seamlessly. It’s well-suited for scenarios where simplicity is a priority.</li><li>Project Reactor: Balancing simplicity and expressiveness, Project Reactor’s API design is intuitive. It integrates well with the Spring ecosystem, providing a familiar environment for Spring developers. It may have a slight learning curve for those new to reactive programming.</li></ul><h4>Efficiency:</h4><ul><li>Akka: Efficient in managing large-scale concurrent systems, providing lightweight actors for isolation and high-speed message passing. Ideal for scenarios where scalability and fault tolerance are critical.</li><li>CompletableFuture: Efficient for small to medium-scale asynchronous tasks, but resource contention may occur in larger systems with a high number of tasks. It may not be as optimized for scenarios with massive parallelism.</li><li>Project Reactor: Efficient for handling reactive streams, particularly in a non-blocking, asynchronous environment. Its focus on reactive principles makes it well-suited for scenarios where responsiveness and efficient event processing are essential.</li></ul><h4>Complexity:</h4><ul><li>Akka: Relatively complex due to the actor model, message passing, and actor interactions. The complexity may be justified for large-scale systems but could be overkill for simpler applications.</li><li>CompletableFuture: Offers simplicity but can become complex when dealing with more intricate scenarios, especially when chaining multiple completion stages. Well-suited for straightforward asynchronous tasks.</li><li>Project Reactor: Strikes a balance between simplicity and expressiveness. While it introduces reactive programming concepts, it integrates seamlessly with the Spring ecosystem, making it suitable for both simple and complex scenarios.</li></ul><h3>Conclusion:</h3><p>Choosing between Akka, CompletableFuture, and Project Reactor depends on the specific requirements and characteristics of your application. Here’s a summary to guide your decision-making process:</p><p>When to Use Akka:</p><ul><li>Scalable, Fault-Tolerant Systems: Akka excels in building large-scale, concurrent, and distributed systems. If scalability and fault tolerance are paramount, especially in scenarios with high concurrency and distributed components, Akka’s actor model is a strong choice.</li><li>Complex Workflows: For applications with intricate business logic and complex workflows, where isolating components and managing concurrency in a message-driven fashion is beneficial, Akka provides a robust framework.</li></ul><p>When to Use CompletableFuture:</p><ul><li>Simpler Asynchronous Tasks: CompletableFuture is well-suited for simpler asynchronous tasks and scenarios where you need to chain asynchronous operations in a straightforward manner.</li><li>Integration with Existing Codebase: If you’re working within an existing codebase and want to introduce asynchronous processing without significant changes, CompletableFuture offers a seamless integration due to its compatibility with traditional synchronous Java code.</li></ul><p>When to Use Project Reactor:</p><ul><li>Reactive Applications: Project Reactor shines in building reactive applications, especially when handling streams of data and events. It integrates seamlessly with the Spring ecosystem, making it a natural choice for Spring-based projects.</li><li>Asynchronous Composition: If your application requires composing and chaining multiple asynchronous tasks with operators like map, flatMap, and other reactive patterns, Project Reactor provides a clean and expressive way to do so.</li><li>Concurrency with Callbacks: Project Reactor’s support for callbacks and reactive programming concepts makes it suitable for scenarios where you need to handle concurrency and asynchronous tasks with a reactive approach.</li></ul><p>Ultimately, the choice between Akka, CompletableFuture, and Project Reactor is influenced by the nature of your application, the complexity of your concurrency requirements, and your team’s familiarity with the respective frameworks. Consider the specific strengths of each approach in terms of scalability, simplicity, and reactive programming to make an informed decision that aligns with your project’s goals.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f89cbfcb045" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Classification and machine learning algorithms comparison]]></title>
            <link>https://techiscoop.medium.com/classification-and-machine-learning-algorithms-comparison-abed11b753eb?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/abed11b753eb</guid>
            <category><![CDATA[logistic-regression]]></category>
            <category><![CDATA[model-comparison]]></category>
            <category><![CDATA[support-vector-machine]]></category>
            <category><![CDATA[naive-bayes]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Mon, 22 May 2023 23:44:34 GMT</pubDate>
            <atom:updated>2023-05-22T23:44:34.285Z</atom:updated>
            <content:encoded><![CDATA[<h4>Background</h4><p>We’ll use iris dataset for explaining machine learning algorithms available in scikit-learn python library. Sample data, the target is one of the 3 types of Iris flower.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*KPQUYzG105t1pLolGKNB1g.png" /></figure><h4>Support Vector Machines (SVM)</h4><p>In simple terms, the formula for Support Vector Machines (SVM) can be explained as follows:</p><p>Imagine you have a set of data points that belong to two different categories (e.g., blue and red). Your goal is to draw a line that separates the blue points from the red points as well as possible. But you don’t just want any line — you want the best line that separates the two categories. In the example below, we are trying to find type of iris flower (2 categories shown in graph).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*s8CiWDieAr7FiobeIi4OEg.png" /></figure><p>To find this line, SVM uses a mathematical formula that considers the distance between the line and the closest data points from each category. These closest data points are called support vectors. The formula tries to find the line that maximizes the distance between the support vectors from each category. This line is called the maximum margin hyperplane.</p><p>The formula also takes into account the weights and biases of the line. These help adjust the line so that it best separates the two categories.</p><p>Once SVM has found the maximum margin hyperplane, it can use it to classify new data points as either blue or red depending on which side of the line they fall on.</p><p>So, in essence, the formula for SVM helps find the best line that separates two categories of data points and can be used to predict the category of new data points.</p><h4>Naive Bayes</h4><p>Naive Bayes is a type of supervised learning algorithm used for classification. The algorithm is based on Bayes’ theorem, which assumes that the features are independent of each other. The mathematical formula for Naive Bayes can be represented as follows:</p><p>P(y|x) = P(x|y) * P(y) / P(x)</p><p>where P(y|x) is the probability of the output y given the input x, P(x|y) is the probability of the input x given the output y, P(y) is the prior probability of the output y, and P(x) is the prior probability of the input x.</p><p>A line graph cannot be used to illustrate the formula for Naive Bayes. Instead, the algorithm uses probabilities to determine the class of the input.</p><h3>SVM vs Naive Bayes using Confusion Matrix</h3><p>To compare the performance of the algorithms, we can use a confusion matrix. A confusion matrix is a table that summarizes the performance of a classification algorithm. The matrix shows the true positive (TP), true negative (TN), false positive (FP), and false negative (FN) rates. Compute confusion matrix to evaluate the accuracy of a classification.</p><p>By definition a confusion matrix C is such that C_{i, j} is equal to the number of observations known to be in group i and predicted to be in group j.</p><p>SVM Confusion matrix</p><pre>[[22  0  0]<br> [ 0 32  2]<br> [ 0  0 19]]</pre><p>Naive Bayes Confusion Matrix</p><pre>[[22  0  0]<br> [ 0 33  1]<br> [ 0  3 16]]</pre><h3>Logistic Regression</h3><p>It’s type of supervised learning where you can find probability of classifying a iris flower to be type a or b. In binary classification the result probability if &gt; 0.5, we classify true and false otherwise. We can determine probability using sigmoid formula:</p><p>y = 1 / 1+ e^-z</p><p>If we reverse engineer the formula:</p><p>y = e^z / 1 + e^z</p><p>z = b0 + b1*x (same as linear regression, where b0 is intercept and b1 is coefficient</p><p>Now, if we put z value in original formula, we get</p><p>y = e^(b0 + b1*x) / 1 + e^(b0 + b1*x)</p><p>we can rewrite it as,</p><p>log(y/1-y) = b0 + b1*x</p><p>Once you plot the sigmoid, the binary classification outcome is either true or false.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*ZYwzlCynLESFyAkiGPLeng.png" /></figure><p>Logistic Regression can be used for multiple categories classification as well.</p><p>Confusion matrix</p><pre>[[27  0  0]<br> [ 0 24  0]<br> [ 0  4 20]]</pre><h3>Random Forest</h3><p>Random Forest is a type of supervised learning algorithm used for classification, regression, and other tasks. The algorithm creates multiple decision trees and combines their outputs to make a final prediction. The mathematical formula for Random Forest is complex and involves multiple decision trees.</p><p>A line graph cannot be used to illustrate the formula for Random Forest. Instead, the algorithm creates a forest of decision trees, and each decision tree represents a branch of the forest.</p><p>more to be continued..</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abed11b753eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding the Popular Machine Learning Algorithm: Linear Regression]]></title>
            <link>https://techiscoop.medium.com/understanding-the-popular-machine-learning-algorithm-linear-regression-ca1adb8336b9?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca1adb8336b9</guid>
            <category><![CDATA[scikit-learn]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[linear-regression]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Wed, 22 Mar 2023 06:30:10 GMT</pubDate>
            <atom:updated>2023-03-22T18:00:45.163Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Machine learning algorithms have become a vital part of many industries, from healthcare to finance, from marketing to entertainment. In this article, we will explore a popular machine learning algorithms, namely Linear Regression. We will explain the mathematical formula behind the algorithm and provide example to illustrate how each algorithm works. Finally, we will compare the algorithms using a confusion matrix and offer suggestions on which algorithm might be the most suitable for different applications.</p><h3>Linear regression</h3><p>Linear regression is a statistical method used to study the relationship between two variables. It is used to predict a numerical value (dependent variable) based on a given input (independent variable). The formula for linear regression is <em>y = mx + b</em>, where <em>y</em> is the dependent variable, <em>x</em> is the independent variable, m is the slope of the line, and b is the y-intercept.</p><p>Let’s take an example to understand this better. Suppose we want to predict the weight of a person based on their height. Here, weight is the dependent variable, and height is the independent variable. We have the following data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*LQDNJRw3znvGZTwcJpqW0Q.png" /></figure><p>To find the equation of the line that best fits this data, we use the linear regression formula. We first calculate the slope of the line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*xpgxXJE5NHXImtdVM1A59A.png" /><figcaption>x̄ is the mean of x, ȳ is the mean of y, and Σ represents the sum of values</figcaption></figure><p>(x-x̄)/Sx is zcore which is how far a point is from mean in terms of std. deviation. For example, for height 65: (65–64.5)/7.14 ~= 0.07 (this is closer to mean); for height 74: (74–64.5)/7.14 ~= 1.33 (taller than the mean)</p><p>The r (coefficient), will give us normalized value how far every data point is from the std. dev, with this value we can calculate slope which will have least error (mean of total squared value red lines) as shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/1*SC4EDdL0_kYID_pCEU5ReA.png" /></figure><p>Plugging in the values, we get:</p><pre>Sx = Std deviation of x = 7.141428429<br>Sy = std deviation of y = 50.11569947<br>x̄ = 64.5<br>ȳ = 120.25<br>r = 0.6639467442<br>m = 4.659313725</pre><p>Next, we calculate the y-intercept: b = ȳ — mx̄</p><pre>Plugging in the values, we get:<br>b = -180.2757353<br>Therefore, the equation of the line that best fits this data is:<br>Weight = 4.659313725 * Height - 180.2757353</pre><p>We can use this equation to predict the weight of a person based on their height.</p><pre>For example, if a person is 70 inches tall, we can predict their weight as follows:<br>Weight = 4.659313725 * Height - 180.2757353<br>Weight = 145.8762255</pre><p>Therefore, the predicted weight of a person who is 70 inches tall is 145.88 pounds.</p><h3>Apply linear regression for a large dataset</h3><p>We’ll use California housing data from scikit-learn python library to predict average house value:</p><pre>from sklearn.datasets import fetch_california_housing<br>from sklearn.linear_model import LinearRegression<br>from sklearn.model_selection import train_test_split<br><br># Load the California Housing dataset into a pandas DataFrame<br>housing = fetch_california_housing()<br>data = pd.DataFrame(housing.data, columns = housing.feature_names)<br>target = pd.DataFrame(housing.target)<br><br># Split the selected data into training and testing sets<br>X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.5, random_state=42)<br><br># Train a linear regression model on the training data<br>model = LinearRegression()<br>model.fit(X_train, y_train)<br><br># Make predictions on the test data<br>y_pred = model.predict(X_test)<br><br># Print the mean squared error of the model on the test data<br>from sklearn.metrics import mean_squared_error<br>print(&quot;Mean squared error: %.2f&quot; % mean_squared_error(y_test, y_pred))</pre><p>Mean squared error: 0.53 (mean of sum of (actual value — predicted value))²</p><p>The least squared line(or slope line) using the slope formula: y_pred=mx+b</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*PBtTU_A2QNpXhO_l2YVUhg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca1adb8336b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Modern API Design]]></title>
            <link>https://techiscoop.medium.com/morder-api-design-7a4d0d1cf53a?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a4d0d1cf53a</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[api-security]]></category>
            <category><![CDATA[rest]]></category>
            <category><![CDATA[jwt]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Thu, 27 Oct 2022 05:16:33 GMT</pubDate>
            <atom:updated>2023-06-30T18:20:44.069Z</atom:updated>
            <content:encoded><![CDATA[<p>API is fundamental building block for modern day software development. It allows two different subsystems to communicate with each other through a common interface.</p><h3><strong>Rest API</strong></h3><p><strong>When and why to use Rest API?</strong></p><p>It’s lightweight service to service communication mechanism.</p><ul><li>Easy and simple to use</li><li>Designing for web, mobile or end user</li><li>Endpoints per resources to be consumed by end user</li><li>Endpoint facade for complex applications on top of existing simple endpoints</li></ul><p><strong>Rest API Facade</strong></p><p>If you have disjoint various services developed over time to support your business needs, we can add a API facade layer to come up with common rest api layer on top of existing subsystems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_0In3ZinkhS9Nw43" /></figure><p>Examples: API facade will expose account deletion endpoint, which will route request to system of records. System of records is soap based legacy web-service. API facade will translate rest request response into soap envelop. API facade will have another endpoint for routing dispute order to dispute service.</p><p><strong>Rest API Design Principles</strong></p><p>Rest works directly on top of HTTP. To get a list of resources, can design a rest api endpoint and make a request using HTTP GET. Response typically will return HTTP Status Codes defined RFC 7231 along with actual response content.</p><ul><li>Use plural nouns, for e.g <a href="https://domain.com/api/movies">https://somedomain.com/api/movies</a></li><li>Actions can be query parameters or HTTP Verbs</li><li>Ex: create new movie POST /api/movies<br>- update a movie PUT or PATCH api/movies/{movie_id}<br>- delete a movie DELETE api/movies/{movie_id}</li><li>update relationships<br>- PUT or PATCH api/movies/{movie_id}/showtimes<br>- { data : [ { “theaterName” : “AMC20SanJose”, “timings” : [ “9AM” ], “screen” : [ “1” ] } ] }</li></ul><p>Rest API Versioning</p><ul><li>Embed part of URL, ex api/v1.0/tickets</li><li>Version Headers: x-api-version: 2.0</li></ul><p>Asynchronous API</p><ul><li>Example: Submit a job which creates 100 movies in bulk from an input csv file<br>- Accept request and return 202 response with job_id<br>- GET /api/jobs/{job_id} to get status</li><li>Error Codes: HTTP response codes : RFC 7231<br>- 200 Success, 404 Not found, 401 Unauthorized, 429 too many requests, 500 internal error</li></ul><p>Error response body (single or multiple error objects): <a href="http://jsonapi.org/">jsonapi.org</a></p><ul><li>code : error code</li><li>title: error title</li><li>detail: detail error message</li></ul><p>Searching and filtering</p><ul><li>query parameters ?search=&lt;search keywords&gt;</li><li>?filter=&lt;filter parameters&gt;</li></ul><p>Pagination</p><ul><li>cursor or page numbers</li><li>query parameters page[offset]=0&amp;page[limit]=10</li></ul><p>Sorting</p><ul><li>sort=field1,field2</li><li>sort=-field1 (descending order sort)<br><a href="https://jsonapi.org/format/">https://jsonapi.org/format/</a></li></ul><h3>GraphQL</h3><p><strong>When to use GraphQL?</strong></p><p>GraphQL is more complex compare to rest, it’s used when you need to combine data from various subsystems and may have limited bandwidth so you are trying to reduce number of API calls to a single GraphQL call to fetch your data. A typical use-case is IOT / mobile devices.</p><ul><li>Strongly typed schema</li><li>Schema stitching</li><li>Bandwidth (IOT / Mobile devices)</li><li>Composite pattern (consolidate data from various places)</li><li>Proxy pattern</li><li>Add fields and deprecated fields on fly (no versioning)</li></ul><p><strong>What is GraphQL</strong></p><ul><li>GraphQL supports 3 types of operations: Query, mutation and subscription</li><li>Runs on HTTP (typically POST, supports GET as well)</li><li>Schema is mandatory</li></ul><p><strong>Sample GraphQL Request</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vT0DRlKYferHOh1j" /></figure><p>GraphQL — Schema Example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xiJRJM6Qo5R77u8r" /></figure><h3>gRPC</h3><ul><li>Remote procedure calls by default uses protocol buffer</li><li>Protocol buffer<br>- platform neutral<br>- extensible mechanism for serializing structured data<br>- fast and simple<br>- binary encoding (compact)</li></ul><h4>When to use gRPC?</h4><p>Recommended for point A to point B communication requires low latency, high performance and lightweight mechanism for remote api and client/server streaming.</p><h3>API Security</h3><p><strong>API Security using JWT (JSON Web Token)</strong></p><p>JWT is typically used in HTTP header to authenticate / authorize end user or client to a server.</p><ul><li>Header contains type of algorithm used for signature</li><li>Payload contains various claims to validate the jwt token<br>- in below example, exp (expiration time), aud (target audience)</li><li>Signature is generated with header + payload using secreteKey.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*Iq8poOS3BjWmQVaKarZOJw.png" /></figure><p><strong>Secure Communication using JWT</strong></p><p>Example Service to service communication using JWT.</p><ol><li>When service1 starts, calls Secure token service to create public key &amp; private key pair. It also store public key id with the public key mapping.</li><li>Using private key, service1 creates JWT token.</li><li>Calls service2’s endpoint with JWT token in auth header</li><li>Service2 calls STS service to lookup Service1’s public key using jwt key id</li><li>Service2 tries to parse the JWT token</li><li>If successful, verifies the claims and call downstream service or resource.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uyl1oWzR-nz6fQuXG09WGQ.png" /></figure><p><strong>API Security Best Practices</strong></p><p><strong>Don’t -</strong></p><ul><li>Store key as a plain text in code</li><li>Store in Dockerfile or environment variables</li></ul><p><strong>How to store key securely -</strong></p><ul><li>Vault (<a href="http://www.vaultproject.io/">www.vaultproject.io</a>)</li><li>AWS Key Management Service (KMS)</li></ul><p><strong>Reference -</strong></p><ul><li><a href="https://owasp.org/www-pdf-archive/Microservice_Security.pdf">https://owasp.org/www-pdf-archive/Microservice_Security.pdf</a></li></ul><h3>API Scalability</h3><ul><li>Measure Response Time and Throughput of your service (# of request processed per given time duration)</li><li>Check avg, p90, p99 to see if we have requests are taking longer than others (aka outliers)</li></ul><p><strong>Improve Latency</strong></p><ul><li>horizontal scaling</li><li>replication: routing to nearest server</li><li>Caching</li></ul><p>Other performance improvements:</p><ul><li>Add new db Indexes for slow queries</li><li>Code optimizations</li></ul><p><strong>Fault Tolerance</strong></p><ul><li>Circuit breakers (resiliancy4j) (Open Circuit from 0–50% when downstream circuit is down and return a default response, if all requests timeout for certain duration open circuit 100%)</li><li>Rate limiter supported headers: RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a4d0d1cf53a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning API and Scaling]]></title>
            <link>https://techiscoop.medium.com/machine-learning-api-and-scaling-e7831733c2ef?source=rss-5ef0e724ea03------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7831733c2ef</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Rupal]]></dc:creator>
            <pubDate>Wed, 14 Sep 2022 04:44:57 GMT</pubDate>
            <atom:updated>2022-09-14T04:44:57.398Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is machine learning?</h3><p>Machine learning tries to understand existing patterns and makes decision. In other words, tries to find relationship among data and use it to make decision.</p><h3>Why do we use machine learning?</h3><p>ML makes it easy to make data driven decisions without human intervention. ML can evolve over time meaning improve decision making accuracy and it can be continuous.</p><h3>ML Use-cases</h3><ul><li>Identifying tags and categories from textual data</li><li>Weather prediction</li><li>Recommendations</li><li>Fraud detections</li></ul><h3>ML Pipeline</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3GllkEvNM-7iMqhhPgIgYQ.png" /></figure><h4>Step 1: Prepare training Data</h4><p>If we have textual data and trying to predict the outcome, we need to convert textual representation into machine readable format which is numerical format.</p><p>As a first step, we can cleanse the data (remove nulls, duplicates, punctuations) and apply stemming (convert riding, rode, ride ==&gt; ride)</p><p>Let’s convert textual data to numerical format. One such conversion can be done using CountVectorizer. Identify Unique feature names in the input.</p><h4>Step 2: Extract Features</h4><p><strong>Input</strong></p><p>‘This is the first document.’</p><p>‘This document is the second document.’</p><p>‘And this is the third one.’</p><p>‘Is this the first document?’</p><p>[‘and’, ‘document’, ‘first’, ‘is’, ‘one’, ‘second’, ‘the’, ‘third’, ‘this’]</p><p><strong>CountVectorizer</strong> (Document term matrix<strong>)</strong></p><p>[[0 1 1 1 0 0 1 0 1]</p><p>[0 2 0 1 0 1 1 0 1]</p><p>[1 0 0 1 1 0 1 1 1]</p><p>[0 1 1 1 0 0 1 0 1]]</p><h4>Step 3: Build ML Model</h4><p>In Supervised machine learning, select an algorithm, specify samples(input data) and target(expected result) for building the model</p><h4>Step 4: Make Predictions</h4><ul><li>Predict live traffic: e.g credit card transaction fraudulent</li><li>Recommendations</li><li>Suggestions</li><li>Auto complete</li></ul><h3>Machine Learning Model</h3><p><strong>Linear Regression </strong>is method to study relationship between a dependent variable (Y) with a given set of independent variables (X). The relationship can be established with the help of fitting a best line.</p><p>Y = mx + b</p><p>Where b is the intercept and m is the slope of the line. So basically, the linear regression algorithm gives us the most optimal value for the intercept and the slope (in two dimensions). The y and x variables remain the same, since they are the data features and cannot be changed. The values that we can control are the intercept and slope. There can be multiple straight lines depending upon the values of intercept and slope. Basically what the linear regression algorithm does is it fits multiple lines on the data points and returns the line that results in the least error.</p><p><strong>Support Vector</strong> is Supervised learning method for <a href="https://en.wikipedia.org/wiki/Support-vector_machine">classification</a>. Maximizes the hyperplane distance from the classes.</p><p><strong>Multinomial Naive Bayes</strong> <a href="https://en.wikipedia.org/wiki/Naive_Bayes_classifier">classifier</a> is suitable for classification with discrete features (e.g., word counts for text classification). each naive Bayes classifier can be considered a way of fitting a probability model.</p><h3>Scaling</h3><h4>ML Prediction API Python in memory</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fXWisLwGsxBSlCltmUyrmg.png" /></figure><h4>ML Prediction API (Batch processing)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XH3HzoLM_BxHvr_mU-3_ug.png" /></figure><h4>ML Prediction API streaming pipeline</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CVANqUVFfUMgd1rSlwzzqA.png" /></figure><h3>Deployment Options</h3><ul><li>AWS SageMaker or GCP cloud API</li><li>Python flas or Django app</li><li>Tensorflow deploy on k8 cluster on GCP or AWS or if you have your own k8 cluster, can deploy there</li><li>Redis-AI module</li></ul><h3>Resources:</h3><p><a href="https://colab.research.google.com/drive/1htb5fRkkR1KZympzXetzdzVDxYiotWK7?usp=sharing">ML Notebook</a></p><p>Python flask service with prediction api (coming soon)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7831733c2ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>