英文:
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('/Users/kk/Documents/Python Programs/admissions_data.csv')
admissions_df = pd.read_csv('/Users/kk/Documents/Python Programs/admissions_data.csv')
# 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 ' '.join(lemmas)
# Use CountVectorizer to convert text to a matrix of word counts
vectorizer = CountVectorizer(preprocessor=preprocess)
text_matrix = vectorizer.fit_transform(admissions_df['text'])
# Use cosine similarity to find the most similar questions to a user'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, 'response']
# Build the chatbot using NLTK and the preprocessed data
pairs = [(preprocess(q), preprocess(r)) for q, r in zip(admissions_df['question'], admissions_df['response'])]
chatbot = nltk.chat.util.Chat(pairs)
# Define a function that uses the chatbot to answer user queries
def admissions_chatbot():
print("Welcome to the college admissions chatbot. How can I assist you today?")
while True:
user_input = input()
if user_input.lower() == 'quit':
break
else:
response = get_most_similar(user_input)
if response:
print(response)
else:
print(chatbot.respond(user_input))
if __name__ == "__main__":
admissions_chatbot()
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'
The above exception was the direct cause of the following exception:
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'
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论