{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 从 0 开始的线性回归" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:57:33.469430Z", "start_time": "2019-07-03T21:57:31.594438Z" }, "attributes": { "classes": [], "id": "", "n": "1" } }, "outputs": [], "source": [ "%matplotlib inline\n", "import d2l\n", "from mxnet import autograd, np, npx\n", "import random\n", "npx.set_np()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "使用如下创建标号\n", "$$\\mathbf{y}= \\mathbf{X} \\mathbf{w} + b + \\mathbf\\epsilon$$\n", "这里 $\\mathbf{w} = [2, -3.4]^\\top$ 和 $b = 4.2$。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:57:33.483583Z", "start_time": "2019-07-03T21:57:33.472382Z" }, "attributes": { "classes": [], "id": "", "n": "2" } }, "outputs": [], "source": [ "def synthetic_data(w, b, num_examples):\n", " X = np.random.normal(0, 1, (num_examples, len(w)))\n", " y = np.dot(X, w) + b\n", " y += np.random.normal(0, 0.01, y.shape)\n", " return X, y\n", "\n", "true_w = np.array([2, -3.4])\n", "true_b = 4.2\n", "features, labels = synthetic_data(true_w, true_b, 1000)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "查看创建的数据。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:57:33.993865Z", "start_time": "2019-07-03T21:57:33.486016Z" }, "attributes": { "classes": [], "id": "", "n": "3" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "features: [2.2122064 0.7740038] \n", "label: 6.000587\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n" ], "text/plain": [ "