173 KiB
173 KiB
Работа с NumPy
In [14]:
import numpy as np
matrix = np.array([[4, 5, 0], [9, 9, 9]])
print("matrix = \n", matrix, "\n")
tmatrix = matrix.T
print("tmatrix = \n", tmatrix, "\n")
vector = np.ravel(matrix)
print("vector = \n", vector, "\n")
tvector = np.reshape(vector, (6, 1))
print("tvector = \n", tvector, "\n")
list_matrix = list(matrix)
print("list_matrix = \n", list_matrix, "\n")
str_matrix = str(matrix)
print("matrix as str = \n", str_matrix, "\n")
print("matrix type is", type(matrix), "\n")
print("vector type is", type(vector), "\n")
print("list_matrix type is", type(list_matrix), "\n")
print("str_matrix type is", type(str_matrix), "\n")
formatted_vector = "; ".join(map(str, vector))
print("formatted_vector = \n", formatted_vector, "\n")
Работа с Pandas DataFrame
Работа с данными - чтение и запись CSV
In [15]:
import pandas as pd
df = pd.read_csv("data/titanic.csv", index_col="PassengerId")
df.to_csv("test.csv")
Работа с данными - основные команды
In [16]:
df.info()
print(df.describe().transpose())
cleared_df = df.drop(["Name", "Ticket", "Embarked"], axis=1)
print(cleared_df.head())
print(cleared_df.tail())
sorted_df = cleared_df.sort_values(by="Age")
print(sorted_df.head())
print(sorted_df.tail())
Работа с данными - работа с элементами
In [17]:
print(df["Age"])
print(df.loc[100])
print(df.loc[100, "Name"])
print(df.loc[100:200, ["Age", "Name"]])
print(df[0:3])
print(df.iloc[0])
print(df.iloc[3:5, 0:2])
print(df.iloc[[3, 4], [0, 1]])
Работа с данными - отбор и группировка
In [18]:
s_values = df["Sex"].unique()
print(s_values)
s_total = 0
for s_value in s_values:
count = df[df["Sex"] == s_value].shape[0]
s_total += count
print(s_value, "count =", count)
print("Total count = ", s_total)
print(df.groupby(["Pclass", "Survived"]).size().reset_index(name="Count")) # type: ignore
Визуализация - Исходные данные
In [19]:
data = df[["Pclass", "Survived", "Age"]].copy()
data.dropna(subset=["Age"], inplace=True)
print(data)
In [20]:
def q1(x):
return x.quantile(0.25)
# median = quantile(0.5)
def q2(x):
return x.quantile(0.5)
def q3(x):
return x.quantile(0.75)
def iqr(x):
return q3(x) - q1(x)
def low_iqr(x):
return max(0, q1(x) - 1.5 * iqr(x))
def high_iqr(x):
return q3(x) + 1.5 * iqr(x)
quantiles = data[["Pclass", "Age"]].groupby(["Pclass"]).aggregate(["min", q1, q2, "median", q3, "max"])
print(quantiles)
iqrs = data[["Pclass", "Age"]].groupby(["Pclass"]).aggregate([low_iqr, iqr, high_iqr])
print(iqrs)
data.boxplot(column="Age", by="Pclass")
Out[20]:
Визуализация - Гистограмма
In [21]:
data.plot.hist(column=["Age"], bins=80)
Out[21]:
Визуализация - Точечная диаграмма
In [22]:
df.plot.scatter(x="Age", y="Sex")
df.plot.scatter(x="Pclass", y="Age")
Out[22]:
Визуализация - Столбчатая диаграмма
In [23]:
plot = data.groupby(["Pclass", "Survived"]).size().unstack().plot.bar(color=["pink", "green"])
plot.legend(["Not survived", "Survived"])
Out[23]:
Визуализация - Временные ряды
In [24]:
from datetime import datetime
import matplotlib.dates as md
ts = pd.read_csv("data/dollar.csv")
ts["date"] = ts.apply(lambda row: datetime.strptime(row["my_date"], "%d.%m.%Y"), axis=1)
ts.info()
print(ts)
plot = ts.plot.line(x="date", y="my_value")
plot.xaxis.set_major_locator(md.DayLocator(interval=10))
plot.xaxis.set_major_formatter(md.DateFormatter("%d.%m.%Y"))
plot.tick_params(axis="x", labelrotation=90)