07 KernelPCA
In [1]:
from sklearn.datasets import make_circles
X, y = make_circles(n_samples=200, random_state=1, noise=0.2, factor=0.1)In [2]:
import pandas as pd
df = pd.DataFrame(X)
df.columns=['a','b']
df["y"]=yIn [3]:
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
.colab-df-container {
display:flex;
flex-wrap:wrap;
gap: 12px;
}
.colab-df-convert {
background-color: #E8F0FE;
border: none;
border-radius: 50%;
cursor: pointer;
display: none;
fill: #1967D2;
height: 32px;
padding: 0 0 0 0;
width: 32px;
}
.colab-df-convert:hover {
background-color: #E2EBFA;
box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);
fill: #174EA6;
}
[theme=dark] .colab-df-convert {
background-color: #3B4455;
fill: #D2E3FC;
}
[theme=dark] .colab-df-convert:hover {
background-color: #434B5C;
box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);
filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));
fill: #FFFFFF;
}
const buttonEl =
document.querySelector('#df-8700022c-31b5-4186-8940-3bf0e3152a79 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-8700022c-31b5-4186-8940-3bf0e3152a79');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'data table notebook'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
| a | b | y | |
|---|---|---|---|
| 0 | -0.923942 | -0.549919 | 0 |
| 1 | -0.711498 | 0.935600 | 0 |
| 2 | -0.522033 | 0.938976 | 0 |
| 3 | -0.030860 | -0.143288 | 1 |
| 4 | -0.101950 | 0.040080 | 1 |
| ... | ... | ... | ... |
| 195 | 0.044067 | -0.176675 | 1 |
| 196 | 0.077252 | 0.335312 | 1 |
| 197 | -0.508993 | -0.829551 | 0 |
| 198 | 0.202898 | -0.016087 | 1 |
| 199 | -0.769211 | 0.480595 | 0 |
200 rows × 3 columns
In [4]:
import seaborn as sns
sns.scatterplot(data=df,x='a',y='b', hue="y")<matplotlib.axes._subplots.AxesSubplot at 0x7f94e3bd1750>
In [5]:
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)In [6]:
df = pd.DataFrame(X_pca)
df.columns=["pc1","pc2"]
df['y']=y
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
.colab-df-container {
display:flex;
flex-wrap:wrap;
gap: 12px;
}
.colab-df-convert {
background-color: #E8F0FE;
border: none;
border-radius: 50%;
cursor: pointer;
display: none;
fill: #1967D2;
height: 32px;
padding: 0 0 0 0;
width: 32px;
}
.colab-df-convert:hover {
background-color: #E2EBFA;
box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);
fill: #174EA6;
}
[theme=dark] .colab-df-convert {
background-color: #3B4455;
fill: #D2E3FC;
}
[theme=dark] .colab-df-convert:hover {
background-color: #434B5C;
box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);
filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));
fill: #FFFFFF;
}
const buttonEl =
document.querySelector('#df-6b6c7c21-07d0-49a6-8e8a-8d3088be1839 button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-6b6c7c21-07d0-49a6-8e8a-8d3088be1839');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'data table notebook'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
| pc1 | pc2 | y | |
|---|---|---|---|
| 0 | -0.936050 | -0.560885 | 0 |
| 1 | -0.731019 | 0.925676 | 0 |
| 2 | -0.541573 | 0.929997 | 0 |
| 3 | -0.045007 | -0.149804 | 1 |
| 4 | -0.117011 | 0.033207 | 1 |
| ... | ... | ... | ... |
| 195 | 0.030085 | -0.182817 | 1 |
| 196 | 0.060716 | 0.329329 | 1 |
| 197 | -0.519711 | -0.838443 | 0 |
| 198 | 0.188113 | -0.021438 | 1 |
| 199 | -0.786461 | 0.470388 | 0 |
200 rows × 3 columns
In [8]:
sns.scatterplot(data=df,x='pc1',y='pc2',hue="y")<matplotlib.axes._subplots.AxesSubplot at 0x7f94e0872e50>
Il grafico PCA mostra che assomiglia molto ai dati originali e non c'è una linea che possa separare i dati di due gruppi.
In [9]:
from sklearn.decomposition import KernelPCA
kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=10, n_components=2)
X_kpca = kpca.fit_transform(X)In [10]:
df = pd.DataFrame(X_kpca)
df.columns=["kpc1","kpc2"]
df['y']=y
df
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
.colab-df-container {
display:flex;
flex-wrap:wrap;
gap: 12px;
}
.colab-df-convert {
background-color: #E8F0FE;
border: none;
border-radius: 50%;
cursor: pointer;
display: none;
fill: #1967D2;
height: 32px;
padding: 0 0 0 0;
width: 32px;
}
.colab-df-convert:hover {
background-color: #E2EBFA;
box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);
fill: #174EA6;
}
[theme=dark] .colab-df-convert {
background-color: #3B4455;
fill: #D2E3FC;
}
[theme=dark] .colab-df-convert:hover {
background-color: #434B5C;
box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);
filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));
fill: #FFFFFF;
}
const buttonEl =
document.querySelector('#df-ca2553a3-78a5-4f41-b7c8-c9a1f704905a button.colab-df-convert');
buttonEl.style.display =
google.colab.kernel.accessAllowed ? 'block' : 'none';
async function convertToInteractive(key) {
const element = document.querySelector('#df-ca2553a3-78a5-4f41-b7c8-c9a1f704905a');
const dataTable =
await google.colab.kernel.invokeFunction('convertToInteractive',
[key], {});
if (!dataTable) return;
const docLinkHtml = 'Like what you see? Visit the ' +
'data table notebook'
+ ' to learn more about interactive tables.';
element.innerHTML = '';
dataTable['output_type'] = 'display_data';
await google.colab.output.renderOutput(dataTable, element);
const docLink = document.createElement('div');
docLink.innerHTML = docLinkHtml;
element.appendChild(docLink);
}
| kpc1 | kpc2 | y | |
|---|---|---|---|
| 0 | -0.336612 | -0.026161 | 0 |
| 1 | -0.312302 | -0.011878 | 0 |
| 2 | -0.348684 | -0.013925 | 0 |
| 3 | 0.567221 | -0.194443 | 1 |
| 4 | 0.614309 | -0.230115 | 1 |
| ... | ... | ... | ... |
| 195 | 0.478055 | 0.015917 | 1 |
| 196 | 0.083302 | 0.351990 | 1 |
| 197 | -0.350440 | -0.046757 | 0 |
| 198 | 0.388488 | 0.609122 | 1 |
| 199 | -0.303656 | -0.023271 | 0 |
200 rows × 3 columns
In [12]:
sns.scatterplot(data=df,x='kpc1',y='kpc2', hue="y")<matplotlib.axes._subplots.AxesSubplot at 0x7f94e07ba950>
possiamo vedere chiaramente che i dati sono linearmente separabili