Scipy installation, its installed why the traceback? - python

I am running the following code (this is the beginning snippet):
# Import necessary modules and functions
########################################
# time module, for timing it.
from time import time
global starttime
starttime = time()
# psutil for indentifying processor and memory usage
#import psutil
# csv module for loading csv files
import csv
# numpy, a standard module for working with arrays
import numpy as np
# for randomly shuffling arrays
from random import shuffle
# for computing mean squared errors
from sklearn import metrics
# importing modules that contain models we will use
from sklearn import linear_model, ensemble, gaussian_process
from sklearn import neural_network, svm
# importing memory profiler to enable us to determine peak memory usage
from memory_profiler import profile
# Helpful functions
###################
#psutilpercent = psutil.virtual_memory()
#print "\n", " --> Memory Check 1 Percent:", str(psutilpercent.percent) + "%\n"
# This function is for simplifying reading CSV files
def readCSV(path):
"""
Read a CSV file of floats, with no headder
"""
data = []
mycsv = csv.reader(open(path), delimiter="|")
for counter, row in enumerate(mycsv):
if counter != 0:
data.append(row)
return np.asarray(data, dtype=np.float32)
However I am receiving the traceback:
Traceback (most recent call last):
File "C:\Users\regression_v6.py", line 38, in <module>
from sklearn import metrics
File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 32, in <module>
from .base import clone
File "C:\Python27\lib\site-packages\sklearn\base.py", line 10, in <module>
from scipy import sparse
ImportError: No module named scipy
But my computer shows Scipy as installed, here is a view of Control Panel\All Control Panel Items\Programs and Features:
Any ideas?

Did you install a special version of scipy? as your screenshot shows 'scipy_umfpack'
I can successfully import sklearn using the official version downloaded from sourceforge: http://sourceforge.net/projects/scipy/files/scipy/

Related

ImportError when using trackviz module

I'm assigned to run a python script, that imports several module
#filename: animate_vid_v6.py
import numpy as np
import pandas as pd
import imageio
from trackviz import animate
import sys
from matplotlib.patches import Circle
from operator import itemgetter
import colorsys
after I installed all the required modules, I get this error message.
Traceback (most recent call last):
File "<some_directories>\animate_vid_v6.py", line 4, in <mo
dule>
from trackviz import animate
File "C:\Python39\lib\site-packages\trackviz\animate.py", line 9, in <module>
from trackviz.tools import FigureAxes
File "C:\Python39\lib\site-packages\trackviz\tools.py", line 3, in <module>
from mpl_toolkits.axes_grid1 import Divider, LocatableAxes, Size
ImportError: cannot import name 'LocatableAxes' from 'mpl_toolkits.axes_grid1' (
C:\Python39\lib\site-packages\mpl_toolkits\axes_grid1\__init__.py)
After I scrutinize that message, it is turn out that the LocatableAxes is no longer exists in scripts inside the C:\Python39\lib\site-packages\mpl_toolkits\axes_grid1 directory.
As you can see here, at the C:\Python39\lib\site-packages\mpl_toolkits\axes_grid1\__init__.py, there are no LocatableAxes module.
from . import axes_size as Size
from .axes_divider import Divider, SubplotDivider, make_axes_locatable
from .axes_grid import Grid, ImageGrid, AxesGrid
from .parasite_axes import host_subplot, host_axes
Does anybody has an idea /suggestions to fix this problem? thanks.
The LocatableAxes classes have been deprecated in Matplotlib 3.0.0, but it seems trackviz hasn't been updated since then to avoid using these classes. Indeed, the trackviz GitHub repository hasn't been updated for about three years.
If you really want to use trackviz you will have to install an older version of Matplotlib.

ModuleNotFoundError: No module named 'sklearn.tree.tree'

I'm trying to learn how to create a machine learning API with Flask, however, following this tutorial, the following error appears when I type the command python app.py:
Traceback (most recent call last):
File "C:\Users\Breno\Desktop\flask-api\app.py", line 24, in <module>
model = p.load(open(modelfile, 'rb'))
ModuleNotFoundError: No module named 'sklearn.tree.tree'
My code:
from flask import Flask, request, redirect, url_for, flash, jsonify
import numpy as np
import pickle as p
import pandas as pd
import json
#from sklearn.tree import DecisionTreeClassifier
app = Flask(__name__)
#app.route('/api/', methods=['POST'])
def makecalc():
j_data = request.get_json()
prediction = np.array2string(model.predict(j_data))
return jsonify(prediction)
if __name__ == '__main__':
modelfile = 'models/final_prediction.pickle'
model = p.load(open(modelfile, 'rb'))
app.run(debug=True,host='0.0.0.0')
Could someone help me please?
Pickles are not necessarily compatible across scikit-learn versions so this behavior is expected (and the use case is not supported). For more details, see https://scikit-learn.org/dev/modules/model_persistence.html#model-persistence. Replace pickle by joblib.
by example :
>>> from sklearn import svm
>>> from sklearn import datasets
>>> clf = svm.SVC()
>>> X, y= datasets.load_iris(return_X_y=True)
>>> clf.fit(X, y)
SVC()
>>> from joblib import dump, load
>>> dump(clf, open('filename.joblib','wb'))
>>> clf2 = load(open('filename.joblib','rb'))
>>> clf2.predict(X[0:1])
array([0])
>>> y[0]
0
For anyone coming across this issue (perhaps dealing with code written long ago), sklearn.tree.tree is now under sklearn.tree (as from v0.24). This can be see from the import error warning:
from sklearn.tree.tree import BaseDecisionTree
/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.tree.tree module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.tree. Anything that cannot be imported from sklearn.tree is now part of the private API.
warnings.warn(message, FutureWarning)
Instead, use:
from sklearn.tree import BaseDecisionTree
The problem is with the version of sklearn. Module sklearn.tree.tree is removed since version 0.24. Most probably, your model has been generated with the older version. Try installing an older version of sklearn:
pip uninstall scikit-learn
pip install scikit-learn==0.20.4

How to save and use a Tensorflow dataset using the Experimental save and load mehods?

I wrote two python files create_save.py and load_use.py as shown below.
create_save.py is running good and saving tf dataset.
But load_use.py is giving errors shown below.
How to fix load_use.py errors please?
create_save.py
import os
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.data.experimental import save as tf_save
ds_dir = os.path.join('./', "save_load_tfds_dir")
ds = tf.data.Dataset.range(12)
tf_save(ds, ds_dir)
load_use.py
import os
import numpy as np
import pandas as pd
import tensorflow as tf
ds_dir = os.path.join('./', "save_load_tfds_dir")
new_ds = tf.data.experimental.load(ds_dir)
for elem in new_ds:
print(elem)
Above load_use.py program is giving following errors:
TypeError Traceback (most recent call last)
in
----> 1 new_ds = tf.data.experimental.load(ds_dir)
TypeError: load() missing 1 required positional argument:
'element_spec'
How to fix above error?
To load a previously saved dataset, you need to specify element_spec argument -- a type signature of the elements of the saved dataset, which can be obtained via tf.data.Dataset.element_spec. This requirement exists so that shape inference of the loaded dataset does not need to perform I/O.
import tempfile
path = os.path.join(tempfile.gettempdir(), "saved_data")
# Save a dataset
dataset = tf.data.Dataset.range(2)
tf.data.experimental.save(dataset, path)
new_dataset = tf.data.experimental.load(path,
tf.TensorSpec(shape=(), dtype=tf.int64)) # element_spec arg
for elem in new_dataset:
print(elem)
When you are creating a tf.data.Dataset, it has the attribute element_spec which is what you should be using while loading your saved file. (Refer: Dataset doc).
In the above example, the element_spec argument in the load() method is given as per the type specification of the data being saved in the code.
TF Data Load Documentation

Cannot import mean from statistics

I'm trying to import statistics module in python. It's giving me an error message when i execute the program.
Here is my code:
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
xs = np.array([1,2,3,4,5,6,7,8])
ys = np.array([2,8,5,0,5,7,3,6])
def best_fit_line(xs ,ys):
m = ( ((mean(xs)* mean(ys))- mean(xs*ys)) /
(mean(xs)*mean(xs))-(mean(xs*xs)))
return m
m = best_fit_line(xs,ys)
The error message:
Traceback (most recent call last):
File "/home/kudzai/Documents/Machine Learning/LinearRegAlg.py", line 1, in <module>
from statistics import mean
ImportError: No module named statistics
The statistics module was added in Python 3.4. Perhaps you're using an older Python version.
If you can't upgrade for whatever reason, you can also just use numpy's mean function: np.mean(xs) etc. For numpy arrays, it's probably faster too.

ImportError with Python 3

I am trying to run an example from ThinkStats2 code. I copied everything from github - the file structure are exactly the same as given on the github.
chap01soln.py
from __future__ import print_function
import numpy as np
import sys
import nsfg
import thinkstats2
def ReadFemResp(dct_file='2002FemResp.dct',
dat_file='2002FemResp.dat.gz',
nrows=None):
"""Reads the NSFG respondent data.
dct_file: string file name
dat_file: string file name
returns: DataFrame
"""
dct = thinkstats2.ReadStataDct(dct_file)
df = dct.ReadFixedWidth(dat_file, compression='gzip', nrows=nrows)
CleanFemResp(df)
return df
def CleanFemResp(df):
"""Recodes variables from the respondent frame.
df: DataFrame
"""
pass
def ValidatePregnum(resp):
"""Validate pregnum in the respondent file.
resp: respondent DataFrame
"""
# read the pregnancy frame
preg = nsfg.ReadFemPreg()
# make the map from caseid to list of pregnancy indices
preg_map = nsfg.MakePregMap(preg)
# iterate through the respondent pregnum series
for index, pregnum in resp.pregnum.iteritems():
caseid = resp.caseid[index]
indices = preg_map[caseid]
# check that pregnum from the respondent file equals
# the number of records in the pregnancy file
if len(indices) != pregnum:
print(caseid, len(indices), pregnum)
return False
return True
def main(script):
"""Tests the functions in this module.
script: string script name
"""
resp = ReadFemResp()
assert(len(resp) == 7643)
assert(resp.pregnum.value_counts()[1] == 1267)
assert(ValidatePregnum(resp))
print('%s: All tests passed.' % script)
if __name__ == '__main__':
main(*sys.argv)
I am getting ImportError like shown below:
Traceback (most recent call last):
File "C:\wamp\www\ThinkStats_py3\chap01soln.py", line 13, in <module>
import nsfg
File "C:\wamp\www\ThinkStats_py3\nsfg.py", line 14, in <module>
import thinkstats2
File "C:\wamp\www\ThinkStats_py3\thinkstats2.py", line 34, in <module>
import thinkplot
File "C:\wamp\www\ThinkStats_py3\thinkplot.py", line 11, in <module>
import matplotlib
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 105, in <module>
import six
ImportError: No module named 'six'
All the files are listed under the src folder. No packages under src. I tried adding packages for nsfg and thinkstats but another error appeared. I tried upgrading python 2.7 to python 3.4. I am still getting the same error. I know I need to install the six package for matplotlib but why am I getting import error on nsfg?
You are getting the import error on nsfg because it internally imports matplotlib (not directly, but it imports thinkstats2 , which imports thinkplot which imports matplotlib , which has a dependency on six module) . And that library is not installed on your computer, so the import fails.
Most probably you do not have six module , you can try installing it using - pip install six .
Or get it from here, unzip it and install it using - python setup.py install

Categories

Resources