<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amdirpherian</title>
    <description>The latest articles on DEV Community by Amdirpherian (@omhaisoj).</description>
    <link>https://dev.to/omhaisoj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F646889%2F96b2b644-1006-4439-bc4d-6e42ad11e39b.jpeg</url>
      <title>DEV Community: Amdirpherian</title>
      <link>https://dev.to/omhaisoj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omhaisoj"/>
    <language>en</language>
    <item>
      <title>A foray into SVM w/ Scikit-Learn</title>
      <dc:creator>Amdirpherian</dc:creator>
      <pubDate>Sat, 16 Jul 2022 05:40:14 +0000</pubDate>
      <link>https://dev.to/omhaisoj/a-foray-into-svm-w-scikit-learn-32oh</link>
      <guid>https://dev.to/omhaisoj/a-foray-into-svm-w-scikit-learn-32oh</guid>
      <description>&lt;p&gt;Hey, Josiah here. Today I’ll be discussing the concept of SVMs (Support Vector Machine), a subset of Machine Learning used commonly for classification and regression analysis. First off, we must discuss how SVM works from a theoretical standpoint.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o1Mv_Vd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fgifo7ynr5h07afyiuw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o1Mv_Vd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fgifo7ynr5h07afyiuw.png" alt="Image description" width="330" height="286"&gt;&lt;/a&gt;&lt;br&gt;
A simple example of an SVM classifying data&lt;br&gt;
Suppose some given data points each belong to one of two classes (”Black” &amp;amp; “White”), and the goal is to decide which class a new data point will be in.&lt;/p&gt;

&lt;p&gt;The three lines seen (H1, H2, and H3) are known as “Hyperplanes”, which divide our data into classes. To define a hyperplane, one must follow a simple procedure:&lt;/p&gt;

&lt;p&gt;Draw a line between the two classes such that the closest data point from each class (known as the support vector) are equidistant from the line. Thus, we have defined a Hyperplane.&lt;/p&gt;

&lt;p&gt;However, it soon becomes apparent that an infinite number of hyperplanes can be derived. The obvious question arises: “How do we determine which hyperplane to use?” The answer is, we use the hyperplane which maximizes the distance to the support vectors. This maximized gap between the two classes is known as the maximum-margin hyperplane, and is the ideal hyperplane to predict the class of future data points.&lt;/p&gt;

&lt;p&gt;Now that we have discussed the basic theoretical concepts of SVM classification, let us get into a simple example involving the breast-cancer dataset from Scikit-learn. (Relatively trivial code snippets will be captioned)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import sklearn&lt;br&gt;
from sklearn import metrics&lt;br&gt;
from sklearn import svm&lt;br&gt;
cn = sklearn.datasets.load_breast_cancer() # load data set&lt;br&gt;
x = cn.data # set x to the entries&lt;br&gt;
y = cn.target # set y to the labels&lt;/code&gt;&lt;br&gt;
Here, we set two variables x and y equal to our data’s entries and labels, respectively. We will need this in the next snippet to separate the data further.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.2)&lt;/code&gt;&lt;br&gt;
We use Scikit-learn’s model_selection.train_test_split method to take our entries and labels and split them into training and testing subcomponents. By setting test_size to 0.2, we allocate 20% of our data for testing purposes, and the other 80% for training purposes. Note that it’s not recommended to set test_size to be greater than 0.3.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Create an SVC&lt;br&gt;
clf = svm.SVC()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;# train the training set&lt;br&gt;
clf.fit(x_train, y_train)&lt;/code&gt;&lt;br&gt;
Here, we create the Support Vector Machine along with the SVC (Support Vector Classifier) and assign it to the variable clf. Next, we train the model on the training entries and labels (20% of the dataset as defined by test_size previously).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Predict the labels for the training data&lt;br&gt;
y_predict = clf.predict(x_test)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;# compare the predicted labels to the actual labels and conver to percentage to output&lt;br&gt;
acc = metrics.accuracy_score(y_test, y_predict)*100&lt;br&gt;
print(f"{acc}% accuracy")&lt;/code&gt;&lt;br&gt;
We set a variable y_predict equal to the results of the classifier predicting the labels of the testing entries. We then use the sklearn.metrics.accuracy_score() method to compare the actual testing labels to that of y_predict, which was the machine’s attempt at classifying them. We then convert this to a percentage by multiplying by a factor of 100, and printing this result. The accuracy you should get is ~96%, which is a very good accuracy!&lt;/p&gt;

&lt;p&gt;If you enjoy articles like this and find it useful or have any feedback, message me on Discord @Necloremius#6412&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>machinelearning</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>An Introduction to Asynchronous Programming in Python and the Asyncio module (Part 1)</title>
      <dc:creator>Amdirpherian</dc:creator>
      <pubDate>Thu, 10 Jun 2021 01:57:00 +0000</pubDate>
      <link>https://dev.to/omhaisoj/an-introduction-to-asynchronous-programming-in-python-and-the-asyncio-module-part-1-2d2l</link>
      <guid>https://dev.to/omhaisoj/an-introduction-to-asynchronous-programming-in-python-and-the-asyncio-module-part-1-2d2l</guid>
      <description>&lt;p&gt;First, before we begin talking about Asyncio, we must differentiate between Asynchronous Programming and Synchronous Programming. Synchronous Programming is essentially “normal” programming, in which code is ran sequentially from the first line to last. Asynchronous Programming, on the other hand, is essentially code that can run different operations while waiting on a function to meet a certain necessary requirement. Now that we know the difference between Synchronous and Asynchronous programming, lets look at how to set up a basic asynchronous program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Coroutine: A function that is defined asynchronously (allowing it to run asynchronously)&lt;br&gt;
Without coroutine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;x&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello World'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With coroutine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;x&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello World'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you call a coroutine, it won’t execute the function as expected. Instead, it returns a warning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RuntimeWarning: coroutine 'main' was never awaited
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason this error happens is because in Asyncio, to run a program, one must first create an event loop. The event loop is essentially the behind-the-scenes loop that runs all the asynchronous tasks in the program. To add a function to the event loop, we must use a certain syntax. Let us take a look.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;x&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello World'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we see the standard definition of a coroutine. Then, we see “asyncio.run(a())”. This line of code adds the function a() to the event loop, which runs it. Then, ‘Hello World’ is printed through a().&lt;br&gt;
However, it is completely redundant to add each coroutine to the event loop individually, as this is very tedious and eliminates the whole point of using Asynchronous Programming. To unlock the full potential of Asynchronous Programming, one must use the ‘await’ keyword.&lt;br&gt;
Let me explain this mysterious keyword with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, lets break down what is happening here (in terms of run sequence, not line by line). on the first line, we simply import the Asyncio module. Next, we run the asynchronous program by adding the coroutine main() to the event loop and running it. Running a() prints ‘a’, and then we see the following line of code: ‘await b()’. What await does is it tells Python ‘Stop running this coroutine and run the specified coroutine instead, then after the specified coroutine is finished running, return to running this coroutine until completion.’ ‘await b()’ runs b(), which prints out ‘b’. Now, since b() has completed its execution, the program returns to running a(), which in turn prints out ‘c’ and completing the program. You see, this is what is so insanely powerful about Asynchronous Programming. The ability to run different functions at different times based on the state of other functions has an abundant amount of applications.&lt;br&gt;
This concludes the article. Some other basic aspects of Asyncio (such as tasks and futures, to list two examples) will be explained in Part Two. If you would like to contact me about anything, please email me &lt;a href="mailto:josmo314@gmai.com"&gt;josmo314@gmai.com&lt;/a&gt;. I hope you enjoyed!&lt;/p&gt;

&lt;p&gt;~Josiah Mo, with love&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
