np.meshgrid
np.c_[xx.ravel(), yy.ravel()]
代码
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
np.arange(y_min, y_max, 0.02)
)
predict = clf.predict(np.c_[xx.ravel(), yy.ravel()])
predict = predict.reshape(xx.shape)
# xx.ravel()
print("xx: ", xx, len(xx))
print("xx.reval: ", xx.ravel(), len(xx.ravel()))
print("yy: ", yy, len(yy))
print("yy.reval: ", yy.ravel(), len(yy.ravel()))
print("np.c_[xx.ravel(), yy.ravel()]", np.c_[xx.ravel(), yy.ravel()])
执行结果
xx: [[3.3 3.32 3.34 ... 7.94 7.96 7.98]
[3.3 3.32 3.34 ... 7.94 7.96 7.98]
[3.3 3.32 3.34 ... 7.94 7.96 7.98]
...
[3.3 3.32 3.34 ... 7.94 7.96 7.98]
[3.3 3.32 3.34 ... 7.94 7.96 7.98]
[3.3 3.32 3.34 ... 7.94 7.96 7.98]] 220
xx.reval: [3.3 3.32 3.34 ... 7.94 7.96 7.98] 51700
yy: [[1. 1. 1. ... 1. 1. 1. ]
[1.02 1.02 1.02 ... 1.02 1.02 1.02]
[1.04 1.04 1.04 ... 1.04 1.04 1.04]
...
[5.34 5.34 5.34 ... 5.34 5.34 5.34]
[5.36 5.36 5.36 ... 5.36 5.36 5.36]
[5.38 5.38 5.38 ... 5.38 5.38 5.38]] 220
yy.reval: [1. 1. 1. ... 5.38 5.38 5.38] 51700
np.c_[xx.ravel(), yy.ravel()] [[3.3 1. ]
[3.32 1. ]
[3.34 1. ]
...
[7.94 5.38]
[7.96 5.38]
[7.98 5.38]]
解释
xx, yy = np.meshgrid([1, 1, 1],
[2, 2, 2, 2, 2]
)
-
np.meshgrid: 会返回两个np.arange类型的列表
-
xx: 共len([2, 2, 2, 2, 2])行,每行元素均为[1, 1, 1]
-
yy:共len([1, 1, 1])列, 每列元素均为[2, 2, 2, 2, 2]
-
返回的两个矩阵均为:len([2, 2, 2, 2, 2])行,len([1, 1, 1])的矩阵
-
具体请参考: https://www.cnblogs.com/klchang/p/10633972.html
-
xx.reval():将多为列表转换为一维列表,
xx = [ [1, 1, 1], [2, 2, 2] ] xx.reval() [1, 1, 1, 2, 2, 2]
-
np.c_[xx.ravel(), yy.ravel()]: np.c_:是按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等,类似于pandas中的merge()。
a = [1, 1, 1] b = [2, 2, 2] np.c_[a, b] ===> [ [1, 2], [1, 2], [1, 2] ]
- 具体请参考: https://www.cnblogs.com/shaosks/p/9890787.html