import requests
import numpy as np
複製**
r = requests.get('')
複製**
with open('iris.data', 'w') as f:
f.write(r.text)
複製**
import pandas as pd
複製**
data = pd.read_csv('iris.data', names =['e_cd', 'e_kd', 'b_cd', 'b_kd', 'cat'])
複製**
data.head(5)
複製**
e_cd
e_kd
b_cd
b_kd
cat0
5.13.5
1.40.2
iris-setosa
14.9
3.01.4
0.2iris-setosa
24.7
3.21.3
0.2iris-setosa
34.6
3.11.5
0.2iris-setosa
45.0
3.61.4
0.2iris-setosa
data.cat.unique()
複製**
array(['iris-setosa', 'iris-versicolor', 'iris-virginica'], dtype=object)
複製**
data['c1'] = np.array(data['cat'] == 'iris-setosa').astype(np.float32)
data['c2'] = np.array(data['cat'] == 'iris-versicolor').astype(np.float32)
data['c3'] = np.array(data['cat'] == 'iris-virginica').astype(np.float32)
複製**
target = np.stack([data.c1.values, data.c2.values, data.c3.values]).t
複製**
shuju = np.stack([data.e_cd.values, data.e_kd.values, data.b_cd.values, data.b_kd.values]).t
複製**
np.shape(shuju), np.shape(target)
複製**
((150, 4), (150, 3))
複製**
import tensorflow as tf
複製**
/anaconda3/envs/py35/lib/python3.5/importlib/_bootstrap.py:222: runtimewarning: compiletime version 3.6 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
return f(*args, **kwds)
/anaconda3/envs/py35/lib/python3.5/site-packages/h5py/__init__.py:36: futurewarning: conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. in future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
複製**
x = tf.placeholder("float", shape=[none, 4])
y = tf.placeholder("float", shape=[none, 3])
複製**
weight = tf.variable(tf.truncated_normal([4,3]))
bias = tf.variable(tf.truncated_normal([3]))
複製**
combine_input = tf.matmul(x, weight) + bias
複製**
pred = tf.nn.softmax(combine_input)
複製**
y.get_shape(), pred.get_shape()
複製**
(tensorshape([dimension(none), dimension(3)]),
tensorshape([dimension(none), dimension(3)]))
複製**
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=combine_input))
複製**
warning:tensorflow:from :1: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
instructions for updating:
future major versions of tensorflow will allow gradients to flow
into the labels input on backprop by default.
see tf.nn.softmax_cross_entropy_with_logits_v2.
複製**
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
複製**
train_step = tf.train.adamoptimizer(0.0005).minimize(loss)
複製**
sess = tf.session()
sess.run(tf.global_variables_initializer())
複製**
for i in range(10000):
index = np.random.permutation(len(target))
shuju =shuju[index]
target = target[index]
sess.run(train_step, feed_dict=)
if i%1000 == 0:
print(sess.run((loss, accuracy), feed_dict=))
複製**
(0.48896936, 0.74)
(0.3105031, 0.91333336)
(0.23715515, 0.9533333)
(0.18639529, 0.96666664)
(0.15062416, 0.96666664)
(0.12501644, 0.98)
(0.10630642, 0.98)
(0.09238311, 0.98)
(0.08186688, 0.98)
(0.07382768, 0.98)
複製**
print(sess.run(weight))
複製**
[[ 1.319767 1.5383282 -1.6495701 ]
[ 4.432047 0.22605467 -3.0211918 ]
[-4.785147 -1.7326641 3.8946218 ]
[-4.521448 -2.30758 3.069917 ]]
複製**
print(sess.run(bias))
複製**
[ 1.521937 2.874068 -4.503109]
複製**
XGBoost解決多分類問題
xgboost官方給的二分類問題的例子是區別蘑菇有無毒,資料集和 都可以在xgboost中的demo資料夾對應找到,我是用的anaconda安裝的xgboost,實現起來比較容易。唯一的梗就是在終端中執行所給命令 xgboost mushroom.conf 時會報錯,是路徑設定的問題,所以我乾脆把x...
邏輯回歸解決多分類問題
第二種方法 從演算法入手 傳統的邏輯回歸只能處理二分類問題,對於多分類任務,主要有如下兩種方案。某個分類演算法有n類,將某一類和另一模擬較作為二分類問題,總共可分為cn2 c cn 2 種不同的二分類模型,給定乙個新的樣本點,求出每種二分類對應的概率,概率最高的一類作為新樣本的 結果。某個分類演算法...
多分類問題 手寫數字 pytorch
手寫數字識別 import torch 資料集處理 from torchvision import transforms from torchvision import datasets from torch.utils.data import dataloader 函式 啟用函式等 import ...