深度神经网络 广告相似人群扩展
import pandas as pd import tensorflow as tf TRAIN_URL = "http://download.tensorflow.org/data/iris_training.csv" TEST_URL = "http://download.tensorflow.org/data/iris_test.csv" # CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species'] # CSV_COLUMN_NAMES = 'label,age,gender,education,consumptionAbility,LBS,house'.split(',') CSV_COLUMN_NAMES = 'label,age,gender,education,consumptionAbility,house'.split(',') # SPECIES = ['Setosa', 'Versicolor', 'Virginica'] # label,age,gender,education,consumptionAbility,LBS,house # label,age,gender,education,consumptionAbility,LBS,house SPECIES = [0, 1] #SPECIES = [1, 0] def maybe_download(): # train_path = tf.keras.utils.get_file(TRAIN_URL.split('/')[-1], TRAIN_URL) # test_path = tf.keras.utils.get_file(TEST_URL.split('/')[-1], TEST_URL) # # return train_path, test_path # return 'iris_training.csv', 'iris_test.csv' return 'myu_oriv_tB.csv', 'myu_oriv_rB.csv' # def load_data(label_name='Species'): def load_data(label_name='label'): train_path, test_path = maybe_download() """Parses the csv file in TRAIN_URL and TEST_URL.""" # Create a local copy of the training set. # train_path = tf.keras.utils.get_file(fname=TRAIN_URL.split('/')[-1], # origin=TRAIN_URL) # train_path now holds the pathname: ~/.keras/datasets/iris_training.csv # Parse the local CSV file. train = pd.read_csv(filepath_or_buffer=train_path, names=CSV_COLUMN_NAMES, # list of column names header=0 # ignore the first row of the CSV file. ) # train now holds a pandas DataFrame, which is data structure # analogous to a table. # 1. Assign the DataFrame's labels (the right-most column) to train_label. # 2. Delete (pop) the labels from the DataFrame. # 3. Assign the remainder of the DataFrame to train_features # label_name = y_name train_features, train_label = train, train.pop(label_name) # Apply the preceding logic to the test set. # test_path = tf.keras.utils.get_file(TEST_URL.split('/')[-1], TEST_URL) test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0) test_features, test_label = test, test.pop(label_name) # Return four DataFrames. return (train_features, train_label), (test_features, test_label) def train_input_fn(features, labels, batch_size): """An input function for training""" # Convert the inputs to a Dataset. dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels)) # Shuffle, repeat, and batch the examples. # dataset = dataset.shuffle(1000).repeat().batch(batch_size) dataset = dataset.shuffle(3).repeat().batch(batch_size) # Return the dataset. return dataset def eval_input_fn(features, labels, batch_size): """An input function for evaluation or prediction""" features = dict(features) if labels is None: # No labels, use only features. inputs = features else: inputs = (features, labels) # Convert the inputs to a Dataset. dataset = tf.data.Dataset.from_tensor_slices(inputs) # Batch the examples assert batch_size is not None, "batch_size must not be None" dataset = dataset.batch(batch_size) # Return the dataset. return dataset # The remainder of this file contains a simple example of a csv parser, # implemented using a the `Dataset` class. # `tf.parse_csv` sets the types of the outputs to match the examples given in # the `record_defaults` argument. # CSV_TYPES = [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]] CSV_TYPES = [[0], [0], [0], [0], [0], [0], [0]] CSV_TYPES = [[0], [0], [0], [0], [0], [0]] def _parse_line(line): # Decode the line into its fields fields = tf.decode_csv(line, record_defaults=CSV_TYPES) # Pack the result into a dictionary features = dict(zip(CSV_COLUMN_NAMES, fields)) # Separate the label from the features # label = features.pop('Species') label = features.pop('label') return features, label def csv_input_fn(csv_path, batch_size): # Create a dataset containing the text lines. dataset = tf.data.TextLineDataset(csv_path).skip(1) # Parse each line. dataset = dataset.map(_parse_line) # Shuffle, repeat, and batch the examples. # dataset = dataset.shuffle(1000).repeat().batch(batch_size) dataset = dataset.shuffle(2).repeat().batch(batch_size) # Return the dataset. return dataset
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """An Example of a DNNClassifier for the Iris dataset.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import tensorflow as tf import qq_iris_data_mystudy parser = argparse.ArgumentParser() parser.add_argument('--batch_size', default=2, type=int, help='batch size') parser.add_argument('--train_steps', default=2, type=int, help='number of training steps') res_f = 'res.txt' with open(res_f, 'w', encoding='utf-8') as fw: fw.write('') def main(argv): args = parser.parse_args(argv[1:]) # Fetch the data (train_x, train_y), (test_x, test_y) = qq_iris_data_mystudy.load_data() my_feature_columns, predict_x = [], {} for key in train_x.keys(): my_feature_columns.append(tf.feature_column.numeric_column(key=key)) #predict_x[key] = [float(i) for i in test_x[key].values] predict_x[key] = [int(i) for i in test_x[key].values] expected = [0 for i in predict_x[key]] # Build 2 hidden layer DNN with 10, 10 units respectively. classifier = tf.estimator.DNNClassifier( feature_columns=my_feature_columns, # Two hidden layers of 10 nodes each. hidden_units=[10, 10], # The model must choose between 3 classes. n_classes=2) # Train the Model. classifier.train( input_fn=lambda: qq_iris_data_mystudy.train_input_fn(train_x, train_y, args.batch_size), steps=args.train_steps) # Evaluate the model. eval_result = classifier.evaluate( input_fn=lambda: qq_iris_data_mystudy.eval_input_fn(test_x, test_y, args.batch_size)) print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result)) predictions = classifier.predict( input_fn=lambda: qq_iris_data_mystudy.eval_input_fn(predict_x, labels=None, batch_size=args.batch_size)) template = ('\nmyProgress{}/{}ORI{}||RESULT{}|| Prediction is "{}" ({:.1f}%), expected "{}"') c, c_all_ = 0, len(expected) for pred_dict, expec in zip(predictions, expected): class_id = pred_dict['class_ids'][0] probability = pred_dict['probabilities'][class_id] ori = ','.join([str(predict_x[k][c]) for k in predict_x]) print(template.format(c, c_all_, ori, str(pred_dict), qq_iris_data_mystudy.SPECIES[class_id], 100 * probability, expec)) c += 1 fw_s = '{}---{}\n'.format(ori,pred_dict['probabilities'][1]) with open(res_f, 'a', encoding='utf-8') as fw: fw.write(fw_s) if __name__ == '__main__': tf.logging.set_verbosity(tf.logging.INFO) tf.app.run(main)