{"id":1051,"hash":"b75a7464cef63e93c32111f484a1a711adf5b0c49a01cd2447138075f92a4348","pattern":"TypeError: cannot perform reduce with flexible type","full_message":"I have been using the scikit-learn library. I'm trying to use the Gaussian Naive Bayes Module under the scikit-learn library but I'm running into the following error. TypeError: cannot perform reduce with flexible type\n\nBelow is the code snippet. \n\ntraining = GaussianNB()\ntraining = training.fit(trainData, target)\nprediction = training.predict(testData)\n\nThis is target\n\n['ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML']\n\nThis is trainData\n\n[['-214' '-153' '-58' ..., '36' '191' '-37']\n['-139' '-73' '-1' ..., '11' '76' '-14']\n['-76' '-49' '-307' ..., '41' '228' '-41']\n..., \n['-32' '-49' '49' ..., '-26' '133' '-32']\n['-124' '-79' '-37' ..., '39' '298' '-3']\n['-135' '-186' '-70' ..., '-12' '790' '-10']]\n\nBelow is the stack trace\n\nTraceback (most recent call last):\nFile \"prediction.py\", line 90, in <module>\n  gaussianNaiveBayes()\nFile \"prediction.py\", line 76, in gaussianNaiveBayes\n  training = training.fit(trainData, target)\nFile \"/Library/Python/2.7/site-packages/sklearn/naive_bayes.py\", line 163, in fit\n  self.theta_[i, :] = np.mean(Xi, axis=0)\nFile \"/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/ core/fromnumeric.py\", line 2716, in mean\n  out=out, keepdims=keepdims)\nFile \"/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/_methods.py\", line 62, in _mean\n  ret = um.add.reduce(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)\nTypeError: cannot perform reduce with flexible type","ecosystem":"pypi","package_name":"python-2.7","package_version":null,"solution":"It looks like your 'trainData' is a list of strings:\n\n['-214' '-153' '-58' ..., '36' '191' '-37']\n\nChange your 'trainData' to a numeric type.\n\n import numpy as np\n np.array(['1','2','3']).astype(np.float)","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/28393103/typeerror-cannot-perform-reduce-with-flexible-type","votes":90,"created_at":"2026-04-19T04:52:13.805079+00:00","updated_at":"2026-04-19T04:52:13.805079+00:00"}