简体   繁体   English

错误:没有名为“sklearn.tree.tree”的模块

[英]Error : No module named 'sklearn.tree.tree

import sys
from os import listdir, sep
import numpy as np
import pickle
from PIL import Image
import cv2
from sklearn.ensemble import RandomForestClassifier
import numpy as np
from PIL import Image


DEFAULT_IMAGE_SIZE = (45,45)
SPLIT_POINT_COEFF = 0.8

class PredictionService:


        def __init__(self):
             #self.dataset = datasetDIR
             #self.testImage = testImage
             pass

        def getImageVector(self, image):
            try:

                #NOTE:from docs
                #When translating a color image to black and white (mode “L”), the library uses the ITU-R 601-2 luma transform:
                #L = R * 299/1000 + G * 587/1000 + B * 114/1000

                imageGrayscale = Image.open(image).convert('L')
                #resize image to default image size - 45 x 45
                imageGrayscale = imageGrayscale.resize(DEFAULT_IMAGE_SIZE, Image.ANTIALIAS)
                #
                imageNP = np.array(imageGrayscale)
                imgList = []
                for line in imageNP:
                    for value in line:
                        imgList.append(value)
                #imgList is 2025 long vector
                return imgList
            except Exception as e:
                print("Error : {}".format(e))
                return None

        def addImagesToSet(self, rootPath, imageList, label, completeImageList = [], labelList = []):
            dashes = ['-','/','-','\\']
            counter = 0
            for image in imageList:
                print('[{}] Images loading...'.format(dashes[counter]))
                counter = (counter + 1) % len(dashes)
                completeImageList.append(self.getImageVector(rootPath + image))
                labelList.append(label)


        def getTrainingAndTestData(self, directoryPath):

            dirList = listdir(directoryPath)
            xTrain, yTrain, xTest, yTest = [], [], [], []
            try:
                if len(dirList) < 1:
                    return None

                imageDirPath = None

                counter = 1
                for directory in dirList:

                    imageDir = listdir('{}/{}'.format(directoryPath, directory))
                    splitPoint = int(SPLIT_POINT_COEFF * len(imageDir))

                    print('[{}] Loading dataset - {} images'.format(counter, directory))
                    counter += 1

                    trainImages, testImages = imageDir[:splitPoint], imageDir[splitPoint:]
                    imageDirPath = directoryPath + sep + directory + sep
                    self.addImagesToSet(imageDirPath, trainImages, directory, xTrain, yTrain)
                    self.addImagesToSet(imageDirPath, testImages, directory, xTest, yTest)

            except Exception as e:
                print('Error: {}'.format(e))
                return [],[],[],[]

            return xTrain, yTrain, xTest, yTest

        def trainModel(self, trainDatasetDir):
            train_dataset_dir = ('C:/Users/MUTHU/Documents/GitHub/Handwritten-math-symbols-recognition/Dataset')
            print('Training.....')
            xTrain, yTrain, xTest , yTest = self.getTrainingAndTestData(trainDatasetDir)
            if [] not in (xTrain, yTrain, xTest , yTest):
                randomForestClassifier = RandomForestClassifier()
                randomForestClassifier.fit(xTrain,yTrain)
                accuracyScore = randomForestClassifier.score(xTrain,yTrain)
                # save classifier
                pickle.dump(randomForestClassifier,open("Model/math_recognition_model.pkl",'wb'))
                print("Model Accuracy Score : {}".format(accuracyScore))
                testAccuracyScore = randomForestClassifier.score(xTest,yTest)
                print("Model Accuracy Score (Test) : {}".format(testAccuracyScore))
            else :
                print("An error occurred.")

        def predict(self, imagePath):
            try:
                image = [self.getImageVector(imagePath)]
                # load saved model
                try:
                    decisionTreeClassifierModel= pickle.load(open("Model/random_forest_classifier.pkl",'rb'))
                    modelPrediction = decisionTreeClassifierModel.predict(image)
                    print(modelPrediction)
                    print("Recognized expression:" +  str(modelPrediction[0]))
                except FileNotFoundError as modelFileError:
                    print("Error : {}".format(modelFileError))
                    self.trainModel(datasetDir)
                    self.predict(imagePath)

            except FileNotFoundError as fileError:
                print("Error : {}".format(fileError))
            except Exception as e:
                print("Error : {}".format(e))

please help me to sort this problem iam using pycharam请帮助我使用 pycharam 解决这个问题

You need to import it separately in your project.您需要在项目中单独导入它。 To do that you can refer below link: https://www.jetbrains.com/pycharm/guide/tips/install-and-import/为此,您可以参考以下链接: https://www.jetbrains.com/pycharm/guide/tips/install-and-import/

Also update your pycharm to its newest version of 2020.1.5 from the menu option go to Help -> Check for updates -> option will pop-up at right hand side below to update it还将您的 pycharm 从菜单选项 go 更新到其最新版本 2020.1.5 到帮助 -> 检查更新 -> 选项将在下方右侧弹出以进行更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入 Porter 错误 ModuleNotFoundError: No module named 'sklearn.tree.tree' - import Porter error ModuleNotFoundError: No module named 'sklearn.tree.tree' ModuleNotFoundError:没有名为“sklearn.tree.tree”的模块 - ModuleNotFoundError: No module named 'sklearn.tree.tree' sklearn.tree.tree 模块在 0.22 版中已弃用,并将在 0.24 版中删除 - The sklearn.tree.tree module is deprecated in version 0.22 and will be removed in version 0.24 决策树 AttributeError:模块“sklearn.tree”在 Jupyter Notebook 中没有属性“plot_tree”错误 - Decision Tree AttributeError: module 'sklearn.tree' has no attribute 'plot_tree' Error in Jupyter Notebook Pyinstaller和sklearn.ensemble:&#39;ModuleNotFoundError:没有名为&#39;sklearn.neighbors.quad_tree&#39;的模块[2760]&#39; - Pyinstaller and sklearn.ensemble: 'ModuleNotFoundError: No module named 'sklearn.neighbors.quad_tree' [2760]' 模块sklearn.tree_tree没有属性DTYPE。 - Module sklearn.tree_tree has no attribute DTYPE. sklearn决策树图的Pydot错误 - Pydot error with sklearn decision tree graph 导入错误:无法从“sklearn.tree”导入名称“tree” - Import Error: cannot import name 'tree' from 'sklearn.tree' 没有名为“sklearn.externals.joblib”的模块错误 - No module named 'sklearn.externals.joblib' ERROR python scikit错误-没有名为sklearn的模块 - python scikit error - no module named sklearn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM