Skip to main content

Posts

Showing posts from June, 2022

Venn Diagram with matplotlib-venn in Python

To Draw Venn Diagram with matplotlib-venn you have to do  pip install matplotlib-venn . !pip install matplotlib-venn Quick start # library import matplotlib.pyplot as plt from matplotlib_venn import venn2 # Use the venn2 function venn2(subsets = (10, 5, 2), set_labels = ('Group A', 'Group B')) plt.show() See /www.python-graph-gallery.com/venn-diagram/ Unicode Characters in Intersection and Union # Unicode Character INTERSECTION ∩ print('\u2229') # Unicode Character Union ∪ print('\u222A') Draw Venn Diagram with matplotlib-venn with set_colors from matplotlib_venn import venn3 # Create set objects: {}, set() A = {2, 4, 6, 8, 10, 12} B = {3, 6, 9, 12, 15, 18} C = {5, 10, 15, 20, 25} # Draw Venn Diagram with matplotlib-venn v = venn3([A, B, C], set_labels = ('Set A', 'Set B', 'Set C'), set_colors=('gray', 'gray', 'gray')) plt.show() Custom the Venn Diagram # Cust...

Convert decimal fraction to binary in Python

What if you divide 7 by 32 in binary? $7\div32=0.21875$ Double the fractional part of the number one after another and write the result below. 1>>> $0.21875\times2=0.4375$, integral part is 0 and fractional part is 0.4375. 2>>> $0.4375\times2=0.875$, integral part is 0 and fractional part is 0.875. 3>>> $0.4375\times2=1.75$, integral part is 1 and fractional part is 0.75. 4>>> $0.75\times2=1.5$, integral part is 1 and fractional part is 0.5. 5>>> $0.5\times2=1.0$, integral part is 1 and fractional part is 0. The binary number you want is the integer parts from the top. So equivalent binary of fractional is 0.00111. When the denominator is a power of 2, it is easier to convert. $$ \begin{aligned} \dfrac{7}{32}&=\dfrac{4}{32}+\dfrac{2}{32}+\dfrac{1}{32}\\ &=\dfrac{1}{8}+\dfrac{1}{16}+\dfrac{1}{32}\\ &=\dfrac{1}{2^3}+\dfrac{1}{2^4}+\dfrac{1}{2^5}\\ &=0.00111\ (2) \end{aligned} $$ So equivalent binary of fractional ...

Count inversions to sort in descending (or ascending) order in Python

I get to think of the Kendall Tau Correlation between predicted cell orders and ground truth cell orders by counting how many swaps of adjacent cells are needed to sort the predicted order into the ground truth oder, after saw the Kagle Competition: Google AI4 Code– Understand Code in Python Notebooks . The Kendall Tau Correlation is the following formula: $$K = 1 - 4 \frac{\sum_i S_{i}}{\sum_i n_i(n_i - 1)}$$ where $S_{i}$ is the number of inversions in the predicted and $n_{i}$ is the number of cells. If you have a list: [3, 2, 1] to be correctly sorted into the ground truth: [1, 2, 3], what the number of swaps is needed? [3, 2, 1] -> [2, 3, 1] -> [2, 1, 3] -> [1, 2, 3] So, there you have 3 swaps to get the correct form. The ground truth is a list in ascending order. In the way to sort in ascending order, an inversion within a sequens A is counted and swaped each adjacent cell into the other when $i < j$ but $A[i] > A[j]$. def count_inversions(predictions): ...

Oscillation Frequency for RC Phase-Shift Oscillator

Oscillation Frequency for RC Phase-Shift Oscillator Oscillation Frequency for RC Phase-Shift Oscillator ¶ In the pahse-shift oscillator the feedback circuit is three cascaded RC secrions. For simple design the capacitors and resistors in each section have the same value R and C. Fig. RC Phase-Shift Oscillator ¶ Calculate the Oscillation Frequency ¶ At each capacitor and resistor the currencies are $ \begin{aligned} i_1 &= j\omega C\,(v_1-v_a)\\ i_2 &= \cfrac{v_a}{R}\\ i_3 &= j\omega C\,(v_a-v_b)\\ i_4 &= \cfrac{v_b}{R}\\ i_5 &= j\omega C\,(v_b-v_2)\\ i_6 &= \cfrac{v_2}{R}\\ i_7 &= \cfrac{v_2}{R_i} \end{aligned} $ Kirchhoff's Current Law gives $ \begin{aligned} i_1 &= i_2+i_3\kern 2em&\dots(1)\\ i_3 &= i_4+i_5&\dots(2)\\ i_5 &= i_6+i_7&\dots(3)\\ \end{aligned} $ Eq.(1) becomes $ \begin{aligned} &j\omega C...