早安,
有人可以幫我嗎? …
我想將數字列的空值替換為平均列值。
我試過這個
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),
);
);
打開“大類”,在列出的列中缺少一些單元格,然後嘗試一下。
嗨伊恩
首先感謝您的幫助和建議。你的腳本運行得很好
所以.... 我嘗試將您的腳本整合到一個更大的腳本中,以減少和簡化資料庫。
這裡是整個作品
名稱預設為此處( 1 );
清除全域變數();
// 用於在所有開啟的表中選擇哪一個進行分析的程式碼
OpenDT = 列表();
For( i = 1, i <= N 表(), i++,
插入( OpenDT, 資料表( i ) )
);
dlgDT = Dialog( "選擇資料表進行分析", VList( selectDT = 列錶框( OpenDT ) ), HList( Button( "確定" ), Button( "取消" ) ) );
顯示( dlgDT );
如果( dlgDT["按鈕"] == 1,
刪除自( dlgDT, N 個項目( dlgDT ) );
評估列表( dlgDT );
);
當前資料表( selectDT[1] );
// ------------------------------------------------ --GUI ------ -----------------------------------//
cd = 列對話框(
Title(「資料預處理」),
List= ColList(" 選擇列 >> ", MinCol( 1 ) ),
HList( " 欄位中空元素的百分比 -> (0-100) ", mypercent1 = EditNumber( 50 ) ),
HList( " 欄位中停滯元素的百分比 -> (0-100) ", mypercent = EditNumber( 80 ) ),
V列表(
numeric = Check Box("列數值 - 以其他儲存格的平均值估算缺失儲存格", 1 ),
字元 = 複選框( "列字元 - 用 N/A 估算缺少的儲存格", 1 ),
),
);
//變數宣告
impute_num_col = cd["數字"]; // 估算數字
impute_char = cd["字元"]; // 估算字符
百分比 = cd["mypercent"];
百分比1 = cd["mypercent1"];
mylist = cd["列表"];
dt=選擇DT[1];
德爾科爾斯 = {};
行數 = n行(dt);
for(i=1; maxi=Nitems(mylist), i<=maxi, i++,
if(nrows(loc(aslist(列(dt,mylist[i])<= numRows * 百分比/100,
插入(delcols, myList[i]);
,
if(N 欄位缺失(列(dt,mylist[i])) >= 行數 * 百分比1/100,
插入(delcols, myList[i]);
);
);
);
nitems = N 項目( delcols );
列名 = {};
列名列表 = {};
For( 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++,
值 = Column( dt, cc[i] ) << 取得值( 1 );
值[Loc( 缺失( 值 ) )] = 平均值( 值 );
Column( dt, cc[i] ) << setvalues( 值, cc[i] );
);
);
// 列字元 - 用 NA 估算缺少的儲存格
如果( impute_char == 1,
FILL = dt << 取得列名(字元);
For( i = 1, i <= N 項( FILL ), i++,
FILL[i][dt << 取得行,其中( Is Missing( FILL[i][] ) )] = "NA";
);
);
但問題是腳本的最後一部分(#Columns 字元 - 用 NA# 估算缺少的單元格)
不知道為什麼…不起作用…
非常奇怪,因為如果沒有整合到上面的工作中,相同的程式碼也可以工作。
dt = 當前資料表();
FILL = dt << 取得列名(字元);
For( i = 1, i <= N 項( FILL ), i++,
FILL[i][dt << 取得行,其中( 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);
也許最好的做法是透過為單元格著色來描繪所估算的值。