![](/img/trans.png)
[英]How to replace scikit-learn (make_circle) to my own dataset?
[英]What does it mean to add gaussian noise = 0.05 in scikit-learn make_circle()? How will it affect the data?
我正在研究神經網絡的超參數調整並通過示例。 我在一個例子中遇到了這段代碼:
train_X, train_Y = sklearn.datasets.make_circles(n_samples=300, noise=.05)
我知道添加噪聲會對數據產生正則化影響。 閱讀文檔說明它增加了高斯噪聲。 但是,在上面的代碼中,我無法理解在數據中添加0.05
噪聲意味着什么。 這將如何在數學上影響數據?
我試過下面的代碼。 我可以看到值發生變化,但無法弄清楚,例如,如何通過將noise= .05
添加到數組 2 中的相應行(即 x_1)來更改數組 1 中 x 的 row1 值?
np.random.seed(0)
x,y = sklearn.datasets.make_circles()
print(x[:5,:])
x_1,y_1 = sklearn.datasets.make_circles(noise= .05)
print(x_1[:5,:])
輸出:
[[-9.92114701e-01 -1.25333234e-01]
[-1.49905052e-01 -7.85829801e-01]
[ 9.68583161e-01 2.48689887e-01]
[ 6.47213595e-01 4.70228202e-01]
[-8.00000000e-01 -2.57299624e-16]]
[[-0.66187208 0.75151712]
[-0.86331995 -0.56582111]
[-0.19574479 0.7798686 ]
[ 0.40634757 -0.78263011]
[-0.7433193 0.26658851]]
根據文檔:
sklearn.datasets。 make_circles (n_samples=100,*,shuffle=True,noise=None,random_state=None,factor=0.8)
在 2d 中制作一個包含小圓的大圓。 一個簡單的玩具數據集,用於可視化聚類和分類算法。噪聲:雙倍或無(默認值=無)添加到數據中的高斯噪聲的標准偏差。
語句make_circles(noise=0.05)
意味着它正在創建遵循高斯分布(也稱為正態分布)的帶有一點變化的隨機圓。 您應該已經知道隨機高斯分布意味着生成的數字具有一定的均值和標准定義。 在這種情況下,調用make_circles(noise=0.05)
意味着標准偏差為 0.05。
讓我們調用這個函數,檢查它的輸出,看看改變參數noise
什么效果。 我將從這個關於生成 scikit-learn 虛擬數據的好教程中大量借鑒。
我們先用noise=0.0
調用make_circles()
,看一下數據。 我將使用 Pandas 數據框,以便我們可以以表格方式查看數據。
from sklearn.datasets import make_circles
import matplotlib.pyplot as plt
import pandas as pd
n_samples = 100
noise = 0.00
features, labels = make_circles(n_samples=n_samples, noise=noise)
df = pd.DataFrame(dict(x=features[:,0], y=features[:,1], label=labels))
print(df.head())
# x y label
# 0 -0.050232 0.798421 1
# 1 0.968583 0.248690 0
# 2 -0.809017 0.587785 0
# 3 -0.535827 0.844328 0
# 4 0.425779 -0.904827 0
您可以看到make_circles
返回數據實例,其中每個實例都是具有兩個特征 x 和 y 以及一個標簽的點。 讓我們繪制它們以查看它們的實際外觀。
# Collect the points together by label, either 0 or 1
grouped = df.groupby('label')
colors = {0:'red', 1:'blue'}
fig, ax = plt.subplots(figsize=(7,7))
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', marker='.', label=key, color=colors[key])
plt.title('Points')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.grid()
plt.show()
所以看起來它正在創建兩個同心圓,每個同心圓都有不同的標簽。
讓我們將噪聲增加到noise=0.05
並查看結果:
n_samples = 100
noise = 0.05 # <--- The only change
features, labels = make_circles(n_samples=n_samples, noise=noise)
df = pd.DataFrame(dict(x=features[:,0], y=features[:,1], label=labels))
grouped = df.groupby('label')
colors = {0:'red', 1:'blue'}
fig, ax = plt.subplots(figsize=(7,7))
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', marker='.', label=key, color=colors[key])
plt.title('Points')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.grid()
plt.show()
看起來噪聲被添加到每個 x、y 坐標中,以使每個點移動一點點。 當我們檢查make_circles()
的代碼時,我們看到實現正是這樣做的:
def make_circles( ..., noise=None, ...):
...
if noise is not None:
X += generator.normal(scale=noise, size=X.shape)
所以現在我們已經看到了數據集的兩個可視化,其中包含兩個noise
值。 但是兩個可視化並不酷。 你知道什么是酷嗎? 噪聲逐漸增加 10 倍的五個可視化。 這是一個執行此操作的函數:
def make_circles_plot(n_samples, noise):
assert n_samples > 0
assert noise >= 0
# Use make_circles() to generate random data points with noise.
features, labels = make_circles(n_samples=n_samples, noise=noise)
# Create a dataframe for later plotting.
df = pd.DataFrame(dict(x=features[:,0], y=features[:,1], label=labels))
grouped = df.groupby('label')
colors = {0:'red', 1:'blue'}
fig, ax = plt.subplots(figsize=(5, 5))
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='x', y='y', marker='.', label=key, color=colors[key])
plt.title('Points with noise=%f' % noise)
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.grid()
plt.tight_layout()
plt.show()
用不同的noise
值調用上面的函數,可以清楚地看到增加這個值會使點移動得更多,即它使它們更“嘈雜”,正如我們直觀地預期的那樣。
for noise in [0.0, 0.01, 0.1, 1.0, 10.0]:
make_circles_plot(500, noise)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.