python - 错误说有一些错误,而我几乎完全复制并粘贴了谷歌教程代码
问题描述
这些都是完整的错误,我对它想说的很困惑。我正在从谷歌做一个教程,我几乎完全复制了教程中的代码,但是用我自己的数据集替换了它的数据集,并且发生了这些错误。
Traceback (most recent call last):
File "C:\Users\julia\Anaconda\envs\myenv\lib\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Objective 1'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/julia/Anaconda/envs/myenv/Mycode.py", line 333, in <module>
validation_targets=validation_targets)
File "C:/Users/julia/Anaconda/envs/myenv/Mycode.py", line 288, in train_nn_regression_model
steps=steps_per_period
File "C:\Users\julia\Anaconda\envs\myenv\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 367, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "C:\Users\julia\Anaconda\envs\myenv\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1158, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "C:\Users\julia\Anaconda\envs\myenv\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1185, in _train_model_default
input_fn, ModeKeys.TRAIN))
File "C:\Users\julia\Anaconda\envs\myenv\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1022, in _get_features_and_labels_from_input_fn
self._call_input_fn(input_fn, mode))
File "C:\Users\julia\Anaconda\envs\myenv\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1113, in _call_input_fn
return input_fn(**kwargs)
File "C:/Users/julia/Anaconda/envs/myenv/Mycode.py", line 268, in <lambda>
training_targets["Objective 1"],
File "C:\Users\julia\Anaconda\envs\myenv\lib\site-packages\pandas\core\frame.py", line 2980, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\julia\Anaconda\envs\myenv\lib\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Objective 1'
我是一名机器学习初学者,当我使用 python 并按照谷歌机器学习教程中的代码进行操作时,出现了一些错误,我不确定发生了什么。
# Step 1 - Set up and import necessary packages
from __future__ import print_function
import math
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Dataset
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.1f}'.format
# Step 2 - Load our data
zerlite_13X_error = pd.read_csv("zerlite_13x_error.csv", sep=",")
# print(zerlite_13X_error.head()) # Load data done
# We will randomize data. just to be sure not to get any pathological ordering effects the
# performance of Stochastic Gradient Descent. And we first consider objective 1
zerlite_13X_error = zerlite_13X_error.reindex(
np.random.permutation(zerlite_13X_error.index))
# Define features and Configure columns
# Define features which are parameters 1 to parameters 8
def preprocess_features(zerlite_13X_error):
"""Prepares input features from zerlite_13X_error
Args:
zerlite_13X_error: A Pandas DataFrame expected to contain data
Return:
A DataFrame that contains the features to be used for the model.
including synthetic features
"""
selected_features = zerlite_13X_error[
["Parameter 1",
"Parameter 2",
"Parameter 3",
"Parameter 4",
"Parameter 5",
"Parameter 6",
"Parameter 7",
"Parameter 8"]]
processed_features = selected_features.copy()
# print(processed_features.head())
return processed_features
def preprocess_targets(zerlite_13X_error):
"""Prepares target features (i.e. labels) from zerlite_13X_error set
Args:
zerlite_13X_error: A Panda dataframe that was expected to contain data from
the zerolite_13X_error data set
Returns:
A dataframe that contains the target feature
"""
output_targets = pd.DataFrame()
# Create the output targets
output_targets["Objective 1"] = zerlite_13X_error["Objective 1"]
print(output_targets.head())
return output_targets
# For training Set, we will choose 14000 out of 20154 number, about 70% of data as training set
training_examples = preprocess_features(zerlite_13X_error.head(14000))
training_examples.describe()
print('-- Training Examples Describe --')
print(training_examples.describe())
training_targets = preprocess_targets(zerlite_13X_error.head(14000))
training_targets.describe()
print('-- Training Targets Describe --')
print(training_targets.describe())
# For Validation Set, we will choose 3000 examples, out of total 20154 examples
validation_examples = preprocess_features(zerlite_13X_error.iloc[14001:17001])
validation_examples.describe()
print('-- Validation Examples Describe --')
print(validation_examples.describe())
validation_targets = preprocess_targets(zerlite_13X_error.iloc[14001:17001])
validation_targets.describe()
print('-- Validation Targets Describe --')
print(validation_targets.describe())
# for Test Set, we will choose the last 3154 examples
test_examples = preprocess_features((zerlite_13X_error.tail(3154)))
test_examples.describe()
print('-- Test Examples Describe --')
print(test_examples.describe())
test_targets = preprocess_targets(zerlite_13X_error.tail(3154))
test_targets.describe()
print('-- Test Targets Describe --')
print(test_targets.describe())
# As we are now working with multiple features, modularize the code for configuring columns into a
# separate function
def construct_feature_columns(input_features):
"""Construct the TensorFlow columns:
Args:
input_features: The name of numerical input features to use
Returns:
A set of feature columns
"""
return set([tf.feature_column.numeric_column(my_feature)
for my_feature in input_features])
# Train and evaluate the model
def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):
"""Trains a linear regression model of multiple features
Args:
features: pandas DataFrame of features
targets: pandas DataFrame of targets
batch_size: Size of batches to be passed to the model
shuffle: True or False. Whether to shuffle the data
num_epochs: Number of epochs for which data should be repeated. None = Repeat indefinitely
Returns:
Tuple of (features, labels) for next data batch
"""
# Convert pandas data into a dict of np arrays
features = {key: np.array(value) for key, value in dict(features).items()}
# Construct a dataset, and configure batching/repeating
ds = Dataset.from_tensor_slices((features, targets)) # Warning: 2GB limit
ds = ds.batch(batch_size).repeat(num_epochs)
# Shuffle the data, if specified
if shuffle:
ds = ds.shuffle(10000)
# Return the next batch of data
features, labels = ds.make_one_shot_iterator().get_next()
return features, labels
# Now we will go creating a train model using neural network
def train_nn_regression_model(learning_rate, steps, batch_size, hidden_units,
training_examples, training_targets,
validation_examples, validation_targets):
"""Trains a neural network regression model of multiple features
In addition to training, this function also prints training progress information,
as well as plot of the training and validation loss over time
Args:
learning_rate: A 'float', the learning rate
steps: A non-zero 'int', the total number of training steps. A training step
consists of a forward and backward pass using a single batch.
batch_size: A non-zero 'int', the batch size.
hidden_size" A 'list' of int values, specifying the number of neurons in each layer
training_examples: A 'DataFrame' containing one or more columns from
'zerlite_13X_error' to use as input features for training
training_targets: A 'DataFrame' containing exactly one column from
'zerlite_13X_error' to use as target for training
validation_examples: A 'DataFrame' containing one or more columns from
'zerlite_13X_error' to use as input features for validation
validation_targets: A 'DataFrame' containing exactly one column from
'zerlite_13X_error' to use as target for validation
Returns:
A 'DNNRegressor' object trained on the training data.
"""
periods = 10
steps_per_period = steps / periods
# Create a DNNRegressor Object
my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)
dnn_regressor = tf.estimator.DNNRegressor(
feature_columns=construct_feature_columns(training_examples),
hidden_units=hidden_units,
optimizer=my_optimizer,
)
# Create input functions.
training_input_fn = lambda: my_input_fn(training_examples,
training_targets["Objective 1"],
batch_size=batch_size)
predict_training_input_fn = lambda: my_input_fn(training_examples,
training_targets["Objective 1"],
num_epochs=1,
shuffle=False)
predict_validation_input_fn = lambda: my_input_fn(validation_examples,
validation_targets["Objective 1"],
num_epochs=1,
shuffle=False)
# Train the model, but do so inside a loop so that we can periodically assess loss metrics
print("Training Models ............")
print("RMSE (on training data): ")
training_rmse = []
validation_rmse = []
for period in range(0, periods): # Python shows error occuring here
# Train the model, starting from the prior state
dnn_regressor.train(
input_fn=training_input_fn,
steps=steps_per_period)
# take a break and compute predictions
training_predictions = dnn_regressor.predict(input_fn=predict_training_input_fn)
training_predictions = np.array([item['predictions'][0] for item in training_predictions])
validation_predictions = dnn_regressor.predict(input_fn=predict_validation_input_fn)
validation_predictions = np.array([item['predictions'][0] for item in validation_predictions])
# Compute training and validation loss
training_root_mean_squared_error = math.sqrt(
metrics.mean_squared_error(training_predictions, training_targets))
validation_root_mean_squared_error = math.sqrt(
metrics.mean_squared_error(validation_predictions, validation_targets))
# Occasionally print the current loss
print(" period %02d: %02f" % (period, training_root_mean_squared_error))
# Add the loss metrics from this period to our list
training_rmse.append(training_root_mean_squared_error)
validation_rmse.append(validation_root_mean_squared_error)
print("Model training finished")
# Output a graph of loss metrics over periods
plt.ylabel("RMSE")
plt.xlabel("Periods")
plt.title("Root Mean Squared Error v.s. Periods")
plt.tight_layout()
plt.plot(training_rmse, label="training")
plt.plot(validation_rmse, label="validation")
plt.legend()
print("Final RMSE (on training data): %0.2f" % training_root_mean_squared_error)
print("Final RMSE (on validation data): %0.2f" % validation_root_mean_squared_error)
return dnn_regressor
# Train NN model
dnn_regressor = train_nn_regression_model(
learning_rate=0.1,
steps=5000,
batch_size=10,
hidden_units=[10, 2],
training_examples=training_examples,
training_targets=training_targets,
validation_examples=validation_examples,
validation_targets=validation_targets) # Python shows error here
解决方案
推荐阅读
- php - WooCommerce:添加自定义类以选择包装器
- java - 如何按日期对对象列表进行分组?
- java - 从 java 执行 python scipt 时找不到文件
- html - 随着浏览器窗口的增加,随时保持横幅响应
- python - pythonnet clr 在 jupyter 笔记本中无法识别
- python - 重新运行 model.fit 时使用阶跃衰减训练不保留最后一个 epoch。学习率错误
- ios - 我应该提交 ios/Runner.xcworkspace/xcshareddata/ 吗?
- sql - 从 SQL Server 中的生产完成还原后,是否可以使用 drop 语句从数据库中删除特定主体?
- java - 使用 ImageIO 写入 TIFF 文件时出现问题
- solidity - Solidity 彩票智能合约,但我只能给第一个奖品。我想把它送给所有的赢家