introduction-to-deep-learning/Le Deep Learning de A a Z/Part 4 - Self_Organizing_Maps/SOM.ipynb

111 lines
2.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Importing the libraries\n",
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Importing the dataset\n",
"dataset = pd.read_csv('Credit_Card_Applications.csv')\n",
"X = dataset.iloc[:, :-1].values\n",
"y = dataset.iloc[:, -1].values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Feature Scaling\n",
"from sklearn.preprocessing import MinMaxScaler\n",
"sc = MinMaxScaler(feature_range = (0, 1))\n",
"X = sc.fit_transform(X)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Training the SOM\n",
"from minisom import MiniSom\n",
"som = MiniSom(x = 10, y = 10, input_len = 15, sigma = 1.0, learning_rate = 0.5)\n",
"som.random_weights_init(X)\n",
"som.train_random(data = X, num_iteration = 100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Visualizing the results\n",
"from pylab import bone, pcolor, colorbar, plot, show\n",
"bone()\n",
"pcolor(som.distance_map().T)\n",
"colorbar()\n",
"markers = ['o', 's']\n",
"colors = ['r', 'g']\n",
"for i, x in enumerate(X):\n",
" w = som.winner(x)\n",
" plot(w[0] + 0.5,\n",
" w[1] + 0.5,\n",
" markers[y[i]],\n",
" markeredgecolor = colors[y[i]],\n",
" markerfacecolor = 'None',\n",
" markersize = 10,\n",
" markeredgewidth = 2)\n",
"show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Finding the frauds\n",
"mappings = som.win_map(X)\n",
"frauds = np.concatenate((mappings[(8,8)], mappings[(2,6)]), axis = 0)\n",
"frauds = sc.inverse_transform(frauds)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}