我正在尝试制作一台机器学习计算机,你输入一个数学问题并判断答案是否正确。但是当我运行它时,终端显示...
File "******** - **Vs Code\Python\math.py", line 2, in <module>
from sklearn.feature_extraction.text import CountVectorizer
ModuleNotFoundError: No module named 'sklearn'
这仅适用于 sklearn。还有同情心。
这是我的代码。粗体代码是错误所在。
import os
import pickle
from ***sklearn.feature_extraction.text*** import CountVectorizer
from ***sklearn.naive_bayes*** import MultinomialNB
from ***sympy*** import solve, symbols
DATA_FILE = "equations_data.pkl"
# Function to convert equations into features
def equation_to_features(equations):
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(equations)
return X
# Function to train the machine learning model
def train_model(equations, labels):
X = equation_to_features(equations)
clf = MultinomialNB()
clf.fit(X, labels)
return clf
# Function to predict solutions for equations
def predict_solution(equations, clf):
X_test = equation_to_features(equations)
predictions = clf.predict(X_test)
return predictions
# Function to solve equations using SymPy
def solve_equations(equations):
solutions = []
x = symbols('x')
for eq in equations:
try:
solution = solve(eq, x)
solutions.append(solution[0] if solution else None)
except:
solutions.append(None)
return solutions
# Function to load data from file
def load_data():
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "rb") as f:
return pickle.load(f)
return [], []
# Function to save data to file
def save_data(equations, labels):
with open(DATA_FILE, "wb") as f:
pickle.dump((equations, labels), f)
# Main function
def main():
equations, solutions = load_data()
while True:
# Ask for an equation
equation = input("Enter an equation (e.g., '2*x + 3 = 7') or type 'exit' to quit: ")
if equation.lower() == 'exit':
break
# Add equation to the list
equations.append(equation)
# Ask if the solution provided by the model is correct
answer = input("Is the solution correct? (y/n): ")
if answer.lower() == 'y':
solutions.append(1)
else:
solutions.append(0)
# Save data to file
save_data(equations, solutions)
# Train the model
clf = train_model(equations, solutions)
while True:
# Ask for an equation
equation = input("Enter an equation (e.g., '2*x + 3 = 7'): ")
if equation.lower() == 'exit':
break
# Predict solution
predicted_solution = predict_solution([equation], clf)[0]
# Solve the equation using SymPy
sym_solution = solve_equations([equation])[0]
if sym_solution is not None:
print("SymPy solution:", sym_solution)
if predicted_solution is not None:
print("Predicted solution:", predicted_solution)
if predicted_solution is not None and sym_solution is not None:
answer = input("Is the solution correct? (y/n): ")
if answer.lower() == 'y':
print("Great!")
# Add equation and correctness to the training data
equations.append(equation)
solutions.append(1)
clf = train_model(equations, solutions)
# Save data to file
save_data(equations, solutions)
else:
print("Oops! Let me learn from that.")
# Add equation and correctness to the training data
equations.append(equation)
solutions.append(0)
clf = train_model(equations, solutions)
# Save data to file
save_data(equations, solutions)
else:
print("Couldn't solve the equation.")
if __name__ == "__main__":
main()
我尝试在 VsCode 中安装额外的扩展,但没有成功。我尝试查看堆栈溢出中的其他问题,但没有一个回答我的问题。请帮忙。
您需要安装 sympy、sklearn 等外部软件包,因为它们不包含在普通 Python 安装中。在您的终端中,运行:
pip install sympy sklearn # include any other packages you need