TensorFlow iris 資料分類

2021-08-29 20:41:19 字數 2077 閱讀 2788

需要用到的python庫有:

sklearn、tensorflow

import pandas as pd

import tensorflow as tf

from sklearn import datasets

# 載入資料集

iris_df = datasets.load_iris()

# 資料集切片

data = iris_df.data

data=pd.dataframe(data,columns=['seqpallength','sepalwidth','petallength','petalwidth'])

data['species']=iris_df.target

# 打亂資料集的順序

data = data.sample(frac=1).reset_index(drop=true)

# 把訓練集與測試集分開

data_train,data_test = data[:120],data[120:]

train_x, train_y = data_train, data_train.pop('species')

test_x,test_y = data_test,data_test.pop('species')

# 提取特徵值

feature_columns =

for key in train_x.keys():

print(feature_columns)

# 構建訓練模型

classifier = tf.estimator.dnnclassifier(

# 這個模型接受哪些輸入的特徵

feature_columns=feature_columns,

# 包含兩個隱藏層,每個隱藏層包含10個神經元.

hidden_units=[10, 10],

# 最終結果要分成幾類

n_classes=3)

def train_func(train_x,train_y):

dataset=tf.data.dataset.from_tensor_slices((dict(train_x), train_y))

dataset = dataset.shuffle(1000).repeat().batch(100)

return dataset

# 進行模型訓練,進行2000 個回合的訓練,每次100調資料

classifier.train(

input_fn=lambda:train_func(train_x,train_y),

steps=2000)

# 模型**

def eval_input_fn(features, labels, batch_size):

features=dict(features)

if labels is none:

# no labels, use only features.

inputs = features

else:

inputs = (features, labels)

dataset = tf.data.dataset.from_tensor_slices(inputs)

assert batch_size is not none, "batch_size must not be none"

dataset = dataset.batch(batch_size)

return dataset

predict_arr =

predictions = classifier.predict(

input_fn=lambda:eval_input_fn(test_x,labels=test_y,batch_size=100))

for predict in predictions:

result = predict_arr == test_y

result1 = [w for w in result if w == true]

print("準確率為 %s"%str((len(result1)/len(result))))

Catalan數(卡特蘭數)

卡特蘭數 規定h 0 1,而h 1 1,h 2 2,h 3 5,h 4 14,h 5 42,h 6 132,h 7 429,h 8 1430,h 9 4862,h 10 16796,h 11 58786,h 12 208012,h 13 742900,h 14 2674440,h 15 969484...

卡特蘭數 Catalan數

卡特蘭數 規定h 0 1,而h 1 1,h 2 2,h 3 5,h 4 14,h 5 42,h 6 132,h 7 429,h 8 1430,h 9 4862,h 10 16796,h 11 58786,h 12 208012,h 13 742900,h 14 2674440,h 15 969484...

Catalan數(卡特蘭數)

2012 04 12 21 08 13 標籤 卡特蘭數 原始出處 作者資訊和本宣告。否則將追究法律責任。卡特蘭數 規定h 0 1,而h 1 1,h 2 2,h 3 5,h 4 14,h 5 42,h 6 132,h 7 429,h 8 1430,h 9 4862,h 10 16796,h 11 58...