Immer Aya! Ich habe den Code oft geschrieben und den Mittelwert und die Kovarianz ausgegeben, aber notiere es mir. Der Inhalt ist wirklich einfach. Es gibt keine Wendung. Es tut mir Leid.
Situation: Ich habe Pandas-Daten und möchte den Mittelwert und die Kovarianz für bestimmte Daten X, Y, Z ermitteln
Der Punkt ist
from pandas import DataFrame
from numpy import random
import json
df = DataFrame(random.randint(0,100,size=(252, 4)), columns=list('XYZW'))
output_data = dict()
# 1. extract XYZ
df_xyz = df.loc[:,list("XYZ")]
# 2-1 mean vector
u = df_xyz.mean()
output_data["mean"] = u.values.tolist()
# 2-2 covariance
s = df_xyz.cov()
output_data["covariance"] = s.values.tolist()
# 3
with open("out.json", 'w') as f:
json.dump(output_data, f, indent=2)
Die Ausgabe-JSON-Datei ist
{
"mean": [
48.34126984126984,
50.52777777777778,
51.492063492063494
],
"covariance": [
[
877.6360589388478,
-44.88202744577245,
-71.94548788971099
],
[
-44.88202744577245,
876.4733289065962,
-32.312527667109336
],
[
-71.94548788971099,
-32.312527667109336,
784.7768291911716
]
]
}
ist.
Ich habe einige Nachforschungen angestellt, bis ich zu dieser Implementierung kam. (Schweiß) Die DataFrame-Kovarianz finden Sie im API-Dokument (hier).
(2020/05/11)
from numpy import isnan
if isnan(x).any():
x = zeros(3)
if isnan(S).any():
S = zeros( (3,3) )
Recommended Posts