# 代码

```from __future__ import print_function, division
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn
from sklearn.cross_validation import train_test_split
import random

# 读取数据
# 拆分数据
X_train, X_test, y_train, y_test = train_test_split(data[["Temperature", "Humidity", "Light", "CO2", "HumidityRatio"]].values, data["Occupancy"].values.reshape(-1, 1), random_state=42)
# one-hot 编码
y_train = tf.concat(1, [1 - y_train, y_train])
y_test = tf.concat(1, [1 - y_test, y_test])

# 设置模型
learning_rate = 0.001
training_epochs = 50
batch_size = 100
display_step = 1

n_samples = X_train.shape[0]
n_features = 5
n_class = 2
x = tf.placeholder(tf.float32, [None, n_features])
y = tf.placeholder(tf.float32, [None, n_class])

# 模型参数
W = tf.Variable(tf.zeros([n_features, n_class]))
b = tf.Variable(tf.zeros([n_class]))
# W = tf.Variable(tf.truncated_normal([n_features, n_class-1]))
# b = tf.Variable(tf.truncated_normal([n_class]))

# 定义模型，此处使用与线性回归一样的定义
# 因为在后面定义损失的时候会加上映射
pred = tf.matmul(x, W) + b

# 定义损失函数
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
# cost = tf.nn.sigmoid_cross_entropy_with_logits(pred, y)

# 梯度下降

# 准确率
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 初始化所有变量
init = tf.initialize_all_variables()

# 训练模型
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(n_samples / batch_size)
for i in range(total_batch):
_, c = sess.run([optimizer, cost],
feed_dict={x: X_train[i * batch_size : (i+1) * batch_size],
y: y_train[i * batch_size : (i+1) * batch_size, :].eval()})
avg_cost = c / total_batch
plt.plot(epoch+1, avg_cost, 'co')

if (epoch+1) % display_step == 0:
print("Epoch:", "%04d" % (epoch+1), "cost=", avg_cost)

print("Optimization Finished!")

print("Testing Accuracy:", accuracy.eval({x: X_train, y:y_train.eval()}))
plt.xlabel("Epoch")
plt.ylabel("Cost")
plt.show()```

# 结果

```Epoch: 0001 cost= 0.402052676091
Epoch: 0002 cost= 0.384176723293
Epoch: 0003 cost= 0.174337043137
Epoch: 0004 cost= 0.131257948328
Epoch: 0005 cost= 0.116865955415
Epoch: 0006 cost= 0.167843505984
Epoch: 0007 cost= 0.0734717650492
Epoch: 0008 cost= 0.134278797712
Epoch: 0009 cost= 0.107930605529
Epoch: 0010 cost= 0.0559994509963
Epoch: 0011 cost= 0.0894105786183
Epoch: 0012 cost= 0.112936254408
Epoch: 0013 cost= 0.0598722950357
Epoch: 0014 cost= 0.0590479530272
Epoch: 0015 cost= 0.085669126667
Epoch: 0016 cost= 0.0516053653154
Epoch: 0017 cost= 0.0587136237348
Epoch: 0018 cost= 0.0668616529371
Epoch: 0019 cost= 0.0612989566365
Epoch: 0020 cost= 0.0527745035828
Epoch: 0021 cost= 0.0758241278226
Epoch: 0022 cost= 0.0845051749808
Epoch: 0023 cost= 0.0364650820122
Epoch: 0024 cost= 0.0526885400053
Epoch: 0025 cost= 0.0451166786131
Epoch: 0026 cost= 0.0508907896573
Epoch: 0027 cost= 0.0619052668087
Epoch: 0028 cost= 0.0560943103227
Epoch: 0029 cost= 0.0425660180264
Epoch: 0030 cost= 0.0601769588033
Epoch: 0031 cost= 0.0461903712789
Epoch: 0032 cost= 0.0437817573547
Epoch: 0033 cost= 0.102960703803
Epoch: 0034 cost= 0.0599972771817
Epoch: 0035 cost= 0.10071516037
Epoch: 0036 cost= 0.101918243971
Epoch: 0037 cost= 0.102948681253
Epoch: 0038 cost= 0.0239826597151
Epoch: 0039 cost= 0.02541697807
Epoch: 0040 cost= 0.039644296052
Epoch: 0041 cost= 0.0564842145951
Epoch: 0042 cost= 0.0651661059895
Epoch: 0043 cost= 0.0559316267733
Epoch: 0044 cost= 0.058336042967
Epoch: 0045 cost= 0.0420891652342
Epoch: 0046 cost= 0.0113296391534
Epoch: 0047 cost= 0.0151269641079
Epoch: 0048 cost= 0.070616901898
Epoch: 0049 cost= 0.0543320648006
Epoch: 0050 cost= 0.0490373939764
Optimization Finished!
Testing Accuracy: 0.973473```

# 与sklearn的比较

```clf = LogisticRegression()
clf.fit(X_train, y_train)
clf.score(X_test, y_test)```

# END

0 条评论

## 相关文章

3629

### Reddit 讨论：Hinton的Capsule网络真的比CNN效果更好吗？

【新智元导读】Hinton 提出用 Capsule 这个概念代替反向传播，引起广泛关注。本文来自Reddit上“Capsule networks为何与众不同，它...

3077

822

941

### kmeans优化算法

①算法可能找到局部最优的聚类，而不是全局最优的聚类。使用改进的二分k-means算法。

963

3489

### 表格属性（特有）

设置边框前提border-collapse取值为sparete（不合并），

863

1424

3639

1793