avatarAayushi Johari

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

22033

Abstract

_in">np</span>.<span class="hljs-built_in">sin</span>(zline) yline = <span class="hljs-built_in">np</span>.<span class="hljs-built_in">cos</span>(zline) ax.plot3D(xline, yline, zline, ‘gray’)# Data <span class="hljs-keyword">for</span> three-dimensional scattered <span class="hljs-built_in">points</span> zdata = <span class="hljs-number">15</span> * <span class="hljs-built_in">np</span>.<span class="hljs-built_in">random</span>.<span class="hljs-built_in">random</span>(<span class="hljs-number">100</span>) xdata = <span class="hljs-built_in">np</span>.<span class="hljs-built_in">sin</span>(zdata) + <span class="hljs-number">0.1</span> * <span class="hljs-built_in">np</span>.<span class="hljs-built_in">random</span>.randn(<span class="hljs-number">100</span>) ydata = <span class="hljs-built_in">np</span>.<span class="hljs-built_in">cos</span>(zdata) + <span class="hljs-number">0.1</span> * <span class="hljs-built_in">np</span>.<span class="hljs-built_in">random</span>.randn(<span class="hljs-number">100</span>) ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap=’Greens’);</pre></div><figure id="890e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Qh3LgjGPuGsJVjlvCS8jMw.png"><figcaption></figcaption></figure><h2 id="a348">3D Contour Plots:</h2><p id="d9e4">The input for the contour plot is a bit different than for the previous plot, as it needs the data on a two-dimensional grid.</p><p id="a8d1">Note that on the following example that after assigning values for x and y, they are combined on a grid by executing <b>“np.meshgrid(x, y)”</b> and then the Z values are created from executing the function f(X,Y) with the values of the grid (Z=f(X,Y)).</p><p id="5516">Again, basic 3d plot simplified in the following code:</p><div id="7817"><pre>def <span class="hljs-built_in">f</span>(x, y): return np.<span class="hljs-built_in">sin</span>(np.<span class="hljs-built_in">sqrt</span>(x ** <span class="hljs-number">2</span> + y ** <span class="hljs-number">2</span>))

x = np.<span class="hljs-built_in">linspace</span>(-<span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">30</span>) y = np.<span class="hljs-built_in">linspace</span>(-<span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">30</span>)

X, Y = np.<span class="hljs-built_in">meshgrid</span>(x, y) Z = <span class="hljs-built_in">f</span>(X, Y)fig = plt.<span class="hljs-built_in">figure</span>() ax = plt.<span class="hljs-built_in">axes</span>(projection=<span class="hljs-string">'3d'</span>) ax.<span class="hljs-built_in">contour3D</span>(X, Y, Z, <span class="hljs-number">50</span>, cmap=<span class="hljs-string">'binary'</span>) ax.<span class="hljs-built_in">set_xlabel</span>(<span class="hljs-string">'x'</span>) ax.<span class="hljs-built_in">set_ylabel</span>(<span class="hljs-string">'y'</span>) ax.<span class="hljs-built_in">set_zlabel</span>(<span class="hljs-string">'z'</span>);</pre></div><figure id="4ef9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Nw7I-x8uM8ZfpzhYlcfjrg.png"><figcaption></figcaption></figure><p id="3bb6">On the previous graphs, the data was generated in order, but in real life sometimes the data is not ordered, for those cases, the surface triangulation is very useful as it creates surfaces by finding sets of triangles formed between adjacent points.</p><h2 id="a164">Surface Triangulation:</h2><div id="bd07"><pre>theta = <span class="hljs-number">2</span> * <span class="hljs-built_in">np</span>.pi * <span class="hljs-built_in">np</span>.<span class="hljs-built_in">random</span>.<span class="hljs-built_in">random</span>(<span class="hljs-number">1000</span>) r = <span class="hljs-number">6</span> * <span class="hljs-built_in">np</span>.<span class="hljs-built_in">random</span>.<span class="hljs-built_in">random</span>(<span class="hljs-number">1000</span>) x = <span class="hljs-built_in">np</span>.ravel(r * <span class="hljs-built_in">np</span>.<span class="hljs-built_in">sin</span>(theta)) y = <span class="hljs-built_in">np</span>.ravel(r * <span class="hljs-built_in">np</span>.<span class="hljs-built_in">cos</span>(theta)) z = f(x, y) ax = plt.<span class="hljs-built_in">axes</span>(projection=’3d’) ax.plot_trisurf(x, y, z,cmap=’viridis’, edgecolor=’none’);</pre></div><figure id="88c2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-TMly4RbjfgcAYJn8lNlUw.png"><figcaption></figcaption></figure><p id="f733">Now that we are familiar with how we can expand our reach on learning Python by looking at external libraries, we can go ahead and check out the next level of Python Projects which is the Advanced Level.</p><h1 id="4278">Advanced Projects With Python</h1><p id="1ee0">Python has vast applications — Everything from “Hello World” all the way to achieving Artificial Intelligence.</p><p id="5242">There are virtually unlimited projects you can work on using Python but here are the major ones that you can consider if you want to dive into the heart of Python.</p><ul><li>Machine Learning with PyTorch, TensorFlow, Keras and whatever Machine Learning libraries you like.</li><li>Computer vision using OpenCV and PIL.</li><li>Creating and publishing your own pip module with tests and documentation.</li></ul><p id="518e">Among all of these, my favorite is definitely to work on Machine Learning and <a href="https://www.edureka.co/blog/deep-learning-with-python?utm_source=medium&amp;utm_medium=content-link&amp;utm_campaign=python-projects">Deep Learning</a>. Let us look at a very nice use-case to learn Python in-depth.</p><h1 id="67d3">Implementation Of CIFAR10 Using TensorFlow In Python</h1><p id="9a96">Let’s <b>train</b> a <b>network</b> to classify images from the <b>CIFAR10 Dataset</b> using a Convolution Neural Network built-in <b>TensorFlow</b>.</p><figure id="5efb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*CkLl7D4StsGuzLfmA9drFA.png"><figcaption></figcaption></figure><p id="96b5">Consider the following <b>Flowchart</b> to <b>understand</b> the <b>working</b> of the use-case:</p><figure id="be66"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GyIKmHRoX05QLbSdr5MeUw.png"><figcaption></figcaption></figure><p id="7957">Let us break down this flowchart into simple terms:</p><ul><li>We first load images into our program</li><li>These images are stored in a place where the program can access it</li><li>We need Python to make sense of the information that is present so we do the process of normalization</li><li>We define the foundation of the neural network</li><li>Defining the loss function to ensure we obtain maximum accuracy on the dataset</li><li>Training the actual model to learn something about the data that it has seen all this while</li><li>testing the model to analyze the accuracy and iterate through the training process to achieve better accuracy</li></ul><p id="d098">This use-case is broken into 2 programs. One is to Train the network and other is to test the network.</p><p id="681c">Let us first train the network.</p><p id="e208"><b>Training the network:</b></p><div id="8303"><pre>import numpy as np

import tensorflow as tf

<span class="hljs-keyword">from</span> time import time

import math

<span class="hljs-keyword">from</span> include.data import get_data_set

<span class="hljs-keyword">from</span> include.model import model, lr

train_x, train_y = get_data_set(<span class="hljs-string">"train"</span>)

test_x, test_y = get_data_set(<span class="hljs-string">"test"</span>)

tf.set_random_seed(21)

x, y, output, y_pred_cls, global_step, learning_rate = model()

global_accuracy = 0

epoch_start = 0

<span class="hljs-comment"># PARAMS</span>

_BATCH_SIZE = 128

_EPOCH = 60

_SAVE_PATH = <span class="hljs-string">"./tensorboard/cifar-10-v1.0.0/"</span>

<span class="hljs-comment"># LOSS AND OPTIMIZER</span>

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(<span class="hljs-attribute">logits</span>=output, <span class="hljs-attribute">labels</span>=y))

optimizer = tf.train.AdamOptimizer(<span class="hljs-attribute">learning_rate</span>=learning_rate,

                               <span class="hljs-attribute">beta1</span>=0.9,

                               <span class="hljs-attribute">beta2</span>=0.999,

                               <span class="hljs-attribute">epsilon</span>=1e-08).minimize(loss, <span class="hljs-attribute">global_step</span>=global_step)

<span class="hljs-comment"># PREDICTION AND ACCURACY CALCULATION</span>

correct_prediction = tf.equal(y_pred_cls, tf.argmax(y, <span class="hljs-attribute">axis</span>=1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

<span class="hljs-comment"># SAVER</span>

merged = tf.summary.merge_all()

saver = tf.train.Saver()

sess = tf.Session()

train_writer = tf.summary.FileWriter(_SAVE_PATH, sess.graph)

try:

<span class="hljs-built_in">print</span>(<span class="hljs-string">"\nTrying to restore last checkpoint ..."</span>)

last_chk_path = tf.train.latest_checkpoint(<span class="hljs-attribute">checkpoint_dir</span>=_SAVE_PATH)

saver.restore(sess, <span class="hljs-attribute">save_path</span>=last_chk_path)

<span class="hljs-built_in">print</span>(<span class="hljs-string">"Restored checkpoint from:"</span>, last_chk_path)

except ValueError:

<span class="hljs-built_in">print</span>(<span class="hljs-string">"\nFailed to restore checkpoint. Initializing variables instead."</span>)

sess.<span class="hljs-built_in">run</span>(tf.global_variables_initializer())

def train(epoch):

global epoch_start

epoch_start = time()

batch_size = int(math.ceil(len(train_x) / _BATCH_SIZE))

i_global = 0

<span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> range(batch_size):

    batch_xs = train_x[s*_BATCH_SIZE: (s+1)*_BATCH_SIZE]

    batch_ys = train_y[s*_BATCH_SIZE: (s+1)*_BATCH_SIZE]

    start_time = time()

    i_global, _, batch_loss, batch_acc = sess.<span class="hljs-built_in">run</span>(

        [global_step, optimizer, loss, accuracy],

        feed_dict={x: batch_xs, y: batch_ys, learning_rate: lr(epoch)})

    duration = time() - start_time

    <span class="hljs-keyword">if</span> s % 10 == 0:

        percentage = int(round((s/batch_size)<span class="hljs-number">*100</span>))

        bar_len = 29

        filled_len = int((bar_len*int(percentage))/100)

        bar = <span class="hljs-string">'='</span> * filled_len + <span class="hljs-string">'&gt;'</span> + <span class="hljs-string">'-'</span> * (bar_len - filled_len)

        msg = <span class="hljs-string">"Global step: {:&gt;5} - [{}] {:&gt;3}% - acc: {:.4f} - loss: {:.4f} - {:.1f} sample/sec"</span>

        <span class="hljs-built_in">print</span>(msg.format(i_global, bar, percentage, batch_acc, batch_loss, _BATCH_SIZE / duration))

test_and_save(i_global, epoch)

def test_and_save(_global_step, epoch):

global global_accuracy

global epoch_start

i = 0

predicted_class = np.zeros(<span class="hljs-attribute">shape</span>=len(test_x), <span class="hljs-attribute">dtype</span>=np.int)

<span class="hljs-keyword">while</span> i &lt; len(test_x):

j = min(i + _BATCH_SIZE, len(test_x)) batch_xs = test_x[i:j, :] batch_ys = test_y[i:j, :] predicted_class[i:j] = sess.<span class="hljs-built_in">run</span>( y_pred_cls, feed_dict={x: batch_xs, y: batch_ys, learning_rate: lr(epoch)} ) i = j correct = (np.argmax(test_y, <span class="hljs-attribute">axis</span>=1) == predicted_class) acc = correct.mean()<span class="hljs-number">*100</span> correct_numbers = correct.sum() hours, rem = divmod(time() - epoch_start, 3600) minutes, seconds = divmod(rem, 60) mes = <span class="hljs-string">"\nEpoch {} - accuracy: {:.2f}% ({}/{}) - time: {:0>2}:{:0>2}:{:05.2f}"</span>

<span class="hljs-built_in">print</span>(mes.format((epoch+1), acc, correct_numbers, len(test_x), int(hours), int(minutes), seconds))

<span class="hljs-keyword">if</span> global_accuracy != 0 <span class="hljs-keyword">and</span> global_accuracy &lt; acc: summary = tf.Summary(value=[ tf.Summary.Value(<span class="hljs-attribute">tag</span>=<span class="hljs-string">"Accuracy/test"</span>, <span class="hljs-attribute">simple_value</span>=acc), ])

train_writer.add_summary(summary, _global_step) saver.save(sess, <span class="hljs-attribute">save_path</span>=_SAVE_PATH, <span class="hljs-attribute">global_step</span>=_global_step) mes = <span class="hljs-string">"This epoch receive better accuracy: {:.2f} > {:.2f}. Saving session..."</span>

    <span class="hljs-built_in">print</span>(mes.format(acc, global_accuracy))

    global_accuracy = acc

elif global_accuracy == 0:

    global_accuracy = acc
<span class="hljs-built_in">print</span>(<span class="hljs-string">"###########################################################################################################"</span>)

def main():

train_start = time()

<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(_EPOCH):

    <span class="hljs-built_in">print</span>(<span class="hljs-string">"\nEpoch: {}/{}\n"</span>.format((i+1), _EPOCH))

    train(i)</pre></div><div id="9550"><pre>    hours, rem = div<span class="hljs-meta">mod</span>(<span class="hljs-meta">time</span>() - train_start, 3600)

minutes, seconds = div<span class="hljs-meta">mod</span>(rem, 60)

mes = <span class="hljs-string">"Best accuracy pre session: {:.2f}, time: {:0&gt;2}:{:0&gt;2}:{:05.2f}"</span>

pr<span class="hljs-meta">int</span>(mes.<span class="hljs-keyword">format</span>(global_accuracy, <span class="hljs-meta">int</span>(hours), <span class="hljs-meta">int</span>(minutes), seconds))

<span class="hljs-keyword">if</span> name == <span class="hljs-string">"main"</span>:

mai<span class="hljs-meta">n</span>()

sess.<span class="hljs-meta">close</span>()</pre></div><h2 id="14c5">Output:</h2><div id="897f"><pre><span class="hljs-attribute">Epoch</span>: <span class="hljs-number">60</span>/<span class="hljs-number">60</span></pre></div><div id="09cd"><pre><span class="hljs-attr">Global step:</span> <span class="hljs-number">23070</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">>-----------------------------</span>] <span class="hljs-number">0</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9531 - loss:</span> <span class="hljs-number">1.5081</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7045.4 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23080</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">>-----------------------------</span>] <span class="hljs-number">3</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9453 - loss:</span> <span class="hljs-number">1.5159</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7147.6 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23090</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=>----------------------------</span>] <span class="hljs-number">5</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9844 - loss:</span> <span class="hljs-number">1.4764</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7154.6 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23100</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">==>---------------------------</span>] <span class="hljs-number">8</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9297 - loss:</span> <span class="hljs-number">1.5307</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7104.4 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23110</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">==>---------------------------</span>] <span class="hljs-number">10</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9141 - loss:</span> <span class="hljs-number">1.5462</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7091.4 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23120</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">===>--------------------------</span>] <span class="hljs-number">13</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9297 - loss:</span> <span class="hljs-number">1.5314</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7162.9 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23130</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">====>-------------------------</span>] <span class="hljs-number">15</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9297 - loss:</span> <span class="hljs-number">1.5307</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7174.8 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23140</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=====>------------------------</span>] <span class="hljs-number">18</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9375 - loss:</span> <span class="hljs-number">1.5231</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7140.0 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23150</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=====>------------------------</span>] <span class="hljs-number">20</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9297 - loss:</span> <span class="hljs-number">1.5301</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7152.8 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23160</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">======>-----------------------</span>] <span class="hljs-number">23</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9531 - loss:</span> <span class="hljs-number">1.5080</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7112.3 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23170</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=======>----------------------</span>] <span class="hljs-number">26</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9609 - loss:</span> <span class="hljs-number">1.5000</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7154.0 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23180</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">========>---------------------</span>] <span class="hljs-number">28</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9531 - loss:</span> <span class="hljs-number">1.5074</span> <span class="hljs-bullet">-</span> <span class="hljs-number">6862.2 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23190</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">========>---------------------</span>] <span class="hljs-number">31</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9609 - loss:</span> <span class="hljs-number">1.4993</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7134.5 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23200</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=========>--------------------</span>] <span class="hljs-number">33</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9609 - loss:</span> <span class="hljs-number">1.4995</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7166.0 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23210</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">==========>-------------------</span>] <span class="hljs-number">36</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9375 - loss:</span> <span class="hljs-number">1.5231</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7116.7 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23220</span> <span class="hljs-bullet">-<

Options

/span> [<span class="hljs-string">===========>------------------</span>] <span class="hljs-number">38</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9453 - loss:</span> <span class="hljs-number">1.5153</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7134.1 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23230</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">===========>------------------</span>] <span class="hljs-number">41</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9375 - loss:</span> <span class="hljs-number">1.5233</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7074.5 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23240</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">============>-----------------</span>] <span class="hljs-number">43</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9219 - loss:</span> <span class="hljs-number">1.5387</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7176.9 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23250</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=============>----------------</span>] <span class="hljs-number">46</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.8828 - loss:</span> <span class="hljs-number">1.5769</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7144.1 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23260</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">==============>---------------</span>] <span class="hljs-number">49</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9219 - loss:</span> <span class="hljs-number">1.5383</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7059.7 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23270</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">==============>---------------</span>] <span class="hljs-number">51</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.8984 - loss:</span> <span class="hljs-number">1.5618</span> <span class="hljs-bullet">-</span> <span class="hljs-number">6638.6 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23280</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">===============>--------------</span>] <span class="hljs-number">54</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9453 - loss:</span> <span class="hljs-number">1.5151</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7035.7 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23290</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">================>-------------</span>] <span class="hljs-number">56</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9609 - loss:</span> <span class="hljs-number">1.4996</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7129.0 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23300</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=================>------------</span>] <span class="hljs-number">59</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9609 - loss:</span> <span class="hljs-number">1.4997</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7075.4 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23310</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=================>------------</span>] <span class="hljs-number">61</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.8750 - loss:</span> <span class="hljs-number">1.5842</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7117.8 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23320</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">==================>-----------</span>] <span class="hljs-number">64</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9141 - loss:</span> <span class="hljs-number">1.5463</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7157.2 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23330</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">===================>----------</span>] <span class="hljs-number">66</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9062 - loss:</span> <span class="hljs-number">1.5549</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7169.3 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23340</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">====================>---------</span>] <span class="hljs-number">69</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9219 - loss:</span> <span class="hljs-number">1.5389</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7164.4 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23350</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">====================>---------</span>] <span class="hljs-number">72</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9609 - loss:</span> <span class="hljs-number">1.5002</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7135.4 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23360</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=====================>--------</span>] <span class="hljs-number">74</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9766 - loss:</span> <span class="hljs-number">1.4842</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7124.2 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23370</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">======================>-------</span>] <span class="hljs-number">77</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9375 - loss:</span> <span class="hljs-number">1.5231</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7168.5 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23380</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">======================>-------</span>] <span class="hljs-number">79</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.8906 - loss:</span> <span class="hljs-number">1.5695</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7175.2 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23390</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=======================>------</span>] <span class="hljs-number">82</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9375 - loss:</span> <span class="hljs-number">1.5225</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7132.1 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23400</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">========================>-----</span>] <span class="hljs-number">84</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9844 - loss:</span> <span class="hljs-number">1.4768</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7100.1 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23410</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=========================>----</span>] <span class="hljs-number">87</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9766 - loss:</span> <span class="hljs-number">1.4840</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7172.0 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23420</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">==========================>---</span>] <span class="hljs-number">90</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9062 - loss:</span> <span class="hljs-number">1.5542</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7122.1 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23430</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">==========================>---</span>] <span class="hljs-number">92</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9297 - loss:</span> <span class="hljs-number">1.5313</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7145.3 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23440</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">===========================>--</span>] <span class="hljs-number">95</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9297 - loss:</span> <span class="hljs-number">1.5301</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7133.3 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23450</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">============================>-</span>] <span class="hljs-number">97</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9375 - loss:</span> <span class="hljs-number">1.5231</span> <span class="hljs-bullet">-</span> <span class="hljs-number">7135.7 </span><span class="hljs-string">sample/sec</span> <span class="hljs-attr">Global step:</span> <span class="hljs-number">23460</span> <span class="hljs-bullet">-</span> [<span class="hljs-string">=============================></span>] <span class="hljs-number">100</span><span class="hljs-string">%</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">acc: 0.9250 - loss:</span> <span class="hljs-number">1.5362</span> <span class="hljs-bullet">-</span> <span class="hljs-number">10297.5</span> <span class="hljs-string">sample/sec</span></pre></div><div id="1048"><pre>Epoch <span class="hljs-number">60</span> - accuracy: <span class="hljs-number">78.81</span>% (<span class="hljs-number">7881</span>/<span class="hljs-number">10000</span>) This epoch receive better accuracy: <span class="hljs-number">78.81</span> > <span class="hljs-number">78.78</span>. Saving session... ###########################################################################################################</pre></div><h2 id="22ce">Run Network on Test DataSet:</h2><div id="0516"><pre>import numpy as np

import tensorflow as tf

<span class="hljs-keyword">from</span> include.data import get_data_set

<span class="hljs-keyword">from</span> include.model import model

test_x, test_y = get_data_set(<span class="hljs-string">"test"</span>)

x, y, output, y_pred_cls, global_step, learning_rate = model()

_BATCH_SIZE = 128

_CLASS_SIZE = 10

_SAVE_PATH = <span class="hljs-string">"./tensorboard/cifar-10-v1.0.0/"</span>

saver = tf.train.Saver()

sess = tf.Session()

try:

<span class="hljs-built_in">print</span>(<span class="hljs-string">"\nTrying to restore last checkpoint ..."</span>)

last_chk_path = tf.train.latest_checkpoint(<span class="hljs-attribute">checkpoint_dir</span>=_SAVE_PATH)

saver.restore(sess, <span class="hljs-attribute">save_path</span>=last_chk_path)

<span class="hljs-built_in">print</span>(<span class="hljs-string">"Restored checkpoint from:"</span>, last_chk_path)

except ValueError:

<span class="hljs-built_in">print</span>(<span class="hljs-string">"\nFailed to restore checkpoint. Initializing variables instead."</span>)

sess.<span class="hljs-built_in">run</span>(tf.global_variables_initializer())

def main():

i = 0

predicted_class = np.zeros(<span class="hljs-attribute">shape</span>=len(test_x), <span class="hljs-attribute">dtype</span>=np.int)

<span class="hljs-keyword">while</span> i &lt; len(test_x):

    j = min(i + _BATCH_SIZE, len(test_x))

    batch_xs = test_x[i:j, :]

    batch_ys = test_y[i:j, :]

    predicted_class[i:j] = sess.<span class="hljs-built_in">run</span>(y_pred_cls, feed_dict={x: batch_xs, y: batch_ys})

    i = j

correct = (np.argmax(test_y, <span class="hljs-attribute">axis</span>=1) == predicted_class)

acc = correct.mean() * 100

correct_numbers = correct.sum()

<span class="hljs-built_in">print</span>()

<span class="hljs-built_in">print</span>(<span class="hljs-string">"Accuracy on Test-Set: {0:.2f}% ({1} / {2})"</span>.format(acc, correct_numbers, len(test_x)))

<span class="hljs-keyword">if</span> name == <span class="hljs-string">"main"</span>:

main()

sess.close()</pre></div><h2 id="6d48">Simple Output:</h2><div id="4ef6"><pre><span class="hljs-attribute">Trying</span> to restore last checkpoint ... <span class="hljs-attribute">Restored</span> checkpoint from: ./tensorboard/cifar-<span class="hljs-number">10</span>-v1.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>/-<span class="hljs-number">23460</span></pre></div><div id="514a"><pre><span class="hljs-attribute">Accuracy</span> <span class="hljs-literal">on</span> Test-Set: <span class="hljs-number">78</span>.<span class="hljs-number">81</span>% (<span class="hljs-number">7881</span> / <span class="hljs-number">10000</span>)</pre></div><p id="bd40">That was a very interesting use-case, wasn’t it? Thus, we saw how machine learning works and developed a basic program to implement it using the TensorFlow library in Python.</p><p id="0656"><b>Conclusion</b></p><p id="10b7">The Python projects discussed in this article should help you kickstart your learning about Python and it will indulge you and push you to learn more about Python practically. This will be very handy when you are trying to consider a problem and providing a solution for that using Python.</p><p id="aa7a">Python will help you solve multiple real-life projects as well and these concepts will get you up to speed with how you can begin exploring and understanding the art of project design, development, and handling.</p><p id="009b">I hope you have enjoyed this post on Python Projects. If you have any questions regarding this tutorial, please let me know in the comments.</p><p id="ecdc">If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to<a href="https://www.edureka.co/blog/?utm_source=medium&amp;utm_medium=content-link&amp;utm_campaign=python-projects"> Edureka’s official site.</a></p><p id="7b26">Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.</p><blockquote id="6488"><p>1. <a href="https://readmedium.com/python-tutorial-be1b3d015745">Python Tutorial</a></p></blockquote><blockquote id="2773"><p>2.<a href="https://readmedium.com/python-programming-language-fc1015de7a6f"> Python Programming Language</a></p></blockquote><blockquote id="ed5e"><p>3.<a href="https://readmedium.com/python-functions-f0cabca8c4a"> Python Functions</a></p></blockquote><blockquote id="2283"><p>4. <a href="https://readmedium.com/file-handling-in-python-e0a6ff96ede9">File Handling in Python</a></p></blockquote><blockquote id="d41a"><p>5.<a href="https://readmedium.com/python-numpy-tutorial-89fb8b642c7d"> Python Numpy Tutorial</a></p></blockquote><blockquote id="c958"><p>6.<a href="https://readmedium.com/scikit-learn-machine-learning-7a2d92e4dd07"> Scikit Learn Machine Learning</a></p></blockquote><blockquote id="40d1"><p>7. <a href="https://readmedium.com/python-pandas-tutorial-c5055c61d12e">Python Pandas Tutorial</a></p></blockquote><blockquote id="76f6"><p>8. <a href="https://readmedium.com/python-matplotlib-tutorial-15d148a7bfee">Matplotlib Tutorial</a></p></blockquote><blockquote id="4823"><p>9. <a href="https://readmedium.com/tkinter-tutorial-f655d3f4c818">Tkinter Tutorial</a></p></blockquote><blockquote id="dedc"><p>10. <a href="https://readmedium.com/python-requests-tutorial-30edabfa6a1c">Requests Tutorial</a></p></blockquote><blockquote id="b00e"><p>11. <a href="https://readmedium.com/pygame-tutorial-9874f7e5c0b4">PyGame Tutorial</a></p></blockquote><blockquote id="31e0"><p>12. <a href="https://readmedium.com/python-opencv-tutorial-5549bd4940e3">OpenCV Tutorial</a></p></blockquote><blockquote id="ccdb"><p>13. <a href="https://readmedium.com/web-scraping-with-python-d9e6506007bf">Web Scraping With Python</a></p></blockquote><blockquote id="1994"><p>14. <a href="https://readmedium.com/pycharm-tutorial-d0ec9ce6fb60">PyCharm Tutorial</a></p></blockquote><blockquote id="44dd"><p>15. <a href="https://readmedium.com/machine-learning-tutorial-f2883412fba1">Machine Learning Tutorial</a></p></blockquote><blockquote id="a6af"><p>16.<a href="https://readmedium.com/linear-regression-in-python-e66f869cb6ce"> Linear Regression Algorithm from scratch in Python</a></p></blockquote><blockquote id="1f28"><p>17.<a href="https://readmedium.com/learn-python-for-data-science-1f9f407943d3"> Python for Data Science</a></p></blockquote><blockquote id="1dbc"><p>18. <a href="https://readmedium.com/loops-in-python-fc5b42e2f313">Loops in Python</a></p></blockquote><blockquote id="d657"><p>19. <a href="https://readmedium.com/python-regex-regular-expression-tutorial-f2d17ffcf17e">Python RegEx</a></p></blockquote><blockquote id="b1ab"><p>20. <a href="https://readmedium.com/machine-learning-projects-cb0130d0606f">Machine Learning Projects</a></p></blockquote><blockquote id="23b1"><p>21. <a href="https://readmedium.com/arrays-in-python-14aecabec16e">Arrays in Python</a></p></blockquote><blockquote id="7883"><p>22. <a href="https://readmedium.com/sets-in-python-a16b410becf4">Sets in Python</a></p></blockquote><blockquote id="b517"><p>23. <a href="https://readmedium.com/what-is-mutithreading-19b6349dde0f">Multithreading in Python</a></p></blockquote><blockquote id="d809"><p>24. <a href="https://readmedium.com/python-interview-questions-a22257bc309f">Python Interview Questions</a></p></blockquote><blockquote id="c7dd"><p>25. <a href="https://readmedium.com/java-vs-python-31d7433ed9d">Java vs Python</a></p></blockquote><blockquote id="c6a7"><p>26. <a href="https://readmedium.com/how-to-become-a-python-developer-462a0093f246">How To Become A Python Developer?</a></p></blockquote><blockquote id="f668"><p>27. <a href="https://readmedium.com/python-lambda-b84d68d449a0">Python Lambda Functions</a></p></blockquote><blockquote id="5ecf"><p>28. <a href="https://readmedium.com/how-netflix-uses-python-1e4deb2f8ca5">How Netflix uses Python?</a></p></blockquote><blockquote id="00ec"><p>29. <a href="https://readmedium.com/socket-programming-python-bbac2d423bf9">What is Socket Programming in Python</a></p></blockquote><blockquote id="ddcd"><p>30. <a href="https://readmedium.com/python-database-connection-b4f9b301947c">Python Database Connection</a></p></blockquote><blockquote id="9669"><p>31. <a href="https://readmedium.com/golang-vs-python-5ac32e1ef2">Golang vs Python</a></p></blockquote><blockquote id="6c01"><p>32. <a href="https://readmedium.com/python-seaborn-tutorial-646fdddff322">Python Seaborn Tutorial</a></p></blockquote><blockquote id="a532"><p>33. <a href="https://readmedium.com/python-career-opportunities-a2500ce158de">Python Career Opportunities</a></p></blockquote><p id="cc97"><i>Originally published at <a href="https://www.edureka.co/blog/python-projects/">www.edureka.co</a> on January 11, 2019.</i></p></article></body>

Top Python Projects That You Should Practice - Easy, Intermediate And Advanced

Python Projects — Edureka

In this article, let us have a look at 3 levels of Python projects that you should learn to master Python and test your project analysis, development and handling skills on the whole. Many people would agree if I say Python is really fun to learn and fiddle around with.

Let’s begin this article by checking out the following list of topics:

  • Introduction to Python
  • How to go about learning Python Projects?
  • Beginner Python Projects: Hangman Game with Python
  • Intermediate Python Projects: Working with Graphs in Python
  • Advanced Python Projects: Machine Learning using Python
  • Conclusion

It will only be fair if I give you a small introduction to Python.

Introduction To Python

Python is a high-level, object-oriented, interpreted programming language, which has garnered worldwide attention. Stack Overflow found out that 38.8% of its users mainly use Python for their projects.

Python was created by a developer called Guido Van Rossum.

Python is and always has been easy to learn and master. It is very beginner friendly and the syntax is extremely simple to read and follow through. This definitely makes us all happy and what’s amazing is that python has millions of happy learners across the globe!

According to the website’s survey, Python’s popularity surpassed that of C# in 2018 — just like it surpassed PHP in 2017. On the GitHub platform, Python surpassed Java as the second-most used programming language, with 40% more pull requests opened in 2017 than in 2016.

How To Go About Learning Python Projects?

The answer to this question is fairly simple and straightforward. It all starts with learning the basics and all the fundamentals of Python. This is basically a measurement index to know how comfortable you are working with Python.

The next prime step involves taking a look at the basic and easy code to familiarize yourself with the syntax and the flow of logic in the code. This is a very important step and helps set a strong foundation for later on as well.

After this, you should definitely look at what python is being used for in real life. This will play a major role in finding out why you want to learn Python in the first place.

If that’s not the case then you will learn about the projects and you can implement certain strategies for the projects that you will consider starting on your own.

Followed by this is definitely to look at what projects you can tackle your current knowledge of Python. Diving into the depth of Python will help you assess yourself at every stage.

Projects are basically used to solve a problem at hand. If providing solutions to the various simple and complex problems are your kind of a thing, then you should definitely consider working on Python projects.

After you’ve got your hands dirty with a couple of projects, you will be one step closer to mastering python. This is important because you will be able to spontaneously implement what you’ve learned on something as simple as writing a calculator program all the way till helping achieve artificial intelligence.

Let’s begin by checking out the first level of Python projects.

Beginner Python Project: Hangman Game with Python

The best beginner project we can consider is the game of Hangman. I am sure the majority of you reading this article has played Hangman at one point of time in your life. To put it in just one single statement, the main goal here is to create a “guess the word” game. As simple as it sounds, it has certain key things you need to note.

  • The user needs to be able to input letter guesses.
  • A limit should also be set on how many guesses they can use.
  • Keep notifying the user of the remaining turns.

This means you’ll need a way to grab a word to use for guessing. Let us keep it simple and use a text file for the input. The text file consists of the words from which we have to guess.

You will also need functions to check if the user has actually inputted a single letter, to check if the inputted letter is in the hidden word (and if it is, how many times it appears), to print letters, and a counter variable to limit guesses.

Key Concepts to keep in mind for this Python Project:

  • Random
  • Variables
  • Boolean
  • Input and Output
  • Integer
  • Char
  • String
  • Length
  • Print

Code:

  1. Hangman.py
from string import ascii_lowercase
from words import get_random_word
 
 
def get_num_attempts():
    """Get user-inputted number of incorrect attempts for the game."""
    while True:
        num_attempts = input(
            'How many incorrect attempts do you want? [1-25] ')
        try:
            num_attempts = int(num_attempts)
            if 1 <= num_attempts <= 25:
                return num_attempts
            else:
                print('{0} is not between 1 and 25'.format(num_attempts))
        except ValueError:
            print('{0} is not an integer between 1 and 25'.format(
                num_attempts))
 
 
def get_min_word_length():
    """Get user-inputted minimum word length for the game."""
    while True:
        min_word_length = input(
            'What minimum word length do you want? [4-16] ')
        try:
            min_word_length = int(min_word_length)
            if 4 <= min_word_length <= 16:
                 return min_word_length
             else:
                 print('{0} is not between 4 and 16'.format(min_word_length))
         except ValueError:
             print('{0} is not an integer between 4 and 16'.format(                 min_word_length))
 def get_display_word(word, idxs):     """Get the word suitable for display."""     
if len(word) != len(idxs):
         raise ValueError('Word length and indices length are not the same')
     displayed_word = ''.join(
         [letter if idxs[i] else '*' for i, letter in enumerate(word)])
     return displayed_word.strip()
 def get_next_letter(remaining_letters):     """Get the user-inputted next letter."""
     if len(remaining_letters) == 0:
         raise ValueError('There are no remaining letters')     while True:
         next_letter = input('Choose the next letter: ').lower()         if len(next_letter) != 1:
             print('{0} is not a single character'.format(next_letter))
         elif next_letter not in ascii_lowercase:             print('{0} is not a letter'.format(next_letter))
         elif next_letter not in remaining_letters:             print('{0} has been guessed before'.format(next_letter))         else:
             remaining_letters.remove(next_letter)             return next_letter def play_hangman():
     """Play a game of hangman.     At the end of the game, returns if the player wants to retry.     """     
# Let player specify difficulty
     print('Starting a game of Hangman...')
     attempts_remaining = get_num_attempts()
     min_word_length = get_min_word_length()
     # Randomly select a word     print('Selecting a word...')     word = get_random_word(min_word_length)
     print()     # Initialize game state variables
     idxs = [letter not in ascii_lowercase for letter in word]     remaining_letters = set(ascii_lowercase)
     wrong_letters = []
     word_solved = False
     # Main game loop
     while attempts_remaining > 0 and not word_solved:
        # Print current game state
        print('Word: {0}'.format(get_display_word(word, idxs)))
        print('Attempts Remaining: {0}'.format(attempts_remaining))
        print('Previous Guesses: {0}'.format(' '.join(wrong_letters)))
 
        # Get player's next letter guess
        next_letter = get_next_letter(remaining_letters)
 
        # Check if letter guess is in word
        if next_letter in word:
            # Guessed correctly
            print('{0} is in the word!'.format(next_letter))
 
            # Reveal matching letters
            for i in range(len(word)):
                if word[i] == next_letter:
                    idxs[i] = True
        else:
            # Guessed incorrectly
            print('{0} is NOT in the word!'.format(next_letter))
 
            # Decrement num of attempts left and append guess to wrong guesses
            attempts_remaining -= 1
            wrong_letters.append(next_letter)
 
        # Check if word is completely solved
        if False not in idxs:
            word_solved = True
        print()
 
    # The game is over: reveal the word
    print('The word is {0}'.format(word))
 
    # Notify player of victory or defeat
    if word_solved:
        print('Congratulations! You won!')
    else:
        print('Try again next time!')
 
    # Ask player if he/she wants to try again
    try_again = input('Would you like to try again? [y/Y] ')
    return try_again.lower() == 'y'
 
 
if __name__ == '__main__':
    while play_hangman():
        print()

2. Words.py

"""Function to fetch words."""
 
import random
 
WORDLIST = 'wordlist.txt'
 
 
def get_random_word(min_word_length):
    """Get a random word from the wordlist using no extra memory."""
    num_words_processed = 0
    curr_word = None
    with open(WORDLIST, 'r') as f:
        for word in f:
            if '(' in word or ')' in word:
                continue
            word = word.strip().lower()
            if len(word) < min_word_length:
                continue
            num_words_processed += 1
            if random.randint(1, num_words_processed) == 1:
                curr_word = word
    return curr_word

The output is as follows:

Now that we saw how we can tackle a beginner project like Hangman, let us step it up a little and check out an intermediate Python Project next.

Intermediate Python Project: Working With Graphs In Python

The best way to get started with learning intermediate stages of programming in Python is to definitely start working with the libraries that Python supports.

There is literally ’n’ number of libraries that you can make use of while coding in Python. Some are very easy and straightforward while some might take some time to grasp and master.

Here are some of the top libraries you can consider starting out with:

  • NumPy
  • SciPy
  • Pandas
  • Matplotlib

NumPy is for scientific computing on a whole.

Scipy uses arrays like basic data structure used for linear algebra, calculus, and other similar concepts.

Pandas are used for data frames and Matplotlib is to visualize data in the form of graphs and notations.

The best possible usage of Python is for data visualization. As helpful as numeric data output is, there are many requirements of a visual representation of the data.

By visual representation, it is just a generalization. Anything ranging from creating your front-end or a Graphical User Interface (GUI) to plotting numeric data as points on a graph.

Matplotlib is used to plot data points on a graph. And Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK+.

There are many options for doing 3D plots in Python, but here are some common and easy ways using Matplotlib.

In general, the first step is to create a 3D axes, and then plot any of the 3D graphs that best illustrates the data for a particular need. In order to use Matplotlib, the mplot3d toolkit that is included with the Matplotlib installation has to be imported:

from mpl_toolkits import mplot3d

Then, to create a 3D axes you can execute this code:

<pre id="3346" class="graf graf--pre graf-after--p">%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection=3d’)</pre>

It is inside this 3D axes that a plot can be drawn, it is important to know what type of plot (or combination of plots) will be better to describe the data.

At this point in time, you need to note that this comprises our base for further plotting.

Points and Lines:

The following image combines 2 plots, one with a line that goes through every point of the data, and others that draw a point on each of the particular 1000 values on this example.

The code is actually very simple when you try to analyze it. We have made use of standard trigonometric functions to plot a set of random values to obtain our projection in 3 dimensions.

Code:

ax = plt.axes(projection=’3d’)# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, ‘gray’)# Data for three-dimensional scattered points
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap=’Greens’);

3D Contour Plots:

The input for the contour plot is a bit different than for the previous plot, as it needs the data on a two-dimensional grid.

Note that on the following example that after assigning values for x and y, they are combined on a grid by executing “np.meshgrid(x, y)” and then the Z values are created from executing the function f(X,Y) with the values of the grid (Z=f(X,Y)).

Again, basic 3d plot simplified in the following code:

def f(x, y):
    return np.sin(np.sqrt(x ** 2 + y ** 2))
 
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
 
X, Y = np.meshgrid(x, y)
Z = f(X, Y)fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');

On the previous graphs, the data was generated in order, but in real life sometimes the data is not ordered, for those cases, the surface triangulation is very useful as it creates surfaces by finding sets of triangles formed between adjacent points.

Surface Triangulation:

theta = 2 * np.pi * np.random.random(1000)
r = 6 * np.random.random(1000)
x = np.ravel(r * np.sin(theta))
y = np.ravel(r * np.cos(theta))
z = f(x, y)
ax = plt.axes(projection=’3d’)
ax.plot_trisurf(x, y, z,cmap=’viridis’, edgecolor=’none’);

Now that we are familiar with how we can expand our reach on learning Python by looking at external libraries, we can go ahead and check out the next level of Python Projects which is the Advanced Level.

Advanced Projects With Python

Python has vast applications — Everything from “Hello World” all the way to achieving Artificial Intelligence.

There are virtually unlimited projects you can work on using Python but here are the major ones that you can consider if you want to dive into the heart of Python.

  • Machine Learning with PyTorch, TensorFlow, Keras and whatever Machine Learning libraries you like.
  • Computer vision using OpenCV and PIL.
  • Creating and publishing your own pip module with tests and documentation.

Among all of these, my favorite is definitely to work on Machine Learning and Deep Learning. Let us look at a very nice use-case to learn Python in-depth.

Implementation Of CIFAR10 Using TensorFlow In Python

Let’s train a network to classify images from the CIFAR10 Dataset using a Convolution Neural Network built-in TensorFlow.

Consider the following Flowchart to understand the working of the use-case:

Let us break down this flowchart into simple terms:

  • We first load images into our program
  • These images are stored in a place where the program can access it
  • We need Python to make sense of the information that is present so we do the process of normalization
  • We define the foundation of the neural network
  • Defining the loss function to ensure we obtain maximum accuracy on the dataset
  • Training the actual model to learn something about the data that it has seen all this while
  • testing the model to analyze the accuracy and iterate through the training process to achieve better accuracy

This use-case is broken into 2 programs. One is to Train the network and other is to test the network.

Let us first train the network.

Training the network:

import numpy as np
 
import tensorflow as tf
 
from time import time
 
import math
 
from include.data import get_data_set
 
from include.model import model, lr
 
train_x, train_y = get_data_set("train")
 
test_x, test_y = get_data_set("test")
 
tf.set_random_seed(21)
 
x, y, output, y_pred_cls, global_step, learning_rate = model()
 
global_accuracy = 0
 
epoch_start = 0
 
# PARAMS
 
_BATCH_SIZE = 128
 
_EPOCH = 60
 
_SAVE_PATH = "./tensorboard/cifar-10-v1.0.0/"
 
# LOSS AND OPTIMIZER
 
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=output, labels=y))
 
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate,
 
                                   beta1=0.9,
 
                                   beta2=0.999,
 
                                   epsilon=1e-08).minimize(loss, global_step=global_step)
 
# PREDICTION AND ACCURACY CALCULATION
 
correct_prediction = tf.equal(y_pred_cls, tf.argmax(y, axis=1))
 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
 
# SAVER
 
merged = tf.summary.merge_all()
 
saver = tf.train.Saver()
 
sess = tf.Session()
 
train_writer = tf.summary.FileWriter(_SAVE_PATH, sess.graph)
 
try:
 
    print("\nTrying to restore last checkpoint ...")
 
    last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=_SAVE_PATH)
 
    saver.restore(sess, save_path=last_chk_path)
 
    print("Restored checkpoint from:", last_chk_path)
 
except ValueError:
 
    print("\nFailed to restore checkpoint. Initializing variables instead.")
 
    sess.run(tf.global_variables_initializer())
 
def train(epoch):
 
    global epoch_start
 
    epoch_start = time()
 
    batch_size = int(math.ceil(len(train_x) / _BATCH_SIZE))
 
    i_global = 0
 
    for s in range(batch_size):
 
        batch_xs = train_x[s*_BATCH_SIZE: (s+1)*_BATCH_SIZE]
 
        batch_ys = train_y[s*_BATCH_SIZE: (s+1)*_BATCH_SIZE]
 
        start_time = time()
 
        i_global, _, batch_loss, batch_acc = sess.run(
 
            [global_step, optimizer, loss, accuracy],
 
            feed_dict={x: batch_xs, y: batch_ys, learning_rate: lr(epoch)})
 
        duration = time() - start_time
 
        if s % 10 == 0:
 
            percentage = int(round((s/batch_size)*100))
 
            bar_len = 29
 
            filled_len = int((bar_len*int(percentage))/100)
 
            bar = '=' * filled_len + '>' + '-' * (bar_len - filled_len)
 
            msg = "Global step: {:>5} - [{}] {:>3}% - acc: {:.4f} - loss: {:.4f} - {:.1f} sample/sec"
 
            print(msg.format(i_global, bar, percentage, batch_acc, batch_loss, _BATCH_SIZE / duration))
 
    test_and_save(i_global, epoch)
 
def test_and_save(_global_step, epoch):
 
    global global_accuracy
 
    global epoch_start
 
    i = 0
 
    predicted_class = np.zeros(shape=len(test_x), dtype=np.int)
 
    while i < len(test_x):
 j = min(i + _BATCH_SIZE, len(test_x))
 batch_xs = test_x[i:j, :]
 batch_ys = test_y[i:j, :]
 predicted_class[i:j] = sess.run( y_pred_cls, feed_dict={x: batch_xs, y: batch_ys, learning_rate: lr(epoch)} )
 i = j
correct = (np.argmax(test_y, axis=1) == predicted_class)
acc = correct.mean()*100 correct_numbers = correct.sum()
hours, rem = divmod(time() - epoch_start, 3600) minutes, seconds = divmod(rem, 60) mes = "\nEpoch {} - accuracy: {:.2f}% ({}/{}) - time: {:0>2}:{:0>2}:{:05.2f}"
 
    print(mes.format((epoch+1), acc, correct_numbers, len(test_x), int(hours), int(minutes), seconds))
 
    if global_accuracy != 0 and global_accuracy < acc: summary = tf.Summary(value=[ tf.Summary.Value(tag="Accuracy/test", simple_value=acc), ])
train_writer.add_summary(summary, _global_step) saver.save(sess, save_path=_SAVE_PATH, global_step=_global_step)
mes = "This epoch receive better accuracy: {:.2f} > {:.2f}. Saving session..."
 
        print(mes.format(acc, global_accuracy))
 
        global_accuracy = acc
 
    elif global_accuracy == 0:
 
        global_accuracy = acc
    print("###########################################################################################################")
 
def main():
 
    train_start = time()
 
    for i in range(_EPOCH):
 
        print("\nEpoch: {}/{}\n".format((i+1), _EPOCH))
 
        train(i)
    hours, rem = divmod(time() - train_start, 3600)
 
    minutes, seconds = divmod(rem, 60)
 
    mes = "Best accuracy pre session: {:.2f}, time: {:0>2}:{:0>2}:{:05.2f}"
 
    print(mes.format(global_accuracy, int(hours), int(minutes), seconds))
if __name__ == "__main__":
 
    main()
 
sess.close()

Output:

Epoch: 60/60
Global step: 23070 - [>-----------------------------]   0% - acc: 0.9531 - loss: 1.5081 - 7045.4 sample/sec
Global step: 23080 - [>-----------------------------]   3% - acc: 0.9453 - loss: 1.5159 - 7147.6 sample/sec
Global step: 23090 - [=>----------------------------]   5% - acc: 0.9844 - loss: 1.4764 - 7154.6 sample/sec
Global step: 23100 - [==>---------------------------]   8% - acc: 0.9297 - loss: 1.5307 - 7104.4 sample/sec
Global step: 23110 - [==>---------------------------]  10% - acc: 0.9141 - loss: 1.5462 - 7091.4 sample/sec
Global step: 23120 - [===>--------------------------]  13% - acc: 0.9297 - loss: 1.5314 - 7162.9 sample/sec
Global step: 23130 - [====>-------------------------]  15% - acc: 0.9297 - loss: 1.5307 - 7174.8 sample/sec
Global step: 23140 - [=====>------------------------]  18% - acc: 0.9375 - loss: 1.5231 - 7140.0 sample/sec
Global step: 23150 - [=====>------------------------]  20% - acc: 0.9297 - loss: 1.5301 - 7152.8 sample/sec
Global step: 23160 - [======>-----------------------]  23% - acc: 0.9531 - loss: 1.5080 - 7112.3 sample/sec
Global step: 23170 - [=======>----------------------]  26% - acc: 0.9609 - loss: 1.5000 - 7154.0 sample/sec
Global step: 23180 - [========>---------------------]  28% - acc: 0.9531 - loss: 1.5074 - 6862.2 sample/sec
Global step: 23190 - [========>---------------------]  31% - acc: 0.9609 - loss: 1.4993 - 7134.5 sample/sec
Global step: 23200 - [=========>--------------------]  33% - acc: 0.9609 - loss: 1.4995 - 7166.0 sample/sec
Global step: 23210 - [==========>-------------------]  36% - acc: 0.9375 - loss: 1.5231 - 7116.7 sample/sec
Global step: 23220 - [===========>------------------]  38% - acc: 0.9453 - loss: 1.5153 - 7134.1 sample/sec
Global step: 23230 - [===========>------------------]  41% - acc: 0.9375 - loss: 1.5233 - 7074.5 sample/sec
Global step: 23240 - [============>-----------------]  43% - acc: 0.9219 - loss: 1.5387 - 7176.9 sample/sec
Global step: 23250 - [=============>----------------]  46% - acc: 0.8828 - loss: 1.5769 - 7144.1 sample/sec
Global step: 23260 - [==============>---------------]  49% - acc: 0.9219 - loss: 1.5383 - 7059.7 sample/sec
Global step: 23270 - [==============>---------------]  51% - acc: 0.8984 - loss: 1.5618 - 6638.6 sample/sec
Global step: 23280 - [===============>--------------]  54% - acc: 0.9453 - loss: 1.5151 - 7035.7 sample/sec
Global step: 23290 - [================>-------------]  56% - acc: 0.9609 - loss: 1.4996 - 7129.0 sample/sec
Global step: 23300 - [=================>------------]  59% - acc: 0.9609 - loss: 1.4997 - 7075.4 sample/sec
Global step: 23310 - [=================>------------]  61% - acc: 0.8750 - loss: 1.5842 - 7117.8 sample/sec
Global step: 23320 - [==================>-----------]  64% - acc: 0.9141 - loss: 1.5463 - 7157.2 sample/sec
Global step: 23330 - [===================>----------]  66% - acc: 0.9062 - loss: 1.5549 - 7169.3 sample/sec
Global step: 23340 - [====================>---------]  69% - acc: 0.9219 - loss: 1.5389 - 7164.4 sample/sec
Global step: 23350 - [====================>---------]  72% - acc: 0.9609 - loss: 1.5002 - 7135.4 sample/sec
Global step: 23360 - [=====================>--------]  74% - acc: 0.9766 - loss: 1.4842 - 7124.2 sample/sec
Global step: 23370 - [======================>-------]  77% - acc: 0.9375 - loss: 1.5231 - 7168.5 sample/sec
Global step: 23380 - [======================>-------]  79% - acc: 0.8906 - loss: 1.5695 - 7175.2 sample/sec
Global step: 23390 - [=======================>------]  82% - acc: 0.9375 - loss: 1.5225 - 7132.1 sample/sec
Global step: 23400 - [========================>-----]  84% - acc: 0.9844 - loss: 1.4768 - 7100.1 sample/sec
Global step: 23410 - [=========================>----]  87% - acc: 0.9766 - loss: 1.4840 - 7172.0 sample/sec
Global step: 23420 - [==========================>---]  90% - acc: 0.9062 - loss: 1.5542 - 7122.1 sample/sec
Global step: 23430 - [==========================>---]  92% - acc: 0.9297 - loss: 1.5313 - 7145.3 sample/sec
Global step: 23440 - [===========================>--]  95% - acc: 0.9297 - loss: 1.5301 - 7133.3 sample/sec
Global step: 23450 - [============================>-]  97% - acc: 0.9375 - loss: 1.5231 - 7135.7 sample/sec
Global step: 23460 - [=============================>] 100% - acc: 0.9250 - loss: 1.5362 - 10297.5 sample/sec
Epoch 60 - accuracy: 78.81% (7881/10000)
This epoch receive better accuracy: 78.81 > 78.78. Saving session...
###########################################################################################################

Run Network on Test DataSet:

import numpy as np
 
import tensorflow as tf
 
from include.data import get_data_set
 
from include.model import model
 
test_x, test_y = get_data_set("test")
 
x, y, output, y_pred_cls, global_step, learning_rate = model()
 
_BATCH_SIZE = 128
 
_CLASS_SIZE = 10
 
_SAVE_PATH = "./tensorboard/cifar-10-v1.0.0/"
 
saver = tf.train.Saver()
 
sess = tf.Session()
 
try:
 
    print("\nTrying to restore last checkpoint ...")
 
    last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=_SAVE_PATH)
 
    saver.restore(sess, save_path=last_chk_path)
 
    print("Restored checkpoint from:", last_chk_path)
 
except ValueError:
 
    print("\nFailed to restore checkpoint. Initializing variables instead.")
 
    sess.run(tf.global_variables_initializer())
 
def main():
 
    i = 0
 
    predicted_class = np.zeros(shape=len(test_x), dtype=np.int)
 
    while i < len(test_x):
 
        j = min(i + _BATCH_SIZE, len(test_x))
 
        batch_xs = test_x[i:j, :]
 
        batch_ys = test_y[i:j, :]
 
        predicted_class[i:j] = sess.run(y_pred_cls, feed_dict={x: batch_xs, y: batch_ys})
 
        i = j
 
    correct = (np.argmax(test_y, axis=1) == predicted_class)
 
    acc = correct.mean() * 100
 
    correct_numbers = correct.sum()
 
    print()
 
    print("Accuracy on Test-Set: {0:.2f}% ({1} / {2})".format(acc, correct_numbers, len(test_x)))
 
if __name__ == "__main__":
 
    main()
 
sess.close()

Simple Output:

Trying to restore last checkpoint ...
Restored checkpoint from: ./tensorboard/cifar-10-v1.0.0/-23460
Accuracy on Test-Set: 78.81% (7881 / 10000)

That was a very interesting use-case, wasn’t it? Thus, we saw how machine learning works and developed a basic program to implement it using the TensorFlow library in Python.

Conclusion

The Python projects discussed in this article should help you kickstart your learning about Python and it will indulge you and push you to learn more about Python practically. This will be very handy when you are trying to consider a problem and providing a solution for that using Python.

Python will help you solve multiple real-life projects as well and these concepts will get you up to speed with how you can begin exploring and understanding the art of project design, development, and handling.

I hope you have enjoyed this post on Python Projects. If you have any questions regarding this tutorial, please let me know in the comments.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

1. Python Tutorial

2. Python Programming Language

3. Python Functions

4. File Handling in Python

5. Python Numpy Tutorial

6. Scikit Learn Machine Learning

7. Python Pandas Tutorial

8. Matplotlib Tutorial

9. Tkinter Tutorial

10. Requests Tutorial

11. PyGame Tutorial

12. OpenCV Tutorial

13. Web Scraping With Python

14. PyCharm Tutorial

15. Machine Learning Tutorial

16. Linear Regression Algorithm from scratch in Python

17. Python for Data Science

18. Loops in Python

19. Python RegEx

20. Machine Learning Projects

21. Arrays in Python

22. Sets in Python

23. Multithreading in Python

24. Python Interview Questions

25. Java vs Python

26. How To Become A Python Developer?

27. Python Lambda Functions

28. How Netflix uses Python?

29. What is Socket Programming in Python

30. Python Database Connection

31. Golang vs Python

32. Python Seaborn Tutorial

33. Python Career Opportunities

Originally published at www.edureka.co on January 11, 2019.

Python
Python Projects
Python Programming
Machine Learning
Machine Learning Projects
Recommended from ReadMedium
avatarAbhay Kumar
OOPs in Python

An easy guide

10 min read