Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions HW2 Numpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# coding: utf-8

# In[1]:


import numpy as np


# In[2]:


a = np.array([[1, 6],
[2, 8],
[3, 11],
[3, 10],
[1, 7]])
a


# In[8]:


mean_a=a.mean(axis = 0)

mean_a


# In[9]:


a_centered=np.subtract(a, mean_a)

a_centered


# In[10]:


c = a_centered[:, 0].copy()

d = a_centered[:, 1].copy()

a_centered_sp=np.dot(c,d)
a_centered_sp


# In[12]:


N=a.shape[0]
N


# In[13]:


covar=a_centered_sp/(N-1)

covar


# In[6]:


a_tr = np.transpose(a)
a_tr
cov_tr = np.cov(a_tr)
covar2 = cov_tr[0, 1].copy()
covar2

109 changes: 109 additions & 0 deletions HW2 Pandas
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env python
# coding: utf-8

# In[29]:


import pandas as pd
import numpy as np


# In[19]:


a = {
"author_id": [1, 2, 3],
"author_name": ['Тургенев', 'Чехов', 'Островский']
}

authors = pd.DataFrame(a)

authors


# In[20]:


a = {
"author_id": [1, 1, 1, 2, 2, 3, 3],
"book_title ": ['Отцы и дети', 'Рудин', 'Дворянское гнездо', 'Толстый и тонкий', 'Дама с собачкой', 'Гроза', 'Таланты и поклонники'],
"price": [450, 300, 350, 500, 450, 370, 290]
}

book = pd.DataFrame(a)

book


# In[21]:


authors_price = authors.merge(book, left_on="author_id", right_on="author_id", how="left")
authors_price


# In[22]:


top5 = authors_price.nlargest(5, "price")
top5


# In[23]:


df1 = authors_price.groupby('author_name').agg({'price': 'min'}).rename(columns={'price':'min_price'})
df1
df2 = authors_price.groupby('author_name').agg({'price': 'max'}).rename(columns={'price':'max_price'})
df2
df3 = authors_price.groupby('author_name').agg({'price': 'mean'}).rename(columns={'price':'mean_price'})
df3

authors_stat = pd.concat([df1, df2, df3], axis = 1)
authors_stat


# In[24]:


authors_price["cover"] = ['твердая', 'мягкая', 'мягкая', 'твердая', 'твердая', 'мягкая', 'мягкая']
authors_price


# In[42]:


book_info = pd.pivot_table(authors_price, index=['author_name'], values="price", columns=['cover'], aggfunc=np.sum,
fill_value=0)
book_info


# In[39]:


get_ipython().run_line_magic('pinfo', 'pd.pivot_table')


# In[43]:


book_info.to_pickle("book_info.pkl")


# In[44]:


book_info2 = pd.read_pickle("book_info.pkl")


# In[45]:


book_info2


# In[ ]: