A brief note on Simulation and Data with pandas: seeded random numbers, Monte Carlo and its error, random walks and decay, the standard error, and pandas tables: selecting, cleaning, grouping and plotting. With a worked example, practice, a program and questions to check yourself.
A brief note on Simulation and Data with pandas. Read it before a test. Try each question at the end before you open its answer.
rng = np.random.default_rng(seed) makes a generator. The same seed gives the same numbers, so a result can be checked.rng.random(n), rng.uniform(a, b, n), rng.normal(mean, sd, n), rng.choice(items) and rng.shuffle(items) cover the usual needs.rng.integers(1, 7, size=n) throws a die times. The stop, 7, is not included.np.cumsum(steps). For many walkers, use a row for each and cumsum(axis=1).rng.binomial(n, p) gives the number of decays among atoms in one step.std(ddof=1):df.shape, df.columns, df.dtypes, df.head() and df.describe().df["a"] is a column and df[["a", "b"]] a smaller table. df.iloc[2] is a row by place, and df.loc picks by label. A loc range includes both ends.pd.read_csv reads a file. df.isna().sum() counts the missing values; dropna and fillna deal with them. Say what you did.df[df["a"] > 2] picks rows. Join conditions with & and |, each in brackets. sort_values, isin and query help.df["speed"] = df["dist"] / df["time"].df.groupby("team")["speed"].mean() splits the rows, summarises each group and combines the results. pd.merge(a, b, on="name") joins two tables.df.plot.scatter(x="a", y="b") draws from a table; import matplotlib.pyplot and call plt.show().A school records six runners. Which team ran faster on average, and who ran 400 m for the red team?
runs["distance_m"] / runs["time_s"] divides place by place, so every runner gets a speed. Anu's is m/s.groupby("team") splits the six rows into two teams. ["speed_m_s"].mean() gives one mean for each: blue 5.97 m/s, red 6.05 m/s.&. Only Anu and Esha are red runners over 400 m.So the red team was a little faster, by about 0.08 m/s.
How likely is a total of 15 or more with three dice? Of the 216 outcomes, 20 give 15 or more. The program throws the dice more and more times and compares the share with the exact answer.
default_rng(42) or any fixed seed.rng.integers(1, 6) for a die. The stop is not included, so 6 never comes. Write rng.integers(1, 7).std() without ddof=1 for a sample. It makes the uncertainty a little too small.df["mass"] stops with KeyError: 'mass'. Look at df.columns for the exact name.and between columns. df[df["a"] > 2 and df["b"] < 5] stops with ValueError: The truth value of a Series is ambiguous. Use & with brackets.rng.integers(0, 2, size=1000). Why does the number of 1s change each time the program runs, and how can she make it the same every time?df.loc[2:4] and df.iloc[2:4] give?df.groupby("house")["points"].sum() gives.Log in to join the discussion.
Loading…