Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions competitions/getting-started/house-price/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,6 @@ import matplotlib.pyplot as plt

### 特征说明


## 步骤:

一. 数据分析
1. 下载并加载数据
2. 总体预览:了解每列数据的含义,数据的格式等
3. 数据初步分析,使用统计学与绘图:初步了解数据之间的相关性,为构造特征工程以及模型建立做准备

二. 特征工程
1. 根据业务,常识,以及第二步的数据分析构造特征工程.
2. 将特征转换为模型可以辨别的类型(如处理缺失值,处理文本进行等)

三. 模型选择
1. 根据目标函数确定学习类型,是无监督学习还是监督学习,是分类问题还是回归问题等.
2. 比较各个模型的分数,然后取效果较好的模型作为基础模型.

四. 模型融合

五. 修改特征和模型参数
1. 可以通过添加或者修改特征,提高模型的上限.
2. 通过修改模型的参数,是模型逼近上限


## 一. 数据分析

### 数据下载和加载
Expand Down
95 changes: 95 additions & 0 deletions competitions/playground/dogs-vs-cats/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# **猫和狗**

![](/static/images/competitions/playground/dogs-vs-cats.jpg)

> 注意:[项目规范](/docs/kaggle-quickstart.md)

## 比赛说明

* 在本次比赛中,您将编写一个算法来分类图像是否包含狗或猫。这对人类,狗和猫来说很容易。你的电脑会觉得有点困难。

深蓝在1997年在国际象棋比赛中击败卡斯帕罗夫。
沃森在2011 年击败了Jeopardy最聪明的琐事。
你能否在2013年从米登斯那里告诉菲多?

> Asirra数据集

Web服务通常受到人们解决这个难题的挑战,但这对计算机来说很困难。这样的挑战通常被称为 [CAPTCHA](http://www.captcha.net/) (完全自动公开的图灵测试来告诉计算机和人类)或HIP(人类交互证明)。HIP用于多种用途,例如减少电子邮件和博客垃圾邮件,防止对网站密码进行暴力攻击。

[Asirra](http://research.microsoft.com/en-us/um/redmond/projects/asirra/)(限制访问的动物物种图像识别)是一项HIP,通过询问用户识别猫和狗的照片而工作。这项任务对于计算机来说很难,但研究表明人们可以快速准确地完成任务。许多人甚至认为这很有趣!以下是Asirra界面的一个例子:

Asirra是独一无二的,因为它与全球最大的网站 [Petfinder.com](http://www.petfinder.com/) 合作, 致力于为无家可归的宠物寻找住所。他们向微软研究院提供了超过三百万张猫和狗的图像,由美国各地数千个动物收容所的人员手动分类。Kaggle很幸运能够提供这些数据的一个子集,用于娱乐和研究。

> 图像识别攻击

虽然随机猜测是最简单的攻击形式,但各种形式的图像识别可以让攻击者做出比随机更好的猜测。照片数据库(各种各样的背景,角度,姿势,照明等)具有巨大的多样性,难以进行准确的自动分类。在多年前进行的一项非正式调查中,计算机视觉专家认为,如果没有现有技术的重大进展,精度高于60%的分类器将很困难。作为参考,60%分类器将12幅图像HIP的猜测概率从1/4096提高到1/459。

> 最先进的

目前的文献表明机器分类器可以在这项任务上得到80%以上的准确度[1]。因此,Asirra不再被认为是安全的。我们创建了这个比赛,以针对这个问题对最新的计算机视觉和深度学习方法进行基准测试 你能破解CAPTCHA吗?你能改善艺术状态吗?你能在猫狗之间创造持久的和平吗?

好的,我们会解决前者。

> 致谢

我们感谢微软研究院为此次比赛提供数据。

* Jeremy Elson,John R. Douceur,Jon Howell,Jared Saul,Asirra:在计算机和通信安全(CCS)第14届ACM会议论文集计算机械协会会刊上发表的利用调整手动图像分类的CAPTCHA, 2007年10月

## 参赛成员

* 开源组织: [ApacheCN ~ apachecn.org](http://www.apachecn.org/)
* 参与人员: [片刻](https://github.com/jiangzhonglian)

## 比赛分析

* 回归问题:价格的问题
* 常用算法: `回归`、`树回归`、`GBDT`、`xgboost`、`lightGBM`

```
步骤:
一. 数据分析
1. 下载并加载数据
2. 总体预览:了解每列数据的含义,数据的格式等
3. 数据初步分析,使用统计学与绘图:初步了解数据之间的相关性,为构造特征工程以及模型建立做准备

二. 特征工程
1.根据业务,常识,以及第二步的数据分析构造特征工程.
2.将特征转换为模型可以辨别的类型(如处理缺失值,处理文本进行等)

三. 模型选择
1.根据目标函数确定学习类型,是无监督学习还是监督学习,是分类问题还是回归问题等.
2.比较各个模型的分数,然后取效果较好的模型作为基础模型.

四. 模型融合

五. 修改特征和模型参数
1.可以通过添加或者修改特征,提高模型的上限.
2.通过修改模型的参数,是模型逼近上限

六. 提交格式
'''(label: 1=dog, 0=cat)
id,label
1,0
2,0
3,0
etc...
'''
```

## 一. 数据分析

### 数据下载和加载

> 数据获取

* 数据集下载地址:<https://www.kaggle.com/c/dogs-vs-cats/data>

> 特征说明

* Dogs vs. Cats是一个传统的二分类问题。
* 训练集包含25000张图片,命名格式为<category>.<num>.jpg, 如cat.10000.jpg、dog.100.jpg
* 测试集包含12500张图片,命名为<num>.jpg,如1000.jpg。
* 参赛者需根据训练集的图片训练模型,并在测试集上进行预测,输出它是狗的概率。
* 最后提交的csv文件如下,第一列是图片的<num>,第二列是图片为狗的概率。

183 changes: 183 additions & 0 deletions src/python/getting-started/digit-recognizer/pytorch_cnn-python3.6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#!/usr/bin/python3
# coding: utf-8
'''
Created on 2017-12-18
Update on 2018-03-27
Author: 片刻
Github: https://github.com/apachecn/kaggle
Result:
BATCH_SIZE = 10 and EPOCH = 10; [10, 4000] loss: 0.069
BATCH_SIZE = 10 and EPOCH = 15; [10, 4000] loss: 0.069
'''
# import csv
import pandas as pd

# third-party library
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader


class CustomedDataSet(Dataset):
def __init__(self, train=True):
self.train = train
if self.train:
trainX = pd.read_csv(
'/opt/data/kaggle/getting-started/digit-recognizer/input/train.csv'
# names=["ImageId", "Label"]
)
trainY = trainX.label.as_matrix().tolist()
trainX = trainX.drop(
'label', axis=1).as_matrix().reshape(trainX.shape[0], 1, 28, 28)
self.datalist = trainX
self.labellist = trainY
else:
testX = pd.read_csv(
'/opt/data/kaggle/getting-started/digit-recognizer/input/test.csv'
)
self.testID = testX.index
testX = testX.as_matrix().reshape(testX.shape[0], 1, 28, 28)
self.datalist = testX

def __getitem__(self, index):
if self.train:
return torch.Tensor(
self.datalist[index].astype(float)), self.labellist[index]
else:
return torch.Tensor(self.datalist[index].astype(float))

def __len__(self):
return self.datalist.shape[0]


train_data = CustomedDataSet()
test_data = CustomedDataSet(train=False)

BATCH_SIZE = 150
train_loader = DataLoader(
dataset=train_data, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)
test_loader = DataLoader(
dataset=test_data, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)


class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Sequential( # input shape (1, 28, 28)
nn.Conv2d(
in_channels=1, # input height
out_channels=16, # n_filters
kernel_size=5, # filter size
stride=1, # filter movement/step
padding=2, # if want same width and length of this image after con2d, padding=(kernel_size-1)/2 if stride=1
), # output shape (16, 28, 28)
nn.ReLU(), # activation
nn.MaxPool2d(
kernel_size=2
), # choose max value in 2x2 area, output shape (16, 14, 14)
)
self.conv2 = nn.Sequential( # input shape (1, 14, 14)
nn.Conv2d(16, 32, 5, 1, 2), # output shape (32, 14, 14)
nn.ReLU(), # activation
nn.MaxPool2d(2), # output shape (32, 7, 7)
)
self.out = nn.Linear(32 * 7 * 7,
10) # fully connected layer, output 10 classes

def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(
x.size(0),
-1) # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
output = self.out(x)
return output, x # return x for visualization


cnn = CNN()
# print(cnn) # net architecture

LR = 0.001 # learning rate
optimizer = torch.optim.Adam(
cnn.parameters(), lr=LR) # optimize all cnn parameters
loss_func = nn.CrossEntropyLoss() # the target label is not one-hotted

# training and testing
print(u'开始训练')
EPOCH = 5 # train the training data n times, to save time, we just train 1 epoch
for epoch in range(EPOCH):
running_loss = 0.0

for step, (x, y) in enumerate(
train_loader
): # gives batch data, normalize x when iterate train_loader
b_x = Variable(x) # batch x
b_y = Variable(y) # batch y

output = cnn(b_x)[0] # 输入训练数据
loss = loss_func(output, b_y) # 计算误差
optimizer.zero_grad() # 清空上一次梯度
loss.backward() # 误差反向传递
optimizer.step() # 优化器参数更新

# 每1000批数据打印一次平均loss值
running_loss += loss.data[
0] # loss本身为Variable类型,所以要使用data获取其Tensor,因为其为标量,所以取0
if step % 500 == 499: # 每2000批打印一次
print('[%d, %5d] loss: %.3f' %
(epoch + 1, step + 1, running_loss / 500))
running_loss = 0.0
print('Finished Training')

# correct = 0
# total = 0
# for img, label in test_loader:
# img = Variable(img, volatile=True)
# label = Variable(label, volatile=True)

# outputs = cnn(img)
# _, predicted = torch.max(outputs[0], 1)
# # print('1-', type(label), '-------', label)
# # print('2-', type(predicted), '-------', predicted)
# total += label.size(0)
# num_correct = (predicted == label).sum()
# correct += num_correct.data[0]

# print('Accuracy of the network on the %d test images: %.3f %%' % (total, 100 * correct / total))

# I just can't throw all of test data into the network,since it was so huge that my GPU memory cann't afford it
ans = torch.LongTensor() # build a tensor to concatenate answers
for img in test_loader:
img = Variable(img)
outputs = cnn(img)
_, predicted = torch.max(outputs[0], 1)
# print('type(predicted) = ', type(predicted), predicted)
ans = torch.cat([ans, predicted.data], 0)

testLabel = ans.numpy() # only tensor on cpu can transform to the numpy array

# # 结果输出保存
# def saveResult(result, csvName):
# with open(csvName, 'w') as myFile:
# myWriter = csv.writer(myFile)
# myWriter.writerow(["ImageId", "Label"])
# index = 0
# for r in result:
# index += 1
# myWriter.writerow([index, int(r)])

# print('Saved successfully...') # 保存预测结果

# saveResult(testLabel,
# '/opt/data/kaggle/getting-started/digit-recognizer/output/Result_pytorch_CNN.csv')

# 提交结果
submission_df = pd.DataFrame(
data={'ImageId': test_data.testID+1,
'Label': testLabel})
# print(submission_df.head(10))
submission_df.to_csv(
'/opt/data/kaggle/getting-started/digit-recognizer/output/Result_pytorch_CNN.csv',
columns=["ImageId", "Label"],
index=False)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.