如何將兩個表相乘我有一個大數據表和一個歸一化因子表。
數據表如下所示:
| ID | 類型 | 參數1 | 參數2 | 參數3 |
| 01 | 1個 | 數據 | 數據 | 數據 |
| 02 | 1個 | 數據 | 數據 | 數據 |
| 03 | 2個 | 數據 | 數據 | 數據 |
| 04 | 2個 | 數據 | 數據 | 數據 |
歸一化因子表如下所示:
| 類型 | 參數1 | 參數2 | 參數3 |
| 1個 | 歸一化因子1 | 歸一化因子2 | 歸一化因子3 |
| 2個 | 歸一化因子4 | 歸一化因子5 | 歸一化因子6 |
如何根據類型和參數將第一個表乘以第二個表,並獲得一個新表,如下所示:
| ID | 類型 | 參數1 | 參數2 | 參數3 |
| 01 | 1個 | 數據*歸一化因子1 | 數據*歸一化因子2 | 數據*歸一化因子3 |
| 02 | 1個 | 數據*歸一化因子1 | 數據*歸一化因子2 | 數據*歸一化因子3 |
| 03 | 2個 | 數據*歸一化因子4 | 數據*歸一化因子5 | 數據*歸一化因子6 |
| 04 | 2個 | 數據*歸一化因子4 | 數據*歸一化因子5 | 數據*歸一化因子6 |
我的原始數據表在行和列中都很大,所以我想要一個快速的解決方案。
非常感謝你。
數據表腳本編寫回复:如何將兩個表相乘您可以使用虛擬聯接和公式列。
虛擬聯接添加了藍色和金色鍵
您可以將它們交互式地鏈接或與JSL鏈接。
這可能與JMP16名稱一起使用, :“ parameter3 [Type]” n句法;您可能需要名稱( “ parameter3 [Type]” ) 反而。
// make example tables. **save them** so virtual join works.
factorDT = New Table( "Imported Data 2",
New Column( "Type", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2] ) ),
New Column( "parameter1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1.1, 1.2] ) ),
New Column( "parameter2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0.8, 0.9] ) ),
New Column( "parameter3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1.1, 0.9] ) )
);
factorDT << save( "$temp/factor.jmp" );
dataDT = New Table( "Imported Data",
New Column( "ID", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3, 4] ) ),
New Column( "Type", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 1, 2, 2] ) ),
New Column( "parameter1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3, 4] ) ),
New Column( "parameter2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4, 3, 2, 1] ) ),
New Column( "parameter3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 3, 3, 2] ) )
);
dataDT << save( "$temp/data.jmp" );
// join the tables
factorDT:type << Set Property( "Link ID", 1 );
dataDT:type << Set Property( "Link Reference", Reference Table( "factor.jmp" ) );
// add formula columns
dataDT << New Column( "p 1", Numeric, "Continuous", Format( "Best", 12 ), Formula( :"parameter1[Type]"n * :parameter1 ) );
dataDT << New Column( "p 2", Numeric, "Continuous", Format( "Best", 12 ), Formula( :"parameter2[Type]"n * :parameter2 ) );
dataDT << New Column( "p 3", Numeric, "Continuous", Format( "Best", 12 ), Formula( :"parameter3[Type]"n * :parameter3 ) );
// force the evaluation to complete
dataDT<<RUNFORMULAS></RUNFORMULAS>
回复:如何將兩個表相乘而且,如果您不需要這些公式,可以考慮矩陣的逐元素乘法,這可能會或可能不會“更好”。 在我的MacBook上,下面的代碼花費了大約15秒鐘。
NamesDefaultToHere(1);
nr = 10000; // Number of rows
nc = 10000; // Number of Columns
dt1 = AsTable(J(nr, nc, RandomInteger(1, 20)));
dt2 = AsTable(J(nr, nc, RandomUniform(0, 1)));
m1 = dt1 << getAsMatrix;
m2 = dt2 << getAsMatrix;
dtFinal = AsTable(m1 :*</img> m2);
回复:如何將兩個表相乘感謝你的回复。 但是,我有很多列,列名很複雜。 因此,我想編寫一個for循環來創建新列。 您可以對此聲明進行更多解釋嗎?
Formula( :"parameter1[Type]"n * :parameter1 )
我正在考慮編寫這樣的for循環:
</CODE></PRE><PRE class="lia-code-sample language-markup"><CODE>colnames = dataDT << Get Column Names();
nc = ncols(dataDT);
DT = New Table("normalized", New Column("ID", values(dataDT:ID << get values)),
New Column("Type", values(dataDT:Type << get values)));
For(i = 3, i<= nc, i++,
DT << New Column( char( colnames[3]), Numeric, "Continuous", Format( "Best", 12 ), Formula( XXXX ) ));但我不知道如何正確構造公式。 它不斷給我錯誤。 你能幫助我嗎?
謝謝你。
回复:如何將兩個表相乘謝謝,它適用於小型數據集。 但是我在表1中有幾百萬行,而在表2中只有兩行。 因此,我想避免創建另一個巨大的表。 如果找不到更好的解決方案,請嘗試這種方法。
回复:如何將兩個表相乘循環並不容易;我認為這可以做到:
// make example tables. **save them** so virtual join works.
factorDT = New Table( "Imported Data 2",
New Column( "Type", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2] ) ),
New Column( "parameter1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1.1, 1.2] ) ),
New Column( "parameter2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0.8, 0.9] ) ),
New Column( "parameter3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1.1, 0.9] ) )
);
factorDT << save( "$temp/factor.jmp" );
dataDT = New Table( "Imported Data",
New Column( "ID", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3, 4] ) ),
New Column( "Type", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 1, 2, 2] ) ),
New Column( "parameter1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3, 4] ) ),
New Column( "parameter2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4, 3, 2, 1] ) ),
New Column( "parameter3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 3, 3, 2] ) )
);
dataDT << save( "$temp/data.jmp" );
originalColumns = dataDT<<GETCOLUMNNAMES></GETCOLUMNNAMES>
- 在通過虛擬聯接添加隱藏的列之前捕獲原始列
- 使用\!來構建表達式e以轉義嵌入的引號
- 使用write()調試您的工作,而不是打印
- << RunFormulas對於速度很重要;列公式的默認背景評估要慢得多