In [54]:
%pylab inline
import pandas as pd
from sklearn.decomposition import PCA
from sklearn import preprocessing
from sklearn import linear_model


Populating the interactive namespace from numpy and matplotlib

In [55]:
train = pd.read_csv('train.csv')
new_labels = train.columns.values
new_labels[-1] = 'total_rentals'
train.columns = new_labels
train[:5]


Out[55]:
datetime season holiday workingday weather temp atemp humidity windspeed casual registered total_rentals
0 2011-01-01 00:00:00 1 0 0 1 9.84 14.395 81 0 3 13 16
1 2011-01-01 01:00:00 1 0 0 1 9.02 13.635 80 0 8 32 40
2 2011-01-01 02:00:00 1 0 0 1 9.02 13.635 80 0 5 27 32
3 2011-01-01 03:00:00 1 0 0 1 9.84 14.395 75 0 3 10 13
4 2011-01-01 04:00:00 1 0 0 1 9.84 14.395 75 0 0 1 1

5 rows × 12 columns


In [56]:
plt.plot(train.casual)
plt.show()
plt.plot(train.registered)
plt.show()
plt.plot(train.total_rentals)
plt.show()



In [68]:
p = 3
X = train.ix[:,1:9]
for i,col in enumerate(X.columns[4:8]):
    X.insert(i+8, col + str(.5), X[col]**.5)
    X.insert(i+8, col + str(2), X[col]**2)
    X.insert(i+8, col + str(3), X[col]**3)
dts = [datetime.datetime.strptime(d,'%Y-%m-%d %H:%M:%S') for d in train.datetime]
years = [d.year for d in dts]
months = [d.month for d in dts]
days = [d.day for d in dts]
hours = [d.hour for d in dts]
X.insert(0,'hour',hours)
X.insert(0,'day',days)
X.insert(0,'month',months)
X.insert(0,'year',years)

X.shape


Out[68]:
(10886, 28)

In [69]:
y = train.ix[:,-1:-4:-1]
y[:2]


Out[69]:
total_rentals registered casual
0 16 13 3
1 40 32 8

2 rows × 3 columns


In [70]:
test_set = pd.read_csv('test.csv')
for i,col in enumerate(test_set.columns[5:9]):
    test_set.insert(i+9, col + str(.5), test_set[col]**.5)
    test_set.insert(i+9, col + str(2), test_set[col]**2)
    test_set.insert(i+9, col + str(3), test_set[col]**3)
dts = [datetime.datetime.strptime(d,'%Y-%m-%d %H:%M:%S') for d in test_set.datetime]
years = [d.year for d in dts]
months = [d.month for d in dts]
days = [d.day for d in dts]
hours = [d.hour for d in dts]
print len(months), len(hours)
test_set.insert(1,'hour',hours)
test_set.insert(1,'day',days)
test_set.insert(1,'month',months)
test_set.insert(1,'year',years)
print test_set.shape


6493 6493
(6493, 29)

In [82]:
lm = linear_model.Ridge(alpha=10,fit_intercept=True)
cas = reg = y_tot = y_reg = y_cas = np.asarray([])
mean_score = 0
for yr in range(2011,2013):
    for m in range(1,13):
        lm.fit(X[X.month <= m][X.year <= yr],y[X.month <= m][X.year <= yr])
        mean_score += lm.score(X[X.month <= m],y[X.month <= m])/24
        y_tot = np.append(y_tot, train.total_rentals[X.month == m][X.year == yr]*0)
        cas = np.append(cas, train.casual[X.month == m][X.year == yr])
        reg = np.append(reg, train.registered[X.month == m][X.year == yr])
        pred = lm.predict(test_set[test_set.month == m][test_set.year == yr].ix[:,1:])
        y_tot = np.append(y_tot, pred[:,0])
        y_reg = np.append(y_reg, pred[:,1])
        y_cas = np.append(y_cas, pred[:,2])
        cas = np.append(cas, [0]*pred.shape[0])
        reg = np.append(reg, [0]*pred.shape[0])

print mean_score
# print lm.alpha_
y_cas[y_cas < 0] = 0
y_reg[y_reg < 0] = 0
y_tot[y_tot < 0] = 0

    
plt.plot(y_cas)
plt.plot(y_reg)
plt.plot(y_cas+y_reg)
#plt.plot(y_tot,'.')
plt.show()


0.300609529565

In [72]:
plt.plot(y_tot[200:600],'.--')
plt.plot(cas[200:600]+reg[200:600],'.--')
# plt.plot(reg[0:1000],'.')
plt.show()
plt.plot(y_tot,'.')
plt.plot(cas+reg,'.')
# plt.plot(reg,'.')
plt.show()


Write out the submission!


In [73]:
sample_submission = pd.read_csv('sampleSubmission.csv')
new_labels = sample_submission.columns.values
new_labels[-1] = 'total_rentals'
sample_submission.columns = new_labels
print sample_submission.shape

my_submission = sample_submission.copy()
new_labels = my_submission.columns.values
new_labels[-1] = 'total_rentals'
my_submission.columns = new_labels
my_submission.total_rentals = np.round(y_cas+y_reg)
plt.plot(my_submission.total_rentals)
print my_submission.shape
my_submission[:5]


(6493, 2)
(6493, 2)
Out[73]:
datetime total_rentals
0 2011-01-20 00:00:00 53
1 2011-01-20 01:00:00 59
2 2011-01-20 02:00:00 61
3 2011-01-20 03:00:00 68
4 2011-01-20 04:00:00 69

5 rows × 2 columns


In [74]:
new_labels = my_submission.columns.values
new_labels[-1] = 'count'
my_submission.columns = new_labels
my_submission.to_csv('m2m-ridge-a{}+poly{}.csv'.format(lm.alpha,p),index=False)