Week 4: Soft Intro to Data Science with Python. Let's Build Our First Deep Neural Network! (Pt. 1)
Spencer Fonbuena @ Eleanor AI

Week 4: Soft Intro to Data Science with Python. Let's Build Our First Deep Neural Network! (Pt. 1)


Pre-Article Notice

Much of this article and it's explanations is going to be written in a software called Google Colab. Some people may prefer to read the written bits in the format of these linkedin articles. Because of that, all the information in the Google Colab will be on this article as well.

That being said, the best way for you to be able to learn the code and be ready for the more complex examples to open the Google Colab link and get your hands dirty playing with and understanding the code.


Learning Objective

After this article, my hope is that you will have an understanding of Google Colab, basic python functions, and a glimpse into how to apply the topics of last weeks article. If you haven't read it yet, you can find it here. We will also improve on the model we made from the article in week 2, which you can find here. Without further ado, let's get into it!


Article Breakdown

  1. What is Google Colab
  2. Off To The Notebooks
  3. Improving On Previous Models



What is Google Colab

Google Colab is a free, cloud-based platform that allows users to write and execute Python code directly from their web browser. It is especially popular among data scientists, machine learning practitioners, and AI enthusiasts due to its simplicity, accessibility, and integration with powerful computational resources.

If you're interested in writing and running models directly on your own machine, you'll want to use Jupyter Notebooks, which can be thought of as the desktop equivalent of Google Colab, and is available with most popular development environments.


Off to the Notebooks

Google Colab link here


What Even Is Python?

Python is often described as the "Swiss Army Knife" of programming languages. Whether you're automating tasks, building websites, analyzing data, or creating AI models, Python has it all. That being said, all of that functionality doesn't come built in to the programming language. It achieves all using third party functionality called packages.

A Python Package: Your Toolkit for Faster Development

A package in Python is like a pre-built toolkit designed to simplify your life as a developer. It bundles functionalities together, letting you build faster by focusing on solving problems rather than reinventing the wheel.

If you need a specific functionality—whether it's data analysis, machine learning, or file handling—there’s likely a Python package for that. Think of packages as ready-made solutions waiting to supercharge your project.


A Simple Analogy: Packages Are Like Websites

Imagine the internet without websites. It’s a powerful platform, but without sites like LinkedIn or YouTube, its utility is limited. Similarly:

  • Python Without Packages: It offers foundational tools (math operations, file handling, etc.) but lacks specialized capabilities like advanced machine learning or data visualization.
  • Adding Packages: Just like websites make the internet more functional, packages like numpy, pandas, and matplotlib unlock Python's true potential.


Modules: The Building Blocks of Packages

A module is like a single page or feature on a website. It's a Python file containing specific functionalities (functions, classes, etc.) focused on a particular task. Combine modules, and you get a package.


A Quick Example: Creating a Math Package

Imagine building a package for math with different modules for algebra, calculus, and financial math.

  • Algebra Module: Solves equations.
  • Calculus Module: Performs derivatives and integrals.
  • Financial Math Module: Calculates interest or solves money-related equations.

By organizing these as separate modules, your package becomes reusable and efficient.


Popular Python Packages You Should Know

Here are some industry favorites:

  • numpy: For numerical computing.
  • pandas: For data manipulation and analysis.
  • matplotlib: For data visualization.
  • scikit-learn: For machine learning tasks.
  • PyTorch: For deep learning projects.


How to Use Packages: Understanding Imports

Python imports make packages and modules accessible. Here's how they work:

Import the Entire Package:

When you import an entire package, you bring all its functionalities into your program. To use a specific function, you reference it with the package name.

For example, Python’s math package contains various mathematical functions like square root, trigonometry, and more. Here’s how to import and use the whole package:

import math
math.sqrt(100)        

Here, we:

  1. Imported the entire math package.
  2. Accessed the sqrt function using the syntax math.function_name.

The output of this code is 10 (the square root of 100)

Import Specific Modules:

If you only need certain parts of a package, you can import those specific functionalities directly. This can improve memory efficiency and make your code cleaner.

from math import sqrt
sqrt(100)        

Key differences here:

  • We imported only the sqrt function from the math package.
  • Since we didn’t import the entire package, we call sqrt directly without prefixing it with math.

This is ideal for large packages where you only need a subset of its functionality.

Use an Alias:

Sometimes, package or function names are long or cumbersome to type repeatedly, or are not descriptive. In these cases, you can assign an alias (an alternate name) when importing. This is also useful for adhering to community standards, as many libraries have common aliasing conventions.

from math import sqrt as square_root 
square_root(100)         

Here’s what happens:

  • The sqrt function is renamed to square_root during the import. You can now use square_root as a shorthand in your code.


Now Let's Start Coding


Google Colab: The Playground for Python

If you're using Google Colab, its cell-based structure lets you experiment and document code step by step.

Running a Cell in Colab

  1. Write Python code in a code cell.
  2. Press Shift + Enter to run it.

Example:

2 + 2
# this code will print out 4        

Writing Our Own Module in Python

You may have noticed the funny looking way that we write code. For example

- What is with the "." in "math.sqrt?"

- What is with the parenthesis in math.sqrt(20)

I think the best way to understand this, is to re-write our very own math package with a sqrt module. (as a disclaimer to those that may be experienced, first off, why are you even reading this part?? And furthermore, I know that there are some slight inaccuracies in calling what we are doing a package and module, but it serves the purpose)


Create the myMath package

For this example, we are going to create the same exact math package we imported, but because it is ours, we are going to name it "myMath." We do so by using a keyword in python called a class. Think of a class just as you did a package. It is a way to bundle together a group of functionality. In our case, math functionality.

Step 1: Build a Basic Class

class MyMath:        

After we have the class called MyMath, we are going to add a method. Think of a method as a functionality in our package, or as a module. We do that using the keyword "def." After def, we name our module appropriately, and then, in parenthesis, we add arguments. See the below example where we re-create the math.sqrt function

Step 2: Add a Module

We’ll define a class to group related functionalities, then add a method to calculate square roots.

class MyMath:
  def sqrt(num):
    square_root = num**.5  
    return square_root
     return num ** 0.5         



Step 2: Create Your Own Module

Now it’s your turn! Add a method that takes two numbers and returns their sum.

# We name our class MyMath
class MyMath:
  def sqrt(num):
    square_root = num**.5  
    return square_root
  def add_two_nums(num1, num2):
    summed_value = # Solution goes here
    return summed_value        

Test Your Module:

MyMath.add_two_nums(10, 10) # Should print out 20 (obviously;)        


What’s Next?

Now that we’ve built a solid foundation in Python, we’re ready to level up! In the next edition, we’ll dive into PyTorch, one of the most popular deep learning packages out there. Alongside other essential packages like numpy and pandas, we’ll use our Python knowledge to rebuild the Name Network, a simple neural network that can recognize and process names. It’s going to be hands-on, practical, and a lot of fun—so don’t miss it!


How You Can Contribute

I want this to be a two-way conversation. Your feedback, thoughts, and burning questions about AI are always welcome. Are you curious about a specific concept, a new research paper, or even a recent headline? Let me know!

Neural Notes is as much yours as it is mine—a collaborative space where we can explore the fascinating and sometimes perplexing world of AI together. Let’s build, learn, and grow as a community of curious minds.

要查看或添加评论,请登录

Spencer Fonbuena的更多文章

社区洞察

其他会员也浏览了