{"cells":[{"metadata":{"trusted":true,"collapsed":true,"_uuid":"180389da4eca6674c8977d68028947c0e5a93dc6"},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n%matplotlib inline","execution_count":2,"outputs":[]},{"metadata":{"_uuid":"3d4d13f6672ea2b517cb653fc398bfb450ee0a3e"},"cell_type":"markdown","source":"I have noted the number of teams in last month,Now I did some sample prediction of teams and competitors, The result of teams at **2018-06-21** is **1962**. Maybe the ture number **will be lower,**because there will be inflection point in the end,I think."},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"269ad1012fee8216255db0c5c6b6b43dd6ba545c"},"cell_type":"code","source":"df = pd.read_excel('../input/avitoxlsx/avito.xlsx',parse_dates=['date'])","execution_count":3,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70dc8b6f5e3c5ce70c89c18da224ae1ee56a13d2","collapsed":true},"cell_type":"code","source":"df_all = pd.DataFrame({'date':pd.date_range('2018-5-15','2018-6-11')})","execution_count":4,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"147c340ab2df63cd2e8ae9badceff1193e9b5831"},"cell_type":"code","source":"#fill nan\ndf = df_all.merge(df,on='date',how='left')","execution_count":5,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b68fd902d6b9b73e3c3a82e7168751a7676c399","collapsed":true},"cell_type":"code","source":"def fillna(vals):\n    lastval = 0\n    lastnum = 0\n    nans = []\n    for i,num in enumerate(vals):\n        if np.isnan(num):\n            nans.append(i)\n        if not np.isnan(num) and nans:\n            for j in nans:\n                vals[j]= (num+lastnum)/2\n            nans = []\n#         print(lastnum,num,not np.isnan(num))\n        if not np.isnan(num):\n            lastnum=num\n    return vals","execution_count":6,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35bdf2d64b5a95dad6b30b373c3dd37b3af17ed7","collapsed":true},"cell_type":"code","source":"df['teams'] = fillna(df.teams.values)\ndf['competitors'] = fillna(df.competitors.values)","execution_count":7,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"660ad67db8e0a27d9bcf966e928ac6be3f8a4b71","collapsed":true},"cell_type":"code","source":"df","execution_count":8,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64824b7a897ccad664bbcd476ffbd2cd07ca34b8","collapsed":true},"cell_type":"code","source":"df.plot.line(x='date',y='competitors')","execution_count":9,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8518c0d3e2e81d135b8af1a8e01d001900d51085","collapsed":true},"cell_type":"code","source":"df.plot.line(x='date',y='teams')","execution_count":10,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"a8212fc33b01f3b89c10c7b44963457ae0e7094b"},"cell_type":"code","source":"from  sklearn.linear_model import *","execution_count":11,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6986b0f049055f6df4363b7173861fe6840c23da"},"cell_type":"code","source":"lr = Ridge(alpha=10)","execution_count":79,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8eac8cb2bc13dceb851078a39c3b096252252213"},"cell_type":"code","source":"lr.fit(df.date.dt.dayofyear.values.reshape(-1,1),df.competitors)","execution_count":80,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"85a8df37673731239cd5e5d9d877d5882e4cd119"},"cell_type":"code","source":"test_df = pd.DataFrame({'date':pd.date_range(start='2018-06-11',end='2018-06-21'),'teams':np.NAN,'competitors':np.NAN})","execution_count":81,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84ee93c0d2c4bfe81913893b6c1d3011a8030f58"},"cell_type":"code","source":"test_df","execution_count":82,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"bd68a48e351671a66471777374deaae16d63918b"},"cell_type":"code","source":"test_df['competitors'] = lr.predict(test_df.date.dt.dayofyear.values.reshape(-1,1))","execution_count":83,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94b764ad09febf15af7b9cbc515b57abe58f65a2"},"cell_type":"code","source":"test_df.plot.line(x='date',y='competitors')","execution_count":84,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"02ceff83ff0268bd0397dabe4e590cdce4431a96"},"cell_type":"code","source":"lr.fit(df.date.dt.dayofyear.values.reshape(-1,1),df.teams)","execution_count":85,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3c907072d90c72d68a69eaa34eed359ae0ee8a82"},"cell_type":"code","source":"test_df['teams'] = lr.predict(test_df.date.dt.dayofyear.values.reshape(-1,1))\n","execution_count":86,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30dad7ad5abd07a0d8c94c404a7225158244b770"},"cell_type":"code","source":"test_df.plot.line(x='date',y='teams')","execution_count":87,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1fa23495adac090bea3b84fdae3ebbad4899c05"},"cell_type":"code","source":"print('teams:',test_df.teams.iloc[-1],'competitimes:',test_df.competitors.iloc[-1])","execution_count":88,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"65db3f730eac6820abded2a03342d77b781d6667"},"cell_type":"code","source":"test_df","execution_count":89,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b885a9a0d47fd3819ba369f94071f607a47aec44"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}