Image for post
Image for post
Photo by h heyerlein on Unsplash

We will learn how to use MongoDB with Python in this piece. MongoDB is a very fast and flexible NoSQL database. It is very popular and widely used.

Before diving, let’s learn 3 terms we will use very often in this piece.

  • Document: MongoDB is a document database which means each record in a collection is a document.
  • Collection: A collection may store a number of documents.
  • Database: Databases hold collections of documents.

Let’s delve into it.

Image for post
Image for post

Data classes are available for Python 3.7 or above. You can use data classes as a data container but not only. Data classes also write boiler-plate code for you and simplify the process of creating classes because it comes with some methods implemented for free. Let’s dive in!

Your First Data Class

Let’s create a data class which represents a point in 3d coordinate system.

@dataclass decorator is used to create a data class. x, y and z are fields in our data class. Note that you need to use type annotations to specify data types for fields and remember that type annotations are not static type declarations, this means someone could still pass any data type other than int for x, y or z fields. …

Bring C into your Python code with only a few steps!

Image for post
Image for post
Photo by Guillaume Jaillet on Unsplash

I’m sure you heard a lot of people complaining that Python is so slow. I see people compare Python to C in the context of performance only, but they don’t compare in the context of fast development. Python development time is so fast because you don’t have to deal with pointers, memory management, etc.

This is one of the reasons we use Python in our company. In most cases, development time is more important than performance. Python has a great community and great libraries out there that work for what you need most of the time. That being said, I will talk about Cython in this article to make your Python code fast. …

Python in memory

Image for post
Image for post
Photo by Steve Johnson on Unsplash

In this piece, I will talk about reference counting in Python. I will use the list object, which is mutable for illustration purposes. I hope you will enjoy it. Note that I will not go into C implementation details.

P.S: Output of snippets may differ on your hardware.

Variables Are Memory References

Variables in Python are memory references. What happens when you say x=[1, 2]? [1, 2] is the object. Recall that everything is an object in Python. [1, 2] will be created in memory. x is the memory reference of [1, 2] object.

Focus on the example below — you can find the memory address whichx references to. Note that you can just use id(x) which will give you in base-10 and hex function converts it to hexadecimal. …

Three problems and their solutions

Image for post
Image for post
Photo by NeONBRAND on Unsplash

In this piece, I will talk about three common mistakes you wouldn’t want to do!

The Problem: Default Mutable Object

Hey, wait—what is a mutable object in Python? Mutable objects are objects you can change. List, set, and dict are examples of mutable objects.

For the sake of argument, I will define a very simple and useless function.

def add(x, y=[]):
return y

In this add function, we set the y variable to a list(mutable object) as a default. Let’s use this function.

def add(x, y=[]):
return y


The output is :

['halil', 'yıldırım']

What is going on here? It’s overwriting the same list. When we first called add('halil'), the default argument became y=['halil'] and started overwriting the same list. …

Image for post
Image for post
Photo by Tyler Daviaux on Unsplash

In this piece, we are going to delve into context managers in Python. I will keep it short — less talk, more work! I hope you enjoy it.

Why Context Managers?

Context managers are used to give resources back. Here’s what I mean:

f = open('file.txt', 'w')
f.write('Something to write')

We opened a file named file.txt and wrote it. Hang on — we didn’t close this file, so we can still use thef instance:

f = open('file.txt', 'w')
f.write('Something to write')
print('another work')
f.write('I can still write to file.txt')

This is a problem — resources are not unlimited so we have to give it back when we’re done with it. …

Image for post
Image for post
Photo by Hush Naidoo on Unsplash

Let’s say I want to build a system that say whether someone is tuberculosis or not. We collected a data set including 50.000 images. 49.950 of these images are negative examples and rest are positive examples.

In our data set, Just 50 people are tuberculosis while we have 50.000 images. (50/50.000)*100 = 0.1% are tuberculosis. We call it imbalanced data set. Even without building a deep learning algorithm, with a simple statement, you can achieve around %99 accuracy. With that being said, you want your algorithm to predict someone is tuberculosis whilst we don’t know if our algorithm can predict correctly if someone is tuberculosis. We just know accuracy and no more! Under such circumstances, you can use precision and recall evaluation metrics. …

I’m keeping on making publications on implementing from scratch. This time is for logistic regression! Let’s go about this!

Image for post
Image for post
Photo by Mikhail Vasilyev on Unsplash

Logistic regression

Logistic regression is used for binary classifications. You can train your model to predict if someone is cancer or not or your model could be trained to predict if it is cat or not in the photo.

Equation implementation

Do you remember the equation:

An in-depth tutorial on setting up an AI network

Image for post
Image for post
Photo by Clarisse Croset on Unsplash

Hello AI fans! I am so excited to share with you how to build a neural network with a hidden layer! Follow along and let’s get started!

Importing Libraries

The only library we need for this tutorial is NumPy.

import numpy as np

Activation Function

In the hidden layer, we will use the tanh activation function and in the output layer, I will use the sigmoid function. It is easy to find information on both the sigmoid function and the tanh function graph. I don’t want to bore you with explanations, so I will just implement it.

So you can handle user errors and server errors appropriately

Image for post
Image for post
Photo by Jan Genge on Unsplash

Me on youtube: Me on github:

Imagine that you developed a calculator using Python. Someone who uses your software wants to divide a number by zero (e.g. 4/0). Because division by zero is mathematically undefined, Python is going to throw an error like below.

ZeroDivisionError: division by zero

It wouldn’t be nice to show an error like this. For these cases, we can use Try/Except Blocks.

I will give you a very basic example of this to give you a good understanding of how you can use it.

except ZeroDivisionError:
print("Division by zero is undefined")

The output of this code is going to…


Engineer. Software Developer. Follow me on Medium! Donate:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store