<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ning-Yu Kao on Medium]]></title>
        <description><![CDATA[Stories by Ning-Yu Kao on Medium]]></description>
        <link>https://medium.com/@kaoningyu?source=rss-1da685d0ee0d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*se1QE2rKeI9uFmo4OaVubA.png</url>
            <title>Stories by Ning-Yu Kao on Medium</title>
            <link>https://medium.com/@kaoningyu?source=rss-1da685d0ee0d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 23:45:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kaoningyu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Streamlit Trick: Automatically Reload Webpage]]></title>
            <link>https://medium.com/@kaoningyu/streamlit-trick-automatically-reload-webpage-3a8beaa3fb85?source=rss-1da685d0ee0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a8beaa3fb85</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[streamlit]]></category>
            <dc:creator><![CDATA[Ning-Yu Kao]]></dc:creator>
            <pubDate>Wed, 05 Oct 2022 04:08:28 GMT</pubDate>
            <atom:updated>2022-10-05T04:08:28.661Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*t8Kdul0cmL0yefUP" /><figcaption>Photo by <a href="https://unsplash.com/@domenicoloia?utm_source=medium&amp;utm_medium=referral">Domenico Loia</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As a Data Scientist, and a <strong>Streamlit</strong> lover, we often need to make a frontend webpage with dashboards or images. Those information only loaded once as we open the webpage. However, sometimes we want them to be refreshed or reloaded over times.</p><p><strong><em>So, how should we do to make that happend?</em></strong></p><p>Let’s start with some introduction of Streamlit, for those aren’t familiar with.</p><h3>What is Streamlit?</h3><p>Streamlit is a python-based packages that can build shareable web application without any frontend knowledge. For more details, please visit their official website <a href="https://streamlit.io/">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/1*EgMXOwmmjoEgZ2vGw0wjpQ.png" /><figcaption>Streamlit Logo</figcaption></figure><p>Owing to its convenience, Streamlit was often used by Data Scientists to create apps for data visualization and dashboarding.</p><p><strong><em>Okay, let’s move back to our topic today!!!</em></strong></p><h3>How refreshing works in Streamlit?</h3><p>After web server is running, all the code will re-run and the web application will refresh once you edit and save any code or function. This machanism is setup by default in Streamlit.</p><p>Therefore, we have to leverage this machanism by creating a python file that can help automatically update the main application file that we are running.</p><p><strong><em>The following steps are how it works~~~</em></strong></p><h3>Methodology</h3><ul><li><strong>Enable the Machanism</strong></li></ul><p>Make sure that reloading machanism is enabled in Streamlit configuration file. This file often store in path: <strong><em>“C:\Users\user\.streamlit\config.toml”.</em></strong></p><p>Open the file <strong><em>“config.toml”</em></strong>, and add the following text in it:</p><pre>[server]</pre><pre>runOnSave = true</pre><ul><li><strong>Create a python file to activate refreshing — <em>“refresh.py”</em></strong></li></ul><p>Here, we create a python file that will automatically edit another python file, <strong><em>“dummy.py”</em></strong>. This dummy file would be imported later into our main application file, here we use <strong><em>“app.py”.</em></strong></p><p>Thus, what we want to do in <strong><em>“refresh.py”</em></strong> is create a file named<strong><em> “dummy.py”</em></strong> and add some annotation randomly in it every certain time that we want to refresh.</p><p>As we imported dummy file into <strong><em>“app.py”</em> </strong>, once the function in <strong><em>“refresh.py”</em> </strong>triggered, it edit <strong><em>“dummy.py”</em></strong>, and the main file <strong><em>“app.py”</em></strong> will refresh since something changed in it.</p><p>This might seems ambiguous, so let’s directly jump into the code in each file that we created:</p><h4>refresh.py</h4><pre>from random import randint<br>import time<br>import os</pre><pre>def refresher(seconds):</pre><pre>   while True:<br>      mainDir = os.path.dirname(__file__)<br>      filePath = os.path.join(mainDir, &#39;dummy.py&#39;)<br>      with open(filePath, &#39;w&#39;) as f:<br>         f.write(f&#39;# {randint(0, 10000)}&#39;)<br>      time.sleep(seconds)</pre><pre>while(1):<br>   refresher(20) # 20s for example</pre><p>So after we start our application, make sure that you also run <strong><em>“refresh.py”</em></strong> in the backend.</p><h4>dummy.py</h4><p>You should create a blank <strong><em>“dummy.py”</em></strong> first in order to be imported into the main python file. After running <strong><em>“refresh.py”</em></strong>, what’s in <strong><em>“dummy.py”</em></strong> should look like this:</p><pre># 2963</pre><pre># just some random number in it</pre><h4>app.py</h4><p>In <strong><em>“app.py”</em></strong>, you just need to make sure that you’ve imported the dummy file:</p><pre>import dummy</pre><p><strong><em>Yeah, you’re all set!</em></strong></p><p>The above steps is all you need to do to make this work. Now your Streamlit application will automatically reload every given time!</p><p>That’s the end of this article. Please hit the clap button, leave a comment, and follow me if you’re interested about my content!!! Cheers!!!</p><p><a href="https://streamlit.io/">Streamlit * A faster way to build and share data apps</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a8beaa3fb85" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning Techniques: Feature Extraction]]></title>
            <link>https://medium.com/@kaoningyu/machine-learning-techniques-feature-extraction-76ee2f98c8f8?source=rss-1da685d0ee0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/76ee2f98c8f8</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[kaggle]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[feature-engineering]]></category>
            <dc:creator><![CDATA[Ning-Yu Kao]]></dc:creator>
            <pubDate>Mon, 03 Oct 2022 09:26:27 GMT</pubDate>
            <atom:updated>2022-10-03T09:26:27.686Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This serise is my note taking from a book called “</em><a href="https://www.amazon.co.jp/dp/4297108437"><em>Data Analysis Techniques to Win Kaggle</em></a><em>”. Highly recommended this book, please go ahead to check it out if you expect more details.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MpsBcOwTHRKoSl-h" /><figcaption>Photo by <a href="https://unsplash.com/@lukechesser?utm_source=medium&amp;utm_medium=referral">Luke Chesser</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>To improve your model performance, feature extraction is critical! In real world data, a single extracting method might not be valid for every given data. Usually, we use the apperant features to train our model, but sometimes it just doesn’t work well. Therefore, in this article, we’ll mention some method that you might want to try for extracting features. Let’s get started!!</p><h3><strong>Models and Features</strong></h3><ol><li><strong>Gradient Boost Decision Tree (GBDT)</strong></li></ol><p>This models is frequently used in competition since it has the following characteristics:</p><ul><li>Gaps between features will effect model, but values within feature itself won’t</li><li>It can still works with missing value in the dataset</li><li>It reflects relation between features through brenching</li></ul><p>That is to say, we don’t need to worry about the distribution of each feature or missing value when using GBDT.</p><p>2. <strong>Neural Network (NN)</strong></p><ul><li>Values of features will influence training result</li><li>Need to deal with missing value</li><li>Need to include the output of previous layer to reflect relation between features</li></ul><p>For this model, one-hot-encoding was recommended when dealing with class features. (Because distribution of features will influence the model, we can’t just use label encoding.)</p><p><strong>To sum up, we can use GBDT with just label encoding to be our baseline model.</strong></p><h3>Missing value</h3><p>Missing value is everywhere in a given dataset, it may happen due to the following reasons:</p><ul><li><strong>Non-exist value:</strong> dataset includes person and organization information. In this case, ages won’t exist for organization.</li><li><strong>Privacy:</strong> people might not be comfortable to offer their location or time information.</li><li><strong>Unavalibale value: </strong>might be errors from sensor</li></ul><p>Here’s some solutions for missing value:</p><ol><li><strong>Keep missing value: </strong>as we mentioned previously, we can use GBDT to deal with data with missing value.</li><li><strong>Replace with represented value:</strong> if missing value occurs randomly, we can’t just replace it with average value. However, if missing value is not normally distributed, we should use median.</li><li><strong>Replace with predicted value:</strong> we can first build a simple model and use it to predict missing value. <em>(NOTE: you can’t use the original label to predict missing value)</em></li><li><strong>Create a new feature for missing value: </strong>create a binary variable to represent the value is missing or not.</li></ol><h3>Numerical Feature Transformation</h3><h4><strong>Linear transformation</strong></h4><ol><li><strong>Standardization</strong></li></ol><p>The most commonly used method is linear transform:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/130/0*iEZzd5vdPYN2j-FP.png" /></figure><p>For some algorithms, like NN, it’ll influence the trainning process if you don’t do standardization.</p><p>Also you don’t need to do standardization for binary values.</p><pre>from sklearn.processing import StandardScaler</pre><pre>scaler = StandardScaler()<br>scaler.fit(data)<br>data = scaler.transform(data)</pre><p>2. <strong>Min-Max Scale</strong></p><p>If we want every feature in the same scale(0–1), we can use “Min-Max Scale”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/0*bu68y5eyDKPSSEWK.png" /></figure><pre>from sklearn.processing import MinMaxScaler</pre><pre>scaler = MinMaxScaler()<br>scaler.fit(data)<br>data = scaler.transform(data)</pre><p><em>(NOTE: Standardization assumed that features is normally distributed. Therefore, if not, please use Min-Max Scale instead)</em></p><h4>Non-linear transformation</h4><p>With linear transformation, the shape of distribution won’t be changed. However, it will for non-linear transformation.</p><ol><li>Log x, log(x+1), log|x|</li></ol><p>This method usually used to deal with features like “Money” or “Amounts”, which tend be “The lond tail”. Furthermore, data with negative number can’t be transform directly. We need to take the absolute of values, and then add the negative sign back after transformed.</p><pre>logx = np.log(x)</pre><pre>logxp1 = np.log1p(x)</pre><pre>logabs = np.sign(x) * </pre><p>2. Box-Cox, Yeo-Johnson</p><p>After these method, data will be transformed into normal distribution. Yeo-Johnson is specified for data with negative values.</p><pre>from sklearn.preprocessing import PowerTransformer</pre><pre>pt = PowerTransformer(method=&#39;box-cox&#39;) # Box-Cox<br>pt = PowerTransformer(method=&#39;yeo-johnson&#39;) # Yeo-Johnson</pre><pre>pt.fit(train_x[pos_cols])</pre><pre>train_x[pos_cols] = pt.transform(train_x[pos_cols])</pre><h4><strong>Clipping</strong></h4><p>We might have outlier in some dataset. To deal with this problem, we can use “clipping”. Here, we’ll set a maximum and minimum threshold. Whenever the data goes beyond the threshold, we’ll replace the data with the threshold value. We usually use quantile to do so.</p><pre>p01 = train_x[num_col].quantile(0.01) #min<br>p99 = train_x[num_col].quantile(0.99) #max</pre><pre>train_x[num_col] = train_x[num_col].clip(p01, p99, axis=1)<br>test_x[num_col] = test_x[num_col].clip(p01, p99, axis=1)</pre><h4>Binning</h4><p>Simply, this method just change numerical data into class data. That is, we can decide the range of numerical data and then convert them into class data. After we obtain classes with order, we can use them as numerical data or transfer them with one-hot-encoding as class data.</p><p>For Pandas, we can use “cut” function, while using “digitize” for NumPy.</p><pre># cut into 3 range<br>binned = pd.cut(x, 3, labels=False)</pre><pre># define range<br>bin_edges = [-float(&#39;inf&#39;), 3.0, 5.0, float(&#39;inf&#39;)]<br>binned = pd.cut(x, bin_edges, labels=False)</pre><h4>Numerical 2 Order</h4><p>If we want the order between values rather than the magnitude, we’ll like to convert values into ranking.</p><pre># pandas<br>rank = pd.Series(x).rank()</pre><pre># numpy<br>order = np.argsort(x)<br>rank = np.argsort(order)</pre><h4>RankGauss</h4><p>This method changes values into order, and then force them into normally distribution.</p><pre>from sklearn.preprocessing import QuantileTransformer</pre><pre>transformer = QuantileTransformer(n_quantiles=100, random_state=0, output_distribution=&#39;normal&#39;)<br>transformer.fit(x)</pre><pre>x = transformer.transform(x)</pre><h3>Categorical Feature Transformation</h3><p>Sometimes, class features are not in string format. You might need to deal with numerical data that should be considered as class variables.</p><p>Here’s a problem! There might be an error if a certain only contains in training data, but not in testing data. Thus, we need to make sure that we include all the classes in our testing data. Else, we need to consider following solution:</p><ul><li>Just ignore it, if it doesn’t affect performance a lot</li><li>Use medium values or predicted values to fill the lossing values</li><li>Replace with average value when being transformed</li></ul><h4>One-hot Encoding</h4><p>We create binary variables for all the classes. This variable is also called “Dummy variable”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*UOjWvDziH86T2MmiDpp98Q.png" /></figure><p>We can use “get_dummies” in Pandas package to do so:</p><pre>x = pd.get_dummies(x, columns=cat_cols)</pre><p>Or, you can use “OneHotEncoder” with Scikit-learn:</p><pre>from sklearn.preprocessing import OneHotEncoder<br>ohe = OneHotEncoder(sparse=False, categories=&#39;auto&#39;)<br>ohe.fit(x[cat_cols])</pre><pre>columns = []<br>for i, c in enumerate(cat_cols):<br>   columns += [f&#39;{c}_{v}&#39; for v in ohe.categories_[i]]</pre><pre>dummy_vals_x = pd.DataFrame(ohe.transform(x[cat_cols]), columns=columns)</pre><p><strong>Be carefull!!!!</strong> The bad side of using OHE is that the numbers of features will increase as numbers of class variables increase. That’ll increase the time for training and also need more memories. If you encounter this problem, you might need to:</p><ul><li>Seek other feature encoding method, like “Feature Hashing”</li><li>Reduce classes by some rules</li><li>Allocate classes with less occurrance to a class of “Others”</li></ul><h4>Label Encoding</h4><p>Lable encoding transform class into an integer by alphabet and numerical order. However, this method only works for tree-based machine learning method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*Yp6r7m82IoSnnZDPpDpYNw.png" /></figure><p>In Python, we use Scikit-learn to do so:</p><pre>from sklearn.preprocessing import LabelEncoder</pre><pre>for c in cat_cols:<br>   le = LabelEncoder()<br>   le.fit(train_x[c])  # fit with training data</pre><pre>   train_x[c] = le.transform(train_x[c])<br>   test_x[c] = le.transform(test_x[c])</pre><h4>Feature Hashing</h4><p>After applying featuring hashing, we can obtain less classes comparing with OHE. Before hashing, we need to decide number of features after transformation. And then, we use hash function to flag all the events.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*cZbHF48QOY6bqGhS9x2Nlg.png" /></figure><p>We can use Scikit-learn to deal with this:</p><pre>from sklearn.feature_extraction import FeatureHasher</pre><pre>cat_cols=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]</pre><pre>for c in cat_cols:<br>   fh = FeatureHasher(n_features=5, input_type=&#39;string&#39;)<br>   hash_train = fh.transform(train_x[[c]].astype(str).values)<br>   hash_test = fh.transform(test_x[[c]].astype(str).values)</pre><h4>Target Encoding</h4><p>Target encoding is a method that transform class variable into numerical variables.</p><p>However, in some situation like highly time-dependent data, this feature extraction might not return good result. Also, you should be carefull when using this method to deal with private data. Otherwise, you might encounter data leakage problem.</p><p>We’ll not include detail of this method here, please check out my another article: “<a href="https://medium.com/@kaoningyu/dont-use-one-hot-encoding-anymore-25b5882e533f">Don’t use one hot encoding anymore!!!</a>” for more details.</p><h3>The End</h3><p>Here’s the end of the article. For <strong>Natural language processing (NLP)</strong> or <strong>Time Series Prediction, </strong>we might need different feature extraction mehtods to deal with features like words or time.</p><p>Hope you guys enjoy the article!!! Please hit the clap button, leave a comment, and follow me if you’re interested about my content!!!</p><p><a href="https://medium.com/@kaoningyu/dont-use-one-hot-encoding-anymore-25b5882e533f">Don’t use One-Hot Encoding Anymore!!!</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76ee2f98c8f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Network Visualization | NetworkX, Python]]></title>
            <link>https://medium.com/@kaoningyu/network-visualization-networkx-python-d7b9377c1509?source=rss-1da685d0ee0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7b9377c1509</guid>
            <category><![CDATA[network]]></category>
            <category><![CDATA[graph-neural-networks]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Ning-Yu Kao]]></dc:creator>
            <pubDate>Thu, 09 Jun 2022 07:13:17 GMT</pubDate>
            <atom:updated>2022-06-09T07:15:36.146Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jaOkhYIE6qhg1J8S" /><figcaption>Photo by <a href="https://unsplash.com/es/@alinnnaaaa?utm_source=medium&amp;utm_medium=referral">Alina Grubnyak</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article, I’ll describe how to visualize a graph network using NetworkX. For example, I’ll use MUTAG dataset to present the implementation.</p><p>When working on projects using Graph Neural Network, I found it hard to analyze the graph before and after prediction. The reason is that we didn’t visualize them. All we got is a huge amount of input, including edges and nodes, as well as an output contain some value. This is what inspired me to write this article.</p><h4><strong><em>Outline</em></strong></h4><ul><li><em>Data source</em></li><li><em>Package</em></li><li><em>Visualization</em></li></ul><h3>Data Source — MUTAG</h3><p>The MUTAG dataset consists of 188 chemical compounds divided into two classes according to their mutagenic effect on a bacterium. The chemical data was obtained form <a href="http://cdb.ics.uci.edu/">http://cdb.ics.uci.edu</a> and converted to graphs, where vertices represent atoms and edges represent chemical bonds. Explicit hydrogen atoms have been removed and vertices are labeled by atom type and edges by bond type (single, double, triple or aromatic). Chemical data was processed using the Chemistry Development Kit (v1.4).</p><p>Represented as network, nodes and edges is labeled as:</p><pre>Node labels:<br>0  C<br>1  N<br>2  O<br>3  F<br>4  I<br>5  Cl<br>6  Br</pre><pre>Edge labels:<br>0  aromatic<br>1  single<br>2  double<br>3  triple</pre><p>The files that we’ll used here are as follows:</p><pre>|- MUTAG_A.txt             # Adjacency list<br>|- MUTAG_edge_labels.txt   # Edge labels<br>|- MUTAG_node_labels.txt   # Node labels</pre><p><em>Data source: </em><a href="https://paperswithcode.com/dataset/mutag"><em>Paper with code</em></a></p><p><em>Reference paper: </em><a href="http://pubs.acs.org/doi/abs/10.1021/jm00106a046"><em>Debnath, A.K., Lopez de Compadre, R.L., Debnath, G., Shusterman, A.J., and Hansch, C. Structure-activity relationship of mutagenic aromatic and heteroaromatic nitro compounds.</em></a></p><h3>Package — NetworkX</h3><p><a href="https://networkx.org/documentation/stable/index.html">NetworkX</a> is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.</p><ul><li>Installation</li></ul><pre>$ pip install networkx[default]</pre><ul><li>Simple example</li></ul><pre><strong>import</strong> <strong>networkx</strong> <strong>as</strong> <strong>nx</strong><br><strong>import</strong> <strong>matplotlib.pyplot</strong> <strong>as</strong> <strong>plt</strong></pre><pre><em># init network</em><br>G = nx.Graph()</pre><pre><em># add edges</em><br>G.add_edge(1, 2)<br>G.add_edge(1, 3)<br>G.add_edge(1, 5)<br>G.add_edge(2, 3)<br>G.add_edge(3, 4)<br>G.add_edge(4, 5)<br><br><em># explicitly set positions</em><br><a href="https://docs.python.org/3/library/stdtypes.html#dict">pos</a> = {1: (0, 0), 2: (-1, 0.3), 3: (2, 0.17), 4: (4, 0.255), 5: (5, 0.03)}</pre><pre><em># network visualization options</em><br><a href="https://docs.python.org/3/library/stdtypes.html#dict">options</a> = {<br>    &quot;font_size&quot;: 36,<br>    &quot;node_size&quot;: 3000,<br>    &quot;node_color&quot;: &quot;white&quot;,<br>    &quot;edgecolors&quot;: &quot;black&quot;,<br>    &quot;linewidths&quot;: 5,<br>    &quot;width&quot;: 5,<br>}</pre><pre><em># draw network</em><br>nx.draw_networkx(G, <a href="https://docs.python.org/3/library/stdtypes.html#dict">pos</a>, **<a href="https://docs.python.org/3/library/stdtypes.html#dict">options</a>)<br><br><em># plot setting and show</em><br>ax = <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.gca.html#matplotlib.pyplot.gca">plt.gca</a>()<br><a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.margins.html#matplotlib.axes.Axes.margins">ax.margins</a>(0.20)<br><a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axis.html#matplotlib.pyplot.axis">plt.axis</a>(&quot;off&quot;)<br><a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show">plt.show</a>()</pre><p><em>Output:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*zVlkZkPdKTCiekjl.png" /></figure><h3>MUTAG Visualization</h3><p>To visualize MUTAG dataset, we need to load our edges, nodes, and adjacency list first.</p><pre>mut_edge = np.loadtxt(&#39;MUTAG_edge_labels.txt&#39;)<br>mut_node = np.loadtxt(&#39;MUTAG_node_labels.txt&#39;)<br>A= np.loadtxt(&#39;MUTAG_A.txt&#39;, dtype=str)</pre><p>Second, let’s define our nodes as a dictionary of node labels and a color that represent them.</p><pre><em># define colors for each label</em><br>color_dict= {0:&quot;Red&quot;, 1:&quot;Blue&quot;, 2:&quot;Yellow&quot;,3:&quot;Green&quot;, 4:&quot;Gray&quot;, 5:&quot;Purple&quot;, 6:&quot;Pink&quot;}</pre><pre><em># create dict for nodes</em><br>node={}<br>for i in range(3371):<br>   node[str(i)]=dict(color=color_dict[mut_node[i]])</pre><p>For edges, we define a list of edges based on the adjacency list and their labels.</p><pre><em># dict for edge labels</em><br>bond_dict = {0:&quot;aromatic&quot;, 1:&quot;single&quot;, 2:&quot;double&quot;, 3:&quot;triple&quot;}</pre><pre><em># list to define edges</em><br>edges=[]<br>for i in range(7440):<br>   edges.append((A[i][0].split(&#39;,&#39;)[0], A[i][1],                               bond_dict[mut_edge[i]]))</pre><p>Last but not least, let’s create our network and start adding nodes, edges, and style. After all, we can just draw it out and see the result.</p><pre><em># init graph</em><br>original_graph = nx.Graph()</pre><pre><em># add nodes and edges</em><br>original_graph.add_nodes_from(n for n in node.items())</pre><pre>original_graph.add_edges_from((u, v, {&quot;type&quot;: label}) for u, v, label in edges)</pre><pre><em># format plot</em><br>plt.figure(1, figsize=(50, 25), dpi=60)</pre><pre><em># set baseline network plot options</em><br>base_options = dict(with_labels=False, edgecolors=&quot;black&quot;, node_size=200)</pre><pre><em># define layout of position</em><br>pos = nx.spring_layout(original_graph, seed=7482934)</pre><pre><em># set node&#39;s color as we define previously</em><br>node_colors = [d[&quot;color&quot;] for _, d in original_graph.nodes(data=True)]</pre><pre><em># add weights on edges to classify different bonds by widths</em><br>edge_type_visual_weight_lookup = {&quot;aromatic&quot;:1,&quot;single&quot;:2, &quot;double&quot;:3, &quot;triple&quot;:4}</pre><pre>edge_weights = [edge_type_visual_weight_lookup[d[&quot;type&quot;]] for _, _, d in original_graph.edges(data=True)]</pre><pre># draw network<br>nx.draw_networkx(original_graph, pos=pos, node_color=node_colors, width=edge_weights, **base_options)</pre><pre>plt.show()</pre><p><em>Output:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x8T1Dl0kiUiabap4mLr5WQ.png" /></figure><p>Amazing!!! Isn’t it?</p><p>That’s all I have for this article, thank you guys for supporting me!!! Please hit the clap button, leave a comment, and follow me if you’re interested about my content!!!</p><p><em>Check out my article about </em><a href="https://medium.com/@kaoningyu/introduction-of-graph-convolutional-network-gcn-quick-implementation-5dd75e75b261"><em>Graph Convolutional Network</em></a><em> in advance!!!</em></p><p>You may also want to check out some reference below for further information.</p><p><strong><em>See you guys next time!!!</em></strong></p><ul><li><a href="https://networkx.org/documentation/stable/tutorial.html">Tutorial - NetworkX 3.6.1 documentation</a></li><li><a href="https://distill.pub/2021/gnn-intro/">A Gentle Introduction to Graph Neural Networks</a></li><li><a href="https://networkrepository.com/Mutag.php">Mutag | Labeled Networks | Network Data Repository</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7b9377c1509" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction of Graph Convolutional Network (GCN) & Quick Implementation]]></title>
            <link>https://medium.com/@kaoningyu/introduction-of-graph-convolutional-network-gcn-quick-implementation-5dd75e75b261?source=rss-1da685d0ee0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/5dd75e75b261</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ning-Yu Kao]]></dc:creator>
            <pubDate>Wed, 18 May 2022 09:30:40 GMT</pubDate>
            <atom:updated>2022-05-18T09:30:40.381Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*bqJJotJjbY3EC5oc.png" /></figure><p>With modern machine learning methods, tasks like object detection, machine translation, and speech recognition, have been given new life with state-of-the-art deep learning, which is good at capturing hidden patterns of Euclidean data (images, text, videos). However, what about applications where data is generated from non-Euclidean domains, usually composed of <strong>graphs with complex relationships and interdependencies between objects?</strong></p><p>That’s why we need <strong>Graph Neural Networks (GNN)! </strong>Among different kinds of GNN, we introduce one of the most popular model, <strong>Graph Convolutional Network (GCN)</strong>, here — convolutional, because filter parameters are typically shared over all locations in the graph.</p><h3>Graph Convolutional Network (GCN)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*GJ7zWp4Y3H9iom9xriq-KQ.png" /><figcaption>Visualization of the zackary karate club network</figcaption></figure><p>For GCN, the goal is to learn a function of feature from a graph <em>G=(V, E) </em>and take as input:</p><ul><li><strong><em>N×D</em></strong> feature matrix <strong><em>X</em> </strong>(<strong><em>N</em></strong>: number of nodes, <strong><em>D</em></strong>: number of input features), represents feature description for every node.</li><li>An adjacency matrix <strong><em>A, </em></strong>represents description of the graph structure.</li></ul><p>and return an output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/88/0*xgFFIqi7JYBNzotV.png" /><figcaption>(where F is the number of output features per node)</figcaption></figure><p>Thus, every neural network layer can then be written as a non-linear function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/128/0*UGVQ53frlXjQLaKc.png" /><figcaption>(“L” being the number of layers)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/173/0*KJQ1CsNj52alHYxc.png" /></figure><p>where <strong><em>W(l)</em> </strong>is a <strong>weight matrix</strong> for the <em>l</em>-th neural network layer and <strong><em>σ( )</em></strong> is a non-linear <strong>activation function</strong> like the <strong>ReLU.</strong></p><p>At this point, we need to deal with limitations in <strong><em>A </em></strong>by:</p><ul><li>Add the identity matrix to <strong><em>A</em></strong> to fix this by enforcing self-loops in the graph.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/81/0*ECJwAzSPY74JdGC_.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*oWAD2Z79KTyH5ogta8V6Kg.png" /><figcaption>Visualization of A for the zackary karate club network</figcaption></figure><ul><li>Normalizing <strong><em>A</em></strong> such that all rows sum to one in order to avoid changing scale of the feature vectors. We usually used<strong> symmetric normalization</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/83/0*mOzFymJr2oDk_6O-.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*r1UW4UZXuXsqk_TZZ0GN_w.png" /><figcaption>Visualization of A after normalization</figcaption></figure><p><em>Code for dealing with </em><strong><em>A </em></strong><em>provided here:</em></p><pre>import numpy as np<br>import scipy as sp</pre><pre>degrees = list(dict(graph.degree).values())<br>degrees = [d+1 for d in degrees]<br>D = np.matrix(np.diag(degrees))<br>D_normed = np.matrix(sp.linalg.fractional_matrix_power(D, -0.5))<br>A_normed = D_normed*AplusI*D_normed</pre><p>After all these steps, we can now define our GCN layer as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/0*z0rNIx8TBUyrRVqR.png" /></figure><p>Next, we can use the same algorithm such as backpropagation and gradient descent to optimize our weight <strong><em>W. </em></strong>We are not gonna include the algorithm here. Once you know the basic knowledge of GCN, we’ll want to know how to apply it directly. So, let’s dive into how to impelement GCN for our data.</p><h3>Implementation of GCN</h3><blockquote><strong>Tools you need:</strong></blockquote><ol><li><a href="https://github.com/pyg-team/pytorch_geometric"><em>PyTorch Geometric</em></a></li></ol><p>PyG <em>(PyTorch Geometric)</em> is a library built upon <a href="https://pytorch.org/">PyTorch</a> to easily write and train Graph Neural Networks (GNNs) for a wide range of applications related to structured data.</p><p>This library help us quickly download some open source data and build our model.</p><p>2. <a href="https://arxiv.org/abs/1707.04638"><em>PPI Dataset</em></a></p><p>This is protein-protein interaction network that contains physical interactions between proteins. Nodes represent human proteins and edges represent physical interaction between proteins in a human cell. This data contains positional gene sets, motif gene sets and immunological signatures as features (50 in total) and gene ontology sets as labels (121 in total).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*O3ZlXYYjqJVTVgTW" /><figcaption>Example of PPI network</figcaption></figure><p>3. <a href="https://wandb.ai/site"><em>Weights &amp; Biases</em></a></p><p>Weights &amp; Biases is the <strong>MLOps</strong> platform for developers to build better models faster. With W&amp;B, you can quickly track experiments, version and iterate on datasets, evaluate model performance, reproduce models, visualize results and spot regressions, and share findings with colleagues.</p><p>I’ll then use this tools to track our training log, you guys can see the dashboard right <a href="https://wandb.ai/kaoningyu/gcn-ppi/reports/Predict-PPI-network-using-GCN--VmlldzoyMDIyNDgw?accessToken=0fgywscyq72iiivoyiq1lunofjhvkdqbejovoug456owxpjkax4ize40ug7wtwtr">here</a>!</p><blockquote><strong>Implementation:</strong></blockquote><p>For implementation, please check out my <a href="https://github.com/ning-yu-kao/GCN_quick_implement"><strong>GitHub</strong></a> repository for detail guideline. Here, we briefly mention some steps:</p><ol><li><em>Install W&amp;B and PyTorch Geometric</em></li></ol><p>Create an environment and install all the required packages. <strong>Note that you need to preinstall </strong><a href="https://pytorch.org/"><strong>PyTorch</strong></a><strong> before installing PyG</strong>.</p><p>2. <em>Run the code</em></p><p>Start training and view our result on W&amp;B dashboard. Make sure that you got a W&amp;B account with your login key.</p><h3>The end</h3><p>That’s the end of this article. Please hit the clap button, leave a comment, and follow me if you’re interested about my content!!!</p><p>You may also want to check out some reference below for further information.</p><p><em>See you guys next time!!!</em></p><h3>Reference</h3><ul><li><a href="https://towardsdatascience.com/how-to-do-deep-learning-on-graphs-with-graph-convolutional-networks-7d2250723780">How to do Deep Learning on Graphs with Graph Convolutional Networks</a></li><li><a href="https://tkipf.github.io/graph-convolutional-networks/">How powerful are Graph Convolutional Networks?</a></li><li><a href="https://snap.stanford.edu/biodata/datasets/10000/10000-PP-Pathways.html">Human protein-protein interaction network</a></li><li><a href="https://github.com/pyg-team/pytorch_geometric">GitHub - pyg-team/pytorch_geometric: Graph Neural Network Library for PyTorch</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5dd75e75b261" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Don’t use One-Hot Encoding Anymore!!!]]></title>
            <link>https://medium.com/@kaoningyu/dont-use-one-hot-encoding-anymore-25b5882e533f?source=rss-1da685d0ee0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/25b5882e533f</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[encoding]]></category>
            <category><![CDATA[feature-engineering]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Ning-Yu Kao]]></dc:creator>
            <pubDate>Tue, 03 May 2022 07:04:26 GMT</pubDate>
            <atom:updated>2022-05-03T07:04:26.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*ayeS-QT-D6u9bFHiXkVFng.jpeg" /></figure><p>In machine learning, we usually need to transform categorical features into numerical features for training. This preprocessing method is called <strong>“Categorical Encoding”</strong>.</p><p>The first thing that come up in your mind to solve this problem might be “One-Hot Encoding”, a method that turn categorical features into binary variables. But now, I’m gonna tell you that <strong>“STOP USING ONE-HOT ENCODING”.</strong></p><h3>Why no more one-hot encoding?</h3><p>We’re not gonna talk about what is one-hot encoding here. If you are not familiar with this, please check out some descriptions and sample code at <a href="https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html">Scikit-learn</a> website.</p><p>So why? why we should not use one-hot encoding? Here’s the reason:</p><ul><li><strong><em>It increases the amount of features</em></strong></li></ul><p>Increasing the amount of features is not appriciated for most machine learning methods. Imagine you are encoding categorical features like location or name, it usually end up with 1000+ dimensions.</p><p>The reason behine the thing is the “<a href="https://towardsdatascience.com/the-curse-of-dimensionality-50dc6e49aa1e">Curse of Dimensionality</a>”, which make our machine learning model harder to converge and generalize. Also, more features means more training time and memeory!</p><ul><li><strong><em>It generates too many zeros</em></strong></li></ul><p>Too many zero values always confuse machine learning models, especially deep learning. This is because we are optimizing loss function at a relatively flat gradient with many zeros. That is to say that it might be harder to converge, which is what we don’t want to see when training.</p><h3>What else can we do?</h3><p>So please~please tell me what else can we do to encode categorical features? Hopefully, we got bunch of method to do so!!! Let’s take a look at them!</p><h4>Frequency Encoding</h4><p>The simplest method is using frequency encoding. Just replace categorical features with their occurrence and we’re done! The assumption behind this is that the frequency of a category is informative. So this method make sense for most cases.</p><p>We can simply use “pandas.DataFrame.value_counts()” to get the occurrence of each category.</p><pre>df[&#39;cat_col&#39;].value_counts()</pre><p>However, when we got the same frequency for multiple categories, the model might value different categories as the same.</p><h4>Target Encoding</h4><p>Also known as “mean encoding”, target encoding transform categorical values to numerical values by taking the average of all the target value for each category. We can see the following image to present this process:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*L3lQQNlevEyQH0DEc6kQ_g.png" /></figure><p>However, when using this method, we need prevent model from overly rely on these encoded values. Also, we need to be careful of outlier because that might greatly influence the mean value.</p><p>But don’t worried! Let’s introduce “<a href="https://github.com/scikit-learn-contrib/category_encoders">Categorical Encoders</a>”! This package do all the things for you.</p><p>It’s similar to how you do in Scikit-learn package. Just use<em> .fit()</em>, <em>.transform()</em>,and<em> .fit_transform() </em>to transform your features.</p><pre># use target encoding<br>te = TargetEncoder(cols=[&#39;A&#39;, &#39;B&#39;])<br><br># transform the datasets<br>training_data = te.fit_transform(X_train, y_train)<br>testing_data = te.transform(X_test)</pre><h3>What’s more?</h3><p>That’s not the end, let’s also include some advance encoding methods!!!!</p><h4>Leave-One-Out Encoding (LOO Encoding)</h4><p>This is a modified version of target encoding. As we mention earlier, model might be overly rely on those value after target encoded. Fortunately, LOO encoding helps us mitigate this problem by leaving out its own target value when encoding. That is to say, we only include the other target values that is in the same category as the one that we are encoding.</p><p>To apply this method, we can also use the package - “<a href="https://github.com/scikit-learn-contrib/category_encoders">Categorical Encoders</a>”.</p><pre>import category_encoders as ce</pre><pre>loo_encoder = ce.LeaveOneOutEncoder(cols=[A, B], sigma=0.05)</pre><pre>loo_encoder.fit(X, y)<br>X_encoded = loo_encoder.transform(X)</pre><p>The hyperparameter, <strong>‘sigma’</strong>, in LOO encoder add an noise of normal distribution of <strong>mean=0</strong> and <strong>standerd deviation=’sigma’</strong> to make our model more stable. The recommended value of ‘<strong>sigma</strong>’ is between <strong>0.05–0.6</strong>.</p><h4>Generalized Linearn Mixed Model (GLMM)</h4><p>Simply, this method use “Generalized Linear Model” to analyize categorical feature. We’re not including all the math and detail in this article, but you can just regard this method as applying “linear regression” on target encoding.</p><p>The advantage of using GLMM is that we don’t need to adjust any hyperparameter, and we can still return a bunch of robust values.</p><p>Here’s the code by using the same package — “<a href="https://github.com/scikit-learn-contrib/category_encoders">Categorical Encoders</a>”.</p><pre>import category_encoders as ce</pre><pre>glmm_encoder = ce.GLMMEncoder(cols=[A, B], binary_target=True)</pre><pre><strong><em># binomial_target = True (for Classification)<br># binomial_target = False (for Regression)</em></strong></pre><pre>glmm_encoder.fit(X, y)<br>X_encoded = glmm_encoder.transform(X)</pre><p>However, this method is more time-consuming compare with other methods.</p><h3>The end.</h3><p>That’s the end of this article. Please hit the clap button, leave a comment, and follow me if you’re interested about my content!!!</p><p>Specially thanks to <a href="https://medium.com/u/ddf7ed97f132">林倢愷</a>, his article inspire me to write this article. All credict belongs to him. Please check out his article for more AI information!! I’ll list some of his article about this content below.</p><ul><li><a href="https://axk51013.medium.com/%E4%B8%8D%E8%A6%81%E5%86%8D%E5%81%9Aone-hot-encoding-b5126d3f8a63">不要再做One Hot Encoding！！</a></li><li><a href="https://axk51013.medium.com/kaggle-categorical-encoding-3%E5%A4%A7%E7%B5%95%E6%8B%9B-589780119470">Kaggle Categorical Encoding 3大絕招</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25b5882e533f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python: Send Line Message with Sticker and Image (Python串接Line)]]></title>
            <link>https://medium.com/@kaoningyu/python-send-line-message-with-sticker-and-image-a656e3b02300?source=rss-1da685d0ee0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a656e3b02300</guid>
            <category><![CDATA[line]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[chatbots]]></category>
            <dc:creator><![CDATA[Ning-Yu Kao]]></dc:creator>
            <pubDate>Thu, 10 Mar 2022 09:02:32 GMT</pubDate>
            <atom:updated>2022-03-10T09:17:00.245Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*FESZE2-wwqDLjwbHoYSytQ.png" /></figure><p>To be honest, it’s actually really easy to set up all the stuff you need. Just follow the steps below until the end:</p><ol><li>Go to LINE Notity website and log in</li></ol><p><a href="https://notify-bot.line.me/">LINE Notify</a></p><p>2. Generate your line access token</p><ul><li>click your ID on top-right of the page, and select “My page”.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nbVpnq3C7gUQImSSjMk3wg.png" /></figure><ul><li>And then, click “Generate token” to access your token.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*BdJi0YjP2JzsqLA-bOT7WQ.png" /></figure><ul><li>Type the a name that you want to start with in your message, and select a group you want to notify. And then you can click “Generate token”.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*XjTTYZF3RVtt5e9DbSfoOQ.png" /></figure><ul><li>Copy your token and store whatever you like (just make sure you save it properly, you won’t see your token again).</li><li>After that, you’ll see your connected server on top of the page.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*0ZNfhLgLGMz_qbdazvCStw.png" /></figure><p>That’s all for the Line part, we can leave it along so far.</p><p>Let’s start with our coding part with Python!!!!</p><p>3. Package Installation</p><ul><li>Install python package “py-topping” using PIP:</li></ul><pre>pip install py-topping</pre><ul><li>We’re gonna use a class called “lazy_LINE” in the library (please check out the library’s Github if you want)</li></ul><p><a href="https://github.com/chanon-kr/Shared_Function">GitHub - chanon-kr/Shared_Function: Shared functions within my team</a></p><p>4. Jupyter notebook testing</p><ul><li>I want to sent a message with current time, a sticker, and a picture at the same time. Here’s the code:</li></ul><pre>from py_topping.general_use import lazy_LINE<br>import datetime</pre><pre>time = str(datetime.datetime.now())<br># Create Class<br>line = lazy_LINE(token = &#39;your token&#39;)<br># Send message<br>line.send(&#39;message: &#39;+time<br>          , notification = True<br>          , stickerPackageId = &#39;6359&#39;<br>          , stickerId = &#39;11069861&#39;<br>          , picture = &#39;test_line_img.jpg&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SUPBbfl9kMUycQ2xthyN3g.png" /></figure><p>5. Run code and see the result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*w6j8qBrQUSGWy-n1kBvU3w.png" /></figure><p>For stickers, you can go to the Official Line website to choose your favorite:</p><p><a href="https://developers.line.biz/en/docs/messaging-api/sticker-list/#sticker-definitions">List of available stickers</a></p><p>That’s all for “Python Line message”, hope this helps! See you guys later!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a656e3b02300" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>