Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Friday, September 13, 2019

How to self learn programming from youtube tutorial or online courses

We all know there are tons of online resources you can learn at your own pace, such as youtube videos (courses, tutorials, playlists, etc), online courses you purchase from Udemy, O'Reilly, etc; subscriptions like codeakid.com, apexlearning.com, etc, etc, etc...

How do you make good use of these online resources and learn at your own pace and get most out of these resources? I highly recommend you the following:


  1. Code along -- play the videos, code along as the videos, if you need more time, pause the videos and continue, if you miss some part, wind back and replay;
  2. Check source code -- Many of online courses, tutorials provide source code from the author, a lot of time you can download or find on the github, try to download it or fork it into your own repository (I also recommend you learn about the basic usage of github/git for source code control and management)
  3. Participate in Q&A -- Many of the online courses, tutorials (like on yourtube) has views' comment, or online forum for Q&A, participate there, ask your questions, answer others' questions if you know the answer. You learn from participation
  4. Practice, Practice, and Practice -- Think of your own project ideas, and implement these great ideas with the skills you just learned from the online resources, apply your knowledge into these projects

Thursday, September 5, 2019

How to create a WordCloud image with 20 lines of python code

Instruction for Mac OS, you can easily find the windows equivalence.

Pre-requsite:
1. Install python3

  • Follow this link to download the latest python3 OS X package
  • Run the package and follow the steps to install python3
  • Once the installation is done, on your Terminal, run the following to confirm python 3.7.4 is installed
       python3 --version


2. Install pip3

  • Securly download the get-pip.py file from this link
  • From the directory where the file was downloaded to, run the following command in the Terminal
      python3 get-pip.py

Setup:
Install the following package: 
   pip3 install wikipedia wordcloud matplotlib

Python code:
Create a python file name: generatewc.py, and paste the following 20 lines:

import wikipedia
from wordcloud import WordCloud, STOPWORDS
import os
from PIL import Image
import numpy as np
currdir = os.path.dirname( __file__ )
def get_wiki(query):
    title = wikipedia.search(query)[0]
    page = wikipedia.page(title)
    return page.content
def create_wordcloud(text):
    mask = np.array(Image.open(os.path.join(currdir, "cloud.png")))
    stopwords = set(STOPWORDS)
    wc = WordCloud(background_color="pink",
        mask = mask,
        max_words=200,
        stopwords=stopwords)
    wc.generate(text)
    wc.to_file(os.path.join(currdir, "wc.png"))
create_wordcloud(get_wiki("Sichuan cuisine"))

Explanation: What this code does is -
1. first function get_wiki(query) to generate the page content of a given query, for example: "Sichuan cuisine"
2. second function create_wordcloud(text) to generate the word cloud image of the most seen words in the wikipedia page retrieve from the first function
3. the image is saved in the same location of the generatewc.py as "wc.png"

Results: