Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow类型错误:Can';t转换为';numpy.int64';对象隐式地访问str_Python_Pandas_Numpy_Tensorflow - Fatal编程技术网

Python Tensorflow类型错误:Can';t转换为';numpy.int64';对象隐式地访问str

Python Tensorflow类型错误:Can';t转换为';numpy.int64';对象隐式地访问str,python,pandas,numpy,tensorflow,Python,Pandas,Numpy,Tensorflow,这是我的jupyter笔记本: import pandas as pd from pprint import pprint import pickle import numpy as np with open('preDF.p', 'rb') as f: preDF = pickle.load(f) #pprint(preDF) df = pd.DataFrame(data=preDF) #df.rename(columns={166: '166'}, inplace=True) d

这是我的jupyter笔记本:

import pandas as pd
from pprint import pprint
import pickle 
import numpy as np

with open('preDF.p', 'rb') as f:
    preDF = pickle.load(f)
#pprint(preDF)
df = pd.DataFrame(data=preDF)
#df.rename(columns={166: '166'}, inplace=True)
df.head()
将numpy导入为np
#msk=np.random.rand(len(df))<0.8
#列车=df[msk]
#测试=df[~msk]
从sklearn.model_选择导入KFold
kf=KFold(n_拆分=2)
列车=df.iloc[列车索引]
test=df.iloc[测试索引]
train.columns=train.columns.astype(np.int32)
test.columns=test.columns.astype(np.int32)
导入tensorflow作为tf
def序列输入(特征、标签、批次大小):
“”“用于培训的输入函数”“”
#将输入转换为数据集。
dataset=tf.data.dataset.from_tensor_切片((dict(features.astype(np.int32)),labels.astype(np.int32)))
#洗牌、重复和批处理示例。
dataset=dataset.shuffle(1000).repeat().batch(批大小)
#返回数据集。
返回数据集
def eval_输入_fn(特征、标签、批次大小):
“”“用于计算或预测的输入函数”“”
features=dict(features.astype(np.int32))
如果标签为“无”:
#无标签,仅使用特征。
输入=特征
其他:
输入=(特征、标签)
#将输入转换为数据集。
dataset=tf.data.dataset.from_tensor_切片(输入)
#批处理示例
断言批次大小不是无,“批次大小不能是无”
dataset=dataset.batch(批次大小)
#返回数据集。
返回数据集
def加载数据(列车,测试,y_名称=166):
train\u x,train\u y=train,train.pop(y\u名称)
test\u x,test\u y=test,test.pop(y\u名称)
返回(列车x,列车y),(测试列车x,测试列车y)
def总管(传动系,测试):
批次大小=np.int32(100)
列车步数=np.int32(1000)
#获取数据
物种=['neg'、'stable'、'pos']
(列车x,列车y),(测试列车x,测试列车y)=负载数据(列车,测试)
#功能列描述如何使用输入。
my_feature_columns=[]
对于车内钥匙×钥匙():
my\u feature\u columns.append(tf.feature\u column.numeric\u column(key=key))
#建造2个隐藏层DNN,分别为10、10个单元。
分类器=tf.estimator.DNNClassifier(
功能列=我的功能列,
#两个隐藏层,每个层包含10个节点。
隐藏单位=[30,10,30],
#模型必须在3个类之间进行选择。
n_类=3)
列车(
输入\ fn=λ:列\输入\ fn(列\ x,列\ y,
批次(单位尺寸),
步数=列车步数)
#评估模型。
eval_结果=分类器.evaluate(
输入fn=λ:评估输入fn(测试x,测试y,
批次(单位尺寸)
打印('\n测试集精度:{精度:0.3f}\n'。格式(**eval_结果))
#根据模型生成预测
预期=['exp neg'、'exp stable'、'exp pos']
预测_x={
“开放”:[5.1,5.9,6.9],
“高”:[3.3,3.0,3.1],
‘低’:[1.7,4.2,5.4],
“关闭”:[0.5,1.5,2.1],
}
预测=分类器。预测(
输入fn=λ:评估输入fn(预测x,
标签=无,
批次大小=批次大小)
模板=('\n预测为“{}”({.1f}%),应为“{}”)
对于预测,请在zip中进行expec(预测,预期):
class_id=pred_dict['class_id'][0]
概率=pred_dict['probabilities'][class_id]
打印(模板格式(物种[类别id],
100*概率,expec)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#tf.logging.set_详细性(tf.logging.INFO)
tf.应用程序运行(主(列车、测试))
所以我得到了这个错误:

INFO:tensorflow:使用默认配置。
警告:tensorflow:使用临时文件夹作为模型目录:/tmp/tmpz7rw1puj
信息:tensorflow:使用配置:{u任务类型:'worker','u cluster'u spec','u tf'u random'u seed':None','u keep'u checkpoint'u max':5','u is'u chief':True','u master':'','u session'u config':None','u log'u step'u count'u steps':100','u global'id'u cluster'u中的'u'u'u-id':0','u-evaluation'u-master:','u-service','None','u-save'u-summary'u-steps':100','u-save'u-secus'u-secus':checkpoints':100','u-id'u-num 600','u:0,“\u num\u worker\u replications”:1,“\u model\u dir':”/tmp/tmpz7rw1puj',“保存检查点\u步骤”:无,“\u每小时保留一次检查点”:10000}
信息:tensorflow:正在调用模型\u fn。
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
98如果uuuu name uuuuuu=='\uuuuuuuu main\uuuuuuuuu':
99#tf.logging.set_详细信息(tf.logging.INFO)
-->100 tf.应用程序运行(主(列车、测试))
主(列车、测试)
64输入\u fn=λ:列输入\u fn(列x,列y,
65批次(单位尺寸),
--->66步=列车(步)
67#评估模型。
68评估结果=分类器。评估(
/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py in-train(self、input\u fn、hook、steps、max\u steps、saving\u侦听器)
350
351保存侦听器=\u检查侦听器\u类型(保存侦听器)
-->352损失=自我训练模型(输入、挂钩、保存侦听器)
353 logging.info('最后一步丢失:%s',丢失)
354回归自我
/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py in_train_模型(self、input、hook、saving_侦听器)
810工人挂钩。扩展(输入挂钩)
811估计器\u规格=自身。\u调用\u模型\u fn(
-->812特性、标签、型号(fn_lib.ModeKeys.TRAIN、self.config)
813
814如果自热启动设置:
/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py in_call_model_fn(self、features、label、mode、config)
791
792 logging.info('Calling model_fn'))
-->793模型结果=自我。\模型(特征=特征,**kwargs)
794 logging.info('Done calling model_fn'))
795
/usr/local/lib/python
0 1   2   3   4   5   6   7   8   9   ... 157 158 159 160 161 162 163 164 165 166
0 3   8   1   13  15  13  9   12  12  1   ... 0   0   0   0   0   0   0   0   0   1
1 3   1   13  15  13  9   12  12  1   27  ... 0   0   0   0   0   0   0   0   0   1
2 3   8   1   13  15  13  9   12  12  1   ... 0   0   0   0   0   0   0   0   0   1
3 13  5   20  18  9   3   1   18  9   1   ... 0   0   0   0   0   0   0   0   0   1
4 3   8   12  15  18  8   5   24  9   4   ... 0   0   0   0   0   0   0   0   0   2
5 rows × 167 columns
import numpy as np 
#msk = np.random.rand(len(df)) < 0.8
#train = df[msk]
#test = df[~msk]

from sklearn.model_selection import KFold
kf = KFold(n_splits=2)
train = df.iloc[train_index]
test = df.iloc[test_index]
train.columns = train.columns.astype(np.int32)
test.columns = test.columns.astype(np.int32)


import tensorflow as tf

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.astype(np.int32)), labels.astype(np.int32)))

    # Shuffle, repeat, and batch the examples.
    dataset = dataset.shuffle(1000).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.astype(np.int32))
    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

def load_data(train,test,y_name=166):

    train_x, train_y = train, train.pop(y_name)

    test_x, test_y = test, test.pop(y_name)

    return (train_x, train_y), (test_x, test_y)

def main(train,test):
    batch_size = np.int32(100)
    train_steps = np.int32(1000)
    # Fetch the data

    SPECIES = ['neg', 'stable', 'pos']
    (train_x, train_y), (test_x, test_y) = load_data(train,test)

    # Feature columns describe how to use the input.
    my_feature_columns = []
    for key in train_x.keys():
        my_feature_columns.append(tf.feature_column.numeric_column(key=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=[30, 10,30],
        # The model must choose between 3 classes.
        n_classes=3)

    classifier.train(
        input_fn=lambda:train_input_fn(train_x, train_y,
                                                 batch_size),
        steps=train_steps)
    # Evaluate the model.
    eval_result = classifier.evaluate(
        input_fn=lambda:eval_input_fn(test_x, test_y,
                                                batch_size))

    print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))

    # Generate predictions from the model
    expected = ['exp neg', 'exp stable', 'exp pos']
    predict_x = {
        'open': [5.1, 5.9, 6.9],
        'high': [3.3, 3.0, 3.1],
        'low':   [1.7, 4.2, 5.4],
        'close': [0.5, 1.5, 2.1],
    }

    predictions = classifier.predict(
        input_fn=lambda:eval_input_fn(predict_x,
                                                labels=None,
                                                batch_size=batch_size))

    template = ('\nPrediction is "{}" ({:.1f}%), expected "{}"')

    for pred_dict, expec in zip(predictions, expected):
        class_id = pred_dict['class_ids'][0]
        probability = pred_dict['probabilities'][class_id]

        print(template.format(SPECIES[class_id],
                              100 * probability, expec))


if __name__ == '__main__':
    #tf.logging.set_verbosity(tf.logging.INFO)
    tf.app.run(main(train,test))
df.columns = df.columns.astype(str)