In this example the pyvi library was applied to create the graph. You can touch the nodes and move the structure. This library creates an html file that you can move and also if you want to locate examples like this. Just compile it in your python framework and the generation of html file add it in your own server.

  
 
from pyvis.network import Network
import networkx as nx
nx_graph = nx.cycle_graph(10)
nx_graph.nodes[1]['title'] = 'Number 1'
nx_graph.nodes[1]['group'] = 1
nx_graph.nodes[3]['title'] = 'I belong to a different group!'
nx_graph.nodes[3]['group'] = 10
nx_graph.add_node(20, size=20, title='couple', group=2)
nx_graph.add_node(21, size=15, title='couple', group=2)
nx_graph.add_edge(20, 21, weight=5)
nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3, color=  '#3155a8')
nt = Network('500px', '500px')
# populates the nodes and edges data structures
nt.from_nx(nx_graph)
nt.show('nx.html')

  
Girl in a jacket

Numpy library and use

The NumPy library supports all the default Python data types in addition to some of its intrinsic data types. This means that the default Python data types, e.g., strings, integers, floats, Booleans, and complex data types, can be stored in NumPy arrays. You can check the data type in a NumPy array using the dtypeprperty.

  
 
import numpy as np
my_array = np.array([ 10 , 12 , 14 , 16 , 20 , 25 ])
print (my_array)
print (my_array.dtype)
print (my_array.dtype.itemsize)
  

An array is a type of structure that already can be converted to the numpy type.

Why to do this? Because it had different working things that will help to work with this type of data. In this case you can obtain the type with dtype and also obtain the size of the type of the array. When we work with more data for example a list of values from a data base this has more meaning.

  
zeros_array = np.zeros(( 6 , 4 ))
print (zeros_array)
  

This example will generate a matrix of 6 cols and 4 rows, the zero type arrays or matrices will help to give values on this particular positions.

  
Output:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
  

The random.rand() function from the NumPy module can be used to create a NumPy array with uniform distribution.

  
uniform_random = np.random.rand( 4 , 5 )
print (uniform_random)
  

The random.randint() function from the NumPy module can be used to create a NumPy array with random integers between a certain range. The first parameter to the randint() function specifies the lower bound, the second parameter specifies the upper bound, and the last parameter specifies the number of random integers to generate between the range.

  
integer_random = np.random.randint( 10 , 50 , 5 )
print (integer_random)
  

To obtain the size of an array that is really important to go through it with our python code this returns it size:

  
my_array=[10 12 14 16 20 25]
print (my_array.ndim)
Output:

(6,)

To implement the backpropagation, we define a find_derivatives() function, as follows:

  
def find_derivatives (w, b, X):
zh = np.dot(X,w[ 0 ]) + b[ 0 ]
ah = sigmoid(zh)
zo = np.dot(ah, w[ 1 ]) + b[ 1 ]
ao = sigmoid(zo)
# Backpropagation phase 1
m = y.shape[ 0 ]
dcost_dao = ( 1 /m)*(ao-y)
dao_dzo = sigmoid_der(zo)
dzo_dwo = ah.T
dwo= np.dot(dzo_dwo, dcost_dao * dao_dzo)
dbo = np.sum(dcost_dao * dao_dzo)
# Backpropagation phase 2
# dcost_wh = dcost_dah * dah_dzh * dzh_dwh
# dcost_dah = dcost_dzo * dzo_dah
dcost_dzo = dcost_dao * dao_dzo
dzo_dah = w[ 1 ].T
dcost_dah = np.dot(dcost_dzo, dzo_dah)
dah_dzh = sigmoid_der(zh)
dzh_dwh = X.T
dwh = np.dot(dzh_dwh, dah_dzh * dcost_dah)
dbh = np.sum(dah_dzh * dcost_dah)
return dwh, dbh, dwo, dbo

elizabeth morales munoz

Doubly linked lists

A doubly linked list is a singly linked list with two pointers a pointer to the next node and a pointer to the previous node.

  

class Node(object):
    def __init__ (self, data=None, next=None, prev=None):
        self.data = data
        self.next = next
        self.prev = prev


class DoublyLinkedList(object):
    def init (self):
        self.head = None
        self.tail = None
        self.count = 0


    def append(self, data):
        """ Append an item to the list. """
        new_node = Node(data, None, None)
        if self.head is None:
            self.head = new_node
            self.tail = self.head
        else:
            new_node.prev = self.tail
            self.tail.next = new_node
            self.tail = new_node
            self.count += 1

    def delete(self, data):
        """ Delete a node from the list. """
        current = self.head
        node_deleted = False
        if current is None: #Item to be deleted is not found in the
      #  list
            node_deleted = False
        elif current.data == data: #Item to be deleted is found at
      #  starting of list
            self.head = current.next
            self.head.prev = None
            node_deleted = True
        elif self.tail.data == data: #Item to be deleted is found at the
     #   end of list.
            self.tail = self.tail.prev
            self.tail.next = None
            node_deleted = True
        else:
            while current: #search item to be deleted, and delete
    #    that node
                if current.data == data:
                    current.prev.next = current.next
                    current.next.prev = current.prev
                    node_deleted = True
                    current = current.next
        if node_deleted:
            self.count -= 1

        
    def contain(self, data):
        for node_data in self.iter():
            if data == node_data:
                return True
        return False


a = DoublyLinkedList()
a.init()
n1= Node(4)
n2= Node(7)
n3= Node(8)

n1.next=n2
n2.next=n3
n2.prev=n1
n3.prev=n2

a.append(n1)
a.append(n2)
a.append(n3)

a.delete(n1)

current = a.head.data
while current:
    print(current.data)
    current = current.next

  

K-means algorithm

The k-means algorithm uses the mean points in a given dataset to cluster and discover groups within the dataset. The K is the number of clusters that we want and are hoping to discover. After the k-means algorithm has generated the groupings/clusters, we can pass unknown data to this model to predict which cluster the new data should belong to.


import numpy as np
import matplotlib.pyplot as plt
original_set = -2 * np.random.rand(100, 2)
second_set = 1 + 2 * np.random.rand(50, 2)
original_set[50: 100, :] = second_set
from sklearn.cluster import KMeans
kmean = KMeans(n_clusters=2)
kmean.fit(original_set)
#print(kmean.cluster_centers_)
#print(kmean.labels_)
plt.plot(kmean.cluster_centers_,
'*', c='r', ms=10)