pythonscikit-learnimportvscode-extensions

Can't install imports in VsCode using 'pip install' and extra python extensions


I am trying to make a machine learning algorithm that when you enter a math equation you say if the answer is right or not. But when I run it the terminal says...

File "******** - **Vs Code\Python\math.py", line 2, in <module>
    from sklearn.feature_extraction.text import CountVectorizer
ModuleNotFoundError: No module named 'sklearn'

This is not just the sklearn but also sympy. And when I do the pip install sklearn this error pops up

pip : The term 'pip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + pip install sympy sklearn + ~~~ + CategoryInfo : ObjectNotFound: (pip:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

This is my code. The bolded code is where the errors are.

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()

I tried installing extra extensions in VsCode but it didn't work. I tried looking at other qustions in stack overflow but none of them answer the question I have. Please help.


Solution

  • You need to install the external packages like sympy, sklearn, etc as they are not included with a vanilla Python installation. In your terminal, run:

    pip install sympy sklearn # include any other packages you need