Advertisement
fevzi02

Untitled

Dec 25th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import numpy as np
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.preprocessing import OneHotEncoder
  4. from sklearn.metrics import classification_report
  5. import matplotlib.pyplot as plt
  6.  
  7. # Function to implement a single-layer neural network
  8. class SingleLayerNeuralNetwork:
  9. def __init__(self, input_size, hidden_size, output_size):
  10. # Initialize weights and biases
  11. self.weights1 = np.random.rand(input_size, hidden_size)
  12. self.weights2 = np.random.rand(hidden_size, output_size)
  13. self.bias1 = np.random.rand(hidden_size)
  14. self.bias2 = np.random.rand(output_size)
  15.  
  16. def sigmoid(self, x):
  17. return 1 / (1 + np.exp(-x))
  18.  
  19. def sigmoid_derivative(self, x):
  20. return x * (1 - x)
  21.  
  22. def forward(self, x):
  23. self.hidden = self.sigmoid(np.dot(x, self.weights1) + self.bias1)
  24. output = self.sigmoid(np.dot(self.hidden, self.weights2) + self.bias2)
  25. return output
  26.  
  27. def backward(self, x, y, output, learning_rate):
  28. error = y - output
  29. d_output = error * self.sigmoid_derivative(output)
  30. error_hidden = d_output.dot(self.weights2.T)
  31. d_hidden = error_hidden * self.sigmoid_derivative(self.hidden)
  32.  
  33. # Update weights and biases
  34. self.weights1 += x.T.dot(d_hidden) * learning_rate
  35. self.weights2 += self.hidden.T.dot(d_output) * learning_rate
  36. self.bias1 += np.sum(d_hidden, axis=0) * learning_rate
  37. self.bias2 += np.sum(d_output, axis=0) * learning_rate
  38.  
  39. def train(self, x, y, epochs, learning_rate):
  40. for epoch in range(epochs):
  41. output = self.forward(x)
  42. self.backward(x, y, output, learning_rate)
  43.  
  44. # Preparing the data
  45. iris_data = np.array(iris_data)
  46. X = iris_data[:, 0:2] # features
  47. y = iris_data[:, 2] # labels
  48.  
  49. # One-hot encode the labels
  50. encoder = OneHotEncoder(sparse=False)
  51. y_encoded = encoder.fit_transform(y.reshape(-1, 1))
  52.  
  53. # Splitting the dataset into training and test sets
  54. X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)
  55.  
  56. # Neural Network parameters
  57. input_size = 2 # two features
  58. hidden_size = 5 # arbitrary choice
  59. output_size = 3 # three classes
  60.  
  61. # Initialize the neural network
  62. nn = SingleLayerNeuralNetwork(input_size, hidden_size, output_size)
  63.  
  64. # Train the neural network
  65. nn.train(X_train, y_train, epochs=1000, learning_rate=0.01)
  66.  
  67. # Predictions for the test set
  68. y_pred = np.array([np.argmax(nn.forward(x)) for x in X_test])
  69.  
  70. # Evaluation
  71. print(classification_report(y_test.argmax(axis=1), y_pred))
  72.  
  73. # Plotting classification map
  74. def plot_classification_map(nn, X, y):
  75. x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
  76. y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
  77. h = 0.01
  78. xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
  79.  
  80. Z = np.array([np.argmax(nn.forward(np.array([xx.ravel()[i], yy.ravel()[i]]))) for i in range(xx.ravel().shape[0])])
  81. Z = Z.reshape(xx.shape)
  82.  
  83. plt.figure(figsize=(10, 6))
  84. plt.contourf(xx, yy, Z, alpha=0.8)
  85. plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', marker='o')
  86. plt.xlabel('Feature 1')
  87. plt.ylabel('Feature 2')
  88. plt.title('Classification Map of Iris Dataset')
  89. plt.show()
  90.  
  91. plot_classification_map(nn, X, y)
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement