Posts

Showing posts with the label Python Bits

How to Mount Your Google Drive on Google Colab

Image
 Easy peasy! 1. Use Google Package and put the following lines inside your Colab Workspace 2. A new authentication dialogue will be popped in. Select your respective Google account and press continue  button until the dialogue disappears 2. You will return to your Colab workspace and Voila! Happy working!

Regex: Substitution

Image
  Suppose we have an original string to be manipulated, namely test . In this particular case, we will find all matching pattern (all words follow by dot character) and replace the dot with a versatile symbol (or a hashtag, "#").

Creating Python Virtual Environment: How-to

Image
Hi all, In this posting, I'm going to show how we can create a Python virtual environment using Windows Command Prompt (CMD). Here's the plan. I'm going to create a new folder named upwork where I will store all documents and source codes from Upwork projects. In this upwork folder, I will create a new python virtual environment named vupwork. I'm using Windows Command Prompt but I'm sure there will be more similar tutorials for you, non-windows users. Let's dive in! 1. Create your first virtual environment. The command would be in the following form: python[python_version] -m venv [your_venv_name] 2. Now, activate the newly created virtual environment using the following command: [your_venv_name]\Scripts\activate 3. Now, as your virtual environment is already activated, you may want to install some packages using pip. Let's try to add pandas!         pip install [packages_to_be_installed] 4. Last thing: deactivate your virtual environment using the deacti...

Solving [SSL: WRONG_SIGNATURE_TYPE] wrong signature type when Scraping Websites

So, recently I worked on a project where I was required to collect all available course information at a targeted university website. You know the drill. I started to import requests and BeautifulSoup and was about to pass the target URL into the requests.get(). I thought it was going to be smooth and easy as it used to be. Unfortunately, an error came up and prevented the scraping to be running.  This was the error: [SSL: WRONG_SIGNATURE_TYPE] wrong signature type So what I did (as always) was started surfing StackOverflow. Without taking much time, I found this post . Here's the workaround, just add the TLSAdapter class (as shown below) and you're good to go. So, instead of using requests.get() directly, it is suggested to make use of the session in order to incorporate the TLSAdapter() prior to reaching the target URL. Credit goes to pyOliv . Hope it helps you too.

Adding Months to Current Date? Use relativedelta()

Image
After minutes of surfing in the ocean of Stackoverflow, find this trick to add months to the current date (dealing with date is painful already in any language, at least to me personally). Use relativedelta() from dateutil library. and the result shows: Check out the date after adding two months. Voila! Just like that. Credit: https://stackoverflow.com/questions/4130922/how-to-increment-datetime-by-custom-months-in-python-without-using-library

Using .isin() to select multiple rows in Pandas DataFrame

Image
ource:  https://stackoverflow.com/questions/12096252/use-a-list-of-values-to-select-rows-from-a-pandas-dataframe

Machine Learning and Deep Learning Study Index

Advanced CNN dl2ai: Deep CNN (AlexNet) Chapter 7 Activation Function KDNuggets: Neural Network Foundations Explained, Activation Function Machine Learning Mastery: How to Choose Activation Function StatsExchange: Activation Function for First Layer Nodes in an ANN Activation Function in Neural Networks: Sigmoid, Tanh, ReLU, Leaky ReLU, Parametric ReLU, ELU, Softmax, GeLU | Medium Using Activation Functions in Neural Networks - MachineLearningMastery.com CNN Training a CNN from Scratch Padding, Stride Codebasics: Convolution Padding and Stride dl2ai: Padding and Stride Chapter 6.3 Deep Learning Deep Learning Illustrated, Part 3: Convolutional Neural Networks | by Shreya Rao | May, 2024 | Towards Data Science Deep Learning Illustrated, Part 4: Recurrent Neural Networks | by Shreya Rao | Jun, 2024 | Towards Data Science Decision Tree 11.1 - Construct the Tree | STAT 508 (psu.edu) Image Augmentation dl2ai: Image Augmentation Chapter 13 Nanonets: Data Augmentation Roboflow: Why and How to I...

Computing Loss in PyTorch

Image
How it works: How the implementation in PyTorch looks like:   Rule of thumb: The more accurate the network, the smaller the loss. reference: Datacamp's Introduction to Deep Learning with PyTorch

Using Softmax in Pytorch

Image
First thing, import torch and torch.nn In the next steps, define a random input tensor with the shape of (2,3). See the difference when softmax is applied on the dimension-0 (dim=0) and dimension-1 (dim=1). Hope this helps. reference:  https://pytorch.org/docs/stable/generated/torch.nn.Softmax.html

Convert Epoch Time to Datetime in Python

  import datetime epoch_time =  1541290680 result_datetime = datetime.datetime.fromtimestamp(epoch_time) print(result_datetime)  # prints 2018-11-04 12:18:00 or alternatively, if you're in rush, head to online epoch converter  here the above code is modified from:  https://www.javatpoint.com/python-epoch-to-datetime

Word Wrapping in Google Colab using textwrap

 import textwrap wrapper = textwrap.TextWrapper(width=40, initial_indent=" " * 4, subsequent_indent=" " * 4, break_long_words=False, break_on_hyphens=False) print( wrapper.fill (string)) source

Using dict.get()

  dict.get()  is used to get the value of an item in a given dictionary using its key (see  W3Schools  for further reference). Why do we use e this method? We can access the item value just by calling its key directly, can't we? (e.g. dict[key]) I think the main advantage of this method is we can actually check whether or not a key exists in the given dictionary without having the hassle of getting an error returned if the key does not exist. How come? dict.get()  accepts two parameters: the key itself and ...  the value   (optional).  The value  will return a specified value if the key we look for does not exist in the dictionary. Example: Suppose we have a dictionary, namely d, as follows: d = {'a': 1, 'b':2, 'c':3} Suppose we would like to get the value of an item with key = 'z'.  d['z'] will raise an error due to the fact that there is no item in the dictionary d whose key is 'z'.  Now, if we apply dict.get() as follows:...

How to Print Today's Date and Current Timestamp in Python

Image

Punctuation Removal using Python

Image
Let a string variable named  s  = '!!ab#c???' Use the following lines to remove all punctuations from  s The result of the code above: Clean and concise! London, January 26 2022