- Hands-On Neural Networks
- Leonardo De Marchi Laura Mitchell
- 230字
- 2021-06-24 14:00:16
The XOR problem
Let's try to solve the XOR problem we presented earlier with a simple FFNN, by performing the following steps:
- First of all, let's import everything we will need for this task and seed our random function:
import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_auc_score
from sklearn.metrics import mean_squared_error
import matplotlib
matplotlib.use("TkAgg")
# initiating random number
np.random.seed(11)
- To make it more similar to a real-word problem, we will add some noise to the XOR input, and we will try to predict a binary task:
#### Creating the dataset
# mean and standard deviation for the x belonging to the first class
mu_x1, sigma_x1 = 0, 0.1
# Constant to make the second distribution different from the first
# x1_mu_diff, x2_mu_diff, x3_mu_diff, x4_mu_diff = 0.5, 0.5, 0.5, 0.5
x1_mu_diff, x2_mu_diff, x3_mu_diff, x4_mu_diff = 0, 1, 0, 1
# creating the first distribution
d1 = pd.DataFrame({'x1': np.random.normal(mu_x1, sigma_x1,
1000) + 0,
'x2': np.random.normal(mu_x1, sigma_x1,
1000) + 0,'type': 0})
d2 = pd.DataFrame({'x1': np.random.normal(mu_x1, sigma_x1,
1000) + 1,
'x2': np.random.normal(mu_x1, sigma_x1,
1000) - 0,'type': 1})
d3 = pd.DataFrame({'x1': np.random.normal(mu_x1, sigma_x1,
1000) - 0,
'x2': np.random.normal(mu_x1, sigma_x1,
1000) - 1,'type': 0})
d4 = pd.DataFrame({'x1': np.random.normal(mu_x1, sigma_x1,
1000) - 1,
'x2': np.random.normal(mu_x1, sigma_x1,
1000) + 1, 'type': 1})
data = pd.concat([d1, d2, d3, d4], ignore_index=True)
In this way, we will get a noisy XOR, as shown in the following screenshot:

推薦閱讀
- 繪制進(jìn)程圖:可視化D++語言(第1冊(cè))
- 人工免疫算法改進(jìn)及其應(yīng)用
- Mobile DevOps
- VMware Performance and Capacity Management(Second Edition)
- 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ)
- 變頻器、軟啟動(dòng)器及PLC實(shí)用技術(shù)260問
- Ruby on Rails敏捷開發(fā)最佳實(shí)踐
- Machine Learning with the Elastic Stack
- DevOps Bootcamp
- Applied Data Visualization with R and ggplot2
- 嵌入式操作系統(tǒng)原理及應(yīng)用
- Hands-On Dashboard Development with QlikView
- 傳感器與自動(dòng)檢測
- 21天學(xué)通Linux嵌入式開發(fā)
- 穿越計(jì)算機(jī)的迷霧