Django – Part 2

Intro

Django’s slogan is “The web framework for perfectionists with deadlines.” It accomplishes this in a few different ways, forced compartmentalization, built in development tools, and every feature you could ask for. Below we will discuss how to get started, and how see how Django handles MVC.

Getting started:

You first want to setup a virtual env. I am using Anaconda, so I would type in:

  1. conda create –name <your env name> django
  2. source activate <your env name>

Then you would use Django’s great command line tools to create your first project:

  • django-admin startproject <your app name>

Once you the command above you will have your very first Django app. However it won’t do very much. You can execute the following command to run the application:

  • python manage.py runserver

As you can see its pretty boring and does not due much. Django is built around the idea of applications, being different modules in your over all application. These modules are separate from the main project and encapsulate the different features of your application. You create a Django app by then running:

  • python manage.py startapp <app name>

By running the command you get a new directory generated in your folder structure.

Getting started with MVC (MTV):

Dajango does not use the MVC design pattern per say, rather it uses the MTV design pattern. MTV stands for:

M – model : This is a simple python classes.

As you can see it is very easy to start building the inter model relationships. All models need to inherit from the Model class. This is all possible due to Djangos built in ORM. The ORM abstracts away the whole concept of having to work with a SQL DB, since it also provides a easy method revise your tables to reflect changes to your python models. This is simply done by using the:

  • python manage.py migrate
  • python manage.py make migrations < Dajango app name>

T – template : This is your html that displays model data through server side rendering.

Note that to serve these files, they need to be outside your main Django app, and they need to be reregistered in the Django settings file.

V – view : This is interestingly your controller.

As you can see above, you tie the data from your controller to your view by way of dictionaries. And tie your templates to your controllers to your views by specifying the url path name, and the file name.

A point of interest here, is that unlike some other frameworks your mapping a url to a method, rather then a whole class that contains different methods. This method mapping to is done using url.py files that exist both in the directory of the main project and the django app.

The main project directory:

The Django app:

As you can see your using regex, to find a specific url, then mapping it to a method. The main urls.py file contains the main urls for the whole project and imports the urls specifically for the Django app. Whereas the Django app just contains the sub urls for that particular app.

The “http://127.0.0.1:8000/first_app&#8221; would hit the “first_app” app, then it would then hit any of the sub urls stored in the Django app urls file.

As you can see Django is not that hard use at all, and in most ways is significantly easier then other frameworks, that don’t have all these built in features. In the next blog post I will be discussing Models, the Django admin console, and how the ORM handles model changes.

You can find part one here.

 

Machine Learning Explained – Part 1.2

Please note that I will not be covering the mathematical portions, but rather the big ideas.

Model Representation

Can come in different forms, however at the end of the day we are trying to learn a function in order to map or training. Such that it becomes a good predictor of an output given a inputs, this is a sort of regression problem. Where as functions who’s outputs are limited to a few discrete outputs, given various inputs are for classification problems.

Cost Functions

These functions are used to measure the level of accuracy of our hypothesis functions (the function we learned), by measuring the difference between our predicted value and the true output. And then computing the average error by way of the “Mean Squared Error” function. In a single variable regression problem the hypothesis function reduces to being the equation of a line.

The objective becomes to minimize the cost (or error) function. If you have ever taken Calculus before you can do that easily by taking the derivate of the function, setting it equal to zero, solving for the parameter, and using that value in your learned function.

However its not always easy when given a large set of parameters. Therefor you can also use contour plots, that act like maps to the values that reduce cost function to zero.

Gradient Descent

An easy way to think about gradient descent is by, imagining your a blind person trying to find a ball in a hilly area. You don’t know where the ball is, but you know the ball has rolled into the deepest valley in the area. However your blind so you can’t see the depth in your surroundings. Therefor you have to use your feet to feel for the steepness of the ground in front of you. By doing so you take little steps in the direction of the most steepness. This is exactly what the gradient descent algorithm does as well. It takes little steps, gauges the steepness, and then moves in that directions till it finds the global minimum of the cost function’s derivative.

You can find the first part of this series here.

Intro to Python ? – Part 2

You can find Part 1 here.

Intro

In this part of the series we are going to cover the very basics of Python. You may be asking: “Its Python, how hard can it get ? Its just sudo code” well that is both true and false to a degree.

Variables

Python is a dynamically typed language, however it is strongly typed. This just means that you can set a variable to any sort of data type you want without declaring it first. However it doesn’t implicitly convert types for you.

Collections

Python also contains different types array like structures. And they are all get dynamically sized of course. You have Lists and Dictionaries, they all work the way you intuitively think do, coming from another programming language. The only new sort of collection type, you will encounter is the Tuple. A Tuple is pretty much the same thing as a List however they are immutable and can not contain repeats of the same value.

Control Flow

It’s pretty basic.

Loops

Python does not find “for” loops pythonic that is way it favors “For In” loops instead. maybe asking: “What if I just want to print something 3 times ? do I have to make a list with three elements ?” The answer is no, you can use a “generator” shown in the gists below. Python also has something called “list comprehension” its very nifty to write compact and concise code.

 

You can go here to find all the relevant code, in order.

Stella – Chap 1 Continued

I think back to the day I first meet her, thinking about what she would be doing now, where she could possibly be. I know that hers, was a life filled with melancholy. Periods of great pain, pain that no one could ever understand. And periods of great joy, joy that no one could ever attain. For she was what everyone ever wanted. Her value could never be translated into another medium. Her value was in the form of filling the space inside us. To be able to take the shape, of the void that will inevitably consume us all.

However, she never felt happiness, the same sort felt by you or me. Happiness to her could never be translated to what you or I could understand. For her happiness, could never come cheaply. I often wonder what would have become of us if we had gotten together. If we would have had kids, if we could grow old with one another, if we could find joy in one another till the day we died.

I often wonder what has become of us. Us with our overly materialist views of the world. Us with our need and want of things both big and small, both emotional and physical. These things begin to consume who we are until we disappear into the ether.

Do we want to exist ? Or is it easier to give up, and let the world consume us ?

A streak of sadness comes over me, it shatters what solus I had taken in my lover’s arms. However temporary of a relationship ours would be, I would always remember her. Her slender frame and milk white skin only serviced to extenuate the grace she carried herself with. She was the antithesis of what woman had become today. She would still never compare to the girl of melancholy.

The girl of melancholy was there, here, and even in my dreams. Even now I ask myself, why ? Why did she ever turn into the person she is was ? Why did she choose to love the person she did ? It doesn’t matter anymore, nothing will matter to me anymore.

I wake my lover up, she greets me the smile and warmth of the sun. I can never understand why others do the things they do. I put on a mask of normality when with other people, especially for the woman I am with. However I am never too sure if they are also wearing one. We both laugh and enjoy each other’s company, I secretly hope. No, pray that she would just disappear. I wondered why she even decided to come home with me.

I am a man with no real face, just a closet full of masks. Maybe she wants to be with one the masks ?

She finally leaves and makes plans for us to meet again, I know I am never going to see her again. As I walk her out, into the now hot and humid air. The world has gone quiet. There are only the distant rumblings of cars and buses. The sound of children has waned, all that can be heard are the cicadas, and rustling of the leaves in the ocean breeze. It reminds me of my child hood.

I wonder what the girl of melancholy would think, if she saw me right now ? Would she like me more or less ? Would I finally be accepted by her ?

Its 6pm, I have spent the last few hours at work. I am not sure why they want me there. I feel like I am wandering through a graveyard of broken dreams. No one wants to admit that they have given up their dreams to work here. I see nothing but sadness as far as the eye can see, in both woman and men alike. I consider by self a ghost at work. I am shocked when people actually acknowledge my existence, every time they do, I  want to ask, “Do you actually see me ?”.

I am the VP of … its not important. I am not really sure what is anymore. All I know is that I never really have to work, I simple put on a mask when needed. And perform the part I am asked to, sometimes taking the mask off is harder than the others.

People think I am someone to aspire too … I am not. I wonder if the girl of melancholy would say differently? Sometimes I wonder if she ever existed in the first place.

It was 1993 I was … I wasn’t important, however she was … I get a streak of angry whenever I think of it. She was wanted by every man that ever gazed upon her, in one way or another. And she was envied by woman everywhere. Me ? I sat down and watched it all unfold. I think that if god ever existed, he created people that existed to offset all the evil in this world. She was one of them, but to others she was simply an object to fill the void within themselves.

My reminiscing is interrupted by the same little girl, only now I see she has golden eyes. She’s with her mother, she gazes at me with a deep cold stare. I shrink down in my office chair, and pull the monitor in front of my face. Does she know I have no face ? I realize it’s 7pm.

My ritual for the night consists of a number of illicit substances to make me … normal? no … I am not sure any more. I put on my mask of pure chauvinistic ignorance, and walk down to Zanzibar. I see nothing but men to the left. They range in all ages, shapes, and sizes. Some look wealthy, others look strange, but the looks on their faces are all the same. I feel like I am about the cry. As I walk further in, I am greeted by the hostess, loud music, the darkness, and the neon lights. The hostess looks like all the girls on stage, except she has a smile and warm eyes that seem painted on. The smell of smoke hangs in the air, I take a sit next to an older gentleman, as I wait for my associates.

I order a Macallan and watch. I take a few sips of the drink. Why did I just order something that for all intensive purposes, taste like urine and paint thinner?  I over hear a few of the men above me, say “Beer is proof god loves us” . I turn around, and I see a group of men in their mid-thirties. If god had loved us, why did he leave us defenseless against the devil ? Did the devil really exist ? Or did we create him to take the burden of virtue, off us.

The group of men only moments ago, had taken off their wedding rings. I wonder what their wives would do. Would they eventually forgive them? Would the part of them that once loved them die? Would they ignore it, and continue with a union that had lost its meaning?

The women in front of me, make me remember that the female form is one that can never be forgotten, can never be unseen, can never be improved upon. The women that dance on the brass poles are all … they are all … I am not sure what to say. They range from every color from ebony to ivory with skin as smooth as porcelain. They are the epitome of the female form, they are a sea of flowers in all the various tones of human flesh. The strobing lights in neon colors scatter in every which direction as they hit the gleaming skin of the woman before me.

I feel like I am the only one at Zanzibar that ever looks at floor. The floor has seen many, it has been there during the day and the night. It has been there when alcohol was spilt, when fights were broken apart. It has been stepped on by all the woman, and all the men. If the floor could talk I wonder what it would say ? After all the years it’s been there, what would it say about us?

I like it there. The music, lights, and the flowers made of flesh, all contribute to making you forget, forgetting who you are, what you are, where you came from, and why you walked in. I want to forget so much.

I like that it’s the only place I feel like no one can see me. The women on stage look like dolls dancing in the wind. Dolls that have no real eyes, mouths, or ears. The men stare at them, like they are looking into an abyss, waiting for answers to their life’s greatest questions, waiting and watching for something they know will never come. And I am just there … sitting staring at the floor.

Machine Learning Explained – Part 1.1

You can see why I started this series here.

What is machine learning ?

  • My definition:
    • When you tell a machine to learn from experience rather then, explicitly giving it a bunch of instructions.
  • Course Definitions:
    • “the field of study that gives computers the ability to learn without being explicitly programmed.” – Arthur Samuel
    • “A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.” – Tom Mitchell

Supervised Learning:

You can think of this like teaching a small child how to do something you already know. Such as counting objects, or throwing a ball. Another way of thinking about it is that you give the machine the data, knowing that there is some relationship there. Then having the machine find it by its self.

This type of learning comes in two different flavours:

Regression

Given a bunch of data and asked to predict what will happen next. An example will could be: “given all the historical data about housing prices, what will be the price of a house in 2020 ?” We are mapping input data to a continuous function to.

Classification

Take the input data, and give me discrete outputs (classifications) . For example if you were to take data on students and predict which students would become engineers. Here we still know what factors really influence the result, which still makes it supervised learning.

Unsupervised Learning:

We have mountains of data that we think is random and has no structure. We have no idea what the relationships are between the variables. So we let our machine loose on the data to discover the relationships between the different variables. And it starts to cluster the data into different piles.

Stella

Chapter 1

For

The woman who loved Noah

I am sorry

I was lying in bed, it was a Sunday. The smell of perfume still meandering through the air. I am not sure what it smelt like, it captured me and wouldn’t let go. I wanted to lay in bed with this smell engulfing my very being for all eternity. It made my primordial instincts confused, it smelt like what pleasures of the flesh felt like. I wondered if I lay here, with my eyes closed, and covers drawn, would I ever have the strength to raise up? As I stared up at the ceiling, who’s radiance was amplified by the shafts of light penetrating the window. I was filled with a lingering emptiness, the sort that can’t be filled, the sort that doesn’t ever get filled. For I never the emptiness wasn’t there as I grew. It only appeared after I gave all myself to another person. I shift my body to the right side of bed, without looking I feel around in the draw, for cigarettes. I take one and smoke. As I stare up at the ceiling, I can see the smoke raise up disappear into air almost as if it never existed to begin with. I close my eyes as I think that my life has been a collection of fleeting moments intertwined to others, by the red strings of fate.

I open my eyes. The sun has lurched higher in sky cover in gradients of blues, and streaked willowing white wisps. The light streamed into the room, and reflected off the mirror, creating a kaleidoscopic effect that drowned the room in ever which color of the rainbow. I turned in bed. I was greeted by her angelic face. I wondered why she had come home with me. A creature with so much grace, so much potential has chosen me. I reached out to caressed her face, I stopped half way and bring my hand back. I didn’t want to disturb the peace that she had, it was probably the only peace she would ever have. The world would only ever allow true piece in death. I lay there recovering from the night before, and the actions I had taken. My mind drifted through the vague memories of my life, from the time I first rode a bike, to when I first fell in love.

I felt as though I had lived a life other men could only dream of. I finally mustered up the strength to sit up right. I looked around the space. Was this my space ? There was little to no furniture, just large windows, and minimalist design everywhere. I stare out the window, hoping to reorient my mind. I lost my self, staring out the window, watching the rhythmic crashing of the waves, and smelling the salt in the air. Everything was frozen, everything was clear, I could feel rather than think.

I put on my robe. It wrapped me with a plush softness, and tightness that could only ever be felt, when hugged by a small child. I felt like the only thing in this world that accepted me for all that I was, was the robe. I walked out onto the balcony and let the wind flow over my skin, let the sun’s warmth reach every part of me. I must have stood there for hours.

My trance broke, when I thought I saw a boy and his dog playing catch from the corner of my eye. A smile ran across my face, for the boy reminded me of days where my happiness was never tied to any substance, or anyone. I think I had everything that anyone could ever want when I was five. Everything in the world was so foreign, my state of mind and conceptions of ideas and feels where still so new. I think those were the days I could call myself human. I don’t know how I became the person I am now.

I started to smell iron in the air; my nose was bleeding. It had made a pool on the dark slate floor. Sometimes I felt like it was the only time that I knew without a doubt I was alive. So much of my life was a husk of what it once was. At this point I felt as though it was okay to go back inside. The sand and sea, had stared into me as much as I had into them. I drifted across the room feeling the warmth of my feet, being draw away by the stone floor. I saw myself in the mirror, and asked myself, was that actually me? Is this what I looked like?

I made my way to the front porch, with a black cup of coffee. The bitterness of the dark liquid I swallowed, was drowned away by the intense amount of light and warmth. The sun was always worshiped in civilizations past, how far have we come that we never marvel at the warmth and light it provides us. The world had woken up, I was in the space between spaces, times that don’t have a name. It wasn’t early enough to be early, neither was it late enough to be the late. The exact time would be useless in order to find this time, for what was happening around me could only happen when the fate of objects both be and small where aligned just so.

There was still a bit of a chill in the air, there was still morning dew on the grass, and it wasn’t warm. It was one those rare times where the natural environment matched the artificial ones we created around us.

As I lay in my arm chair, I heard the cries and shouts of children playing. It was 3pm. My nights of debauchery had always resulted me in sleeping and rising at all times of the day. Without warning I was assaulted with a teddy bear. It had been thrown by one of the children. I stared at her as she did me. I saw it, I saw into her, I saw her entire life.

She would never live a normal life that the others did, and I wouldn’t be around to ever see it. This unassuming little girl, with pig tails, curly hair, and missing teeth, would one day hold such unfathomable value.  Although I had seen it time and time again. I could do nothing to help her to transition into the woman she would become.

I had filled my life with everything that people could ever want. I could never find anything satisfying, so I gave up trying. I had thought my life would have been much different as a younger man. I had thought that I would never achieve as much as I had done.

The girl defiantly run up to me and took back her teddy bear. I laughed as she ran away, it was the first time that a laugh wasn’t forced, in a long time. And then… then I remembered her. At the time it was the first time I had ever seen a creature like her. Her face was one that brought out the lust, jealousy, and want in others. I was much younger at that time, much more open to the world around me. That was over 30 years ago.

I finish my coffee, and slipped back into the arms of the past night’s lover. Sometimes I feel as though pleasures of the flesh are nothing but a sick illusion from god himself. As a form spit, since god is god. God, is he who is absolute, as a mere mortal I think god could never feel the same pleasures we could. We are human, and it is our fundamental flaws that allow as to feel the pleasures we do.

Django – Part 1

Django_Reinhardt_(Gottlieb_07301)
Django Reinhardt a jazz legend

No I am not talking about this guy to the left.

I am actually talking about the web framework written in Python. Now the reason that I have even started learning this, is cause I wanted to see what all the hype is about. Plus I wanted to get a better understanding of the Python language its self.

Now I come from the world of Express.js which is kinda of like Flask, in terms of its bare-bones nature. So when I first saw Django I was reminded of ASP.NET, and Ruby on Rails. You might be thinking what sort of nonsnese is this kid spouting ? How the can he put two of the most loved opens sourced projects next to, Microsofts evil ways. And to that I respond with: A) Microsoft is not evil, and B) I say that since all three of these frameworks have so much built in functionality, that they become opaque. Opaque as in not transparent as in have to use for along time to really understand how they work.

Anyways this is a post letting you guys or gals out there know, I am going to be making a tutorial series as I start to learn the framework.

Yes I know that all my posts are starting to seem like tutorials or updates. I promise they will eventually be about something else 😅