<jupyter_start><jupyter_text>Basic Data Exploration<jupyter_code>df_sams.info()
df_sams.count()
df_sams.describe()
df_sams.loc[df_sams["count"]==df_sams["count"].min(),:]
df_sams.loc[df_sams["count"]==df_sams["count"].max(),:]
#Which factors contribute most to the number of bicycles being checkout out over the course of a given day?
#How much of an impact does weather have on demand?
df_sams.corr()<jupyter_output><empty_output><jupyter_text>Data Wrangling & Feature Engineering<jupyter_code>df_sams.isnull()
df_sams.dropna()
df_sams.shape
df2  = df_sams.copy()
df2 = df2.reset_index()
df2.shape
#Downsampling
df_day=df_sams.resample('7d').mean()
df_day=df_day.dropna()
df_day_index=df_day.reset_index()
print(df_day_index.shape)
df_day_index.head()
df_day_long=df_day.stack()
df_day_long.shape
df_day_long.head<jupyter_output><empty_output><jupyter_text>In Deep Data Exploration & Visualisation<jupyter_code>import matplotlib.pylab as plt
import seaborn as sns
fig, axs = plt.subplots(2)
axs[0].plot(df_day.index, df_day["count"])
axs[1].plot(df_sams.index, df_sams["count"])
sns.lmplot(x='temp',y='count', data=df_day, col='season')<jupyter_output><empty_output><jupyter_text>Distribution<jupyter_code>sns.scatterplot(x='temp', y='registered', data=df_day)
sns.scatterplot(x='temp', y='casual', data=df_day)<jupyter_output><empty_output><jupyter_text>How does the behavior of casual users differ from subscribers?<jupyter_code>sns.distplot(df_day["casual"] ,  kde=True, color="red", bins=30, label='casual')
sns.distplot(df_day["registered"] ,  kde=True, color="blue", bins=30, label='registered')
df_sams.groupby('hours')['count'].mean().plot()
df_sams.groupby('season')['count'].mean().plot()
df_sams.groupby('temp')['count'].mean().plot()
df_sams.groupby('humidity')['count'].mean().plot()<jupyter_output><empty_output><jupyter_text>Lineare Regression Lineare Regression für countKey steps:1. exploratory data analysis2. train/test split3. feature engineering4. train a model5. cross-validation6. optimize the model iteratively, select features,     try different regressors     (e.g. Linear Regression, Random Forest Regressor, SVR)7. calculate a test score when you are done<jupyter_code>#module
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
#train/test split
X_df_sams = df_sams[['temp','humidity','season', 'hours']]
y_df_sams = df_sams['count']
X_train, X_test, y_train, y_test = train_test_split(X_df_sams,y_df_sams,test_size=0.2, random_state=42)
m=LinearRegression()<jupyter_output><empty_output><jupyter_text>train a model<jupyter_code>from sklearn.preprocessing import PolynomialFeatures
# interaktionsterm mit season
poly = PolynomialFeatures(interaction_only=True)
poly_features = poly.fit_transform(X_train[['temp','humidity','hours', 'season']])

#linear regression
m.fit(poly_features, y_train)
ypred_exp = m.predict(poly_features)
poly.get_feature_names()
m.score(poly_features, y_train)
ypred_exp
poly_features_test = poly.transform(X_test[['temp','humidity','hours', 'season']])
poly_features
ypred_exp_test = m.predict(poly_features_test)
m.score(poly_features_test, y_test)
ypred_exp_test
from sklearn.metrics import mean_squared_error, mean_squared_log_error
mean_squared_error(y_train, ypred_exp)<jupyter_output><empty_output><jupyter_text>Predicition<jupyter_code>#A)Transform the interactionterm
#B)Predict
while True:
    temp_input=input('Input temperature: ')
    humidity_input=input('Input humidity: ')
    hour_input=input('Input time: ')
    season_input=input('Input season: ')

    poly_features_konkret = poly.transform([[temp_input,humidity_input, hour_input, season_input]])
    #poly_features
    ypred_exp_konkret = m.predict(poly_features_konkret)
    print('How many bikes needed '+str(np.round(ypred_exp_konkret)))
    abfrage= input('Another query? y/n?')
    if abfrage== 'y':
        continue
    if abfrage== 'n':
        break<jupyter_output>Input temperature: 12
Input humidity: 15
Input time: 12
Input season: 1
How many bikes needed [252.]
Another query? y/n?y
Input temperature: 20
Input humidity: 18
Input time: 12
Input season: 3
How many bikes needed [435.]
Another query? y/n?n<jupyter_text>Regularisation<jupyter_code>#DataWrangling
df['weekday']=df.index.dayofweek
df['hours']=df.index.hour
df.head()
df.isnull()
df.dropna()
df.shape
#module
from sklearn.linear_model import Ridge, Lasso, ElasticNet
alpha = 5
#train/test split
X_df_lasso = df[['temp','humidity','season', 'hours','weekday']]
y_df_lasso= df['count']
X_train_lasso, X_test_lasso, y_train_lasso, y_test_lasso = train_test_split(X_df_lasso,y_df_lasso,test_size=0.2, random_state=42)
m_lasso = Lasso(alpha=alpha)
from sklearn.preprocessing import PolynomialFeatures
# interactionterm with season
poly = PolynomialFeatures(interaction_only=True)
poly_features_lasso = poly.fit_transform(X_train_lasso[['temp','humidity','hours', 'season','weekday']])
poly_features
#linear regression
m_lasso.fit(poly_features_lasso, y_train_lasso)
#m_lasso.fit(X_train_lasso, y_train_lasso)
ypred_exp_lasso = m_lasso.predict(poly_features_lasso)
#ypred_exp_lasso = m_lasso.predict(X_train_lasso)
poly.get_feature_names()
m_lasso.score(poly_features_lasso, y_train_lasso)
ypred_exp_lasso
poly_features_test_lasso = poly.transform(X_test_lasso[['temp','humidity','hours', 'season', 'weekday']])
poly_features
ypred_exp_test_lasso = m_lasso.predict(poly_features_test_lasso)
m_lasso.score(poly_features_test_lasso, y_test_lasso)
ypred_exp_test_lasso
mean_squared_error(y_train_lasso, ypred_exp_lasso)
from sklearn.preprocessing import StandardScaler, MinMaxScaler
scaler = StandardScaler()
x_poly_features_scaled = scaler.fit_transform(poly_features_lasso)

m_ridge = Ridge(alpha=alpha)
m_ridge.fit(x_poly_features_scaled, y_train_lasso)
ypred_ridge = m_ridge.predict(x_poly_features_scaled)

mean_squared_error(y_train_lasso, ypred_ridge)
m_ridge.score(x_poly_features_scaled, y_train_lasso)
#graphical representation
import matplotlib.pyplot as plt

plt.plot(y_train_lasso, ypred_exp_lasso,'.', label='y_lasso')
plt.plot(y_train, ypred_exp,'.', label='y_lin')
plt.plot(y_train_lasso, ypred_ridge, '.', label='y_rid')
plt.legend()
plt.show()
#PCA
# Lasso drops variables, but all important to the model
# Ridge didnt improve the model  
import seaborn as sns
import warnings
#pair plot
plot_df = pd.concat((X_df_lasso, pd.DataFrame(y_df_lasso, columns=['count'])), axis=1)
warnings.filterwarnings('ignore')
sns.pairplot(plot_df,diag_kind = 'kde', hue='season')<jupyter_output><empty_output><jupyter_text>Logistic Regression casual vs. registered<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
X_log = df[['season', 'hours','weekday']]
y_log = df['registered']

X_train_log, X_test_log, y_train_log, y_test_log = train_test_split(X_log, y_log, test_size=.25, random_state=0)

clf = LogisticRegression(random_state=0)

# training
clf.fit(X_train_log, y_train_log)
# predicting with train
y_pred_train_log = clf.predict(X_train_log)

# now testing
y_pred_test_log = clf.predict(X_test_log)

X_train.shape, y_train.shape,X_test.shape, y_test.shape
clf.score(X_train_log,y_train_log),clf.score(X_test_log,y_test_log)

accuracy_score(y_test_log, y_pred_test_log),accuracy_score(y_train_log, y_pred_train_log)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12462625713509105, 'fraction_numerical': 0.007203044305517803, 'mean_word_length': 9.06703146374829, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '466665', 'license': 'MIT', 'n_tokens_mistral': 2841, 'n_tokens_neox': 2753, 'n_words': 464, 'path': 'Regression.ipynb', 'repo_name': 'Pijanes/Portfolio', 'size': '7358'}