Posts

Artificial neural networks

 ## defining architecture def softmax(a):     ea = np.exp(a)     return ea/np.sum(ea,axis=1,keepdims=True) class NeuralNetwork:     #constructo         def __init__(self,input_size,layers,output_size):             np.random.seed(0)# so that we are able to reproduce the results every time             model = {}             model['W1'] = np.random.randn(input_size,layers[0])             model['b1'] = np.zeros((1,layers[0]))             model['W2'] = np.random.randn(layers[0],layers[1])             model['b2'] = np.zeros((1,layers[1]))             model['W3'] = np.random.randn(layers[1],output_size)             model['b3'] = np.zeros((1,output_size))             self.mod...

CORRELATION BETWEEN TWO NUMPY ARRAYS

""" corr gives the correlation of these parameters. """   sigx = np.sqrt(np.mean((X-X.mean())**2))  sigy = np.sqrt(np.mean((Y-Y.mean())**2))  corr = np.mean((X-X.mean())*(Y-Y.mean()))/(sigx*sigy) """ THIS IS HOW YOU CALCULATE CORRELATION BETWEEN FEATURES. """

K MEANS E AND M STEP (image segmentation also)

Image
 import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_blobs X,y = make_blobs(n_samples=500,n_features=2,centers=5,random_state=13) print(X.shape,y.shape) plt.figure(0) plt.scatter(X[:,0],X[:,1],cmap=y) plt.grid(True) plt.show() #this is an unsupervised learning algorithm we are not going to give y values but only x #we will give y later for accuracy color = ['red','green','yellow','orange','blue'] k=5 clusters={} for kx in range(k):     center = 10*(2*np.random.random((X.shape[1],))-1)     #this generates 5 vectors in the range(-10,10)     points = []     cluster = {         'center':center,         'color':color[kx],         'points':points       }     clusters[kx] = cluster           def distance(v1,v2):     return np.sqrt(np.sum((v1-v2)**2)) """ K means is an special case of expectati...