第一問
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了鐵嶺免費建站歡迎大家使用!
Hello,This is a fruit!
Hello,The is an apple!
因為子類構(gòu)造方法中
Apple() {
System.out.println("Hello,The is an apple!");
}
在沒有特定聲明時等價于
Apple() {
super();//這里將執(zhí)行父類的構(gòu)造方法
System.out.println("Hello,The is an apple!");
}
第二問題
執(zhí)行結(jié)果是
T0
gender is wrong
T3
T4
try{
System.out.println("T0");// 1這里始終會執(zhí)行,一進到try就執(zhí)行
if(!flag1||!flag2)
throw new GenderException();// 2、這里判斷得到,當兩個都為false時拋出異常,但沒有打印信息
System.out.println("T1");
}catch(GenderException e){
System.out.println("gender is wrong");//3,步驟2拋出的異常在這里被截取,所以打印出來
}catch(Exception){
System.out.println("T2");
}finally{
System.out.println("T3");//4,try catch下 finally始終會執(zhí)行,也打印
}
System.out.println("T4");// 5、try catch 外,繼續(xù)執(zhí)行
}
}
模糊C均值聚類算法,可將輸入的數(shù)據(jù)集data聚為指定的cluster_n類
【函數(shù)描述】
語法格式
[center, U, obj_fcn] = FCM(data, cluster_n, options)
用法:
1. [center,U,obj_fcn] = FCM(Data,N_cluster,options);
2. [center,U,obj_fcn] = FCM(Data,N_cluster);
輸入變量
data ---- n*m矩陣,表示n個樣本,每個樣本具有m維特征值
cluster_n ---- 標量,表示聚合中心數(shù)目,即類別數(shù)
options ---- 4*1列向量,其中
options(1): 隸屬度矩陣U的指數(shù),1(缺省值: 2.0)
options(2): 最大迭代次數(shù)(缺省值: 100)
options(3): 隸屬度最小變化量,迭代終止條件(缺省值: 1e-5)
options(4): 每次迭代是否輸出信息標志(缺省值: 0)
輸出變量
center ---- 聚類中心
U ---- 隸屬度矩陣
obj_fcn ---- 目標函數(shù)值
以下代碼調(diào)試通過:
1234567class?LuciaClass:??#?定義類????def?luciaprint(self,?text):??#?類里面的方法????????print('\n',?text)??#?方法就是輸出?text??x?=?LuciaClass()??#?方法的實例?xx.luciaprint('today?is?a?bad?day?~~~')??#?實例調(diào)用類方法
運行效果:
Java中如果除運算符“/”,在不加任何限制的情況下,兩個整數(shù)相除,得到的是整數(shù),小數(shù)點后的被舍棄。但是有些場景下我們需要拿到除得的小數(shù),還要指定位數(shù)的小數(shù)。這時候有以下處理方法:
1.使用DecimalFormat來限定得到的小數(shù)位數(shù)
int pcm = 98;
int fcm = 11;
DecimalFormat df = new DecimalFormat("0.00");
double tmpVal = Double.parseDouble(df.format((double) pcm/(pcm+fcm)));
//get value 0.89
注意,它默認返回的是String,如果需要double/float要做一下轉(zhuǎn)換。
2.直接使用Decimal運算
@Test
public void testDecimalOper(){
int pcm = 94;
int fcm = 11;
BigDecimal pcmbd = new BigDecimal(pcm);
BigDecimal fcmbd = new BigDecimal(fcm);
BigDecimal rate = new BigDecimal(0.00);
rate = pcmbd.divide(pcmbd.add(fcmbd), 2, RoundingMode.HALF_UP);
System.out.println(rate);//0.90
}
float/double在工程運算中使用的比較多,在商業(yè)計算中使用Decimal類型的比較多。(注:
在《Effective Java》這本書中也提到這個原則,float和double只能用來做科學計算或者是工程計算,在商業(yè)計算中我們要用 java.math.BigDecimal,另外,我們?nèi)绻枰_計算,要用String來夠造BigDecimal。在《Effective Java》一書中的例子是用String來夠造BigDecimal的。(注意:divide方法中推薦使用枚舉RoundingMode.HALF_UP)
)
兩種方式都可以。推薦使用第二種方式來處理精度和round mode的設(shè)置。
附BigDecimal rouding mode:
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a nonzero discarded fraction. Note that this rounding
* mode never decreases the magnitude of the calculated value.
*/
public final static int ROUND_UP = 0;
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*/
public final static int ROUND_DOWN = 1;
/**
* Rounding mode to round towards positive infinity. If the
* {@code BigDecimal} is positive, behaves as for
* {@code ROUND_UP}; if negative, behaves as for
* {@code ROUND_DOWN}. Note that this rounding mode never
* decreases the calculated value.
*/
public final static int ROUND_CEILING = 2;
/**
* Rounding mode to round towards negative infinity. If the
* {@code BigDecimal} is positive, behave as for
* {@code ROUND_DOWN}; if negative, behave as for
* {@code ROUND_UP}. Note that this rounding mode never
* increases the calculated value.
*/
public final static int ROUND_FLOOR = 3;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code ROUND_UP} if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
* that this is the rounding mode that most of us were taught in
* grade school.
*/
public final static int ROUND_HALF_UP = 4;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code ROUND_UP} if the discarded
* fraction is {@literal } 0.5; otherwise, behaves as for
* {@code ROUND_DOWN}.
*/
public final static int ROUND_HALF_DOWN = 5;
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code ROUND_HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code ROUND_HALF_DOWN} if it's even. Note that this is the
* rounding mode that minimizes cumulative error when applied
* repeatedly over a sequence of calculations.
*/
public final static int ROUND_HALF_EVEN = 6;
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* {@code ArithmeticException} is thrown.
*/
public final static int ROUND_UNNECESSARY = 7;
模糊c均值聚類
函數(shù): fcm
格式: [center,U,obj_fcn] = fcm(data,cluster_n)
舉例如下所示:
data = rand(100, 2);
[center,U,obj_fcn] = fcm(data, 2);
plot(data(:,1), data(:,2),'o');
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2, :) == maxU);
line(data(index1,1), data(index1, 2), 'linestyle', 'none', 'marker', '*', 'color', 'g');
line(data(index2,1), data(index2, 2), 'linestyle', 'none', 'marker', '*', 'color', 'r');
當前標題:FCM代碼java FCM代碼
當前網(wǎng)址:http://jinyejixie.com/article32/doohhpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站策劃、微信小程序、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)