pythonで実行すると文字が出てきてグラフとして表示されません。なぜでしょうか。
Mu1= [0. 0.]
Sigma1= [[2. 0. ]
[0. 0.5]]
Mu2= [5. 0.]
Sigma2= [[ 2.5 -1.5]
[-1.5 2.5]]
Traceback (most recent call last):
File "report7.py", line 181, in <module>
plt.show()
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 378, in show
return _backend_mod.show(*args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 3575, in show
manager.show() # Emits a warning for non-interactive backend.
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/backends/backend_gtk3.py", line 393, in show
self.canvas.draw()
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/backends/backend_gtk3agg.py", line 70, in draw
backend_agg.FigureCanvasAgg.draw(self)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 406, in draw
self.figure.draw(self.renderer)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/figure.py", line 2780, in draw
mimage._draw_list_compositing_images(
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/_api/deprecation.py", line 431, in wrapper
return func(*inner_args, **inner_kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 2921, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/collections.py", line 1012, in draw
super().draw(renderer)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/collections.py", line 406, in draw
renderer.draw_markers(
TypeError: must be real number, not str
python
1#!/usr/bin/env python3 2#coding:utf-8 3# 4# パターンが2次元連続変数で 5# 多変量正規分布(Multivariate Normal Distribution) でモデル化した場合の 6# 識別境界面 7# 8from mpl_toolkits.mplot3d import Axes3D 9import numpy as np 10import matplotlib.pyplot as plt 11import matplotlib.tri as tri 12import matplotlib.cm as cm 13import functools 14import scipy.stats as st 15import scipy.special as sp 16import math 17import sys 18from mpl_toolkits.axes_grid1 import make_axes_locatable 19 20 21class Norm2D: 22 """ 23 2変量正規分布 (2 dimensional normal distribution) 24 """ 25 def __init__(self, X): 26 self.__X = np.copy(X) 27 self.__Mu = np.mean(self.__X, axis=0) 28 self.__Sigma = np.cov(self.__X, rowvar=False, bias=True) 29 self.__dim = len(self.__Mu) 30 self.__N = len(self.__X) 31 32 def get_N(self): 33 """ 学習データ数の取得 """ 34 return self.__N 35 36 def get_X(self): 37 """ 学習データの取得 """ 38 return self.__X 39 40 def get_param(self): 41 """ 正規分布パラメタの取得 """ 42 return self.__Mu, self.__Sigma 43 44 def pdf(self, x): 45 """ 与えられた分布の(x)における確率密度値を求める""" 46 return st.multivariate_normal.pdf(x, mean=self.__Mu, cov=self.__Sigma) 47 48 def sampling(self, N): 49 """ 与えられた分布に従ってN点サンプリングする""" 50 return st.multivariate_normal.rvs(mean=self.__Mu, cov=self.__Sigma, size=N) 51 52 def this_likelihood(self, x): 53 """ パターンxが与えられた時の与えられた分布での尤度を求める """ 54 L = 1.0 55 for n in range(len(x)): 56 L *= self.pdf(x[n]) 57 return L 58 59 def this_log_likelihood(self, x): 60 """ パターンxが与えられた時の与えられた分布での対数尤度を求める """ 61 logL = 0.0 62 for n in range(len(x)): 63 logL += log(self.pdf(x[n])) 64 return logL 65 66 67# 例題2 68# x(ω1) = {(0, 4√2), (4, 4√2), (2, 4√2-1), (2, 4√2+1)} 69# x(ω2) = {(0, 0), (4, 0), (2, -1), (2, 1), (2(1-√3), 0), (2(1+√3), 0), (2, -√3), (2, √3)} 70""" 71x1 = np.array([[0.0, 4.0*np.sqrt(2.0)], 72 [4.0, 4.0*np.sqrt(2.0)], 73 [2.0, 4.0*np.sqrt(2.0)-1.0], 74 [2.0, 4.0*np.sqrt(2.0)+1.0]]) 75x2 = np.array([[0.0, 0.0], 76 [4.0, 0.0], 77 [2.0,-1.0], 78 [2.0, 1.0], 79 [2.0*(1.0-np.sqrt(3.0)), 0], 80 [2.0*(1.0+np.sqrt(3.0)), 0], 81 [2.0,-np.sqrt(3.0)], 82 [2.0, np.sqrt(3.0)]]) 83# Set the drow range 84x = y = np.arange(-2, 18, 0.1) 85""" 86 87# 88# 例題3 89# x(ω1) = {(-2, 0), (0, 1), (0, -1), (2, 0)} 90# x(ω2) = {(3, 2), (4, -1), (6, 1), (7, -2)} 91x1 = np.array([[-2.0, 0.0], 92 [0.0, 1.0], 93 [0.0,-1.0], 94 [2.0, 0.0]]) 95x2 = np.array([[3.0, 2.0], 96 [4.0,-1.0], 97 [6.0, 1.0], 98 [7.0,-2.0]]) 99# Set the drow range 100x = y = np.arange(-5, 10, 0.1) 101 102# 103# Create Distributions 104# 105dist1 = Norm2D(x1) 106dist2 = Norm2D(x2) 107 108# 109# Show Mu and Sigma 110# 111Mu1, Sigma1 = dist1.get_param() 112print('Mu1=', Mu1); 113print('Sigma1=', Sigma1) 114 115Mu2, Sigma2 = dist2.get_param() 116print('Mu2=', Mu2) 117print('Sigma2=', Sigma2) 118 119 120# 121# Create the drawing mesh 122# 123X, Y = np.meshgrid(x, y) 124pos = np.dstack((X,Y)) 125 126# 127# Calc. pdf 128# 129Z1 = dist1.pdf(pos) 130Z2 = dist2.pdf(pos) 131 132Z = np.log(np.fmax(Z1, Z2)) # Logarithmic expression 133Zdiff = Z1 - Z2 134 135maxZ = np.max(Z) 136minZ = np.min(Z) 137#print('max Z=', maxZ) 138#print('min Z=', minZ) 139 140minZ = -20 # Clip the lowest Z value 141 142 143# 144# Init. a graph 145# 146fig = plt.figure(figsize=(7,5)) 147 148ax = fig.add_subplot(111, aspect='equal') 149ax.grid() 150plt.xlabel('x') 151plt.ylabel('y') 152 153 154# 155# draw the contour 156# 157levels = np.linspace(minZ, maxZ, 50) 158cs = ax.contourf(X, Y, Z, levels=levels, cmap=cm.inferno, extend='both') 159 160# 161# draw the boundary 162# 163ax.contour(X, Y, Zdiff, levels=[-1.0e-300, 1.0e-300], colors='r', linestyles='-') 164 165# 166# data 167# 168ax.scatter(dist1.get_X()[:,0], dist1.get_X()[:,1], s=40, c='red', marker='o', alpha=0.5, linewidths='2') 169ax.scatter(dist2.get_X()[:,0], dist2.get_X()[:,1], s=40, c='blue', marker='o', alpha=0.5, linewidths='2') 170 171# 172# draw a colorbar 173# 174divider = make_axes_locatable(ax) # create the same size divider as the contour graph 175cax = divider.append_axes('right', size='5%', pad=0.1) # append 5% space to the right-end 176fig.colorbar(cs, cax=cax, format='%.1f') # show a colorbar 177 178# 179# Draw 180# 181plt.show()