当我尝试运行下面提供的代码时,我遇到了KeyError。为什么会这样?

huangapple go评论92阅读模式
英文:

When i try to run the below given code i get a keyError. Why is that?

问题

Traceback (most recent call last):
  File "/Users/kk/Library/Python/3.9/lib/python/site-packages/pandas/core/indexes/base.py", line 3802, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'text'

上述异常是下面异常的直接原因:

Traceback (most recent call last):
  File "/Users/kk/Documents/Python Programs/Chatbot V4.py", line 23, in <module>
    text_matrix = vectorizer.fit_transform(admissions_df['text'])
  File "/Users/kk/Library/Python/3.9/lib/python/site-packages/pandas/core/frame.py", line 3807, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/Users/kk/Library/Python/3.9/lib/python/site-packages/pandas/core/indexes/base.py", line 3804, in get_loc
    raise KeyError(key) from err
KeyError: 'text'
英文:
import nltk
import numpy as np
import pandas as pd
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Load data from a CSV file into a Pandas DataFrame
open(&#39;/Users/kk/Documents/Python Programs/admissions_data.csv&#39;)
admissions_df = pd.read_csv(&#39;/Users/kk/Documents/Python Programs/admissions_data.csv&#39;)

# Preprocess data using NLP techniques
lemmatizer = WordNetLemmatizer()
def preprocess(text):
    tokens = nltk.word_tokenize(text.lower())
    lemmas = [lemmatizer.lemmatize(token) for token in tokens]
    return &#39; &#39;.join(lemmas)

# Use CountVectorizer to convert text to a matrix of word counts
vectorizer = CountVectorizer(preprocessor=preprocess)
text_matrix = vectorizer.fit_transform(admissions_df[&#39;text&#39;])

# Use cosine similarity to find the most similar questions to a user&#39;s query
def get_most_similar(query):
    query_vector = vectorizer.transform([query])
    similarity_scores = cosine_similarity(query_vector, text_matrix)
    most_similar_index = np.argmax(similarity_scores)
    return admissions_df.loc[most_similar_index, &#39;response&#39;]

# Build the chatbot using NLTK and the preprocessed data
pairs = [(preprocess(q), preprocess(r)) for q, r in zip(admissions_df[&#39;question&#39;], admissions_df[&#39;response&#39;])]
chatbot = nltk.chat.util.Chat(pairs)

# Define a function that uses the chatbot to answer user queries
def admissions_chatbot():
    print(&quot;Welcome to the college admissions chatbot. How can I assist you today?&quot;)
    while True:
        user_input = input()
        if user_input.lower() == &#39;quit&#39;:
            break
        else:
            response = get_most_similar(user_input)
            if response:
                print(response)
            else:
                print(chatbot.respond(user_input))


if __name__ == &quot;__main__&quot;:
    admissions_chatbot()

Traceback (most recent call last):
  File &quot;/Users/kk/Library/Python/3.9/lib/python/site-packages/pandas/core/indexes/base.py&quot;, line 3802, in get_loc
    return self._engine.get_loc(casted_key)
  File &quot;pandas/_libs/index.pyx&quot;, line 138, in pandas._libs.index.IndexEngine.get_loc
  File &quot;pandas/_libs/index.pyx&quot;, line 165, in pandas._libs.index.IndexEngine.get_loc
  File &quot;pandas/_libs/hashtable_class_helper.pxi&quot;, line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File &quot;pandas/_libs/hashtable_class_helper.pxi&quot;, line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: &#39;text&#39;

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File &quot;/Users/kk/Documents/Python Programs/Chatbot V4.py&quot;, line 23, in &lt;module&gt;
    text_matrix = vectorizer.fit_transform(admissions_df[&#39;text&#39;])
  File &quot;/Users/kk/Library/Python/3.9/lib/python/site-packages/pandas/core/frame.py&quot;, line 3807, in __getitem__
    indexer = self.columns.get_loc(key)
  File &quot;/Users/kk/Library/Python/3.9/lib/python/site-packages/pandas/core/indexes/base.py&quot;, line 3804, in get_loc
    raise KeyError(key) from err
KeyError: &#39;text&#39;

答案1

得分: 1

你正在引用 admissions_df['text'] 列,而 Python 告诉你该列不存在。尝试运行 admissions_df.columns 来检查该列的实际名称。

英文:

You're referencing a column admissions_df['text'], and Python is telling you it does not exist. Try running admissions_df.columns to check what the columns is actually called

huangapple
  • 本文由 发表于 2023年3月20日 23:51:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定