我有一些問題,我希望你能幫助我謝謝!!!!
我有一張看起來像這樣的桌子:
| 計算機 | 資料 | 數數 |
|---|---|---|
| 一種 | 2021 年 1 月 1 日 | 43 |
| 一種 | 2021 年 2 月 1 日 | 64 |
| 一種 | 2021 年 3 月 1 日 | 333 |
| 一種 | 2021 年 4 月 1 日 | 656 |
| 乙 | 2021 年 1 月 1 日 | 41 |
| 乙 | 2021 年 2 月 1 日 | 436 |
| 乙 | 2021 年 3 月 1 日 | 745 |
| 乙 | 2021 年 4 月 1 日 | 234 |
我想只在部分表上運行隔離森林演算法
我不會像 df[df['Computer'] == A]['Count'] 那樣手動執行每臺計算機的操作,因為每臺計算機都有 500 臺不同的計算機。所以我不知道該怎么做:
scaler = StandardScaler()
np_scaled = scaler.fit_transform(df[df['Computer'] == A]['Count'].values.reshape(-1, 1))
data = pd.DataFrame(np_scaled)
# train isolation forest
model = IsolationForest(contamination=float(.01))
model.fit(data)
df['anomaly'] = model.predict(data)
500 次(對于 A 和 B 和 C 等等)有辦法做到這一點高效感謝!!!
結果,它應該看起來像這樣,但每次它的檢查例外僅針對 A 單獨,B 單獨等等
| 計算機 | 資料 | 數數 | 例外 |
|---|---|---|---|
| 一種 | 2021 年 1 月 1 日 | 43 | 1 |
| 一種 | 2021 年 2 月 1 日 | 64 | 1 |
| 一種 | 2021 年 3 月 1 日 | 333 | 1 |
| 一種 | 2021 年 4 月 1 日 | 656 | -1 |
| 乙 | 2021 年 1 月 1 日 | 41 | 1 |
| 乙 | 2021 年 2 月 1 日 | 436 | 1 |
| 乙 | 2021 年 3 月 1 日 | 745 | 1 |
| 乙 | 2021 年 4 月 1 日 | 234 | 1 |
uj5u.com熱心網友回復:
您可以分組Computer并使用transform來執行您在每個組上已經擁有的函式,將與原始索引相同的索引回傳到anomaly列。
def train_isolation_group(group_count):
scaler = StandardScaler()
np_scaled = scaler.fit_transform(group_count.values.reshape(-1, 1))
data = pd.DataFrame(np_scaled)
# train isolation forest
model = IsolationForest(contamination=float(.01))
model.fit(data)
return model.predict(data)
df['anomaly'] = df.groupby('Computer')['Count'].transform(train_isolation_group)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/456590.html
