<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 To start with, we'll need to compute the number of rentals per station per day. Use pandas to do that.
Step1:  a. Our stations data has a huge number of quantitative attributes
Step2:  Plot the predicted values (model.predict(x)) against the actual values and see how they compare.
Step3:  c. In this case, there are 129 input variables and only 185 rows which means we're very likely to overfit. Look at the model coefficients and see if anything jumps out as odd.
Step4:  d. Go back and split the data into a training set and a test set. Train the model on the training set and evaluate it on the test set. How does it do?
Step5:  It looks too scattered and doesn't look like accurate.
1.a. Since we have so many variables, this is a good candidate for regularization. In particular, since we'd like to eliminate a lot of them, lasso seems like a good candidate. Build a lasso model on your training data for various values of alpha. Which variables survive?
Step6:  b. How does this model perform on the test set?
Step7:  I can see some correlation in this.
1.No matter how high I make alpha, the coefficient on crossing ("number of nearby crosswalks") never goes away. Try a simple linear regression on just that variable.
<ASSISTANT_TASK:>
Python Code:
count = usage['station_start'].value_counts()
average_rental_df = DataFrame({ 'average_rental' : count / 365})
average_rental_df
Explanation: To start with, we'll need to compute the number of rentals per station per day. Use pandas to do that.
End of explanation
from sklearn import linear_model
indexed_avg_df = DataFrame(average_rental_df.index, columns=['station'])
indexed_avg_df['avg_rentals'] = average_rental_df.values
indexed_avg_df['station'] = average_rental_df.index
avgerage_stations_df = pd.merge(left=indexed_avg_df, right=stations, on='station')
x = avgerage_stations_df[list(avgerage_stations_df.columns.values[8:])]
y = avgerage_stations_df[list(avgerage_stations_df.columns.values[1:2])]
linear_reg = linear_model.LinearRegression()
linear_reg.fit(x, y)
Explanation: a. Our stations data has a huge number of quantitative attributes: fast_food, parking, restaurant, etc... Some of them are encoded as 0 or 1 (for absence or presence), others represent counts. To start with, run a simple linear regression where the input (x) variables are all the various station attributes and the output (y) variable is the average number of rentals per day.
End of explanation
plt.scatter(y, linear_reg.predict(x), s=50)
plt.show()
Explanation: Plot the predicted values (model.predict(x)) against the actual values and see how they compare.
End of explanation
linear_reg.coef_
Explanation: c. In this case, there are 129 input variables and only 185 rows which means we're very likely to overfit. Look at the model coefficients and see if anything jumps out as odd.
End of explanation
from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=42)
lin_reg = linear_model.LinearRegression()
lin_reg.fit(x_train, y_train)
plt.scatter(lin_reg.predict(x_test), y_test, s=50)
plt.xlabel('predicted value')
plt.ylabel('actual value')
plt.show()
Explanation: d. Go back and split the data into a training set and a test set. Train the model on the training set and evaluate it on the test set. How does it do?
End of explanation
from sklearn.linear_model import Lasso
lasso_model = Lasso(alpha=0.1)
lasso_model.fit(x_train, y_train)
lasso_model.coef_
lasso_model = Lasso(alpha=0.5)
lasso_model.fit(x_train, y_train)
lasso_model.coef_
lasso_model = Lasso(alpha=1)
lasso_model.fit(x_train, y_train)
lasso_model.coef_
Explanation: It looks too scattered and doesn't look like accurate.
1.a. Since we have so many variables, this is a good candidate for regularization. In particular, since we'd like to eliminate a lot of them, lasso seems like a good candidate. Build a lasso model on your training data for various values of alpha. Which variables survive?
End of explanation
plt.scatter(lasso_model.predict(x_test), y_test, s=50)
plt.xlabel('predicted value')
plt.ylabel('actual value')
plt.show()
Explanation: b. How does this model perform on the test set?
End of explanation
x = avgerage_stations_df[list(avgerage_stations_df.columns.values[111:112])]
y = avgerage_stations_df[list(avgerage_stations_df.columns.values[1:2])]
lin_regr = linear_model.LinearRegression()
lin_regr.fit(x, y)
plt.scatter(lin_regr.predict(x), y, s=50)
plt.xlabel('predicted value')
plt.ylabel('actual value')
plt.show()
Explanation: I can see some correlation in this.
1.No matter how high I make alpha, the coefficient on crossing ("number of nearby crosswalks") never goes away. Try a simple linear regression on just that variable.
End of explanation
<END_TASK>