為了確認相關的演算法函數庫,執行書本中的mytest1.py程式
結果得到以下的錯誤訊息:
Traceback (most recent call last):
File "D:\python_learning\mytest1_1.py", line 13, in
plt.scatter(dataMat[0],dataMat[1],c='red',marker='o')
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3378, in scatter
edgecolors=edgecolors, data=data, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 1717, in inner
return func(ax, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 3996, in scatter
x, y, s, c, colors, edgecolors, linewidths)
File "C:\Python27\lib\site-packages\matplotlib\cbook\__init__.py", line 1640, in delete_masked_points
raise ValueError("Masked arrays must be 1-D")
ValueError: Masked arrays must be 1-D
經由google查詢後,得到scatter中需要放的是array
原始碼及解法如下
改完後就可以執行了
# -*- coding: utf-8 -*-
# Filename : mytest1.py
import numpy as np
from numpy import *
import matplotlib.pyplot as plt
dataSet = [[-0.017612,14.053064], [-1.395634,4.662541],[-0.752157
,6.538620],[-1.322371,7.152853],[0.423363,11.054677],[0.406704
,7.067335],[0.667394,12.741452],[-2.460150,6.866805],[0.569411
,9.548755],[-0.026632,10.427743],[0.850433,6.920334],[1.347183
,13.175500],[1.176813,3.167020],[-1.781871,9.097953]]
dataMat = mat(dataSet).T
#plt.scatter(dataMat[0],dataMat[1],c='red', market='o') #原始代碼
plt.scatter(array(dataMat[0]),array(dataMat[1]),c='red',marker='o') #修改後代碼
X = np.linspace(-2,2,100)
Y = 2.8*X+9
plt.plot(X,Y)
plt.show()
執行結果為: