早上好,
有人可以幫助我嗎? ...
我想用替換數字列的空值平均列值。
我試過了
dt =當前數據表();
填充 = dt <<獲取列名(數字);
為了( 我 = 1個, 我 <= N 項( 填充 ), i++,
填充[i][dt <<獲取行在哪裡(不見了( 填充 [i][] ) )] =均值(填充[i]);
);
提前致謝
詹保羅
我沒有詳細研究您的代碼,但感覺您所做的工作超出了您的需要。 為了使事情更具可讀性,您可以考慮使用如下函數:
NamesDefaultToHere(1);
// Only impute if more than n cells are missing
imputeContinuousCol =
Function({col, n}, {Default Local},
values = col << getValues;
missingValuePos = Loc(IsMissing(values));
if(NRow(missingValuePos) > n,
values[Loc(IsMissing(values))] = Mean(values);
col << setValues(values);
);
);
// Only impute if more than n cells are missing
imputeCharacterCol =
Function({col, n}, {Default Local},
values = col << getValues;
missingValuePos = Loc(values, "");
if(NRow(missingValuePos) > n,
values[Loc(values, "")] = "NA";
col << setValues(values);
);
);
// Data table . . .
dt = DataTable("Big Class.jmp");
// List of column names that satidfy your imputation criteria . . .
imputeList = {"name", "sex", "height"};
// Loop over this list, and impute if necessary
for (c=1, c<=NItems(imputeList), c++,
col = Column(dt, imputeList[c]);
if(
(col << getModelingType) == "Continuous",
imputeContinuousCol(col, 1),
(col << getDataType) == "Character",
imputeCharacterCol(col, 1),
);
);
打開“Big Class”,在列出的列中缺少一些單元格,然後嘗試一下。
嗨,伊恩
首先感謝您的幫助和建議。你的腳本工作得很好
所以.... 我試圖將您的腳本集成到一個更大的腳本中,以減少和簡化數據庫。
這裡是整個工作
名稱默認為此處(1);
清除全局變量();
// 用於在所有打開的表中選擇哪一個表進行分析的代碼
OpenDT = 列表();
For( i = 1, i <= N 表(), i++,
插入(OpenDT,數據表(i))
);
dlgDT = Dialog( "選擇要分析的數據表", VList( selectDT = List Box( OpenDT ) ), HList( Button( "OK" ), Button( "Cancel" ) ) );
顯示(dlgDT);
如果(dlgDT[“按鈕”] == 1,
刪除自(dlgDT,N 項(dlgDT));
評估列表(dlgDT);
);
當前數據表( selectDT[1] );
// ----------------------------------------GUI ------ ----------------------------------//
cd = 列對話框(
Title("數據預處理"),
List= ColList( "選擇列 >> ", MinCol( 1 ) ),
HList( " % of Empty elements in the column -> (0-100) ", mypercent1 = EditNumber( 50 ) ),
HList( " % of Stagnant elements in the column -> (0-100) ", mypercent = EditNumber( 80 ) ),
列表(
numeric = Check Box( "Columns Numeric - Impute missing cells with the mean of the others", 1),
character = Check Box( "Columns character - Impute missing cells with N/A", 1 ),
),
);
//變量聲明
impute_num_col = cd["數字"]; // 估算數字
impute_char = cd["字符"]; // 估算字符
百分比 = cd["我的百分比"];
percent1 = cd["mypercent1"];
mylist = cd["列表"];
dt=選擇DT[1];
delcols = {};
numRows = nrows(dt);
for(i=1; maxi=Nitems(mylist), i<=maxi, i++,
如果(nrows(loc(aslist(column(dt,mylist[i])<= numRows * percent/100,
插入(delcols,myList[i]);
,
if(Col N Missing(column(dt,mylist[i])) >= numRows * percent1/100,
插入(delcols,myList[i]);
);
);
);
nitems = N 項(delcols);
列名 = {};
colnamelist = {};
對於 ( p = 1, p <= nitems, p++,
colname = Column( delcols[p] ) << 獲取名稱;
插入(colnamelist,colname(p));
);
dt << 刪除列(colnamelist);
//好的! 列數字 - 用其他單元格的平均值估算缺失的單元格
如果(impute_num_col == 1,
cc = dt << 獲取列名(連續);
ncols = N 項( cc );
對於 ( i = 1, i <= ncols, i++,
values = Column( dt, cc[i] ) << get values( 1 );
values[Loc( Is Missing( values ) )] = Mean( values );
Column( dt, cc[i] ) << setvalues( values, cc[i] );
);
);
// 列字符 - 用 NA 估算缺失的單元格
如果(impute_char == 1,
FILL = dt << 獲取列名(字符);
For( i = 1, i <= N Items( FILL ), i++,
FILL[i][dt << get rows where( Is Missing( FILL[i][] ) )] = "NA";
);
);
但問題是腳本的最後一部分(#Columns character - Impute missing cells with NA#)
不知道為什麼……不起作用……
很奇怪,因為如果沒有集成到上面的工作中,相同的代碼就可以工作。
dt = 當前數據表();
FILL = dt << 獲取列名(字符);
For( i = 1, i <= N Items( FILL ), i++,
FILL[i][dt << get rows where( Is Missing( FILL[i][] ) )] = "NA";
);
你知道為什麼會這樣嗎... ?
感謝合作回复:如何用平均列值替換數字列的空值請找到一種方法來做到這一點:
NamesDefaultToHere(1);
// Make some test data
dt = NewTable("Test", NewColumn("Data", Numeric, Continuous, Formula(if(Mod(Row(), 2) == 0, RandomNormal()))));
dt << addRows(20);
dt << runFormulas;
Column(dt, "Data") << deleteFormula;
// Impute missing cells with the mean of the others
Wait(3);
col = Column(dt, "Data");
values = col << getValues;
values[Loc(IsMissing(values))] = Mean(values);
col << setValues(values);
描繪通過為單元格著色而估算的值可能是一種很好的做法。