python實(shí)現(xiàn)PolynomialFeatures多項(xiàng)式的方法
import numpy as npfrom sklearn.preprocessing import PolynomialFeatures #這哥用于生成多項(xiàng)式x=np.arange(6).reshape(3,2) #生成三行二列數(shù)組reg = PolynomialFeatures(degree=3) #這個(gè)3看下面的解釋reg.fit_transform(x)
x是下面這樣:
我們發(fā)現(xiàn)規(guī)律如下:
編寫實(shí)現(xiàn)函數(shù)如下:
def multi_feature(x,n): c = np.empty((x.shape[0],0)) #np.empty((3,1))并不會(huì)生成一個(gè)3行1列的空數(shù)組,np.empty((3,0))才會(huì)生成3行1列空數(shù)組 for i in range(n+1): for m in range(i,-1,-1): h=(x[:,0]**m) * (x[:,1]**(i-m)) c=np.c_[c,h] return cmulti_feature(x,3)
和上面實(shí)現(xiàn)的一模一樣
print(’n=4時(shí),sklearn的輸出是:’)reg = PolynomialFeatures(degree=4) print(reg.fit_transform(x))print(’n’)#對(duì)比print(’n=4時(shí),函數(shù)的輸出是:’)print(multi_feature(x,4))
也是一樣的,當(dāng)然這個(gè)函數(shù)僅適用于2維數(shù)組,如果是n維數(shù)組,又該怎么實(shí)現(xiàn)呢?
到此這篇關(guān)于python實(shí)現(xiàn)PolynomialFeatures多項(xiàng)式的方法的文章就介紹到這了,更多相關(guān)python PolynomialFeatures多項(xiàng)式內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. XML實(shí)體注入深入理解3. 不要在HTML中濫用div4. XML入門的常見(jiàn)問(wèn)題(三)5. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera6. XML入門的常見(jiàn)問(wèn)題(四)7. XML 非法字符(轉(zhuǎn)義字符)8. Xpath語(yǔ)法格式總結(jié)9. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼10. JavaScript多級(jí)判定代碼優(yōu)化淺析
