我從 tim 的技術 Python 課程中學習,我將他的代碼用于基本的線性回歸演算法,并嘗試制作我自己的小資料表進行嘗試,每當我匯入我的資料表時,它都會顯示以下錯誤:
KeyError: 'xvalue'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
4 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2898 return self._engine.get_loc(casted_key)
2899 except KeyError as err:
-> 2900 raise KeyError(key) from err
2901
2902 if tolerance is not None:
KeyError: 'xvalue'
我的代碼如下:
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc
import tensorflow as tf
# Load dataset.
train = "https://docs.google.com/spreadsheets/d/1qQfNL2ePWVsOiqSNgJu_ZwmI9KFwFTrtdb1boqIKFZQ/edit?usp=sharing"
eval = "https://docs.google.com/spreadsheets/d/1Ip1Key9NYx3boTUFkPUOzEWdtUBMeGJJEY3MbSpVkUo/edit?usp=sharing"
dftrain = pd.read_csv(train, sep='\t,\s*') # training data
dfeval = pd.read_csv(eval, sep='\t,\s*') # testing data
y_train = dftrain.pop('xvalue')
y_eval = dfeval.pop('xvalue')
我正在使用 google python notebook 對此進行編碼,任何幫助將不勝感激!
uj5u.com熱心網友回復:
如果要匯出 Google Sheets 電子表格的內容,可以使用此答案中的建議。
在您的情況下,您使用的是 Pandas,因此您不需要使用該requests軟體包。
所以你可以定義這個函式:
def get_docs_url(doc_id):
url = f"https://docs.google.com/spreadsheet/ccc?key={doc_id}&output=csv"
return url
并像這樣使用它:
train = get_docs_url('1qQfNL2ePWVsOiqSNgJu_ZwmI9KFwFTrtdb1boqIKFZQ')
dftrain = pd.read_csv(train) # training data
PS:我洗掉了該sep='\t,\s*'部分,因為它不需要決議 CSV 檔案。
PPS:測驗資料集的列名與訓練資料集不同。我需要添加dfeval = dfeval.rename(columns={'X': 'xvalue', 'Y': 'yvalue'})以使其正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408859.html
標籤:
上一篇:交叉熵驗證損失是一條直線
