{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 6-Machine Learning (QBoost) with Quantum Annealing" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/OpenJij/OpenJijTutorial/blob/master/source/en/006-Machine_Learning_by_QA.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this section, we describe machine lerning (ML) as an example of an application of quantum annealing (QA) optimization.\n", "\n", "In the first, we show clustering using PyQUBO and OpenJij. \n", "In the seconde, we execute an ensamble study called QBoost with PyQUBO and D-Wave sampler." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clustering\n", "\n", "Clustering is the task of deviding given set of data into $n$ clusters ($n$ is our input). For the sake of simplicity, let us consider the number of cluster is 2 in this time.\n", "\n", "### Importing the required libraries\n", "\n", "We import scikit-learn library for ML." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# import libraries\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "from sklearn import cluster\n", "import pandas as pd\n", "from scipy.spatial import distance_matrix \n", "from pyqubo import Array, Constraint, Placeholder, solve_qubo\n", "import openjij as oj\n", "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Make artificial data\n", "\n", "In this case, let us generate linearly separable data in a two-dimensional plane artificially." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data = []\n", "label = []\n", "for i in range(100):\n", " # generate 0 to 1 random number\n", " p = np.random.uniform(0, 1)\n", " # set class 1 when certain condition are met, and -1 when it are not met\n", " cls =1 if p>0.5 else -1\n", " # create random numbers following a normal distribution\n", " data.append(np.random.normal(0, 0.5, 2) + np.array([cls, cls]))\n", " label.append(cls)\n", "# formatted as a DataFrame\n", "df1 = pd.DataFrame(data, columns=[\"x\", \"y\"], index=range(len(data)))\n", "df1[\"label\"] = label" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAEGCAYAAABsLkJ6AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAbyElEQVR4nO3df5BdZXkH8O9z91difhjcjSEkwUgXq8EKwi1al7EYnJZGiFWU0RaVak1phw62tQkOVdvaqbPo+Ac/OjQDTKWlagRtUsFR6OJoqKRsmE2ARGtw1OyKkmwDsiG5u3v36R/33uTu3XPvPeee8573fc/5fmYYN/fe3Pvck/V93h/P+x5RVRARUf4UbAdARER2MAEQEeUUEwARUU4xARAR5RQTABFRTnXbDiCKgYEBXb9+ve0wiIi8snfv3qOqurLxca8SwPr16zE6Omo7DCIir4jIT4Me5xQQEVFOMQEQEeUUEwARUU4xARAR5RQTABFRTllLACKyTkQeEZEDIvK0iNxgKxYi6szkVAn7Dj+PyamS7VCoAzbLQGcB/JWqPiEiywDsFZGHVPWAxZiIKKSdYxPYdv9+9BQKmJmbw81XvQGbL1hjOyyKwNoIQFWfVdUnqj+/COAgAP72EHlgcqqEbffvx8mZObxYmsXJmTlsvX9/YiMBjizS4cRGMBFZD+CNAPbYjYSIwhg/dgI9hQJOYu7UYz2FAsaPnUD/0r5Y782RRXqsLwKLyFIA9wP4mKr+KuD5LSIyKiKjR44cST9AIlpg7RmLMTM3N++xmbk5rD1jcaz3NT2yoPmsJgAR6UGl8b9XVb8W9BpV3a6qRVUtrly54CgLIrKgf2kfbr7qDVjUU8Cyvm4s6ing5qveELv3XxtZ1KuNLCh51qaAREQA3AXgoKp+wVYcRNSZzReswdDgAMaPncDaMxbHbvwBcyMLCmZzBDAE4AMANorIWPW/TRbjIaKI+pf24fx1KxJp/GvvZ2JkQcGsjQBUdTcAsfX5ROQmEyMLCuZ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