Math, C# style

Hello everybody,

Michael here, and in today’s post-my last one for 2024-I thought we’d take a quick look at math, C# style.

What do I mean by that? Well, today we’re going to be exploring how C# performs basic math operations. Seems fun, right? Well, let’s dive in!

Math Class, C# style

Like several other programming languages we’ve covered on this blog, C# also has its own way of doing math. Let’s take a look at some of the many mathematical methods of C#:

Console.WriteLine(Math.Max(3, 12));
Console.WriteLine(Math.Abs(-333.22));
Console.WriteLine(Math.Min(3, 12));
Console.WriteLine(Math.Sqrt(49));

Here are the respective outputs of these four methods:

12
333.22
3
7

In this example, I demonstrated four basic methods of the C# Math class-Max, Abs, Min and Sqrt. These methods calculate the maximum value in a numerical range, the absolute value of a number, the minimum value in a numerical range, and the square root of a number, respectively.

  • If you want to print out the results of these calculations, remember to wrap each Math method call inside a Console.WriteLine() method call.

More complex C# calculations

So now that we’ve demonstrated simple C# math methods, let’s show some more complex methods:

Console.WriteLine(Math.Pow(2, 5));
Console.WriteLine(Math.Log10(14));
Console.WriteLine(Math.Cos(84));
Console.WriteLine(Math.Round(45.2423242152332, 2));

32
1.146128035678238
-0.6800234955873388
45.24

In this example, I’m demonstrating four of the Math class’s more complex calculation capabilities-Pow, Log10, Cos, and Round-which calculate the value of a number raised to a specific power, the base-10 logarithm of a number, the cosine of an angle, and a decimal rounded to 2 places, respectively.

  • You may recognize some of these concepts from the math lessons I posted earlier in this blog’s run.

A special note about very big and very small numbers

Now, what if you wanted to use a very big or very small number in your C# math calculation? Theoretically, you could write out the number in its entirety, such as 20,000,000,000 (20 billion). However, that doesn’t seem like the most efficient way to code, does it?

If you’ve ever taken pre-algebra, you might recall a little concept known as scientific notation, which is a convenient shorthand way of writing very big and very small numbers. For instance, 20 billion would be written as 2 x 10^10 in scientific notation, which seems much more readable than 20000000000.

C# works in a similar manner. However, it doesn’t use the 10^[something] notation that you might be familiar with from math class. Rather, C# uses what’s called e-notation, which serves the same purpose as conventional scientific notation, but replaces the 10^[something] with e[something]. So in the case of 20 billion, the e-notation for that number would be 2e10.

Also, similar to scientific notation, numbers smaller than 1 would be represented with e-[something]. For instance, the number 0.0000002 would be represented as 2 x 10^-7 in scientific notation and 2e-7 in e-notation.

Console.WriteLine(Math.Pow(4e-5, 5));
Console.WriteLine(Math.Log10(16e4));
Console.WriteLine(Math.Cos(8e1));
Console.WriteLine(Math.Round(42e5+2.16, 2));

1.0240000000000004E-22
5.204119982655925
-0.11038724383904756
4200002.16

As you can see, the calculations worked the same as they did with the regular numbers. The only difference is that if a number is too big or too small to feasibly display on the output, that number will be represented with e-notation (as shown by our first example with the Math.Pow method).

Also, if you want to learn more about the C# Math class, check out Microsoft’s documentation (all documentation for C# is provided by Microsoft)-https://learn.microsoft.com/en-us/dotnet/api/system.math?view=net-9.0.

Before I go, here’s the GitHub link to my script for this lesson-https://github.com/mfletcher2021/blogcode/blob/main/MathClass.cs.

Thanks for coding along with me in 2024! It’s been another great coding year, and I hope you and your loved ones have a happy and healthy and festive and joyous holiday season! With that said, Michael’s Programming Bytes will return with new content in…

What creative coding content will I come up with in 2025? You’ll just have to follow along and see.

Happy holidays and thanks as always for coding along.

Michael

C# Variables, Comments & Data Types

Hello everyone,

Michael here, and in today’s post, we’ll explore C# variables, comments (notes that developers leave on code to explain it to other developers), and data types.

For many of the other programming tools we’ve covered on this blog’s journey like Python and Java, C# has its own ways of dealing with variables, comments, and data types.

In Python, here are some common variables, comments, and data types:

Variables

a = 3
b = 2

print(a+b)

5

Comments (single hashtag)

# This will print out the sum of 3+2

a = 3
b = 2

print(a+b)

5

Data types

complex
int 
str
bool 
dict

Likewise, in Java, here are some common variables, comments, and data types

Variables

String name = "Michael";
int year = 2024;

Comments

// This is a code comment
System.out.println("Hello world")

Data types

int
float
String
char

The variables, comments, and data types (C# version)

Now that we’ve reviewed variables, comments, and data types in Java and Python, let’s explore how these concepts work in C#.

Take this simple C# script, filled with variables, comments and data types:

// This code is writing a simple sentence with both string and int variables
int year = 2024;
string name = "Michael";
string month = "December";

Console.WriteLine(name + " is writing this post in " + month + " " + year);

Let’s examine this script line-by-line:

  • The top line with the double backslashes // signifies a comment being added to the script. Like Java and Python, C# comments are single-line deals.
  • int year = 2024; indicates that the int value in this script will be 2024
  • The two string variables in this script indicate that the two string values I will use in this script are Michael and December, respectively.
  • The Console.WriteLine() section of the script will print out my output to a command line-in this case, the string I want to output is Michael is writing this post in December 2024.
  • Another thing worth noting-to concatenate values in C#, use the + (plus) sign to do so. As you can see, I used the plus sign several times to concatenate my string values with other text in order to generate the following line of code: name + " is writing this post in " + month + " " + year.

Now that we’ve explored some basic examples of variables, data types, and comments in C#, what are some other data types in C#:

  • long-think of this as the 8-bit version of the int data type in C# (int is a 4-bit data type) that stores a broader range of integers
  • float-a 4-bit decimal type that stores values up to 6-7 decimal places
  • double-an 8-bit decimal type that stores values up to 15 decimal places
  • bool-the C# boolean type which stores true or false values
  • char-the C# character type which stores single characters
  • string-the C# string type which stores string values

Variables, Data Types, and Comments in C#: Things to Note

What are some important things to note about variables, data types, and comments in C# that are either similar to or perhaps quite different from variables, data types, and comments in other languages we’ve covered throughout this blog’s run?

  • In C#, comments are always indicated with the // (double backslash) symbol.
  • Just like in Java, you must specify a variable’s data type in the script (like I did with int year = 2024; for example).
  • Unlike Python, C# has no data types to handle complex/imaginary numbers (like 3+5i for example).
  • The boolean truth table rules that apply to languages like Java and Python also apply to C# (e.g. individual statements A & B must be true for a boolean statement to be true).

Also, here’s my script from this lesson on my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IntroToCSharp.cs.

Thanks for reading,

Michael

Hello World, C# style

Hello everybody,

Michael here, and today I thought I’d introduce a new programming language to this wonderful blog-C#. This will be the ninth new programming tool I’ve introduced on this blog, after:

  • R
  • MySQL
  • Java
  • Python
  • GitHub
  • HTML
  • CSS
  • Bootstrap

Yea, we’ve covered a lot of programming in the last 6-and-a-half years, but hey, nothing wrong with picking up a new language, am I right?

With that said, let’s introduce ourselves to C#

What is C#?

C# is a general-purpose, object-oriented (much like Python and Java) programming language developed by Microsoft in 2000 that runs on a little something called the .NET framework.

What is the .NET framework? The .NET framework is Microsoft-developed open-source software framework that acts as a runtime environment for C# (.NET also works with other languages too like PowerShell and F#).

Setting up C#

Now, how do we set up an IDE to play around in the C# sandbox? Since C# is a Microsoft-developed application, I’d suggest using a Microsoft-developed IDE for our C# development journey-Visual Studio. Here’s the link to download it; follow all necessary instructions to install the IDE-https://code.visualstudio.com/. Also, unless you want to develop C# applications for a giant (or even small) business, download the Community version of Visual Studio, as that IDE is free to use.

Hello world, C# style

Once you’ve installed the IDE, let’s go play around with it by doing the most cliche of introductory programming tasks-learning to print Hello world (yea I know hello world is a bit overrated for programming language introductions, but I think it works, so I’m gonna use it).

Now, once we’ve installed Visual Studio, let’s set up our new C# project.

In the Visual Studio interface, create a new project. You should see an interface that looks like this:

In the search bar at the top, type in console app and scroll down until you see something that looks like Console App (.NET Framework). Most importantly, remember to select the Console App option with the green C# icon, as we are working with C# (as you can see from this screenshot, you can also build Visual Basic and F# applications).

Click Next, which will take you to this screen:

All you really have to do here is give your project a name (I used MichaelsSandbox here) and click Next, which will take you to the following screen:

Assuming these are the default options, leave them as they are and click Create to create your first C# project. Also, copy this code onto the newly generated file:

using System;

namespace HelloWorld
{
    class MichaelsSandbox
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

After you’ve copied the code onto the new project file, save the file and click on the green-outlined triangle icon to run the script.

Interestingly, the output won’t show on the interface itself but rather on a separate command prompt. Granted, all the code really did was print Hello World! on a command prompt, but that was the whole point-to give you a very very basic introduction to the world of C#.

  • Don’t mind the process ... exited with code 0 message-it’s standard to see it when running your C# programs (though you can turn off this message)

The script, explained

Now, since this is my first C# lesson, let’s explain the script, shall we?

  • using System-this indicates that the script will use the System namespace. In C#, a namespace is a means of organizing your code into containers, which contain various classes and methods. The System namespace acts as the runtime environment for C#, as it contains various fundamental components for C# to run like fundamental data types and input/output mechanisms.
  • namespace HelloWorld-as I mentioned earlier, a namespace is a means of organizing your code into containers with their own classes and methods. In this script, my code will be in the HelloWorld “container”
  • class MichaelsSandbox-Much like in Java and Python, a C# class holds data and methods for a certain aspect of a program’s functionality
  • static void main (String[] args)-This is C#’s version of the main method, much like Java’s public static void main (String[] args) or Python’s def main():-interestingly, Java’s and C#’s main methods are nearly identicial
  • Console.WriteLine("Hello World");-This line prints the specific text onto the console/IDE, much like Java’s System.out.println() method or Python’s print() method.
  • Two things to note about the Console.WriteLine() method-you always need a semicolon at the end of the method call (much like in Java) and you always need to wrap the text value to be printed in double quotes (also much like Java).

Also, here’s the script in my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IntroToCSharp.cs.

Thanks for reading,

Michael

Python, Linear Regression & the 2024-25 NBA season

Hello everybody!

Michael here, and in today’s post, we’ll continue where we left off from the previous post Python, Linear Regression & An NBA Season Opening Day Special Post. As I mentioned in that post, we’ll use the linear regression equation we obtained to see if we can obtain predictions for the current 2024-25 NBA season?

Disclaimer

Yes, I know I’m trying to predict the various juicy outcomes of the 2024-25 NBA season, but these predictions are purely meant for educational purposes to display the methodology of the predictions, not for game-day parlays and/or your fantasy NBA team. After all, I am your friendly neighborhood coding blogger, but I am not your friendly neighborhood sportsbook. If you do decide to bet on anything during the NBA season, please bet responsibly :-).

Previously on Michael’s Programming Bytes…

In the previous post, we used data from the last 10 NBA seasons for each of the 30 teams to predict season record results, which in turn gave us this linear regression equation that I will use to predict team-by-team results and standings for the 2024-25 NBA season:

Just to recap, here’s what in this equation:

  • -0.47x (represents team’s losses in a given season)
  • -1.31x (represents team’s conference finish from 1-15 in a given season)
  • 0.4x (represents average age of team’s roster)
  • 34.13x (represents % of field goals made)
  • -22.12x (represents % of 3-pointers made)
  • 50.95 (linear regression model intercept)

Our predictions generated in the previous post came back with 91% accuracy/9% mean absolute percentage error, so I can tell we’re gonna get some good predictions here.

And now, for the predictions…

Yes, here comes the fun part, the predictions. For the predictions, I gathered the weighted averages of the five features we used in our model (losses, conference finish, average roster age, % of field goals made and % of 3-pointers made) and placed them into this spreadsheet:

Now, how did I calculate the weighted averages of these five features for each team? Well, I simply assigned different weights for different seasons like so:

  • 2021-22 to 2023-24 seasons-0.2 weight (higher weight for the three most recent seasons)
  • 2018-19 to 2020-21 seasons-0.1 weight (they’re a little further back, plus I factored in COVID impacts to the 2019-20 and 2020-21 seasons)
  • 2014-15 to 2017-18 season-0.025 weight (smaller weight since these are the furthest in the past, plus many players in the league during this time have since retired)

After assigning these weights, I calculated averages using the standard procedure for average calculation.

Here’s the basic Python code I used to calculate projected wins for all 30 NBA teams:



import pandas as pd

NBAAVG = pd.read_csv(r'C:\Users\mof39\OneDrive\Documents\NBA weighted averages.csv')

for n in NBAAVG['Team']:
    print(str(-0.47*NBAAVG['L']-1.31*NBAAVG['Finish']
                                      +0.4*NBAAVG['Age']+34.13*NBAAVG['FG%']
                                      -22.15*NBAAVG['3P%']+50.95))
    break

And here are the projected win totals for each team using this equation:

0     37.911934
1     52.863761
2     40.819851
3     31.742252
4     37.524958
5     40.441851
6     42.540851
7     51.103223
8     24.263654
9     45.852691
10    33.197160
11    38.736829
12    47.364055
13    41.338946
14    41.297291
15    45.202762
16    53.722600
17    39.185063
18    37.443009
19    38.462010
20    39.284500
21    32.296571
22    47.795819
23    45.063567
24    33.312626
25    37.493793
26    32.519145
27    42.072515
28    41.920285
29    31.773436

Granted, you don’t actually see the team names in this output, but since the team names are organized alphabetically in the dataset you can tell which team corresponds to which projected win total. However, just for clarity, I’ll elaborate on those totals below:

Atlanta Hawks: 37.911934 wins (38-44)
Boston Celtics: 52.863761 wins (53-29)
Brooklyn Nets: 40.819851 wins (41-41)
Charlotte Hornets: 31.742252 wins (32-50)
Chicago Bulls: 37.524958 wins (38-44)
Cleveland Cavaliers: 40.441851 wins (40-42)
Dallas Mavericks: 42.540851 wins (43-39)
Denver Nuggets: 51.103223 wins (51-31)
Detroit Pistons: 24.263654 wins (24-58)
Golden State Warriors: 45.852691 wins (46-36)
Houston Rockets: 33.197160 wins (33-49)
Indiana Pacers: 38.736829 wins (39-43)
LA Clippers: 47.364055 wins (47-35)
LA Lakers: 41.338946 wins (41-41)
Memphis Grizzlies: 41.297291 wins (41-41)
Miami Heat: 45.202762 wins (45-37)
Milwaukee Bucks: 53.722600 wins (54-28)
Minnesota Timberwolves: 39.185063 wins (39-43)
New Orleans Pelicans: 37.443009 wins (37-45)
New York Knicks: 38.462010 wins (38-44)
Oklahoma City Thunder: 39.284500 wins (39-43)
Orlando Magic: 32.296571 wins (32-50)
Philadelphia 76ers: 47.795819 wins (48-34)
Phoenix Suns: 45.063567 wins (45-37)
Portland Trailblazers: 33.312626 wins (33-49)
Sacramento Kings: 37.493793 wins (37-45)
San Antonio Spurs: 32.519145 wins (33-49)
Toronto Raptors: 42.072515 wins (42-40)
Utah Jazz: 41.920285 wins (42-40)
Washington Wizards: 31.773436 wins (32-50)

As you can see above, I have managed to predict the records for each team for the 2024-25 NBA season. A few things to note about my predictions:

  • Since NBA records are only counted in whole numbers, I rounded each team’s projected win total up or down to the nearest whole number. For instance, for the Milwaukee Bucks, since their projected win total was 53.722600, I rounded that up to 54 wins (and a 54-28 record).
  • According to my model, all team’s projected win totals fall between 24 and 54 wins. This make sense since in a given NBA season, a majority of teams’ win totals fall in the 24-54 win range. In the last NBA season (2023-24), 21 teams fell within the 24-54 win range.
  • Four teams obtained over 54 wins (Celtics with 64, Thunder and Nuggets with 57, and Timberwolves with 56) while five teams obtained less than 24 wins (Spurs with 22, Hornets and Trailblazers with 21, Wizards with 15 and Pistons with 14).
  • One thing to note about my predictions is that while I rounded up or down to the nearest whole number to get a projected record total, I’ll still factor in the entire decimal (e.g. 45.202762 for the Heat) when deciding how to seed teams, as teams with a higher decimal will be seeded higher in their respective conference.

Michael’s Magnificently Way-To-Early Playoff Picture

Yes, now that we have projected record totals for each of the 30 teams, the next thing we’ll do is predict each team’s seeding.

How will we seed the teams? Well, for one, I’ll rank the teams with the higher projected records higher in their respective conference. For instance, since the Bucks have a higher projected record than the Celtics, I’ll rank the Bucks higher than the Celtics.

However, what if two teams have a really, really close margin between them? For instance, the Minnesota Timberwolves and Oklahoma City Thunder’s projected records of 39.185063 wins and 39.284500 wins respectively are very close to each other. However, since OKC has a slightly higher projected win total, I’ll rank them higher than the Timberwolves.

So without further ado, here’s Michael’s Magnificently Way-Too-Early Playoff Picture!

Eastern Conference

INTO THE PLAYOFFSINTO THE PLAY-INOUT OF PLAYOFF RUNNING
1. Milwaukee Bucks7. Cleveland Cavaliers11. Chicago Bulls
2. Boston Celtics8. Indiana Pacers12. Washington Wizards
3. Philadelphia 76ers9. Atlanta Hawks13. Charlotte Hornets
4. Miami Heat10. New York Knicks14. Orlando Magic
5. Toronto Raptors15. Detroit Pistons
6. Brooklyn Nets

Western Conference

INTO THE PLAYOFFSINTO THE PLAY-INOUT OF PLAYOFF RUNNING
1. Denver Nuggets7. LA Lakers11. Sacramento Kings
2. LA Clippers8. Memphis Grizzlies12. New Orleans Pelicans
3. Golden State Warriors9. Oklahoma City Thunder13. San Antonio Spurs
4. Phoenix Suns10. Minnesota Timberwolves14. Portland Trailblazers
5. Dallas Mavericks15. Houston Rockets
6. Utah Jazz

And now, for some insights

Now that we have our predictions for both team’s projected win totals and projected conference seeding, let’s see if we can gather some insights into what the 2024-25 NBA season might bring for all 30 teams. Without further ado, here are insights across the NBA that I think will be interesting to see play out over the course of the season:

Will the Celtics repeat as champs?

For those who don’t know, the Boston Celtics came out on top as the champions of the 2023-24 NBA season, beating the Dallas Mavericks in 5 games in the 2024 NBA Finals.

Question is, can they do it again? There’s a good chance that can happen, even with the projected 2-seed in the Eastern Conference. After all, the Celtics have kept many of their key playmakers from their championship squad such as Al Horford, Derrick White, Jaylen Brown and of course, Jayson Tatum.

Interestingly, we’ve had SIX different teams win the NBA championship in the last six seasons, such as:

  • 2019-Raptors
  • 2020-Lakers
  • 2021-Bucks
  • 2022-Warriors
  • 2023-Nuggets
  • 2024-Celtics

Could we have a repeat champ for the first time since those seemingly endless Warriors-Cavs finals (remember those)? I’ll reiterate that it’s certainly possible, especially with Tatum in his prime.

Warriors for a deep playoff run?

Yes, I know they’ve had their ups and downs over the last 10 years, but after all, the Golden State Warriors have won 4 championships over the last 10 years, so I have reason to believe they’ll go on another deep playoff run.

Will the loss of Klay Thompson hurt? Yes. Stephen Curry is also on the back-nine of his career (he turns 37 in March), but he did put up the most points per game of anyone on the Warriors’ roster last season (26.4). Curry also had the highest 3-pointer percentage of anyone on the Warriors’ roster last season (40.8%)-recall that successful 3-pointer percentage was one of the five features I used in the linear regression model. Plus, Draymond Green will be returning to the Warriors this eason; he proved to be one of the Warriors’ strongest 3-point shooters and rebounders last season (though he is also in his later career as he will be 35 in March).

Interestingly, this model has the Warriors going 46-36 as the 3-seed in the Western Conference. Funny enough, the Warriors finished 46-36 last season but ended up as the 10-seed in the Western Conference and failed to make it past play-in.

This brings me to my next point…

Will the West be close again?

Last season, the Western Conference was incredibly close when it came to win totals and playoff seeding. After all, the 6-seed in the West last year (Phoenix Suns) still finished with a 49-33 record…and were promptly swept in the Western Conference first round (though that’s neither here nor there).

Another thing to put the closeness of last year’s Western Conference playoff race into perspective-the Warriors finished 46-36 yet only notched a 10-seed and the Houston Rockets finished with an even 41-41 record but missed the postseason entirely (they got the 11-seed).

Which brings be to my next point…

Will the East be far apart?

While last year’s Western Conference was quite competitive, the Eastern Conference was, well, another story:

Image from Wikipedia: https://en.wikipedia.org/wiki/2023%E2%80%9324_NBA_season.

Yes, the Celtics not only got the 1-seed in the East but also finished FOURTEEN games ahead of the 2-seed New York Knicks (yes, the Knicks finished 50-32 and still got the 2-seed). Two teams that had very up-and-down seasons-the Bulls and Hawks-both finished with under 40 wins yet still qualified for the play-in as the 9- and 10-seeds in the East, respectively.

Miami Heat to the play…offs?

Throughout the last 10 years, the Miami Heat have had a great deal of success, making it to the Finals twice in that span (’20 and ’23) and making it to the playoffs 7 of the last 10 seasons (exceptions being ’15, ’17 and ’19).

However, while they did make the playoffs the last two seasons, they had to do so through first making it through the play-ins-both times they made it as the 8-seed in the play-in (meaning they had to play two play-in games to even get a playoff slot).

In this model however, the Miami Heat will earn the 4-seed and make the actual playoffs, not the play-in. What could possibly work to their advantage? Here are a few factors:

  • While their successful field goal percentage was in the bottom half of the league last season, they came in 12th amongst all teams in successful 3-pointer percentage, which should help their case.
  • After losing Jimmy Butler and Terry Rozier before play-offs last season, both are now (as of this writing) healthy and ready to play.
  • Those 42.3 rebounds (both offensive and defensive) last year look pretty good.
  • Tyler Herro, Bam Adebayo and Jimmy Butler were the top-3 scorers on the Heat in both points per game and field goals last year…those stats certainly matter for big games. Plus Herro is 24 and Adebayo is 27, so both are still in the primes of their careers (though Jimmy Butler at 35 still plays like he’s in his prime in my opinion)

Will the Heat win an NBA championship or make another Finals appearance? TBD. However, it looks like (according to this model I made) that they will at least make it to the play-offs without needing to go through play-ins first (though their 8-seed to-the-Finals run in 2023 was certainly memorable).

And now, for the bottom of the conference

Most of my insights discussed more successful teams and (potential) deep playoff runs. However, I wanted to offer one more insight concerning the two teams at the (projected) bottom of their conferences-the Pistons in the East and Rockets in the West.

First off-the Detroit Pistons, who, according to my model, are projected to be the 15-seed again (they were the 15-seed last season); will they manage to improve this season? My guess is yes-at least in terms of having more wins this season (only 14 wins last year)-but I don’t think they’ll make a strong playoff run, and I know a 28 game losing streak last season to drop the Pistons to 2-30 at one point didn’t help make a case for their postseason hopes. However, give the Pistons credit for changing their coach (now JB Bickerstaff) and GM (now Trajan Langdon) and adding some solid free agents like Tobias Harris (48.7% of successful field goals last season-not too shabby). Again, I doubt they’ll make a strong playoff run, but they could very well finish higher than the 15-seed.

As for the Houston Rockets (projected 15-seed in the West), they finished in the 11-seed last year in a competitive Western Conference with an even 41-41 record. Judging from last years stats-coming in 9th on defense but 20th on offense-they do have some work to do to make a deep playoff run. However, with a good mix of young players like Tari Eason and veterans like Fred VanVleet (who was on the championship 2019 Toronto Raptors), the Rockets could make it past play-in.

Just for fun…Michael’s Play-In Predictions

Now for an added bonus for my loyal readers, here are my educated guess, just-for-fun play-in predictions for both the Eastern and Western conferences. Granted, while the model I made did help predict regular-season seeding in each conference, it didn’t predict who would make it past play-in to grab the 7- and 8-seeds in the conference. So without further ado, here are my play-in predictions based on what I saw in the teams last season:

Eastern Conference

Predictions: Pacers 7-seed, Cavaliers 8-seed

Western Conference

Predictions: Lakers 7-seed, Timberwolves 8-seed

Thanks for reading and I hope you learned something new from this post! Enjoy the NBA season and I will follow up with a Part 3 post on this topic sometime in April, or at least some time after the conclusion of the regular season. It will be interested to see how accurate or off my predictions were.

Michael

Python, Linear Regression & An NBA Season Opening Day Special Post

Hello readers,

Michael here, and in today’s lesson, we’re gonna try something special! For one, we’re going back to this blog’s statistical roots with a linear regression post; I covered linear regression with R in the way, way back of 2018 (R Lesson 6: Linear Regression) on this blog, so I thought I’d show you how to work the linear regression process in Python. Two, I’m going to try something I don’t normally do, which is predict the future. In this case, the future being the results of the just-beginning 2024-25 NBA season. Why try to predict NBA results you might ask? Well, for one, I wanted to try something new on this blog (hey, gotta keep things fresh six years in), and for two, I enjoy following along with the NBA season. Plus, I enjoyed writing my post on the 2020 NBA playoffs-R Analysis 10: Linear Regression, K-Means Clustering, & the 2020 NBA Playoffs.

Let’s load our data and import our packages!

Before we get started on the analysis, let’s first load our data into our IDE and import all necessary packages:

import pandas as pd
from sklearn.model_selection import train_test_split
from pandas.core.common import random_state
from sklearn.linear_model import LinearRegression

You’re likely quite familiar with pandas but for those of you that don’t know, sklearn is an open-source Python library commonly used for machine learning projects (like the linear regression we’re about to do)!

A note about uploading files via Google Colab

Once we import our necessary packages, the next thing we should do is upload the data-frame we’ll be using for this analysis.

This is the file we’ll be using; it contains team statistics such as turnovers (team total) and wins for all 30 NBA teams for the last 10 seasons (2014-15 to 2023-24). The data was retrieved from basketball-reference.com, which is a great place to go if you’re looking for juicy basketball data to analyze. This site comes from https://www.sports-reference.com/, which contains statistics on various sports from NBA to NFL to the other football (soccer for Americans), among other sports.

Now, since I used Google Colab for this analysis, I’ll show you how to upload Excel files into Colab (a different process from uploading Excel files into other IDEs):

To import local files into Google Colab, you’ll need to include the lines from google.colab import files and uploaded = files.upload() in the notebook since, for some odd reason, Google Colab won’t let you upload local files directly into your notebook. Once you run these two lines of code, you’ll need to select a file from the browser tool that you want to upload to Colab.

Next (and ideally in a separate cell), you’ll need to add the lines import io and dataframe = pd.read_csv(io.BytesIO(uploaded['dataframe name'])) to the notebook and run the code. This will officially upload your data-frame to your Colab notebook.

  • Yes, I know it’s annoying, but that’s just how Colab works. If you’re not using Colab to follow along with me, feel free to skip this section as a simple pd.read_csv() will do the trick to upload your data-frame onto the IDE.

Let’s learn about our data-frame!

Now that we’ve uploaded our data-frame into the IDE, let’s learn more about it!

NBA.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 300 entries, 0 to 299
Data columns (total 31 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Season  300 non-null    object 
 1   Team    300 non-null    object 
 2   W       300 non-null    int64  
 3   L       300 non-null    int64  
 4   Finish  300 non-null    int64  
 5   Age     300 non-null    float64
 6   Ht.     300 non-null    object 
 7   Wt.     300 non-null    int64  
 8   G       300 non-null    int64  
 9   MP      300 non-null    int64  
 10  FG      300 non-null    int64  
 11  FGA     300 non-null    int64  
 12  FG%     300 non-null    float64
 13  3P      300 non-null    int64  
 14  3PA     300 non-null    int64  
 15  3P%     300 non-null    float64
 16  2P      300 non-null    int64  
 17  2PA     300 non-null    int64  
 18  2P%     300 non-null    float64
 19  FT      300 non-null    int64  
 20  FTA     300 non-null    int64  
 21  FT%     300 non-null    float64
 22  ORB     300 non-null    int64  
 23  DRB     300 non-null    int64  
 24  TRB     300 non-null    int64  
 25  AST     300 non-null    int64  
 26  STL     300 non-null    int64  
 27  BLK     300 non-null    int64  
 28  TOV     300 non-null    int64  
 29  PF      300 non-null    int64  
 30  PTS     300 non-null    int64  
dtypes: float64(5), int64(23), object(3)
memory usage: 72.8+ KB

Running the NBA.info() command will allow us to see basic information about all 31 columns in our data-frame (such as column names, amount of records in dataset, and object type).

In case you’re wondering about all the abbreviations, here’s an explanation for each abbreviation:

  • Season-The specific season represented by the data (e.g. 2014-15)
  • Team-The team name
  • W-A team’s wins in a given season
  • L-A team’s losses in a given season
  • Finish-The seed a team finished in during a given season in their conference (e.g. Detroit Pistons finishing 15th seed in the East last season)
  • Age-The average age of a team’s roster as of February 1 of a given season (e.g. February 1, 2024 for the 2023-24 season)
  • Ht.-The average height of the team’s roster in a given season (e.g. 6’6)
  • Wt.-The average weight (in lbs.) of the team’s roster in a given season
  • G-Total amount of games played by the team in a given season
  • MP-Total minutes played as a team in a given season
  • FG-Field goals scored by the team in a given season
  • FGA-Field goal attempts made by the team in a given season
  • FG%-Percent of successful field goals made by team in a given season
  • 3P-3-point field goals scored by the team in a given season
  • 3PA-3-point field goal attempts made by the team in a given season
  • 3P%-Percent of successful 3-point field goals made by the team in a given season
  • 2P-2-point field goals scored by the team in a given season
  • 2PA-2-point field goal attempts made by the team in a given season
  • 2P%-Percent of successful 2-point field goals made by the team in a given season
  • FT-Free throws scored by the team in a given season
  • FTA-Free throw attempts made by the team in a given season
  • FT%-Percent of successful free throw attempts made by the team in a given season
  • ORB-Team’s total offensive rebounds in a given season
  • DRB-Team’s total defensive rebounds in a given season
  • TRB-Team’s total rebounds (both offensive and defensive) in a given season
  • AST-Team’s total assists in a given season
  • STL-Team’s total steals in a given season
  • BLK-Team’s total blocks in a given season
  • TOV-Team’s total turnovers in a given season
  • PF-Team’s total personal fouls in a given season
  • PTS-Team’s total points scored in a given season

Wow, that’s a lot of variables! Now that understand know the data we’re working with better, let’s see how we can make a simple linear regression model!

The K-Best Way To Set Up Your Model

Before we start the juicy analysis, let’s first pick the features we will use for the model. In this post, we’ll explore the Select K-Best algorithm, which is an algorithm commonly used in linear regression to help select the best features for a particular model:

X = NBA.drop(['Season', 'Team', 'W', 'Ht.'], axis=1)
y = NBA['W']

from sklearn.feature_selection import SelectKBest, f_regression
features = SelectKBest(score_func=f_regression, k=5)
features.fit(X, y)

selectedFeatures = X.columns[features.get_support()]
print(selectedFeatures)

Index(['L', 'Finish', 'Age', 'FG%', '3P%'], dtype='object')

According to the Select K-Best algorithm, the five best features to use in the linear regression are L, Finish, Age, FG% and 3P%. In other words, a team’s end-of-season seeding, total losses, average roster age, and percentage of successful field goals and 3-pointers are the five most important features to predict a team’s win total.

How did the model arrive to these conclusions? First of all, I set the X and y variables-this is important as the Select K-Best algorithm needs to know what is the dependent variable and what are possible independent variable selections that can be used in the model. In this example, the dependent (or y) variable is W (for team wins) while the X variable includes all other dataset columns except for W, Team, Season, and Ht. because W is the y variable and the other three variables are categorial (or non-numerical) variables, so they really won’t work in our analysis.

Next we import the SelectKBest and f_regression packages from the sklearn.feature_selection module. Why do we need these two packages? Well, SelectKBest will allow us to use the Select K-Best algorithm while f_regression is like a back-end feature selection method that allows the Select K-Best algorithm to select the best x-amount of features for the model (I used five features for this model).

After setting up the Select K-Best algorithm, we then fit both the X and y variables to the algorithm and then print out our top five selectedFeatures.

Train, test…split!

Once we have our top five features for model, it’s time for the train, test, splitting of the model! What is train, test, split you ask? Well, our linear regression model will be split into two types of data-training data (the data we use for training the model) and testing data (the data we use to test our model). Here’s how we can utilize the train, test, split for this model:

X = NBA[['L', 'Finish', 'Age', 'FG%', '3P%']]
y = NBA['W']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

How does the train, test, split work? Using sklearn’s train_test_split method, we pass in four parameters-our independent variables (X), our dependent variable (y), the size of the test data (a decimal between 0 and 1), and the random state (this can be kept at 0, but it doesn’t matter what number you use-42 is another common number). In this model, I will utilize an 80/20 train, test, split, which indicates that 80% of the data will be for training while the other 20% will be used for testing.

Other common train, test, splits are 70/30, 85/15, and 67/33, but I opted for 80/20 because our dataset is only 300 rows long. I would utilize these other train, test, splits for larger datasets.

  • Something worth noting: What we’re doing here is called multiple linear regression since we’re using five X variables to predict a Y variable. Simple linear regression would only use one X variable to predict a Y variable. Just thought I’d throw in this quick factoid!

And now, for the model-making

Now that we’ve done all the steps to set up our model, the next thing we’ll need to do is actually create the model!

Here’s how we can get started:

NBAMODEL = LinearRegression()
NBAMODEL.fit(X_train, y_train)

LinearRegression()

In this example, we create a LinearRegression() object (NBAMODEL) and fit it to both the X_train and y_train data.

Predictions, predictions

Once we’ve created our model, next comes the fun part-generating the predictions!

yPredictions = NBAMODEL.predict(X_test)

yPredictions

array([53.20097648, 28.89541793, 52.26551381, 53.22220829, 35.90676716,
       32.15874993, 47.72090936, 48.32896277, 39.4193884 , 40.1548429 ,
       19.62678175, 48.3263792 , 32.13473281, 43.50887634, 43.85260484,
       52.79795145, 27.35822648, 40.23392095, 18.85423981, 61.69624816,
       51.59650403, 23.86311747, 56.18087097, 54.15867678, 49.75211403,
       46.90177259, 31.80109001, 46.82531833, 37.50563942, 32.19863141,
       52.41205133, 25.09011881, 48.94542256, 38.80244997, 24.80146638,
       42.50107728, 43.27320835, 37.45199938, 46.7795962 , 28.11289951,
       57.64388881, 29.35812466, 18.3222965 , 36.26677012, 20.56912227,
       22.15266241, 19.9955299 , 44.84930613, 45.14740453, 23.19471644,
       53.940611  , 26.0780373 , 27.88093669, 61.23347337, 52.99948229,
       34.66653881, 30.04421016, 27.21669768, 48.55215233, 47.11060905])

The yPredictions are obtained through using the predict method on the model’s X_train data, which in this case consists of 60 of the 300 records..

Evaluating the model’s accuracy

Once we’ve created the model and made our predictions on the training data, it’s time to evaluate the model’s accuracy. Here’s how to do so:

from sklearn.metrics import mean_absolute_percentage_error

mean_absolute_percentage_error(y_test,yPredictions)

0.09147159762376074

There are several ways you can evaluate the accuracy of a linear regression model. One good method as shown here is the mean_absolute_percentage_error (imported from the sklearn.metrics package). The mean absolute percentage error evaluates the model’s accuracy by indicating how off the model’s predictions are. In this model, the mean absolute percentage error is 0.09147159762376074, indicating that the model’s predictions are off by roughly 9%-which also indicates that overall, the model’s predictions are roughly 91% accurate. Not too shabby for this model!

  • Interestingly, the two COVID impacted NBA seasons in the dataset (2019-20 and 2020-21) didn’t throw off the model’s accuracy much.

Don’t forget about the equation!

Evaluating the model’s accuracy isn’t the only thing you should do when analyzing the model. You should also grab the model’s coefficients and intercept-they will be important in the next post!

NBAMODEL.coef_

array([ -0.4663858 ,  -1.30716212,   0.39700734,  34.1325687 ,
       -22.12258585])
NBAMODEL.intercept_

50.945769772855854

All linear regression models will have a coefficient and an intercept, which form the linear regression equation. Since our model had five X variables, there are five coefficients.

Now, what would our equation look like?

Here is the equation in all it’s messy glory. We’re going to be using this equation in the next post.

Linear regression plotting

For the visual learners among my readers, I thought it would be nice to include a simple scatterplot to visualize the accuracy of our linear regression model. Here’s how to create that plot:

import matplotlib.pyplot as plt
plt.scatter(y_test, yPredictions, color="red")
plt.xlabel('Actual values', size=15)
plt.ylabel('Predicted values', size=15)
plt.title('Actual vs Predicted values', size=15)
plt.show()

First, I imported the matplotlib.pyplot module. Then, I ran the plt.scatter() method to create a scatterplot. I used three parameters for this method: the y_test values, the yPredictions values, and the color="red" parameter (this just indicated that I wanted red scatterplot dots). I then used the plt.xlabel(), plt.ylabel(), and plt.title() methods to give the scatterplot an x-label title, y-label title, and title, respectively. Lastly, I used the plt.show() method to display the scatterplot in all of its red-dotted glory.

As you can see from this plot, the predicted values match the actual values fairly closely, hence the 91% accuracy/9% error.

Thanks for reading, enjoy the upcoming NBA season action, and stay tuned for my next post where I reveal my predicted records and standings for each team, East and West! It will be interesting to see how my predictions pan out over the course of the season-after all, it’s certainly something different I’m trying on this blog!

And yes, perfect timing for this blog to come out on NBA season opening day! Serendipity am I right?

Also, here’s a link to the notebook in GitHub-https://github.com/mfletcher2021/DevopsBasics/blob/master/NBA_24_25_predictions.ipynb.

A Quick Dive Into Google Colab

Hello everybody,

Michael here, and in today’s post, we’ll do something special-a quick dive (or you could call it a Programming Byte) into another IDE you can use for all sorts of fun Python coding adventures-Google Colab! Think of this post as a long-awaited follow-up to Python Program Demo 1: Using the Jupyter Notebook (written in December 2019).

What is Google Colab?

Google Colab (short for Colaboratory) is similar to Jupyter Notebook since both can be used as Python IDEs. However, what more should you know about Google Colab, and what are its differences/similarities to Jupyter Notebook:

Google ColabJupyter Notebook
Requires an internet connection and a Gmail account to useCan be utilized offline and doesn’t require a special account
It’s easy to save your code to GitHub with one click of a buttonIt’s a lot harder to save your code to GitHub; version control via Git is also more challenging since Jupyter notebooks are saved as JSON files
It’s free to use, but if you want better computing power from Colab, better to upgrade to a paid planIt’s always free to use, with no upgrade-to-paid-plan options
Certain commonly-used packages (e.g. pandas) come pre-installed with Google ColabJupyter will require you to install any package you wish to use (though if the package is already on your device, no need to install it again)
Only works with Python and HTML markdownWorks with Python, HTML markdown, R and Julia (which is a dynamic programming language)
There could be security risks as the code you work with in Colab is stored on Google Cloud serversMost code is stored on your local drive, not on cloud servers

Now that we’ve explored the gist of Google Colab, let’s see how we can use it!

Let’s start Colab-orating!

To start using Google Colab, click this link-https://colab.research.google.com/. It should take you to the Google Colab homepage, which looks something like this (as of September 2024):

  • If you’re not signed in to your Gmail account, you would need to do so before signing in to Colab.

As you can see, we have landed on the Google Colab homepage-and in case you’re wondering, we’re not going to explore the Gemini API today (though feel free to do so on your own).

Now, how do we start developing? Click on File–>New Notebook in Drive to create a new Colab notebook file; doing so will open up a new tab on your browser with a blank notebook file that looks like this:

Granted, the new file will come with a boring default title like UntitledNotebook4.ipynb or something like that, but you can change the notebook’s name by clicking on the textbook to the right of the multicolored triangle icon. Note that all Colab notebooks, like Jupyter notebooks, will use the IPYNB extension. Personally, I think Exploring Google Colab works for this lesson.

Colab Coding

Once we have our Colab notebook set up, it’s time to start coding!

Here’s how we’d write and execute some code in Colab!

In this example, we executed two simple lines of code in Colab by first writing said code into a blank Colab cell before clicking the Play icon in the cell to run the code. Since the code in this screenshot was already ran, we can see the output in the white cell below. Simple, yet effective!

  • If you’d like to edit the code in a certain cell, click on the code cell once to be able to edit that cell. Then click the Play button in that cell to run that code.

HTML Markdown

As I mentioned earlier in this post, Google Colab can also render HTML markdown text in addition to Python code. How can we generate some HTML text?

To generate HTML text, click on the Text button on the top of the screen to add a new text cell to your Colab notebook. Once the text cell is added, you can add all the HTML text you want-the best part about Colab is that, unlike in Jupyter Notebook, you can see how the text will appear as you are typing and formatting it (Jupyter Notebook only allows you to see the text once you’re done formatting/typing it).

Once your done using the editor, click on the pencil-like icon with a slash through it to close the editor. After you close the editor, you’ll see a textbox that looks like this:

As you can see, Google Colab managed to neatly format the text according to the HTML formatting we specified earlier.

  • Also, if you wish to further format the HTML text in a given text box, simply double click the text box to reopen the text editor.

A little CSS, perhaps?

Now, you might be wondering whether Colab can implement cool CSS styling (particularly inline CSS styling). The short answer is no, although I did try to do so here:

In this text cell, I tried coloring the text From the mind of Michael blue using inline CSS styling but lo and behold, that didn’t work. Apparently Google Colab doesn’t work with fancy CSS stylings:

As you can see, despite my best efforts to give this text some color, the output is a standard <h3> tagged-text.

Off to GitHub

And now, let’s see how we can send our Colab creation to GitHub!

But first, to save the notebook, click File–>Save (or use CTRL+S) to save the latest version of your notebook.

Now, how do we get our notebook to GitHub? First click File–>Save a copy in GitHub, which will take you to this screen:

Click Sign in to login to your GitHub account and continue along the sign-in process until you see this screen:

Click the Authorize googlecolab button to authorize Google Colab to connect to your GitHub account; this button will allow Google Colab to have read and write access to your GitHub public gists (which are Git repositories).

Once you click the green button, you should be taken back to your Google Colab notebook where a screen like this should appear:

In order to copy your Colab notebook to GitHub, select the Repository and its respective branch to indicate where to place the notebook copy in GitHub. Of course, feel free to change the commit message and include a link to the Colab notebook during the copying process.

Once you click OK, you should see the Colab notebook in the GitHub repository’s respective branch that you selected earlier:

As you can see, we have a copy of our Colab notebook in GitHub in the main branch of my blogcode repository (which is where I will store all the scripts from my posts). Here’s the link to the Colab notebook copy in GitHub-https://github.com/mfletcher2021/blogcode/blob/main/Exploring_Google_Colab.ipynb.

Interestingly, even though the line From the mind of Michael didn’t display blue in the Colab notebook, the blue came through here in the GitHub copy.

Thanks for reading,

Michael

Encryption, SHA Style (Python Lesson 54)

Hello everyone,

Michael here, and in today’s lesson, we’ll explore Python encryption, SHA style.

What is SHA encryption? SHA stands for Secure Hash Algorithm, and it is what’s known as a cryptographic hash function. The SHA algorithms were developed by the US National Security Agency (NSA) and published by the National Institute of Standards and Technology*. SHA is also not a symmetric-key or an asymmetric-key algorithm, as the main purpose of SHA is data security and integrity, not encrypting/decrypting communications.

Now, let’s dive into some SHA coding!

  • *The National Institute of Standards and Technology is an agency under the US Department of Commerce.

Types of SHA hashing functions

There are five different types of SHA hashing functions. Let’s explore each of them:

  • SHA-256-This hashing function will produce a 256-bit hash
  • SHA-384-This hashing function will produce a 384-bit (or 48-byte) hash
  • SHA-224-This hashing function will produce a 224-bit (or 28-byte) hash
  • SHA-512-This hashing function will produce a 512-bit (or 64-byte) hash; because of the long hash output, this is seen as one of the more secure hashing algorithms
  • SHA-1-This was the first SHA hashing function developed by the NSA and produced a 160-bit (or 20-byte) hash; however, this algorithm was found to have many security weaknesses and was thus phased out after 2010

Encrypting text, SHA style

Now that we know about the five different types of hashing functions, let’s see how they work on a text string in Python!

First, let’s import any necessary modules!

import hashlib

The hashlib module will be the only one we’ll need for this post. No need to pip install it since it comes built-in as long as your Python version is 2.5 or higher (and Python 2 has long since been sunset).

Next, let’s set the text we’ll use along with the SHA-256 hash of the text:

text = 'Michaels Programming Bytes: Byte sized programming classes for all coding learners'

SHA256 = hashlib.sha256(text.encode())
print(SHA256.hexdigest())

9854a9d55046c36afafbe47cbaa21ab85c8137d81955c3e344d76156a636a66b

As you see here, we used the name and tagline of this blog for our SHA demonstration. For our SHA encryption, keep these two methods in mind-.encode() and .hexdigest()-as we will use them throughout our demonstrations of the SHA hashing functions.

The .encode() method encodes the text string into a hexadecimal hash while the .hexdigest() method will display the hexadecimal hash.

As you can see here, we have generated a SHA-256 hash from our text with just two lines of code!

Now, if your curious what the decimal number of this hash equals, let’s check it out for fun:

68901140284153216712479841246928981776103555384517524587226404491787878442603

It’s a large, 77-digit number that’s roughly 68 quattuorvigintillion (this is a number followed by 75 zeroes). Quite a big number to come out of that hash!

Next, let’s try the SHA-384 encryption method:

SHA384 = hashlib.sha384(text.encode())
print(SHA384.hexdigest())

2777dfcb92166aa95f0796e3ed4edc12d7da9db3226c8831b40aca5576e8aafa106bb26b3eafe07fc9124238e5e7a6be

This time, we appear to have gotten a larger hash than we did from the SHA-256 function. What could be the decimal number of this hash?

6074720975275609937364706669124284930019665411073401970740713484318034757229843193804707918050212936621238163842750

Now, this number is even larger-it’s approximately 6 septentrigintillion (this is a number followed by 114 zeroes). I honestly never even seen such massive numbers, but as a numbers guy, massive numbers are fun to learn about!

Next, let’s try the SHA-224 method:

SHA224 = hashlib.sha224(text.encode())
print(SHA224.hexdigest())

8ed6b6c67f8f543d41472f2b0da146516cc6e28a20637d4fc3018518

Since SHA-224 uses less bits/bytes than the SHA-384 and SHA-256 functions, it makes sense that the generated hash would be shorter than the hashes generated from those functions.

I imagine that the decimal number would be shorter too:

15042673619469762912744046603652644639808686980332500102671983805720

Not surprisingly, the decimal number from this hash is also shorter, but still fairly massive-it’s approximately 15 unvigintillion (a number followed by 66 zeroes).

Next, let’s try the SHA-512 method:

SHA512 = hashlib.sha512(text.encode())
print(SHA512.hexdigest())

8d42047543b1bea09199838101c6ba0b5fb5d2631c1bb9b772333f2faa51b7cd0c53946e79758475929244d33e02b8e8cd994d79c63747ffdeeec3d6e9a66719

Since SHA-512 generates the largest hashes of all the SHA hashing functions, it’s not surprising that the generated hash is so large.

With a hash so large, I can only imagine what the decimal number would look like:

7398275510411850389223316484636795314726283670587045768118856918282820596862752633935810532260756989519458279298581715671072351034597240609479775136147225

The number above is approximately 7 quinquagintillion, which sounds like a word Dr. Seuss would make up, but it’s a number followed by 153 zeroes.

Last but not least, let’s try the retired, and original, SHA hashing function on our text-SHA-1:

SHA1 = hashlib.sha1(text.encode())
print(SHA1.hexdigest())

5ed4ecb61ff2323823781c514cbe2181e034a186

Since the hashes generated from the SHA-1 function use the least bytes (20) from the five functions we explored, the generated hash would also be the shortest of the five hashes we’ve generated.

With that said, it won’t be surprising that the decimal number of this hash is also fairly short:

541393510912863704690262334257797793251671187846

Even though this number is shorter than the other four numbers we got from the other four hashes, it’s still fairly massive-541 quattuordecillion (a number followed by 45 zeroes).

All in all, we got to see the power of SHA hashing functions through some simple text encryption. The massive decimal numbers we got from each of the generated hashes put the power of the SHA algorithm into perspective (plus, I had fun exploring massive numbers).

An interesting application of SHA hashing algorithms

Before we go, I wanted to explain an interesting application of the SHA hashing algorithm-Bitcoin mining. Yes, Bitcoin mining utilizes the SHA-256 algorithm during the mining process-this is because SHA-256 is both computationally efficient and offers a good deal of security throughout the process. Granted, SHA-512 could also offer a great deal of security, but the hashes also take up a lot of memory.

So, how would one mine Bitcoin? Check out this illustration below for an explanation:

For a very simplistic explanation of Bitcoin mining, a miner would need to utilize a very powerful computer (with processing power much greater than even your standard gaming laptop) to continually generate SHA-256 hashes until the correct hash is found. Once that happens, the miner is entitled to some Bitcoin as a reward. How much Bitcoin they get is determined by Bitcoin’s value at a given time-for instance, the reward as of August 2024 is 3.125 Bitcoin (or 3 1/8 Bitcoin).

Here’s the GitHub link to the script from this post (SHA is the script name)-https://github.com/mfletcher2021/blogcode/blob/main/SHA.py.

Thanks for reading!

Michael

Image Encryption (Python Lesson 53)

Hello everybody,

Michael here, and in today’s post (unofficially the summer of encryption series), we’ll learn how to encrypt images with Python!

During our encryption lessons, we’ve learned how to encrypt text and files with Python. We also learned some interesting mathematics behind RSA encryption too.

Anyway, let’s get started with our image encryption! Here’s the image we’ll be using:

Yes, it’s yours truly in front of the Deadpool & Wolverine movie poster, giving a thumbs up to indicate my approval (and I highly recommend seeing it). Anyway, onto the image encryption.

Encrypting the image

Before we encrypt the image, let’s first create a Fernet key. If you recall from my previous post, Fernet key encryption is a simple symmetric-key encryption method that utilizes the same key to encrypt and decrypt something (in this case, the image).

Encryption key generation!

Before we encrypt and decrypt the image, let’s generate a file for our image key! We’d generate the image encryption/decryption key in a similar manner to the way we generated the file encryption/decryption key (take a look at the code below to see how we generated the file for the image encryption/decryption key):

from cryptography.fernet import Fernet

imageKey = Fernet.generate_key()

with open(r'C:/Users/mof39/OneDrive/Documents/imageKey.key', 'wb') as keyFile:
    keyFile.write(imageKey)

After importing the Fernet module from cryptography.Fernet (install the cryptography package if you haven’t done so already), we create our imageKey then write it to the aptly-named imageKey file-with a .key extension-to a specific file path (you can use any location on your local drive that you can access).

What does the key look like? Let’s find out:

j8P7M-h1v6_KxFkNncCYIfRIQwWmxlLToatX3lg5Jcg=

This is the key we will need to both encrypt and decrypt the image.

It’s image encryption time!

Now, let’s actually encrypt the image. Here’s how to do so:

with open(r'C:/Users/mof39/OneDrive/Documents/imageKey.key', 'rb') as keyFile:
    encryptionKey = keyFile.read()
    
fernetEncryptionKey = Fernet(encryptionKey)

with open(r'C:/Users/mof39/Downloads/20240728_132146.jpg', 'rb') as image:
    originalImage = image.read()
    
encryptedImage = fernetEncryptionKey.encrypt(originalImage)

with open(r'C:/Users/mof39/OneDrive/Documents/encryptedImage.jpg', 'wb') as image:
    image.write(encryptedImage)

What exactly did we do here? Let me explain:

  • First we read the file containing the image encryption/decryption key into the IDE.
  • Next we turned the encryptionKey into a Fernet object.
  • We then read the image we’ll be using into the IDE and encrypted it using the Fernet object we created in the previous step.
  • Finally, we wrote the encrypted image to a specific location on the local drive.
  • Word of advice-I would use different names for the encrypted and decrypted images to avoid confusion.

Now, let’s see what our encrypted image looks like:

As you can see, we can’t view our encrypted image, which is a good thing since the whole point of encrypting the image is to keep it from being seen.

However, let’s see what happens when we open this image on Notepad:

As you can see, we get this beautiful-looking gibberish, which I would love for a would-be hacker to see if they tried to intercept the image transmission to my intended recipient.

Decrypting the image

Now that we have successfully encrypted the image, let’s now decrypt it. Here’s how we’ll accomplish that:

with open(r'C:/Users/mof39/OneDrive/Documents/imageKey.key', 'rb') as keyFile:
    imageKeyFile = keyFile.read()
    
fernetDecryptionKey = Fernet(imageKeyFile)

with open(r'C:/Users/mof39/OneDrive/Documents/encryptedImage.jpg', 'rb') as image:
    encryptedImage = image.read()
    
decryptedImage = fernetDecryptionKey.decrypt(encryptedImage)

with open(r'C:/Users/mof39/OneDrive/Documents/decryptedImage.jpg', 'wb') as image:
    image.write(decryptedImage)

What exactly did we do here to decrypt the image? Let’s explain:

  • First, we read the image key file (the one containing our encryption/decryption key) into the IDE.
  • Next, we created a fernetDecryptionKey object from the imageKeyFile-we’ll use this to decrypt the image.
  • We then read the encryptedImage onto the IDE and, using out fernetDecryptionKey, decrypted the image.
  • Last but not least, we wrote the decryptedImage onto the local drive with the name decryptedImage.
  • Yes I know fernetDecryptionKey and fernetEncryptionKey are the same thing. I just thought it would be easier to use different variable names since they are being used for different purposes (decryption and encryption respectively).

And now, the moment of truth…here’s our decrypted image:

Yup, there I am in all my Deadpool & Wolverine-loving decrypted glory!

Here’s the link to today’s script in GitHub-https://github.com/mfletcher2021/blogcode/blob/main/imageencryption.py

Thanks for reading,

Michael

File Encryption (Python Lesson 52)

Hello everybody,

Michael here, and in today’s post, we’re going to cover file encryption with Python. The previous two posts simply covered text encryption, but today, we’re going to explore something a little different-encrypting and decrypting files!

This will be the file we’ll work with for this tutorial-

Yes, this is an old file and if you want to read the post where I originally used this dataset, here’s the link-R Analysis 10: Linear Regression, K-Means Clustering, & the 2020 NBA Playoffs (written in November 2020).

And now, to start the encryption!

To start with our encryption, let’s import the Fernet class from the cryptography.fernet module like so:

from cryptography.fernet import fernet

Next, let’s create our Fernet key and a file that will store the key:

key = Fernet.generate_key()

with open('C:/Users/mof39/OneDrive/Documents/filekey.key', 'wb') as fileKey:
    fileKey.write(key)

Using the with open() method, we store our Fernet key into a file in (this case) the Documents directory. This method takes two parameters-the file path (where we will store the key in this case) and the mode you wish to open the file in. The mode is a two-character string value with the following options for modes:

First string (denotes method to open the file)
  • r-reads file into the IDE, errors out if the file doesn’t exist or path provided is incorrect
  • a-appends contents to an existing file or creates the file to append content to if the file provided doesn’t exist
  • w-writes content to an existing file or create the file to write content to if the file provided doesn’t exist
  • x-creates the file in the specified file path, errors out if file already exists
SECOND STRING (denotes method to handle the file)
  • t-handles file in text mode
  • b-handles file in binary mode (this is good for handling images)

Now, what does the key look like?

In this example, we wrote our encryption key into a file called filekey.key and stored in the Documents folder.

  • Something to note: The encryption keys should be saved as a .key file, but if you want to view the key file, opening it with a text editor like Notepad will work.

The actual file encryption

Now that we have the encryption key file, let’s encrypt the file! Here’s how to do so:

with open('C:/Users/mof39/OneDrive/Documents/filekey.key', 'rb') as fileKey:
    key = fileKey.read()
    
fernetKey = Fernet(key)

with open('C:/Users/mof39/OneDrive/Documents/2020-nba-playoffs.xlsx', 'rb') as testFile:
    originalFile = testFile.read()
    
encryption = fernetKey.encrypt(originalFile)

with open('C:/Users/mof39/OneDrive/Documents/2020-nba-playoffs-encrypted.xlsx', 'wb') as encryptedFile:
    encryptedFile.write(encryption)

with open('C:/Users/mof39/OneDrive/Documents/2020-nba-playoffs-encrypted.xlsx', 'rb') as encryptedFile:
    encryptedFile.read()

So, what exactly am I doing here? Let me explain

  • I first read the file key that we generated in the previous section into the IDE.
  • I then created a Fernet key object from the file key we generated.
  • I then read the dataset we’re using into the IDE-note the originalFile variable.
  • I encrypted the originalFile using the Fernet key we created earlier-note the encryption variable.
  • Finally, I encrypted the file using the encryption variable and saved it to my Documents folder.

Now what does the encrypted file look like:

In this example, our test Excel file looks like a bunch of gibberish after being encrypted-and that’s the point of the encryption as its supposed to make the file unreadable during transmission from point A to point B.

  • Excel files such as this one might not open after they are encrypted as the encryption process could also possibly corrupt the file. In this case, if you want to see the contents of the Excel file, opening it with Notepad (as I did here) should do the trick.

It’s decryption time!

Now that we have successfully encrypted our file, assume we want to prepare it before it reaches its intended recipient. In this case, it’s time to decrypt the file! Here’s how to do so:

decryption = fernetKey.decrypt(encryption)

with open('C:/Users/mof39/OneDrive/Documents/2020-nba-playoffs-decrypted.xlsx', 'wb') as decryptedFile:
    decryptedFile.write(decryption)
    
with open('C:/Users/mof39/OneDrive/Documents/2020-nba-playoffs-decrypted.xlsx', 'rb') as decryptedFile:
    decryptedFile.read()

How did I decrypt the file? Let me explain:

  • I used the Fernet key we generated earlier for file encryption to decrypt the file.
  • I then created a decrypted file (which is the same thing as our original file) and read that file into the IDE.

What does our decrypted file look like? Let’s take a look:

Ta-da! Our decrypted file is the same as our original file, just with the -decrypted at the end of the file name

  • My advice: Although you don’t absolutely need to use different file names for the encrypted and decrypted versions of the file, I like to do so to be able to tell the difference between the encrypted and decrypted files.

Notice a familiar concept?

If you read my 6th anniversary post, you may recall that I discussed the concepts of symmetric and asymmetric-key encryption.

What does this type of encryption/decryption look like to you? If you guessed symmetric-key encryption, you’d be correct! Fernet key encryption-the method we used to encrypt/decrypt this file-is symmetric key encryption because it uses the same key to encrypt and decrypt the file. Granted, I also mentioned that symmetric-key encryption is less secure than asymmetric-key encryption; there are likely many ways to encrypt/decrypt the file using asymmetric-key encryption, but I thought Fernet key encryption would be an easy enough method to utilize to demonstrate basic file encryption/decryption with Python.

Just one more thing…

Six years into this blogging journey, I still strive to find ways to improve how I get my content to you-the readers. With that said, I will now upload scripts I use in my posts to my GitHub so that you can download and play along with the scripts too!

Here’s the link to the repo with the scripts-mfletcher2021/blogcode: Various scripts from Michael’s Programming Bytes (github.com). The script for this lesson is fileencryption.py.

Thanks for reading,

Michael

Encryption: The Mathematics Behind the Algorithm

Hello readers,

Michael here, and in today’s post, we’re exploring a special topic-the mathematics behind encryption algorithms. More specifically, we’ll explore the mathematics behind the RSA asymmetric-key encryption algorithm that we discussed in the post 6 (honestly wanted to write this post because I love math and think the mathematics behind encryption algorithms are interesting).

The mathematical public exponent

Before we dive into the mathematics of encryption, let’s review what the public exponent does! As I mentioned in 6, the public exponent is a crucial part of the RSA algorithm’s public key that is utilized to verify both data encryption and access signatures for anyone trying to access the data.

I also mentioned the use of the number 65537 as a public exponent. Why is that such a common value for the public exponent? Well, 65537 is what’s known as a Fermat number.

The fun Fermat numbers

What exactly is a Fermat number? A simple explanation would be that a Fermat number is an integer that can be derived from the following expression:

The x in this case represents any positive integer, including 0. In simple terms, a Fermat number can be derived from 2 to the power of 2^x plus 1. The first five Fermat numbers are 3, 5, 17, 257 and 65537-pretty impressive range if I do say so myself.

Now, fun historical nerdy fact for this post-Fermat numbers were named after 17th century French mathematician Pierre de Fermat who first discovered these numbers. He’s also known for his early contributions to calculus, number theory, and probability, among other fields.

One of his more notable mathematical contributions is Fermat’s last theorem, which can be best described with this equation:

de Fermat stated that there are no three positive integers for a, b, and c that can satisfy this equation if n is greater than 2 (quite the opposite of the Pythagorean theorem where a^2+b^2=c^2).

  • Fermat numbers aren’t required as public exponents for RSA keys but they are quite practical. They allow for efficient encryption and decryption and are more secure than non-Fermat numbers.

Now, how does this relate to RSA?

Good question. The magic number 65,537 (the default public exponent for RSA keys) is the perfect public exponent for RSA public keys because for one, it’s a prime number (and prime numbers usually make better public exponents), and for two, its neither too small nor too large of a public exponent.

Smaller public exponents like 3, 5 and 17 would make the data more vulnerable to attacks since hackers could use these low public exponents to decrypt the data without knowing the private key. On the other hand, a larger public exponent like 4,294,967,297 utilizes a lot of computing power while providing no significant security advantage. The public exponent 65,537 strikes the perfect balance between secure encryption and computational overhead.

Another thing worth noting about public exponents is that if you’re trying to figure out a good public exponent to use for your RSA encryption algorithm, prime numbers will do the trick (more so if they are Fermat prime numbers), as they provide mathematical efficiency (after all, prime numbers are only divisible by 1 and themselves) and more security during the encryption/decryption process since they make it much harder for hackers to figure out the public exponent and in turn, the RSA keys.

Thanks for reading,

Michael