用上帝的戀愛公式講線性迴歸-上

上帝他老人家手裏有一條神奇的戀愛公式!特別神奇,只要你告訴他怎麼追妹子的,他就能告訴你將在多少天以後追上妹子!

接下來我們將用代碼把整個過程描述出來:準備好了嗎?

準備好開發環境

!pip install torch
#安裝torch模塊

輸出如下:

Requirement already satisfied: torch in /Users/mac/anaconda3/envs/pytorch_learn/lib/python3.7/site-packages (1.0.1.post2)
!pip install sklearn
!安裝sklearn模塊

輸出如下:

Collecting sklearn
Collecting scikit-learn (from sklearn)
[?25l  Downloading https://files.pythonhosted.org/packages/bd/1a/5dc99dfa7e7bf2e1f22a07f440b61299148a42d13dee1d5153360313ed67/scikit_learn-0.20.3-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (8.0MB)
[K    100% |████████████████████████████████| 8.0MB 361kB/s ta 0:00:011
[?25hRequirement already satisfied: numpy>=1.8.2 in /Users/mac/anaconda3/envs/pytorch_learn/lib/python3.7/site-packages (from scikit-learn->sklearn) (1.16.2)
Collecting scipy>=0.13.3 (from scikit-learn->sklearn)
[?25l  Downloading https://files.pythonhosted.org/packages/dd/6c/ccf7403d14f0ab0f20ce611696921f204f4ffce99a4fd383c892a6a7e9eb/scipy-1.2.1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (27.3MB)
[K    100% |████████████████████████████████| 27.3MB 104kB/s ta 0:00:011    55% |█████████████████▉              | 15.3MB 360kB/s eta 0:00:34    99% |████████████████████████████████| 27.2MB 134kB/s eta 0:00:01    99% |████████████████████████████████| 27.3MB 123kB/s eta 0:00:01
[?25hInstalling collected packages: scipy, scikit-learn, sklearn
Successfully installed scikit-learn-0.20.3 scipy-1.2.1 sklearn-0.0
import torch
from torch.utils.data import Dataset,DataLoader
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as I

切換成上帝的身份

#顏值分數
appearance=torch.randint(low=60,high=90,size=(1000,1),)
#讓女朋友不開心的次數
unhappy=torch.randint(low=1,high=10,size=(1000,1),)
#上自習學習的次數
study=torch.randint(low=1,high=30,size=(1000,1),)
#一起遊戲的次數
game=torch.randint(low=1,high=50,size=(1000,1),)
features = torch.cat((appearance, unhappy, study, game), dim=1).type(torch.FloatTensor)
#前三條數據
features[:3]

輸出如下:

tensor([[81.,  7., 27., 40.],
        [69.,  6., 14., 37.],
        [87.,  7., 15.,  8.]])

上帝手中的真實公式是這樣的y=0.5appearance+3.8unhappy+0.1study+0.2game+by = 0.5*appearance + 3.8 * unhappy + 0.1*study + 0.2*game + b
執行中的偏差項b是1

true_w = [0.5, 3.8, 0.1,  0.1]
true_b = 1
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_w[2] * features[:, 2]+ true_w[3] * features[:, 3] + true_b
labels = labels.unsqueeze(dim=1).type(torch.FloatTensor)
#前三條的lable
labels[:3]

輸出如下:

tensor([[74.8000],
        [63.4000],
        [73.4000]])
features.shape, labels.shape

輸出如下:

(torch.Size([1000, 4]), torch.Size([1000, 1]))

以上操作相當於建立了如下數據:

數據

切換成數據科學家的視角

好了,你現在是一名凡人了,所以你是不知道上帝手裏的公式是什麼樣的,你只能靠觀察,然後去猜。
這裏我提出一個問題,你觀察的數據會不會有噪音呢?
一定會有偏差啊,假設科比真的用了19天追求到了他的女朋友,可是你在做調查問卷的時候,他記錯了,他記成了20,科比的顏值在上帝那裏是71,在你採集數據的時候可能採集成70,這都是有可能的。這個沒有辦法,作爲一名凡人平靜接受就好了。

so,你真是拿到的數據集還需要加一下噪音:

noise = torch.randn(size=(1000,1))
noise[:3]

輸出如下:

tensor([[-0.7166],
        [ 0.4368],
        [-0.0055]])
labels = labels + noise
labels[:3]

輸出如下:

tensor([[75.4070],
        [63.9435],
        [72.6591]])

只是爲了演示,所以我並未給features中添加噪音。那最後作爲數據科學家你手裏獲得的數據是這個樣子的:

數據

加載數據

class myDataset(Dataset):
    def __init__(self, features, lables):
        self.features = features
        self.labels = labels

    def __getitem__(self, index):
        sample = (self.features[index], labels[index])
        
        return sample
    def __len__(self):
        return len(self.features)
batch_size = 5
train_features = features[:500]
train_labels = labels[:500]

test_features = features[500:]
test_labels = labels[500:]

train_dataset = myDataset(train_features, train_labels)
train_data_iter = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
test_dataset = myDataset(test_features, test_labels)
test_data_iter = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
#看到了嗎?這個batch_size和剛纔講mse中的點相關。
for f, l in train_data_iter:
    print("features: {} labels: {}".format(f,l))
    break

輸出如下:

features: tensor([[71.,  6., 22., 24.],
        [79.,  6., 26., 45.],
        [88.,  7., 11., 14.],
        [79.,  3., 13., 34.],
        [69.,  5.,  5., 41.]]) labels: tensor([[65.3587],
        [68.8864],
        [72.7446],
        [54.5244],
        [58.2802]])

好了,到此數據就準備好了,接下來我們將用線性迴歸破解上帝手中的公式了。

用上帝的戀愛公式講線性迴歸-下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章