2014-01-30 90 views
1

我打算在兩幅圖像之間建立一個線性迴歸模型,但我需要先遮蔽雲層。如何屏蔽線性迴歸模型的Python numpy數組中的雲?

根據一些討論,掩模陣列可能會有所幫助(http://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html#the-numpy-ma-module)。

但是,這不是我想要的,因爲該函數似乎將缺失值(例如雲)掩蓋爲0,而不是將其刪除。

這是我的隨機數測試。我用scikit-learn作爲迴歸模型的基準。然後,我通過蒙版數組添加了噪音和蒙版噪音。

import numpy as num 
from sklearn import linear_model 
import numpy.ma as ma 

#the initial array 
a=num.array([7,1,10,5,4,5,9,9,3,3]) 
b=num.array([10,9,3,4,7,7,8,1,10,9]) 
a=a.reshape(10,1) 
b=b.reshape(10,1) 
regr = linear_model.LinearRegression() 
regr.fit(a,b) 
regr.coef_ #-0.616504854 
regr.intercept_ #10.25242718 

#add noise by 0 
a=num.array([7,1,10,0,5,4,5,0,9,9,3,3]) 
b=num.array([10,9,3,0,4,7,7,0,8,1,10,9]) 
a=a.reshape(12,1) 
b=b.reshape(12,1) 
maa = ma.masked_array(a, mask=[0, 0, 0, 1,0, 0,0,1,0,0,0,0]) 
mbb = ma.masked_array(b, mask=[0, 0, 0, 1,0, 0,0,1,0,0,0,0]) 
regr = linear_model.LinearRegression() 
regr.fit(maa,mbb) 
regr.coef_ #0.09405941 
regr.intercept_ #5.22772279 

任何暗示達到我的目標?我的目標是刪除圖像中的點,而不是查看爲0.

+0

你有什麼問題?你想消除你的圖像中的點?爲什麼你想要在兩幅圖像之間進行線性迴歸? – phyrox

+0

@phyrox是的,我想擺脫圖像中的點,它們被雲標記。我正在進行圖像預處理。線性迴歸的係數和截距是方程的組成部分。 – Vicky

回答

0

我從Scipy API中找到了用於屏蔽陣列操作的解決方案。這裏是頁面:http://docs.scipy.org/doc/numpy/reference/routines.ma.html

ma.compressed(x)或ma.MaskedArray.compressed()可以消除這些缺失的值。 這是修改後的代碼。

#add noise by 0 
a=num.array([7,1,10,0,5,4,5,0,9,9,3,3]) 
b=num.array([10,9,3,0,4,7,7,0,8,1,10,9]) 
a=a.reshape(12,1) 
b=b.reshape(12,1) 
maa = ma.masked_array(a, mask=[0, 0, 0, 1,0, 0,0,1,0,0,0,0]) 
mbb = ma.masked_array(b, mask=[0, 0, 0, 1,0, 0,0,1,0,0,0,0]) 
maa=ma.compressed(maa).reshape(10,1) 
mbb=ma.compressed(mbb).reshape(10,1) 
regr = linear_model.LinearRegression() 
regr.fit(maa,mbb) 
regr.coef_ #-0.616504854 
regr.intercept_ #10.25242718