Here is my code
from pandas import read_csv
from pandas.tools.plotting import scatter_matrix
from matplotlib import pyplot
filename = 'iris.data.csv'
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = read_csv(filename, names=names)
print(dataset.shape)
print(dataset.head(20))
# Data visualizations
dataset.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)
pyplot.show()
When i run above code. Then following error is shown
Traceback (most recent call last):
File "/media/k/UBUNTU2/Work and stuff/coding language/Python/Machine learning/exp.py", line 43, in <module>
dataset.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)
File "/usr/local/lib/python2.7/dist-packages/pandas/tools/plotting.py", line 2090, in plot_frame
raise ValueError('Invalid chart type given %s' % kind)
ValueError: Invalid chart type given box
Any idea ? What should i do? Please help
Your pandas version (0.14) is already 3 years old. The "box" kind was introduced in version 0.15. Now we are at version 0.20.
The solution is thus to install a newer version of pandas in order to be able to use kind="box" in the plotting wrapper.
If you need to use version 0.14 you can get boxplot using the DataFrame.boxplot() method. The usage according to documentation would be:
df = DataFrame(rand(10,5))
plt.figure();
bp = df.boxplot()
Related
Actually I have a problem when calling a Matlab script from Python.
import matlab.engine
import os
import random
import numpy as np
a=[str(random.randint(1,3)) for _ in range(3)]
print(a)
eng=matlab.engine.start_matlab()
eng.cd("/Users/dha/Documents/MATLAB/test-matlab/",nargout=0)
sr, state=eng.test_func()
print(sr)
print(state)
In fact I want to return "sr" which is a float and an array of integer "state", e.g. sr = 34.31 and state = [1,2,5]. The function test_func() work well on Matlab, but when I run this in Python from terminal (python test_matlab_engine.py) I received the following error:
Traceback (most recent call last):
File "test_matlab_engine.py", line 10, in <module>
sr, state=eng.mabuc_drl(a)
TypeError: 'float' object is not iterable
Anyone please give me the solution. Thank you so much in advance.
It seems that the result from MATLAB to Python has been cut off. If you have two parameters, you only get one which is the first parameter from the MATLAB. So, the question is how to get two or more parameters.
In a word, you should write this in your Python file:
re = eng.your_function_name(parameter1, parameter2, nargout=2)
where re contains two parameters which come from MATLAB.
You can find more information in the official documentation: Call MATLAB Functions from Python
After following the instructions for installation of OSMnx (including explicitly installing spatialindex) with
brew install spatialindex
pip install osmnx
running the very first basic example of
import osmnx as ox
G = ox.graph_from_place('Manhattan Island, New York City, New York, USA', network_type='drive')
ox.plot_graph(ox.project_graph(G))
in the project's readme, I get
Traceback (most recent call last):
File "/Users/Rax/Documents/Projects/Coding/Python/maps/test.py", line 23, in <module>
G = ox.graph_from_place('Manhattan Island, New York City, New York, USA', network_type='drive')
File "/usr/local/lib/python2.7/site-packages/osmnx/core.py", line 1850, in graph_from_place
raise TypeError('query must be a string or a list of query strings')
TypeError: query must be a string or a list of query strings
How do I get OSMnx to run past this error?
This can be caused by having
from __future__ import unicode_literals
in your code, since including it turns all strings to type unicode, while the API expects arguments of type string. If that is present, removing it will prevent the error from occurring.
See also: https://github.com/gboeing/osmnx/issues/185
OSMnx is compatible with Python 2 and 3, so you don't need to import from the future package to use it. If you use Python 2 and import unicode_literals from future, all of your strings will be of type unicode instead. As you can see in the documentation, graph_from_place expects the query to be of type string, not of type unicode.
Following Pickle figures from matplotlib, I am trying to load a figure from a pickle. I am using the same code with the modifications that are suggested in the responses.
Saving script:
import numpy as np
import matplotlib.pyplot as plt
import pickle as pl
# Plot simple sinus function
fig_handle = plt.figure()
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)
# plt.show()
# Save figure handle to disk
pl.dump(fig_handle,file('sinus.pickle','wb'))
Loading script:
import matplotlib.pyplot as plt
import pickle as pl
import numpy as np
# Load figure from disk and display
fig_handle = pl.load(open('sinus.pickle', 'rb'))
fig_handle.show()
The saving script produces a file named "sinus.pickle" but the loading file does not show the anticipated figure. Any suggestions?
Python 2.7.13
matplotlib 2.0.0
numpy 1.12.1
p.s. following a suggestion replaced fig_handle.show() with pat.show() which produced an error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/
site-packages/matplotlib/backends/backend_macosx.py", line 109,
in_set_device_scale
self.figure.dpi = self.figure.dpi / self._device_scale * value
File "/usr/local/lib/python2.7/site-packages/matplotlib/figure.py",
line 416, in _set_dpi
self.callbacks.process('dpi_changed', self)
File "/usr/local/lib/python2.7/site-packages/matplotlib/cbook.py",
line 546, in process
if s in self.callbacks:
AttributeError: 'CallbackRegistry' object has no attribute 'callbacks'
What you call your "loading script" doesn't make any sense.
From the very link that you provided in your question, loading the picked figure is as simple as:
# Load figure from disk and display
fig_handle2 = pl.load(open('sinus.pickle','rb'))
fig_handle2.show()
Final solution included modification of
fig_handle.show()
to
plt.show()
and modification of the backend to "TkAgg", based to an advice given by ImportanceOfBeingErnest
I am trying to perform an image classification task using a pre-trained VGG16 model in Keras. The code I wrote, following the instructions in the Keras application page, is:
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
model = VGG16(weights='imagenet', include_top=True)
img_path = './train/cat.1.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
(inID, label) = decode_predictions(features)[0]
which is quite similar to the code shown in this question already asked in the forum. But in spite of having the include_top parameter as True, I am getting the following error:
Traceback (most recent call last):
File "vgg16-keras-classifier.py", line 14, in <module>
(inID, label) = decode_predictions(features)[0]
ValueError: too many values to unpack
Any help will be deeply appreciated! Thanks!
It's because (according to a function definition which might be found here) a function decode_predictions returns a triple (class_name, class_description, score). This why it claims that there are too many values to unpack.
Fairly new to coding and have been browsing this site for a while, not clued up enough to give anything back yet though.
I'm trying to calculate the Hurst exponent using this code originally from QuantStart but modified to import data from Yahoo. Daily Hurst Exponent
When running in Powershell I return these errors:
C:\Program Files\Anaconda2\lib\site-packages\pandas\io\data.py:35: FutureWarning:
The pandas.io.data module is moved to a separate package (pandas-datareader) and will be removed from pandas in a future
version.
After installing the pandas-datareader package (https://github.com/pydata/pandas-datareader), you can change the import
from pandas.io import data, wb to from pandas_datareader import data, wb.
FutureWarning)
When changing from pandas.io import data, wb to from pandas_datareader import data, wb:
Traceback (most recent call last):
File "hurst.py", line 23, in
aapl = DataReader("AAPL", "yahoo", datetime(2012,1,1), datetime(2015,9,18))
NameError: name 'DataReader' is not defined
Please can someone help and guide me in what changes I'm missing to get the script to run properly.
Thanks,
James
from pandas_datareader.data import DataReader
...
ts1 = DataReader(symbol, "yahoo", start_date, end_date)
See the usage in the documentation
for pandas datareader:
from pandas_datareader import data
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
f = data.DataReader("F", 'yahoo', start, end)