VS Code not recognizing variables while using numpy - python-3.x

Numpy is not working on VS Code
I tried using numpy on jupyter notebook. It worked over there.
import numpy s np
speed = [99, 54, 68, 90]
x = np.mean(speed)
print(x)
I am getting error that x is not defined.
I retried downloading numpy

Related

Numpy in VS Code Ubuntu 20.04

I am new to Ubuntu and is trying to get Numpy working on VS Code. Following is my code:
import matplotlib.pyplot as plt
import numpy as np
x = [1,2,3,4]
y = [3,5,7,9]
plt.grid(True)
plt.xlabel("My X values")
plt.ylabel("My Y values")
plt.plot(x,y, 'b-^', lineWidth = 3, markersize = 7, label = "Blue Line")
plt.legend(loc = "upper center")
plt.show()
But the debugger gives me the following error:
No module named 'numpy.core._multiarray_umath'
Does anyone knows how to install that module? Thank you so much in advance.

Keras model.fit in Azure ML fails [duplicate]

How to troubleshoot this? I've tried setting dtype=None in the image.img_to_array method.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
from keras.preprocessing import image
image_size = (180, 180)
batch_size = 32
model = keras.models.load_model('best_model.h5')
img = keras.preprocessing.image.load_img(
"GarnetCreek_7-15-2019.jpeg", target_size=image_size
)
img_array = image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create batch axis
predictions = model.predict(img_array)
score = predictions[0]
This raises the following error:
Traceback (most recent call last):
img_array = image.img_to_array(img, dtype=None)
return image.img_to_array(img, data_format=data_format, **kwargs)
x = np.asarray(img, dtype=dtype)
return array(a, dtype, copy=False, order=order)
TypeError: __array__() takes 1 positional argument but 2 were given
Has anyone seen this before? Many thanks!
This error sometimes is due to a bug in Pillow 8.3.0 as it is here. (You may not use import PIL directly in your code, however some libraries such as tf.keras.preprocessing.image.load_img use PIL internally)
So, downgrading from PIL 8.3.0 to 8.2.0 may work.
Check PIL version:
import PIL
print(PIL.__version__)
If it is 8.3.0, then you may downgrade to 8.2.0:
!pip install pillow==8.2.0

LaTeX was not able to process the following string: b'lp'

I just begin with Python, scypi and matplotlib, I had copy this code:
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
data = stats.exponweib.rvs(a=1, c=2.09, scale=10.895, loc=0, size=2500)
plt.plot(data, stats.exponweib.pdf(data, *stats.exponweib.fit(data, 1, 1, scale=02, loc=0))
_ = plt.hist(data, bins = np.linspace(0, 16, 33), normed=True, alpha=0.5)
plt.show()
But it show an error:
'LaTeX was not able to process the following string: b'lp''
The file ...Lib\site-packages\matplotlib\mpl-data\matplotlibrc show:
#text.usetex : False
os : windows 7
dis: winpython
What can I do?
Thanks.

Creating PDF using pydot

I got the below code from Visualizing a Decision Tree - Machine Learning
import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
test_idx = [0, 50 , 100]
train_target = np.delete(iris.target, test_idx)
train_data = np.delete(iris.data, test_idx , axis=0)
test_target = iris.target[test_idx]
test_data = iris.data[test_idx]
clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)
print(test_target)
print(clf.predict(test_data))
#viz_code
from sklearn.externals.six import StringIO
import pydot
dot_data = StringIO()
tree.export_graphviz(clf,
out_file=dot_data,
feature_names = iris.feature_names,
class_names = iris.target_names,
filled = True, rounded = True,
impurity = False)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
I tried to run it in my python 3.5 but i get an error saying that graph is a list.
Traceback (most recent call last):
File "Iris.py", line 31, in <module>
graph.write_pdf("iris.pdf")
AttributeError: 'list' object has no attribute 'write_pdf'
Press any key to continue . . .
How come graph here is a list?
I think this is a duplicate, here is answered the same question link
because pydot.graph_from_dot_data return a list the solution is:
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf")
This solved the problem for me with Python 3.6.5 :: Anaconda, Inc.
Pydot will not work in Python3.
You can use Pydotplus (graph.write_pdf("iris.pdf") AttributeError: 'list' object has no attribute 'write_pdf'") for python3 instead of pydot.
Although, the code shown on youtube is for Python2. So, it will be better if you use Python2.

Why does the kernel restart when I try sklearn PCA?

I use Ipython Notebook and when I input the code:
import numpy as np
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(data)
I receive a notice that the kernel has died and has restarted. What is going on?
Also my data is in this format:
array([[ 0.00000000e+00, 3.13000000e+02, 3.10000000e+02, ...,
9.00000000e+00, 6.00000000e+00, 2.00000000e+01],
[ 3.00000000e+00, 2.06900000e+03, 2.06700000e+03, ...,
1.90000000e+01, 7.00000000e+00, 3.20000000e+01],
[ 4.00000000e+00, 2.54200000e+03, 2.54000000e+03, ...,
1.10000000e+01, 1.10000000e+01, 1.10000000e+01],
EDIT:
The data itself is not that large (~3 MB). If it helps, I am using ipython notebook.
I tried a simple 3x3 test matrix as input and same problem, so it's probably not something with the data size either:
data = np.array([[1,2,3],[1,4,6],[2,8,11]])
import numpy as np
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(data)
I tried the sklearn's pca in the terminal with python as well:
>>> from sklearn.decomposition import PCA
>>> pca = PCA()
>>> import numpy as np
>>> X = np.array([[1,2,3],[1,5,7],[2,6,10]])
>>> y = np.array[1,2,3]
>>> y = np.array([1,2,3])
>>> pca.fit(X, y)
And got:
Illegal instruction (core dumped)
It seems that sklearn will not run nicely on a 32 bit machine so when I ran this later on a 64 bit server it worked!!!!!

Resources