""" Tutorial Machine Learning II - 19.04.2018 (Single Layer Perceptron) """ import numpy as np import matplotlib.pyplot as plt def generate_blobs(n_Data, cluster_label, n_dim, distance, shift): n_Cluster = cluster_label.size n_Classes = len(set(cluster_label)) x=np.zeros([n_Data,n_dim],dtype=float) label=np.zeros([n_Data,n_Classes],dtype=int) cov=np.zeros([n_dim,n_dim],dtype=float) np.fill_diagonal(cov,1) for i in range(n_Cluster): start=int(np.ceil(n_Data/float(n_Cluster)*i)) finish=int(np.floor(n_Data/float(n_Cluster)*(i+1))) x[start:finish,:]=np.random.multivariate_normal([(i//2)*distance,(i%2)*distance-shift],cov,size=n_Data//n_Cluster) label[start:finish,cluster_label[i]]=1 return x, label def initialize_weights(n_input,n_output): return np.random.normal(0,0.01,[n_input, n_output]) def feedforward(x,w1): ################ # your code here ################ return activation def train(x,w1,label,eps): ################ # your code here ################ return w1 ############################################################################## # MAIN # Settings: cluster_label=np.array([0,1]) n_Data_per_Cluster = 1000 dim = 2 distance = 20 shift = 10 learning_rate = 0.01 #### n_Cluster=cluster_label.size n_Data = n_Data_per_Cluster * n_Cluster x,label = generate_blobs(n_Data, cluster_label, dim, distance, shift) s = np.arange(x.shape[0]) np.random.shuffle(s) x = x[s,:] label = label[s,:] # Plot: plt.figure() plt.scatter(x[:,0],x[:,1], c=np.argmax(label,axis=1)) plt.show