<?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 JLorenc on Medium]]></title>
        <description><![CDATA[Stories by JLorenc on Medium]]></description>
        <link>https://medium.com/@joshua.lorenc?source=rss-f5dbf8250ded------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by JLorenc on Medium</title>
            <link>https://medium.com/@joshua.lorenc?source=rss-f5dbf8250ded------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:45:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joshua.lorenc/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[Hello World with TensorFlow and Keras]]></title>
            <link>https://medium.com/@joshua.lorenc/hello-world-with-tensorflow-and-keras-6f0e740f641e?source=rss-f5dbf8250ded------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f0e740f641e</guid>
            <dc:creator><![CDATA[JLorenc]]></dc:creator>
            <pubDate>Mon, 20 Jul 2020 23:52:12 GMT</pubDate>
            <atom:updated>2020-07-20T23:52:12.895Z</atom:updated>
            <content:encoded><![CDATA[<p>My last beginner machine project left me wanting just a little bit more. After a bit of searching I found myself on TensorFlow.org doing beginner projects!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*iKYlBnjhNY7MmllxlfEMeg.gif" /></figure><p>The guide I will be referencing today can be found <a href="https://www.tensorflow.org/tutorials/keras/classification">here</a>. It is TensorFlow.org’s Basic classification lab that uses the MNIST data set. <em>“This guide will give a fast-paced overview of a complete TensorFlow program…”</em></p><p>To get started lets import everything we need!</p><pre># TensorFlow and tf.keras</pre><pre>import tensorflow as tf # &quot;open source library to help you develop and train ML models&quot;</pre><pre>from tensorflow import keras # &quot;high-level API to build and train models in TensorFlow&quot;</pre><pre># Helper libraries</pre><pre>import numpy as np # &quot;package for scientific computing with Python&quot;</pre><pre>import matplotlib.pyplot as plt # &quot;library for creating visualizations in Python&quot;</pre><pre>print(tf.__version__)# check that version yo (2.2.0)</pre><p><strong><em>Import the </em></strong><a href="http://yann.lecun.com/exdb/mnist/"><strong><em>Fashion MNIST Dataset</em></strong></a><strong><em> (</em></strong><em>Modified National Institute of Standards and Technology)</em></p><pre>fashion_mnist = keras.datasets.fashion_mnist</pre><pre>(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*GNLB2jtcfb_xTqgQd9ntJA.png" /></figure><p><em>“The images are 28x28 NumPy arrays, with pixel values ranging from 0 to 255. The labels are an array of integers, ranging from 0 to 9. These correspond to the class of clothing the image represents…”</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/1*gUnD3AbNmFwCa2J8o09PGw.png" /></figure><p><em>“Each image is mapped to a single label. Since the class names are not included with the dataset, store them here to use later when plotting the images: ”</em></p><pre>class_names = [&#39;T-shirt/top&#39;, &#39;Trouser&#39;, &#39;Pullover&#39;, &#39;Dress&#39;, &#39;Coat&#39;, &#39;Sandal&#39;, &#39;Shirt&#39;, &#39;Sneaker&#39;, &#39;Bag&#39;, &#39;Ankle boot&#39;]</pre><p><strong><em>Explore the Data</em></strong></p><pre>print(&quot;\ntrain_images.shape&quot;) <br>print(train_images.shape) # 60,000 training images that are 28x28</pre><pre>print(&quot;\nlen(train_labels)&quot;)<br>print(len(train_labels)) # 60,000 labels in the training set</pre><pre>print(&quot;\ntrain_labels&quot;)<br>print(train_labels) # each label is a int 0-9</pre><pre>print(&quot;\ntest_images.shape&quot;)<br>print(test_images.shape) # 10,000 test images that are 28x28 </pre><pre>print(&quot;\nlen(test_labels)&quot;)<br>print(len(test_labels)) # 10,000 labels in the test set</pre><p><strong><em>Preprocess the Data</em></strong></p><pre>plt.figure()<br>plt.imshow(train_images[0])<br>plt.colorbar()<br>plt.grid(False)<br>plt.show()</pre><pre>train_images = train_images / 255.0 # Pixel values for this set are 0-255</pre><pre>test_images = test_images / 255.0  # Divide the value by 255 to get a set of numbers 0-1 that we can actually feed to the neural network model</pre><pre>plt.figure(figsize=(10,10))<br>for i in range(25):<br>plt.subplot(5,5,i+1)<br>plt.xticks([])<br>plt.yticks([])<br>plt.grid(False)<br>plt.imshow(train_images[i], cmap=plt.cm.binary)<br>plt.xlabel(class_names[train_labels[i]])<br>plt.show()</pre><p><strong><em>Build the Model</em></strong></p><pre># Layers</pre><pre>model = keras.Sequential([</pre><pre>keras.layers.Flatten(input_shape=(28, 28)), # transforms 2d array to 1d</pre><pre>keras.layers.Dense(128, activation=&#39;relu&#39;), # &quot;dense&quot; layer is fully connected. 128 neurons</pre><pre>keras.layers.Dense(10) # Logits array, length 10. &quot;Each node contains a score that indicates the current image belongs to one of the 10 classes.&quot;<br>])</pre><pre># Compile the model</pre><pre>model.compile(optimizer=&#39;adam&#39;,</pre><pre>loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=[&#39;accuracy&#39;])</pre><ul><li><em>Loss function — This measures how accurate the model is during training. You want to minimize this function to “steer” the model in the right direction.</em></li><li><em>Optimizer — This is how the model is updated based on the data it sees and its loss function.</em></li><li><em>Metrics — Used to monitor the training and testing steps. The following example uses accuracy, the fraction of the images that are correctly classified.</em></li></ul><p><strong><em>Train the Model</em></strong></p><p><em>Training the neural network model requires the following steps:</em></p><ol><li><em>Feed the training data to the model. In this example, the training data is in the train_images and train_labels arrays.</em></li><li><em>The model learns to associate images and labels.</em></li><li><em>You ask the model to make predictions about a test set — in this example, the test_images array.-</em></li><li><em>Verify that the predictions match the labels from the test_labels array.</em></li></ol><pre>model.fit(train_images, train_labels, epochs=10)</pre><p><strong><em>Evaluate Accuracy</em></strong></p><p>We can see that our training dataset model performs at about 91% accuracy. Let’s see how that compares to the test dataset.</p><pre>test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)</pre><pre>print(&#39;\nTest accuracy:&#39;, test_acc)</pre><p>We can see that our test dataset performs just slightly worse than our training dataset. This is caused by “overfitting” which you can read about <a href="https://www.tensorflow.org/tutorials/keras/overfit_and_underfit#demonstrate_overfitting">here</a> and <a href="https://www.tensorflow.org/tutorials/keras/overfit_and_underfit#strategies_to_prevent_overfitting">here</a>.</p><p><strong><em>Predictions</em></strong></p><p>Now we can make predictions about some images. We <em>attach a softmax layer to convert the logits to probabilities, which are easier to interpret.</em></p><pre>probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])</pre><pre>predictions = probability_model.predict(test_images)</pre><pre>print(&quot;\npredictions[0]&quot;)<br>print(predictions[0])</pre><pre>print(&quot;\nnp.argmax(predictions[0])&quot;)<br>print(np.argmax(predictions[0]))</pre><pre>print(&quot;\ntest_labels[0]&quot;)<br>print(test_labels[0])</pre><p><strong><em>Set of Ten Class Predictions</em></strong></p><pre>def plot_image(i, predictions_array, true_label, img):<br>  predictions_array, true_label, img = predictions_array, true_label[i], img[i]<br>  plt.grid(False)<br>  plt.xticks([])<br>  plt.yticks([])</pre><pre>plt.imshow(img, cmap=plt.cm.binary)</pre><pre>predicted_label = np.argmax(predictions_array)<br>  if predicted_label == true_label:<br>    color = &#39;blue&#39;<br>  else:<br>    color = &#39;red&#39;</pre><pre>plt.xlabel(&quot;{} {:2.0f}% ({})&quot;.format(class_names[predicted_label],<br>                                100*np.max(predictions_array),<br>                                class_names[true_label]),<br>                                color=color)</pre><pre>def plot_value_array(i, predictions_array, true_label):<br>  predictions_array, true_label = predictions_array, true_label[i]<br>  plt.grid(False)<br>  plt.xticks(range(10))<br>  plt.yticks([])<br>  thisplot = plt.bar(range(10), predictions_array, color=&quot;#777777&quot;)<br>  plt.ylim([0, 1])<br>  predicted_label = np.argmax(predictions_array)</pre><pre>thisplot[predicted_label].set_color(&#39;red&#39;)<br>  thisplot[true_label].set_color(&#39;blue&#39;)</pre><p><strong><em>Verify Predictions</em></strong></p><p>Now we can visualize the predictions. In our case correct labels are blue and incorrect labels are red.</p><pre>i = 0<br>plt.figure(figsize=(6,3))<br>plt.subplot(1,2,1)<br>plot_image(i, predictions[i], test_labels, test_images)<br>plt.subplot(1,2,2)<br>plot_value_array(i, predictions[i],  test_labels)<br>plt.show()</pre><pre>i = 12<br>plt.figure(figsize=(6,3))<br>plt.subplot(1,2,1)<br>plot_image(i, predictions[i], test_labels, test_images)<br>plt.subplot(1,2,2)<br>plot_value_array(i, predictions[i],  test_labels)<br>plt.show()</pre><p><strong><em>Plot the first X test images, their predicted labels, and the true labels.</em></strong></p><pre>num_rows = 5<br>num_cols = 3<br>num_images = num_rows*num_cols<br>plt.figure(figsize=(2*2*num_cols, 2*num_rows))<br>for i in range(num_images):<br>  plt.subplot(num_rows, 2*num_cols, 2*i+1)<br>  plot_image(i, predictions[i], test_labels, test_images)<br>  plt.subplot(num_rows, 2*num_cols, 2*i+2)<br>  plot_value_array(i, predictions[i], test_labels)<br>plt.tight_layout()<br>plt.show()</pre><p><strong><em>Use the trained model</em></strong></p><pre># Grab an image from the test dataset.<br>img = test_images[1]<br>print(img.shape)</pre><pre># Add the image to a batch where it&#39;s the only member.<br>img = (np.expand_dims(img,0))<br>print(img.shape)</pre><pre>predictions_single = probability_model.predict(img)<br>print(predictions_single)</pre><pre>plot_value_array(1, predictions_single[0], test_labels)<br>_ = plt.xticks(range(10), class_names, rotation=45)</pre><pre>np.argmax(predictions_single[0])</pre><p>If you followed along this far, congratulations! You have completed a basic TensorFlow project using Keras! Thanks for reading!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*d7tP7kcM_J0c1C8Ek-Itlg.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f0e740f641e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dip your toes into machine learning with this beginner project!]]></title>
            <link>https://medium.com/@joshua.lorenc/dip-your-toes-into-machine-learning-with-this-beginner-project-b3cae8428a33?source=rss-f5dbf8250ded------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3cae8428a33</guid>
            <dc:creator><![CDATA[JLorenc]]></dc:creator>
            <pubDate>Wed, 08 Jul 2020 18:59:12 GMT</pubDate>
            <atom:updated>2020-07-08T18:59:12.192Z</atom:updated>
            <content:encoded><![CDATA[<p>Everyone keeps talking about machine learning, so I had to try it out. I figured I would just be creating the next Skynet or HAL 9000. Unfortunately that was not the case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*FZaQxrcBVenVM8JS0UviOg.gif" /></figure><p>Using Jason Brownlee’s <a href="https://machinelearningmastery.com/machine-learning-in-python-step-by-step/">guide</a> on machinelearningmastery.com anyone can create a simple, end to end project that will introduce the basics of machine learning.</p><p>Lets start off by installing <a href="https://www.python.org/downloads/">Python</a> and <a href="https://www.scipy.org/install.html">SciPy</a>. You can also use <a href="https://www.anaconda.com/products/individual">Anaconda</a>, which includes everything you need. While in the python interpreter, you can use this script to check if your environment was successfully installed. In the command line simply type “python” then:</p><pre># Check the versions of libraries<br># Python version<br>import sys<br>print(‘Python: {}’.format(sys.version))<br># scipy<br>import scipy<br>print(‘scipy: {}’.format(scipy.__version__))<br># numpy<br>import numpy<br>print(‘numpy: {}’.format(numpy.__version__))<br># matplotlib<br>import matplotlib<br>print(‘matplotlib: {}’.format(matplotlib.__version__))<br># pandas<br>import pandas<br>print(‘pandas: {}’.format(pandas.__version__))<br># scikit-learn<br>import sklearn<br>print(‘sklearn: {}’.format(sklearn.__version__))</pre><p>This script imports each library that will be required. You should get outputs similar to this:</p><pre>Python: 3.8.2 (default, Apr 27 2020, 15:53:34)<br>[GCC 9.3.0]<br>scipy: 1.5.1<br>numpy: 1.19.0<br>matplotlib: 3.1.2<br>pandas: 1.0.5<br>sklearn: 0.23.1</pre><p>Next we need to import the modules, functions and objects that will be used in the tutorial.</p><pre># Load libraries<br>from pandas import read_csv<br>from pandas.plotting import scatter_matrix<br>from matplotlib import pyplot<br>from sklearn.model_selection import train_test_split<br>from sklearn.model_selection import cross_val_score<br>from sklearn.model_selection import StratifiedKFold<br>from sklearn.metrics import classification_report<br>from sklearn.metrics import confusion_matrix<br>from sklearn.metrics import accuracy_score<br>from sklearn.linear_model import LogisticRegression<br>from sklearn.tree import DecisionTreeClassifier<br>from sklearn.neighbors import KNeighborsClassifier<br>from sklearn.discriminant_analysis import LinearDiscriminantAnalysis<br>from sklearn.naive_bayes import GaussianNB<br>from sklearn.svm import SVC</pre><p>If these load without error, we can import the <a href="https://archive.ics.uci.edu/ml/datasets/Iris">iris data set</a> which is often referred to as the “hello world” data set in machine learning and statistics. The data set represents 150 instances of iris flower measurements. Each instance has four attributes and there are three classifications of iris flower.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*qx9PMo3J-qHKz8BA25ioyA.jpeg" /></figure><p>We use pandas to load this data and specify the names of each column to help organize the data.</p><pre># Load dataset<br>url = “<a href="https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv">https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv</a>&quot;<br>names = [‘sepal-length’, ‘sepal-width’, ‘petal-length’, ‘petal-width’, ‘class’]<br>dataset = read_csv(url, names=names)</pre><p>Note: If you have issues loading the dataset, you can download the iris.crv file into your working directory and load it using it’s local file name instead of URL.</p><p>We can see the “shape” of the data using the following code. This simply represents the instances (150) and attributes (5) of the dataset.</p><pre># shape<br>print(dataset.shape)</pre><pre># This should return (150,5)</pre><p>We can see more clearly what the data looks like by printing a part of it.</p><pre># head<br>print(dataset.head(20))</pre><pre>#This will show you the first 20 rows of the data set.</pre><pre>sepal-length  sepal-width  petal-length  petal-width        class<br>0            5.1          3.5           1.4          0.2  Iris-setosa<br>1            4.9          3.0           1.4          0.2  Iris-setosa<br>2            4.7          3.2           1.3          0.2  Iris-setosa<br>3            4.6          3.1           1.5          0.2  Iris-setosa<br>4            5.0          3.6           1.4          0.2  Iris-setosa<br>5            5.4          3.9           1.7          0.4  Iris-setosa<br>6            4.6          3.4           1.4          0.3  Iris-setosa<br>7            5.0          3.4           1.5          0.2  Iris-setosa<br>8            4.4          2.9           1.4          0.2  Iris-setosa<br>9            4.9          3.1           1.5          0.1  Iris-setosa<br>10           5.4          3.7           1.5          0.2  Iris-setosa<br>11           4.8          3.4           1.6          0.2  Iris-setosa<br>12           4.8          3.0           1.4          0.1  Iris-setosa<br>13           4.3          3.0           1.1          0.1  Iris-setosa<br>14           5.8          4.0           1.2          0.2  Iris-setosa<br>15           5.7          4.4           1.5          0.4  Iris-setosa<br>16           5.4          3.9           1.3          0.4  Iris-setosa<br>17           5.1          3.5           1.4          0.3  Iris-setosa<br>18           5.7          3.8           1.7          0.3  Iris-setosa<br>19           5.1          3.8           1.5          0.3  Iris-setosa</pre><p>We can also print a summary of the data.</p><pre># descriptions<br>print(dataset.describe())</pre><pre>sepal-length  sepal-width  petal-length  petal-width<br>count    150.000000   150.000000    150.000000   150.000000<br>mean       5.843333     3.054000      3.758667     1.198667<br>std        0.828066     0.433594      1.764420     0.763161<br>min        4.300000     2.000000      1.000000     0.100000<br>25%        5.100000     2.800000      1.600000     0.300000<br>50%        5.800000     3.000000      4.350000     1.300000<br>75%        6.400000     3.300000      5.100000     1.800000<br>max        7.900000     4.400000      6.900000     2.500000</pre><p>Lastly we can see classes of iris.</p><pre># class distribution<br>print(dataset.groupby(&#39;class&#39;).size())</pre><pre>class<br>Iris-setosa 50<br>Iris-versicolor 50<br>Iris-virginica 50</pre><p>We can use all of this code together to get a good idea of how the dataset looks overall.</p><pre># summarize the data<br>from pandas import read_csv<br># Load dataset<br>url = &quot;<a href="https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv">https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv</a>&quot;<br>names = [&#39;sepal-length&#39;, &#39;sepal-width&#39;, &#39;petal-length&#39;, &#39;petal-width&#39;, &#39;class&#39;]<br>dataset = read_csv(url, names=names)<br># shape<br>print(dataset.shape)<br># head<br>print(dataset.head(20))<br># descriptions<br>print(dataset.describe())<br># class distribution<br>print(dataset.groupby(&#39;class&#39;).size())</pre><p>Now that we have a better understanding of our data, we can show it another way. We can visualize our data using pyplot graphs. First off are the univariate plots. Box and whis</p><pre># box and whisker plots<br>dataset.plot(kind=’box’, subplots=True, layout=(2,2), sharex=False, sharey=False)<br>pyplot.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vFVBSF0-2WJ2nSlUMrDM1w.png" /></figure><pre># histograms<br>dataset.hist()<br>pyplot.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GPX2GLG2RRxmTUtqxgvvTw.png" /></figure><p>Next up, we can see more data by using a multivariate plot. Note the grouping on the attributes. This suggests high correlation and a predictable relationship in the dataset.</p><pre># scatter plot matrix<br>scatter_matrix(dataset)<br>pyplot.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vBFvkkxKgjaABRX1jcKx6Q.png" /></figure><p>The visualization code all put together:</p><pre># visualize the data<br>from pandas import read_csv<br>from pandas.plotting import scatter_matrix<br>from matplotlib import pyplot<br># Load dataset<br>url = &quot;<a href="https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv">https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv</a>&quot;<br>names = [&#39;sepal-length&#39;, &#39;sepal-width&#39;, &#39;petal-length&#39;, &#39;petal-width&#39;, &#39;class&#39;]<br>dataset = read_csv(url, names=names)<br># box and whisker plots<br>dataset.plot(kind=&#39;box&#39;, subplots=True, layout=(2,2), sharex=False, sharey=False)<br>pyplot.show()<br># histograms<br>dataset.hist()<br>pyplot.show()<br># scatter plot matrix<br>scatter_matrix(dataset)<br>pyplot.show()</pre><p>Up to this point we have just modified the data in a way that makes it easier for us to read. Now we will actually begin evaluating the data using a group of algorithms. First we will create a validation set.</p><pre># Split-out validation dataset<br>array = dataset.values<br>X = array[:,0:4]<br>y = array[:,4]<br>X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.20, random_state=1)</pre><p>We will use this data to test on later. Basically, we are splitting the data 80/20. The 80% will be used to train and the other 20% will be used to test. Note if you need a review on slice in python you can check out <a href="https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/">this</a>.</p><p>Now we need to create a test harness to estimate model accuracy. Using a stratified 10-fold cross validation will split our dataset into 10 parts. It will train on nine and test on one. Also it will repeat this for all of the combinations of train-test splits. <em>Stratified means that each fold or split of the dataset will aim to have the same distribution of example by class as exist in the whole training dataset. </em>For more info on k-fold cross-validation check out <a href="https://machinelearningmastery.com/k-fold-cross-validation/">this</a>.</p><p>Set the random seed (random_state) argument to a fixed number to make sure each algorithm is evaluated on the same splits of the dataset. Check out <a href="https://machinelearningmastery.com/introduction-to-random-number-generators-for-machine-learning/">this</a> to learn more about pseudorandom number generators.</p><p>Use the “accuracy” metric to evaluate the models. <em>This is a ratio of the number of correctly predicted instances divided by the total number of instances in the dataset multiplied by 100 to give a percentage. We will be using the scoring variable when we run build and evaluate each model next.</em></p><p>Next it is finally time to build models. We do not know which algorithms or configurations will work on this dataset. We will test on six algorithms.</p><ul><li>Logistic Regression (LR)</li><li>Linear Discriminant Analysis (LDA)</li><li>K-Nearest Neighbors (KNN).</li><li>Classification and Regression Trees (CART).</li><li>Gaussian Naive Bayes (NB).</li><li>Support Vector Machines (SVM).</li></ul><p><em>This is a good mixture of simple linear (LR and LDA), nonlinear (KNN, CART, NB and SVM) algorithms.</em></p><pre># Spot Check Algorithms<br>models = []<br>models.append((&#39;LR&#39;, LogisticRegression(solver=&#39;liblinear&#39;, multi_class=&#39;ovr&#39;)))<br>models.append((&#39;LDA&#39;, LinearDiscriminantAnalysis()))<br>models.append((&#39;KNN&#39;, KNeighborsClassifier()))<br>models.append((&#39;CART&#39;, DecisionTreeClassifier()))<br>models.append((&#39;NB&#39;, GaussianNB()))<br>models.append((&#39;SVM&#39;, SVC(gamma=&#39;auto&#39;)))<br># evaluate each model in turn<br>results = []<br>names = []<br>for name, model in models:<br> kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)<br> cv_results = cross_val_score(model, X_train, Y_train, cv=kfold, scoring=&#39;accuracy&#39;)<br> results.append(cv_results)<br> names.append(name)<br> print(&#39;%s: %f (%f)&#39; % (name, cv_results.mean(), cv_results.std()))</pre><p>Running this will return the following, although your results may vary due to randomness. Check <a href="https://machinelearningmastery.com/randomness-in-machine-learning/">this </a>out for more on randomness in machine learning.</p><pre>LR: 0.960897 (0.052113)<br>LDA: 0.973974 (0.040110)<br>KNN: 0.957191 (0.043263)<br>CART: 0.957191 (0.043263)<br>NB: 0.948858 (0.056322)<br>SVM: 0.983974 (0.032083)</pre><p>We can now visualize this data like before to make them easier to compare.</p><pre># Compare Algorithms<br>pyplot.boxplot(results, labels=names)<br>pyplot.title(&#39;Algorithm Comparison&#39;)<br>pyplot.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Xz2mDq4bPT4GXnoZF7SoQ.png" /></figure><p>Using this information, we can see that the SVM was the most accurate model. We will use it to make predictions.</p><pre># Make predictions on validation dataset<br>model = SVC(gamma=&#39;auto&#39;)<br>model.fit(X_train, Y_train)<br>predictions = model.predict(X_validation)</pre><p>We can then evaluate those predictions by comparing them to the expected results in our validation set. We will also create a confusion matrix and a classification report. <em>The confusion matrix provides an indication of the errors made. The classification report provides a breakdown of each class by precision.</em></p><pre>0.9666666666666667<br>[[11  0  0]<br> [ 0 12  1]<br> [ 0  0  6]]<br>                 precision    recall  f1-score   support</pre><pre>Iris-setosa       1.00      1.00      1.00        11<br>Iris-versicolor       1.00      0.92      0.96        13<br> Iris-virginica       0.86      1.00      0.92         6</pre><pre>accuracy                           0.97        30<br>      macro avg       0.95      0.97      0.96        30<br>   weighted avg       0.97      0.97      0.97        30</pre><p>Putting this together, we have the following script:</p><pre># make predictions<br>from pandas import read_csv<br>from sklearn.model_selection import train_test_split<br>from sklearn.metrics import classification_report<br>from sklearn.metrics import confusion_matrix<br>from sklearn.metrics import accuracy_score<br>from sklearn.svm import SVC<br># Load dataset<br>url = &quot;<a href="https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv">https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv</a>&quot;<br>names = [&#39;sepal-length&#39;, &#39;sepal-width&#39;, &#39;petal-length&#39;, &#39;petal-width&#39;, &#39;class&#39;]<br>dataset = read_csv(url, names=names)<br># Split-out validation dataset<br>array = dataset.values<br>X = array[:,0:4]<br>y = array[:,4]<br>X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.20, random_state=1)<br># Make predictions on validation dataset<br>model = SVC(gamma=&#39;auto&#39;)<br>model.fit(X_train, Y_train)<br>predictions = model.predict(X_validation)<br># Evaluate predictions<br>print(accuracy_score(Y_validation, predictions))<br>print(confusion_matrix(Y_validation, predictions))<br>print(classification_report(Y_validation, predictions))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*nypimu_Lp0waAUwFYfZi3w.gif" /></figure><p>Congratulations! If you have made it this far you have completed a step-by-step guide to complete your first machine learning project in python. Hopefully you can use this as a springboard into the complicated world of machine learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*wN364qEg38TwxB5C0rS4Zw.gif" /><figcaption>Hopefully with more machine learning!</figcaption></figure><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3cae8428a33" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple News Aggregator]]></title>
            <link>https://medium.com/@joshua.lorenc/simple-news-aggregator-3c95f661d81e?source=rss-f5dbf8250ded------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c95f661d81e</guid>
            <dc:creator><![CDATA[JLorenc]]></dc:creator>
            <pubDate>Mon, 22 Jun 2020 23:50:54 GMT</pubDate>
            <atom:updated>2020-06-22T23:50:54.932Z</atom:updated>
            <content:encoded><![CDATA[<p>For my first blog post I wanted to create something that used a language other than ruby. After a bit of searching and experimenting I have decided to create a simple content aggregator using this <a href="https://www.hackersfriend.com/articles/building-news-aggregator-web-app-with-django-using-python-web-scraping">guide</a> by Rajan at HackersFriend.com. In this blog we will cover the basic steps and code used to make this web app.</p><p>Before we start it is good to have basic knowledge of:</p><ol><li>Django Framework</li><li>BeatifulSoup</li><li>Requests Module</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ECHPuPjvSxQr4nmMy6eTdA.png" /><figcaption>What the site will look like when “finished”</figcaption></figure><p>Lets start by installing the required packages. Using the terminal input the following commands.</p><pre>pip install bs4</pre><pre>pip install requests</pre><p>Now lets start creating a scraper to find our headlines on each site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1003/1*dLupWKfY69HDBkY_9hkjZw.png" /></figure><p>On the Onion website we can see that the headlines are h4. With this information we can create this simple scaper to grab them.</p><pre>import requests<br>from bs4 import BeautifulSoup<br><br>onion_r = requests.get(&quot;https://www.theonion.com/&quot;)<br>onion_soup = BeautifulSoup(r.content, &#39;html5lib&#39;)<br><br>onion_headings = soup.find_all(&#39;h4&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vBDgtOcqE_bNBPfi-Gyr6g.png" /></figure><p>Next we should grab from headlines from Car and Driver. We use the /news version of the website to make scraping a bit easier. These headlines are in the “full-item-title item-title” class.</p><pre>cd_r = requests.get(&quot;https://www.caranddriver.com/news&quot;)</pre><pre>cd_soup = BeautifulSoup(cd_r.content, &#39;html5lib&#39;)</pre><pre>cd_headings = cd_soup.findAll(&quot;div&quot;, {&quot;class&quot;: &quot;full-item-title item-title&quot;})</pre><p>These bits of code will act as the base for our scraper.</p><p>Next we need to build the django web app. Run these commands.</p><pre>pip install django</pre><pre>django-admin startproject news_aggregator</pre><p>Once the project is created move into its directory and run the following.</p><pre>python manage.py startapp news</pre><p>Make sure to add ‘news’ to the INSTALLED_APPS in settings.py</p><pre>INSTALLED_APPS = [<br>    &#39;django.contrib.admin&#39;,<br>    &#39;django.contrib.auth&#39;,<br>    &#39;django.contrib.contenttypes&#39;,<br>    &#39;django.contrib.sessions&#39;,<br>    &#39;django.contrib.messages&#39;,<br>    &#39;django.contrib.staticfiles&#39;,<br>    <strong>&#39;news&#39;</strong><br>]</pre><p>Next we need to create a specific directory for our index.html file.</p><ol><li>In the news directory create a directory with the name “templates”</li><li>In templates create another directory called “news”</li><li>In news/template/news create the index.html file</li><li>Copy the django template into index.html</li></ol><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;title&gt;&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css&quot; integrity=&quot;sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm&quot; crossorigin=&quot;anonymous&quot;&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;div class=&quot;jumbotron&quot;&gt;<br>        &lt;center&gt;&lt;h1&gt;Simple News Aggregator&lt;/h1&gt;<br>           &lt;a href=&quot;/&quot; class=&quot;btn btn-danger&quot;&gt;Refresh News&lt;/a&gt;<br>        &lt;/form&gt;<br>    &lt;/center&gt;<br>    &lt;/div&gt;<br>    &lt;div class=&quot;container&quot;&gt;<br>        &lt;div class=&quot;row&quot;&gt;<br>            &lt;div class=&quot;col-6&quot;&gt;<br>                    &lt;h3 class=&quot;text-centre&quot;&gt; News from the Onion&lt;/h3&gt;<br>                    {% for n in onion_news %}<br>                    &lt;h5&gt; -  {{n}} &lt;/h5&gt;<br>                    &lt;hr&gt;<br>                    {% endfor %}<br>                    &lt;br&gt;<br>            &lt;/div&gt;<br>            &lt;div class=&quot;col-6&quot;&gt;<br>                    &lt;h3 class=&quot;text-centre&quot;&gt;News from Car and Driver&lt;/h3&gt;<br>                    {% for htn in cd_news %}<br>                    &lt;h5&gt; - {{htn}} &lt;/h5&gt;<br>                    &lt;hr&gt;<br>                    {% endfor %}<br>                    &lt;br&gt;<br>            &lt;/div&gt;<br>        &lt;/div&gt;<br>&lt;/div&gt;<br>    &lt;script<br>src=&quot;http://code.jquery.com/jquery-3.3.1.min.js&quot;<br>integrity=&quot;sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=&quot;<br>    crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;<br>    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;<br>    &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Next lets work on the views.py file. It should look like this.</p><pre>from django.shortcuts import render<br>import requests<br>from bs4 import BeautifulSoup</pre><pre>onion_r = requests.get(&quot;https://www.theonion.com/&quot;)</pre><pre>onion_soup = BeautifulSoup(onion_r.content, &#39;html5lib&#39;)</pre><pre>onion_headings = onion_soup.find_all(&#39;h4&#39;)</pre><pre>onion_news = []</pre><pre>for th in onion_headings:<br>     onion_news.append(th.text)</pre><pre><br>cd_r = requests.get(&quot;https://www.caranddriver.com/news&quot;)</pre><pre>cd_soup = BeautifulSoup(cd_r.content, &#39;html5lib&#39;)</pre><pre>cd_headings = cd_soup.findAll(&quot;div&quot;, {&quot;class&quot;: &quot;full-item-title item-title&quot;})</pre><pre>cd_headings = cd_headings[2:]</pre><pre>cd_news = []</pre><pre>for hth in cd_headings:<br>     cd_news.append(hth.text)<br></pre><pre>def index(req):</pre><pre>return render(req, &#39;news/index.html&#39;, {&#39;onion_news&#39;:onion_news, &#39;cd_news&#39;: cd_news})</pre><p>Next move up to our main directory “news_aggregator” so we can modify our urls.py file. It should look like this.</p><pre>&quot;&quot;&quot;news_aggregator URL Configuration<br><br>The `urlpatterns` list routes URLs to views. For more information please see:<br>    https://docs.djangoproject.com/en/2.0/topics/http/urls/<br>Examples:<br>Function views<br>    1. Add an import:  from my_app import views<br>    2. Add a URL to urlpatterns:  path(&#39;&#39;, views.home, name=&#39;home&#39;)<br>Class-based views<br>    1. Add an import:  from other_app.views import Home<br>    2. Add a URL to urlpatterns:  path(&#39;&#39;, Home.as_view(), name=&#39;home&#39;)<br>Including another URLconf<br>    1. Import the include() function: from django.urls import include, path<br>    2. Add a URL to urlpatterns:  path(&#39;blog/&#39;, include(&#39;blog.urls&#39;))<br>&quot;&quot;&quot;<br><strong>from django.contrib import admin<br>from django.urls import path<br>from news import views<br><br>urlpatterns = [<br>    path(&#39;admin/&#39;, admin.site.urls),<br>    path(&#39;&#39;, views.index, name = &quot;home&quot;),<br>]</strong></pre><p>With that we should be able to run the file!</p><pre>python manage.py runserver</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*oYXB6aEXytPg-bK5yOn8vg.png" /></figure><p>Now you can use control + click to view the link</p><p>And if everything is working correctly you will get a list of the headlines of each website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*ECHPuPjvSxQr4nmMy6eTdA.png" /></figure><p>With this you have a simple two site news aggregator. It can be simply modified to take any two sites and put their headlines on one page. You could add your own sites to the list by modifying the code. You can also look to add functionality by scraping more data such as URLs and images. This will allow you to actually click the headlines to go to the news article as well as make the site look better.</p><p>Another guide worth looking at <a href="https://data-flair.training/blogs/django-project-news-aggregator-app/">https://data-flair.training/blogs/django-project-news-aggregator-app/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c95f661d81e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>