Plot from mglearn - python

How can I save plot from mglearn? I tried this code but it does not work.
import numpy as np
import matplotlib.pyplot as plt
import mglearn
from fpdf import FPDF
f = mglearn.plots.plot_knn_classification(n_neighbors=3)
f.savefig("n_neighbors.pdf", bbox_inches='tight')
The error was AttributeError: 'NoneType' object has no attribute 'savefig'.
Thanks

As the mglearn package uses matplotlib.pyplot's plotting mechanics you can use their saving mechanics, like so.
mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.savefig("n_neighbors.pdf", bbox_inches='tight')

from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import mglearn.plots
import numpy as np
import mglearn
from fpdf import FPDF
X, y = mglearn.datasets.make_forge()
mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.savefig("n_neighbors.pdf", bbox_inches='tight')
This code work.

Related

Python Error ImportError: No module named skimage.io

When I run this program in Python, it show me this error:
ImportError: No module named skimage.io.
I have already run the command pip install scikit-image, but I still get this error. Can you please help me?
This is my code:
import matplotlib.pyplot as plt
import skimage.io as io
import skimage.color as color
parrots = io.imread('C:\Python27\Example\parrots.bmp')
parrots_hsv= skcolor.convert_colorspace(parrots, 'RGB','HSV')
fig, ax= plt.subplots(nclos = 2, figsize=('8,4'))
ax[0].imshow(parrots)
ax[0].set_title('original image')
restored_image =skcolore.convert_colorspace(parrots_hsv, 'HSV','RGB')
ax[1].imshow(restored_image)
ax[1].set_title('restored image')
plt.show()`enter code here`
I reproduced this error like so:
import math.sqrt as sq
print(sq(4))
Error:
File ".\Solution.py", line 1, in <module>
import math.sqrt as sq
ModuleNotFoundError: No module named 'math.sqrt'; 'math' is not a package
repaired by:
from math import sqrt as sq
print(sq(4))
So I presume if you changed your imports to the below code it might fix it:
from matplotlib import pyplot as plt
from skimage import io as io
from skimage import color as color
In addition, you might want to read a bit here for a simple explanation about imports.

Matplotlib support failed in Pycharm on Windows

I have a problem with matplotlib in Pycharm.my code is:
import matplotlib.pyplot as plt
import numpy as np
T = np.linspace(10,100,10)
es = 611*np.exp(17.27*T/(237.3+T))
plt.plot(T,es)
plt.xlabel('T (degree Celcius)')
plt.ylabel('es (pa)')
plt.show()
and after i ran this code,Pycharm showed this error:
enter link description here
what should i do to solve it??

Bootstraping for DataFrame in python

I am trying to resample my dataset using bootsrtaping technique without success, my code as follow:
import pandas as pd
import numpy as np
from openpyxl import Workbook
from pandas import ExcelWriter
import matplotlib.pyplot as plt
import bootstrap as btstrap
#import scikits.bootstrap as sci
from matplotlib import pyplot as plt
import numpy.random as npr
sta_9147="//Users/talhadidi/Private/Desktop/9147.xlsx"
xlsx=pd.ExcelFile(sta_9147)
df1=pd.read_excel(xlsx,'Sheet1')
df1.columns=df1.columns.astype(str)
x_resample = btstrap(['AveOn','AveOff','AveLd','DOOR_OPEN_SEC'], n=10000)
writer=pd.ExcelWriter("/ Users/talhadidi/Private/Desktop/testt5.xlsx")
df2.to_excel(writer,'Sheet1')
writer.save()
the error i kept getting is :
TypeError: 'module' object is not callable,
could anyone help in, special thanks in advance.

Matplotlib error "cannot import name pyplot" on Ubuntu

I am trying to run this code.
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('games.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()
But I keep getting this error
Traceback (most recent call last):
File "mpl.py", line 3, in <module>
from matplotlib import pyplot as plt
File "/home/megha/matplotlib.py", line 3, in <module>
from matplotlib import pyplot as plt
ImportError: cannot import name pyplot
I googled a solution for this but I am only getting that the matplotlib version needs to be upgraded. I tried that too, and its still showing the same error.
I have also tried
import matplotlib.pyplot as plt but I have python 2.7 version, so that didnt work either.
Use import matplotlib.pyplot as plt instead.

Error while converting rgb to lab : python

import skimage
from skimage import io, color
import numpy as np
import scipy.ndimage as ndi
rgb = io.imread('img.jpg')
lab = color.rgb2lab(skimage.img_as_float(rgb))
l_chan1 = lab[:,:,0]
l_chan1 /= np.max(np.abs(l_chan1))
l_chan_med = ndi.median_filter(l_chan1, size=5)
skimage.io.imshow(l_chan_med)
I am trying to do some image processing. While i was changing the color scheme, i am getting an error for rgb2lab function. " color.rgb2lab module object has no attribute 'rgb2lab'. I have imported all the required libraries.Any suggestions will be appreciated
Try this:
import skimage
from skimage import io
from skimage.color import rgb2lab
import numpy as np
import scipy.ndimage as ndi
rgb = io.imread('img.jpg')
lab = rgb2lab(skimage.img_as_float(rgb))
l_chan1 = lab[:,:,0]
l_chan1 /= np.max(np.abs(l_chan1))
l_chan_med = ndi.median_filter(l_chan1, size=5)
skimage.io.imshow(l_chan_med)
I don't know what's wrong, but your code runs fine on my machine. Python 2.7.6, OS X Yosemite. I would try reinstalling scikit-image.

Categories

Resources