Prepare mods collection

This commit is contained in:
2021-05-03 14:47:07 +02:00
parent 6758c61c00
commit e8ae55611a
16 changed files with 5 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
---
title: What is a shell? 🐚 adalah kerang ajaib?
date: 2020-01-25 11:58:47 +07:00
modified: 2020-02-02 16:49:47 +07:00
tags: [unix/linux, cli]
description: Shell adalah sebuah command-line interpreter; program yang berperan sebagai penerjemah perintah yang diinputkan oleh User yang melalui terminal, sehingga perintah tersebut bisa dimengerti oleh si Kernel.
image: "/apa-itu-shell/shell_evolution.png"
---
<a href="http://www.youtube.com/watch?v=tc4ROCJYbm0&t=70" target="_blank" rel="noopener">Dulu</a> Sebelum adanya <abbr title="Graphical User Interface">GUI</abbr> cara user berinteraksi dengan komputer menggunakan <abbr title="Command Line Interface">CLI</abbr> yaitu mengetik baris perintah pada sebuah antarmuka dalam bentuk baris teks seperti 👇.
<figure>
<img src="/apa-itu-shell/terminal_nginx.gif" alt="installing nginx in ubuntu">
<figcaption>Fig 1. Terminal emulator, instalasi package dan check service.</figcaption>
</figure>
Jika kamu pernah menggunakan unix/linux mungkin pernah menggunakan program diatas, bahkan mungkin setiap hari menggunakannya untuk mengeksekusi suatu perintah melalui <a href="http://en.wikipedia.org/wiki/List_of_terminal_emulators" target="_blank" rel="noopener">terminal emulator</a>.
User<sup id="user">[[1]](#user-ref)</sup> tidak bisa secara langsung berkomunikasi dengan sebuah hardware komputer, maka dari itu kita membutuhkan sebuah sistem operasi; **Kernel** adalah program yang merupakan inti utama dari sistem operasi komputer.
<figure>
<img src="/apa-itu-shell/kernel.png" alt="kernel central of operating system">
<figcaption>Fig 2. bagan kernel.</figcaption>
</figure>
Kernel memfasilitasi interaksi antara komponen perangkat keras dan perangkat lunak, berperan untuk menangani permintaan input/ouput dari perangkat lunak, selanjutnya menerjemahkannya ke dalam pemrosesan data untuk diintruksikan ke CPU, sehingga Hardware(cpu, memory, devices) mengerti perintah yang dimaksud dari pengguna.
Ketika kita menginputkan suatu perintah pada terminal emulator, kernel tidak langsung mengerti perintah yang kita ketik, kita membutuhkan suatu interface sebagai perantara menuju kernel yaitu **Shell**.
<figure>
<img src="/apa-itu-shell/shell.png" alt="shell">
<figcaption>Fig 3. bagan komunikasi shell.</figcaption>
</figure>
<mark>Shell adalah sebuah command-line interpreter; program yang berperan sebagai penerjemah perintah yang diinputkan oleh User yang melalui terminal</mark>, sehingga perintah tersebut bisa dimengerti oleh si Kernel.
Login shell biasanya ditetapkan oleh local System Administrator ketika pada saat pertama user kamu dibuat, kamu bisa lihat login shell yang sedang kamu gunakan dengan perintah dibawah ini.
```bash
$ echo $SHELL
# atau
$ echo $0
```
Setiap shell mempunyai default prompt. beberapa shell yang paling umum:
```bash
$ (dollar sign) # sh, ksh, bash
% (percent sign) # csh, tcsh
```
##### Terminologi pada shell prompt
Shell prompt adalah tempat dimana kita menuliskan suatu perintah, berikut adalah terminologinya ini membantu, jika kamu ingin mengetahui bagian-bagianya.
<figure>
<img src="/apa-itu-shell/term_shell_prompt.png" alt="shell">
<figcaption>Fig 4. bagian-bagin dari shell prompt.</figcaption>
</figure>
Dibawah ini salah satu contoh perintah sederhana untuk menampilkan sebuah arsitektur CPU komputer yang sedang saya gunakan.
<figure>
<img src="/apa-itu-shell/terminal_lscpu.gif" alt="installing nginx in ubuntu">
<figcaption>Fig 5. menampilkan informasi tentang arsitektur CPU.</figcaption>
</figure>
Dari perintah yang contohkan, ketika user mengetikan suatu inputan perintah di terminal dan menekan <kbd>ENTER</kbd>, maka shell akan mengubah perintah user menjadi bahasa yang bisa dipahami oleh kernel, dan Kernel menerjemahkannya ke dalam pemrosesan data untuk diintruksikan ke Hardware sehingga menghasilkan output yg sesuai dengan perintah user.
Shell mempunyai beberapa macam dan turunan, berikut yang paling umum.
<figure>
<img src="/apa-itu-shell/shell_evolution.png" alt="shell evolution">
<figcaption>Fig 6. evaluasi shell dari tahun ke tahun.</figcaption>
</figure>
Sedikit penjelasan dari gambar diatas.
- Bourne shell `sh`
Dikembangkan oleh Stephen Bourne di Bell Labs, yang kala itu sebagai pengganti Thompson shell(diciptakan Ken Thompson), banyak sistem unix-like tetap memiliki `/bin/sh`—yang mana menjadi symbolic link atau hard link, bahkan ketika shell lain yang digunakan tetap `sh` adalah sebagai dasarnya, sebagai kompatibilitas perintah.
- Korn shell `ksh` Unix shell yang dikembangkan oleh David Korn di Bell Labs,
inisialiasi pengembangan ini berdasar pada source code Bourne shell, namun juga memiliki fitur `csh` dan `sh`, pengembanganya pun pada saat saya menulis ini pun terus <a href="http://github.com/att/ast" target="_blank" rel="noopener">terawat</a>.
- Bourne again shell `bash`
adalah proyek ini open source <a href="http://gnu.org/software/bash/" target="_blank" rel="noopener">GNU project</a> memilki kompatibel dengan `sh` yang menggabungkan fitur penting dari `ksh` dan `csh`, dan menjadi salah satu shell yang paling umum digunakan (umumnya menjadi default shell login Linux dan Apple's macOS Mojave).
- Z shell `zsh` ini mempunyai wadah komunitasnya disebutnya <a href="http://ohmyz.sh/" target="_blank" rel="noopener">"Oh My Zsh"</a>, plug-in dan theme `zsh` bisa kita temukan di komunitas ini, saya saat ini menggunakan `zsh`, shell ini juga menjadi default dari sistem operasi macOS Catalina, yang menggantikan bash.
- friendly interactive shell `fish`
yah sesuai dengan <a href="http://fishshell.com/" target="_blank" rel="noopener">deskripsi</a> di web nya, menurut saya shell ini fun banget, fitur yang saya sukai dari shell ini autosuggestions, dan konfigurasi yang mudah melalui web based.
Masih banyak yang belum dijelaskan pada tulisan ini jika masih tertarik, baca lebih <a href="http://en.wikipedia.org/wiki/List_of_command-line_interpreters#Operating_system_shells" target="_blank" rel="noopener">banyak</a> dan juga <a href="http://en.wikipedia.org/wiki/Comparison_of_command_shells" target="_blank" rel="noopener">komparasinya</a> masing-masing shell.
Jika kamu tertarik untuk mengubah default shell login pada sistem operasi, kamu bisa menginstall dengan cara mengikuti didokumentasi/cara penginstallan dimasing-masing shell disini saya tidak membahas karena distro yang kita pakai mungkin berbeda-beda.
Untuk menjadikan default shell login pada OS bisa menggunakan perintah ini.
```bash
# command
$ sudo chsh [options] [LOGIN]
# contoh penggunaan
$ sudo chsh -s /user/bin/zsh harpi
# mengubah default shell user harpi menjadi zsh shell.
$ reboot
# atau kamu juga bisa mengubah file /etc/passwd dan edit secara manual user shellnya.
# jika masih bingung manfaatkan perintah man untuk melihat manual page.
$ man chsh
```
Terakhir untuk tulisan ini, shell memilki berbagai macam, pilihlah shell yang sesuai dengan keinginanmu untuk menunjang produktivitas dan sesuaikan dengan kebutuhan, terlalu banyak plugin dan kebingungan memilih tema itu buruk 😁.
Terimakasih sudah baca, _penulis menerima kritik dan saran._
##### Notes
<small id="user-ref"><sup>[[1]](#user)</sup> Manusia yang mengoperasikan dan mengendalikan sistem komputer.</small>
##### Resources
- [Evolution shells in Linux](http://developer.ibm.com/tutorials/l-linux-shells/)
- [Kernel Defintion](http://www.linfo.org/kernel.html)
- [The Shell](http://www.cis.rit.edu/class/simg211/unixintro/Shell.html)

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 MiB

View File

@@ -0,0 +1,71 @@
---
layout: post
title: Cara ribet memperbarui forked repo
date: 2020-01-29 01:00 +0700
modified: 2020-03-07 16:49:47 +07:00
description: Ada dua cara untuk memperbarui forked repository menggunakan web interface yang disediakan oleh github tapi ribet, atau melalui terminal yang lebih ribet lagi.
tag:
- tips
- git
- software
image: /cara-memperbarui-fork-repository/repo.png
---
Berawal dari saya pengen memperbarui repo yang tua dari suatu organisasi, niatnya pengen rumat ulang nih, ternyata dari orginal reponya ada update, sekalian buat artikel deh, lebih kurang gambaranya seperti ini.
<figure>
<img src="{{ page.image }}" alt="ilustrasi repo yang mau diupdate">
<figcaption>Fig 1. Gambaran ribetnya.</figcaption>
</figure>
Ada dua cara untuk memperbarui forked repository menggunakan web interface yang disediakan oleh github tapi ribet, atau melalui terminal yang lebih ribet lagi.
### Melalui Github (boring way) 💻
1. Buka repo yang hasil fork di Github.
1. Klik **Pull Requests** di sebelah kanan, lalu **New Pull Request**.
1. Akan memunculkan hasil compare antara repo upstream dengan repo kamu(forked repo), dan jika menyatakan "There isnt anything to compare.", tekan link **switching the base**, yang mana sekarang repo kamu(forked repo) akan dibalik menjadi base repo dan repo upstream menjadi head repo.
1. Tekan **Create Pull Request**, beri judul pull request, Tekan **Send Pull Request**.
1. Tekan **Merge Pull Request** dan **Confirm Merge**.
\* _pastikan kamu tidak merubah apapun pada forked repo, supaya melakukan merge secara otomatis, kalo tidak ya paling2 konflik._
### Melalui terminal ⌨️
Tambahkan remote alamat repository yang aslinya disini tak beri nama `upstream`., ganti `ORIGINAL_OWNER` dan `ORIGINAL_REPO` dengan alamat repo aslimu.
```bash
$ git add remote upstream git@github.com:ORIGINAL_OWNER/ORIGINAL_REPO.git
$ git remote -v
> origin git@github.com:piharpi/www.git (fetch) # forked repo
> origin git@github.com:piharpi/www.git (push) # forked repo
> upstream git@github.com:ORIGINAL_OWNER/ORIGINAL_REPO.git (fetch) # upstream repo / original repo
> upstream git@github.com:ORIGINAL_OWNER/ORIGINAL_REPO.git (push) # upstream repo / original repo
```
Checkout ke local branch `master`.
```bash
$ git checkout master
> Switched to branch 'master'
```
Jika sudah, Merge local repo dengan remote `upstream/master`.
```bash
$ git merge upstream/master
```
Terakhir push local repo ke remote `origin`.
```bash
$ git add -A
$ git commit -m "updating origin repo" && git push -u origin master
```
Selamat mencoba cara ribet ini, semoga bisa dipahami, saya sendiri lebih senang melalui terminal, klo ada yang ribet kenapa cari yang mudah.
##### Resources
- [Syncing a fork](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork)
- [Update your fork directly on Github](https://rick.cogley.info/post/update-your-forked-repository-directly-on-github/#top)

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@@ -0,0 +1,64 @@
---
title: How this site was born 👶
date: 2018-09-28 09:45:47 +07:00
modified: 2019-08-29 09:24:47 +07:00
tags: [blog, netlify, jekyll, github]
description: All the services are free, a source code this site placed on github repository and intergration with netlify service, another service that you can use is github page for hosting your own static site.
---
The website was made using Jekyll the one of open source static sites generator, and using my own simple theme, I called [klisé](https://github.com/piharpi/klise).
<hr>
All the services are free, source code the site was placed on my [github](https://github.com/piharpi/mahendrata.now.sh) repository and intergration with ~~[netlify](https://netlify.com)~~ service, another service that you can use is [github page](https://pages.github.com/) for hosting your own static site.
<hr>
#### Let's do this
So, before we start create a site, you need some tools, you can self paced for how to installing each tools, on this guide i'm just want to show you how to install jekyll and deploying in netlify, but make sure you have each tools below.
#### Prerequisites
Requirements before we doing magic show.
- [Ruby](https://www.ruby-lang.org/en/downloads/) programming language
- [Git](https://git-scm.com) (version control)
- [Netlify](https://netlify.com) and [Github](https://github.com) account
- [Bundler](https://bundler.io)
#### Installation
First, you need some [SSG](https://www.staticgen.com/), there are many kind ssg, but in case i'm using Jekyll cause i'm already familiar with it, open your terminal and type command on below
```bash
$ bundle install jekyll # installing jekyll in your machine
$ jekyll new my-site && cd my-site # create new jekyll project
$ jekyll s # run jekyll server
```
Now, jekyll is running on your local machine, open your browser and go to `localhost:4000` is default address from jekyll, press <kbd>CTRL</kbd> + <kbd>C</kbd> to stop the jekyll server.
#### Adding remote repository
Before we adding remote repository, you must have [github](https://github.com/new) repository, if already have repository, just add github remote address to your local folder, with the following commands
```bash
$ git init # initializing project folder
$ git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPO.git # change UPPERCASE with your own!
$ git add -A && git commit -m "Initialize" && git push -u origin master # push code to github
```
Now check your github repository, make sure the files is uploaded correctly.
#### Deploying to netlify
Go [netlify](https://netlify.com) dashboard, and following this step.
1. click `new site from git`, then choose `Github`.
2. then choose your repository where is the jekyll sources uploaded.
3. netlify smart enough to configuring, we just need's are hosting's are hosting's are hosting's are hosting to click `Deploy site button`.
Wait for moment, and voila..! your site's are hosting and using `.netlify.com` tld, if your website wants to look professional, just buy a domain from your favorite domain store. or if you the first time, I advice using namecheap.com*(isn't sponsor)* \*based on my experienced it provides good service and have various TLDs.
So, what you waiting for, just create your own website for free.

View File

@@ -0,0 +1,559 @@
---
title: Python Notes from Intro to Machine Learning
date: 2018-04-14 10:00:00 +07:00
tags: [python, machine-learning]
description: Notes and code snippets of Python that I've been collecting so far throughout the "Intro to Machine Learning" course.
---
**This article is for Demo purpose**
The article was originally on [this repo](https://github.com/risan/risanb.com/blob/master/content/posts/python-notes-from-intro-to-machine-learning/index.md)
I rarely use Python. I only have one repository at Github that is written in Python: [iris-flower-classifier](https://github.com/risan/iris-flower-classifier). And it was written two years ago!
A few days ago I took this free course from Udacity: [Intro to Machine Learning](https://eu.udacity.com/course/intro-to-machine-learning--ud120). The machine learning related codes are quite easy to grasp since it simply uses the [scikit-learn](http://scikit-learn.org/) modules. But most of the supporting Python modules that are provided by this course were like a black-box to me. I had no idea how to download a file in Python or what's the difference between a list, a tuple and a dictionary.
That's why I decided to read all of the provided Python modules and implement it myself. I ended up refactor most of the code so it's easier to understand: [github.com/risan/intro-to-machine-learning](https://github.com/risan/intro-to-machine-learning).
So here are some notes and snippets of Python that I've been collecting so far (I'm not even halfway through the course 😝). Also, note that the codes here are still using Python version 2.7.
### Example Table overflow-table
<div class="overflow-table" markdown="block">
| Markdown | HTML | Rendered Output |
| :---------------------- | :--------------------------------- | :-------------------: |
| `[Example Link](#link)` | `<a href="#link">Example Link</a>` | [Example Link](#Link) |
| `_Be Italic_` | `<em>Be Italic<em/>` | _Be Italic_ |
| `**Be Bold**` | `<strong>Be Italic<strong/>` | **Be Bold** |
</div>
\*) _resize to see difference_
### Example Table without-overflow
| Markdown | HTML | Rendered Output |
| :---------------------- | :--------------------------------- | :-------------------: |
| `[Example Link](#link)` | `<a href="#link">Example Link</a>` | [Example Link](#Link) |
| `_Be Italic_` | `<em>Be Italic<em/>` | _Be Italic_ |
| `**Be Bold**` | `<strong>Be Italic<strong/>` | **Be Bold** |
\*) _resize to see difference_
### Main Entry File
Suppose our Python project is stored in `/foo/bar` directory. And this application has one file that serves as the single entry point. We can name this file `__main__.py` so we can run this project simply be referencing its directory path:
```bash
# Referencing its directory.
$ python /foo/bar
# It's equivalent to this.
$ python /foo/bar/__main__.py
```
### Import Python Module Dynamically
Suppose we would like to import a Python module dynamically based on a variable value. We can achieve this through the [`__import__`](https://docs.python.org/2/library/functions.html#__import__) function:
```py
module_name = "numpy"
__import__(module_name)
```
### Multiple Returns in Python
In Python, it's possible for a function or a method to return multiple values. We can do this simply by separating each return value by a comma:
```py
def test():
return 100, "foo"
someNumber, someString = test()
```
### Importing Modules Outside of the Directory
In order to import a module from outside of the directory, we need to add that module's directory path into the current file with `sys.path.append`. Suppose we have the following directory structure:
```
|--foo
| |-- bar.py
|
|-- tools
| |-- speak_yoda.py
```
If we want to use the `speak_yoda.py` module within the `bar.py`, we can do the following:
```py
# /foo/bar.py
import os
# Use relative path to tools directory.
sys.path.append("../tools")
import speak_yoda
```
However, this won't work if we run the `baz.py` file from outside of its `foo` directory:
```bash
# It works inside of the /foo directory.
$ cd /foo
$ python bar.py
# But it won't work if the code runs from outside of /foo directory.
$ python foo/bar.py
```
To solve this problem we can refer to the `tools` directory using its absolute path.
```py
# /foo/bar.py
import os
import sys
# Get the directory name for this file.
current_dirname = os.path.dirname(os.path.realpath(__file__))
# Use the absolute path to the tools directory
tools_path = os.path.abspath(os.path.join(dirname, "../tools"))
sys.path.append(tools_path)
import speak_yoda
```
## Output
### Print The Emojis
It turns out you can't just print an emoji or any other Unicode characters to the console. You need to specify the encoding type beforehand:
```py
# coding: utf8
print("😅")
```
### Pretty Print
We can use the `pprint` module to pretty-print Python data structure with a configurable indentation:
```py
import pprint
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(people)
```
## Working with Pathname
Read more about pathname manipulations in the [`os.path` documentation](https://docs.python.org/2/library/os.path.html).
### Get Filename From URL
Suppose the last segment of the URL contains a filename that we would like to download. We can extract this filename with the following code:
```py
import os
from urlparse import urlparse
url = "https://example.com/foo.txt"
url_components = urlparse(url)
filename = os.path.basename(url_components.path) # foo.txt
```
### Check if File Exists
To check whether the given file path exists or not:
```py
import os
is_exists = os.path.isfile("foo.txt")
```
### Create a Directory if It Does Not Exists
To create a directory only if it does not exist:
```py
import os
import errno
try:
os.makedirs(directory_path)
except OSError, e:
if e.errno != errno.EEXIST:
raise
```
## Working with Files
### Downloading a File
We can use the `urllib` module to download a file in Python. The first argument is the file URL that we would like to download. The second argument is the optional filename that will be used to store the file.
```py
import urllib
urllib.urlretrieve("https://example.com/foo.txt", "foo.txt")
```
### Extracting Tar File
There's a built-in `tarfile` module that we can use to work with Tar file in Python. To extract the `tar.gz` file we can use the following code:
```py
import tarfile
# Open the file.
tfile = tarfile.open("foo.tar.gz")
# Extract the file to the given path.
tfile.extractall(path)
```
We can pass the `mode` argument to the `open` method. By default, the `mode` would be `r`—reading mode with transparent compression. There are also other mode options that we can use:
- `r:gz`: Reading mode with gzip compression.
- `r:`: Reading mode without compression.
- `a`: Appending mode without compression.
- `w`: Writting mode without compression.
- Checkout other available options in [tarfile documentation](https://docs.python.org/2/library/tarfile.html).
## Working with List
### Generate a List of Random Numbers
Use the `for..in` syntax to generate a list of random numbers in a one-liner style.
```py
import random
# Initialize internal state of random generator.
random.seed(42)
# Generate random points.
randomNumbers = [random.random() for i in range(0, 10)]
# [0.6394267984578837, 0.025010755222666936, 0.27502931836911926, ...]
```
### Pair Values from Two Lists
The built-in `zip` function can pair values from two lists. However, this `zip` function will return a list of tuples instead. To get a list of value pairs, we can combine it with `for..in` syntax:
```py
coordinates = [[x, y] for x,y in zip([5,10,15], [0,1,0])]
# [[5, 0], [10, 1], [15, 0]]
```
### Splitting a List
We can easily split a list in Python by specifying the starting index and it's ending index. Note that the ending index is excluded from the result.
We can also specify a negative index. And also note that both of these indices are optional!
```py
a = [0,1,2,3,4,5]
a[0:3] # 0,1,2
a[1:3] # 1,2
a[2:] # 2,3,4,5
a[:3] # 0,1,2
a[0:-2] # 0,1,2,3
a[-2:] # 4,5
a[:] # 0,1,2,3,4,5
```
### Filtering a List In One Line
We can easily filter a list in Python by combining the `for..in` and the `if` syntax together:
```py
numbers = range(1,11)
# Filter even numbers only.
[numbers[i] for i in range(0, len(numbers)) if numbers[i] % 2 == 0]
# [2, 4, 6, 8, 10]
```
### Sorting a List in Ascending Order
In Python, we can sort a list in ascending order simply by calling the `sort` method like so:
```py
people = ["John", "Alice", "Poe"]
people.sort()
print(people) # ["Alice", "John", "Poe"]
```
### Using Filter Function with a List
Just like its name, we can use the `filter` function to filter out our list:
```py
numbers = range(1, 11)
even_numbers = filter(lambda number: number % 2 == 0, numbers)
# [2, 4, 6, 8, 10]
```
We can break the above statement into two parts:
- `lambda number: statement`: The first part is the function that we would like to run to every item on the list. `number` is the variable name we'd like to use in this function to refer to a single item from the `numbers` list. The following function body must evaluate to truthy/falsy value—falsy means the current item will be removed from the final result.
- `numbers`: The second parameter is the list that we'd like to filter.
### Using Reduce with a List of Dictionary
We can use the `reduce` function to calculate the total of a particular key in a list of a dictionary:
```py
items = [{value:10}, {value:20}, {value:50}]
# Calculate the total of value key.
totalValues = reduce(lambda total, item: total + item["value"], items, 0) # 80
```
It can be broken down into 4 parts:
- `lambda total`: It's the variable name that we'd like to use in the function body to refer to the carried or the accumulative value that will finally be returned.
- `item: statement`: `item` is the name of the variable we'd like to use within the function body to refer to the single item in the `items` list. The following function body will be executed in order to define the accumulative value of `total` for the next iteration.
- `items`: It's the list of item that we would like to "reduce".
- `0`: The last parameter is optional and it's the initial accumulative value for the first iteration.
We can also use this `reduce` function to find a single item from the list. Here's an example of code to find the person with the biggest `total_payments` within the given list of `people` dictionary.
```py
people = [
{"name": "John", "total_payments": 100},
{"name": "Alice", "total_payments": 1000},
{"name": "Poe", "total_payments": 800}
]
person_biggest_total_payments = reduce(lambda paid_most, person: person if person["total_payments"] > paid_most["total_payments"] else paid_most, people, { "total_payments": 0 })
# {'name': 'Alice', 'total_payments': 1000}
```
## Working with Dictionary
### Loop Through Dictionary
We can use the `itervalues` method to loop through a dictionary:
```py
for person in people.itervalues():
print(person["email_address"])
```
We can also use the `iteritems` method if we want to access the key too:
```py
for person in people.iteritems():
print(person[0] + ": " + person[1]["email_address"])
```
### Calculate Total of Particular Dictionary Key
Suppose we would like to calculate the total amount of `salary` key on a `people` dictionary. We can extract the `salary` key and use the `sum` function to get the total:
```py
total_salary = sum([person["salary"] for person in people.itervalues()])
```
## Working with Numpy
### Numpy Create Range of Values with The Given Interval
Use the [`arange`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html) method to create an array with an evenly spaced interval.
```py
import numpy as np
np.arange(0, 5, 1)
# array([0,1,2,3,4])
np.arange(1, 4, 0.5)
# array([1. , 1.5, 2. , 2.5, 3. , 3.5])
```
### Numpy Create Coordinate Matrices from Coordinate Vectors
We can use the Numpy [`meshgrid`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html) method to make coordinate matrices from one-dimentional coordinate arrays.
```py
import numpy as np
np.meshgrid([1, 2, 3], [0, 7])
# [
# array([[1,2,3], [1,2,3]]),
# array([[0,0,0], [7,7,7]])
# ]
```
### Flatten Numpy Array
When we have a multi-dimensional Numpy array, we can easily flatten it with the [`ravel`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html) method:
```py
import numpy as np
arr = np.array([[1,2], [3,4]])
arr.ravel()
# array([1, 2, 3, 4])
```
### Pairing Array Values with Second Axis
We can use Numpy `c_` function to pair array values with another array that will be it's second axis. Read the [`numpy.c_` documentation](https://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html).
```py
import numpy as np
x = [1,2]
y = [10,20]
np.c_[x, y]
# array([1,10], [2,20])
```
### Generate Coordinates Across The Grid
With the knowledge of Numpy [`arange`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html), [`meshgrid`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html), [`ravel`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html) and [`c_`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html) methods, we can easily generate an evenly spaced coordinates across the grid so we can pass it to the classifier and plot the decision surface.
```py
import numpy as np
# Generate an evenly spaced coordinates.
x_points, y_points = np.meshgrid(np.arange(x_min, x_max, step), np.arange(y_min, y_max, step))
# Pair the x and y points.
test_coordinates = np.c_[x_points.ravel(), y_points.ravel()]
```
## Plotting the Data
### Plot The Surface Decision
We can pass an evenly spaced coordinates across the grid to the classifier to predict the output on each of that coordinate. We can then use [`matplotlib.pyplot`](https://matplotlib.org/api/pyplot_api.html) to plot the surface decision.
```py
import matplotlib.pyplot as plt
import pylab as pl
# Pass coordinates across the grid.
predicted_labels = classifier.predict(test_coordinates)
# Don't forget to reshape the output array dimension.
predicted_labels = predicted_labels.reshape(x_points.shape)
# Set the axes limit.
plt.xlim(x_points.min(), x_points.max())
plt.ylim(y_points.min(), y_points.max())
# Plot the decision boundary with seismic color map.
plt.pcolormesh(x_points, y_points, predicted_labels, cmap = pl.cm.seismic)
```
The classifier output would be a one-dimensional array, so don't forget to [`reshape`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html) it back into a two-dimensional array before plotting. The `cmap` is an optional parameter for the color map. Here we use the `seismic` color map from `pylab` module. It has the red-blue colors.
### Scatter Plot
We need to separate the test points based on its predicted label (the speed). So we can plot the test points with two different colors.
```py
# Separate fast (label = 0) & slow (label = 1) test points.
grade_fast = [features_test[i][0] for i in range(0, len(features_test)) if labels_test[i] == 0]
bumpy_fast = [features_test[i][1] for i in range(0, len(features_test)) if labels_test[i] == 0]
grade_slow = [features_test[i][0] for i in range(0, len(features_test)) if labels_test[i] == 1]
bumpy_slow = [features_test[i][1] for i in range(0, len(features_test)) if labels_test[i] == 1]
# Plot the test points based on its speed.
plt.scatter(grade_fast, bumpy_fast, color = "b", label = "fast")
plt.scatter(grade_slow, bumpy_slow, color = "r", label = "slow")
# Show the plot legend.
plt.legend()
# Add the axis labels.
plt.xlabel("grade")
plt.ylabel("bumpiness")
# Show the plot.
plt.show()
```
If we want to save the plot into an image, we can use the `savefig` method instead:
```py
plt.savefig('scatter_plot.png')
```
## Dealing with Data
### Deserializing Python Object
We can use [`pickle`](https://docs.python.org/2/library/pickle.html) module for serializing and deserializing Python object. There's also the `cPickle`—the faster C implementation. We use both of these modules to deserialize the email text and author list.
```py
import pickle
import cPickle
# Unpickling or deserializing the texts.
texts_file_handler = open(texts_file, "r")
texts = cPickle.load(texts_file_handler)
texts_file_handler.close()
# Unpickling or deserializing the authors.
authors_file_handler = open(authors_file, "r")
authors = pickle.load(authors_file_handler)
authors_file_handler.close()
```
### Split Data for Training and Testing
We can use the built-in [`train_test_split`](http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html) function from scikit-learn to split the data both for training and testing.
```py
from sklearn.model_selection import train_test_split
features_train, features_test, labels_train, labels_test = train_test_split(texts, authors, test_size = 0.1, random_state = 42)
```
The `test_size` argument is the proportion of data to split into the test, in our case we split 10% for testing.
### Vectorized the Strings
When working with a text document, we need to vectorize the strings into a list of numbers so it's easier and more efficient to process. We can use the [`TfidfVectorizer`](http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html) class to vectorize the strings into a matrix of TF-IDF features.
```py
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(sublinear_tf = True, max_df = 0.5, stop_words = "english")
features_train_transformed = vectorizer.fit_transform(features_train)
features_test_transformed = vectorizer.transform(features_test)
```
Word with a frequency higher than the `max_df` will be ignored. Stop words are also ignored—stop words are the most common words in a language (e.g. a, the, has).
### Feature Selection
Text can have a lot of features thus it may slow to compute. We can use scikit [`SelectPercentile`](http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectPercentile.html) class to select only the important features.
```py
selector = SelectPercentile(f_classif, percentile = 10)
selector.fit(features_train_transformed, labels_train)
selected_features_train_transformed = selector.transform(features_train_transformed).toarray()
selected_features_test_transformed = selector.transform(features_test_transformed).toarray()
```
The `percentile` is the percentage of features that we'd like to select based on its highest score.
**This article is for Demo purpose**
The article was originally on [this repo](https://github.com/risan/risanb.com/blob/master/content/posts/python-notes-from-intro-to-machine-learning/index.md)

View File

@@ -0,0 +1,112 @@
---
title: React Component with Dot Notation
date: 2018-04-07 23:04:00 +07:00
tags: [javascript, react]
description: Learn how to define a React component that is accessible through the dot notation. A common component pattern to show a parent-child relation.
---
**This article is for Demo purpose**
The article was originally on [this repo](https://github.com/risan/risanb.com/blob/master/content/posts/react-component-with-dot-notation/index.md)
This is my answer to someone's question on [StackOverflow](https://stackoverflow.com/questions/49256472/react-how-to-extend-a-component-that-has-child-components-and-keep-them/49258038#answer-49258038). How can we define a React component that is accessible through the dot notation?
Take a look at the following code. We have the `Menu` component and its three children `Menu.Item`:
```jsx
const App = () => (
<Menu>
<Menu.Item>Home</Menu.Item>
<Menu.Item>Blog</Menu.Item>
<Menu.Item>About</Menu.Item>
</Menu>
);
```
How can we define a component like `Menu`? Where it has some kind of "sub-component" that is accessible through a dot notation.
Well, it's actually a pretty common pattern. And it's not really a sub-component, it's just another component being attached to another one.
Let's use the above `Menu` component for example. We'll put this component to its own dedicated file: `menu.js`. First, let's define these two components separately on this module file:
```jsx
// menu.js
import React from 'react';
export const MenuItem = ({ children }) => <li>{children}</li>;
export default const Menu = ({ children }) => <ul>{children}</ul>;
```
It's just a simple functional component. The `Menu` is the parent with `ul` tag. And the `MenuItem` will act as its children. Now we can use these two components like so:
```jsx
import React from "react";
import { render } from "react-dom";
import Menu, { MenuItem } from "./menu";
const App = () => (
<Menu>
<MenuItem>Home</MenuItem>
<MenuItem>Blog</MenuItem>
<MenuItem>About</MenuItem>
</Menu>
);
render(<App />, document.getElementById("root"));
```
Where's the dot notation? To make our `MenuItem` component accessible through the dot nation, we can simply attach it to the `Menu` component as a static property. To do so, we can no longer use the functional component for `Menu` and switch to the class component instead:
```jsx
// menu.js
import React, { Component } from 'react';
export default const MenuItem = ({ children }) => <li>{children}</li>;
export default class Menu extends Component {
static Item = MenuItem;
render() {
return (
<ul>{this.props.children}</ul>
);
}
}
```
Now we can use the dot notation to declare the `MenuItem` component:
```jsx
import React from "react";
import { render } from "react-dom";
import Menu from "./menu";
const App = () => (
<Menu>
<Menu.Item>Home</Menu.Item>
<Menu.Item>Blog</Menu.Item>
<Menu.Item>About</Menu.Item>
</Menu>
);
render(<App />, document.getElementById("root"));
```
You can also put the `MenuItem` component definition directly within the `Menu` class. But this way you can no longer import `MenuItem` individually.
```jsx
import React, { Component } from "react";
export default class Menu extends Component {
static Item = ({ children }) => <li>{children}</li>;
render() {
return <ul>{this.props.children}</ul>;
}
}
```
**This article is for Demo purpose**
The article was originally on [this repo](https://github.com/risan/risanb.com/blob/master/content/posts/react-component-with-dot-notation/index.md)

View File

@@ -0,0 +1,29 @@
---
layout: post
title: "Welcome to Jekyll!"
date: 2020-08-29 09:29:20 +0700
categories: jekyll update
---
Youll find this post in your `_posts` directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run `jekyll serve`, which launches a web server and auto-regenerates your site when a file is updated.
Jekyll requires blog post files to be named according to the following format:
`YEAR-MONTH-DAY-title.MARKUP`
Where `YEAR` is a four-digit number, `MONTH` and `DAY` are both two-digit numbers, and `MARKUP` is the file extension representing the format used in the file. After that, include the necessary front matter. Take a look at the source for this post to get an idea about how it works.
Jekyll also offers powerful support for code snippets:
{% highlight ruby %}
def print_hi(name)
puts "Hi, #{name}"
end
print_hi('Tom')
#=> prints 'Hi, Tom' to STDOUT.
{% endhighlight %}
Check out the [Jekyll docs][jekyll-docs] for more info on how to get the most out of Jekyll. File all bugs/feature requests at [Jekylls GitHub repo][jekyll-gh]. If you have questions, you can ask them on [Jekyll Talk][jekyll-talk].
[jekyll-docs]: https://jekyllrb.com/docs/home
[jekyll-gh]: https://github.com/jekyll/jekyll
[jekyll-talk]: https://talk.jekyllrb.com/