<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive')
import glob
for dir in glob.glob("/content/drive/My Drive/regression_data_files_csv/Bike-rental-count/*"):
  print(dir)
import pandas as pd
bike_df = pd.read_csv("/content/drive/My Drive/regression_data_files_csv/Bike-rental-count/day.csv")
bike_df.head(100)
bike_df.shape
bike_df.rename(columns={'instant':'rec_id','dteday':'datetime','yr':'year','mnth':'month','weathersit':'weather_condition',
                       'hum':'humidity','cnt':'total_count'},inplace=True)
bike_df.head(10)
#Type casting the datetime and numerical attributes to category

bike_df['datetime']=pd.to_datetime(bike_df.datetime)

bike_df['season']=bike_df.season.astype('category')
bike_df['year']=bike_df.year.astype('category')
bike_df['month']=bike_df.month.astype('category')
bike_df['holiday']=bike_df.holiday.astype('category')
bike_df['weekday']=bike_df.weekday.astype('category')
bike_df['workingday']=bike_df.workingday.astype('category')
bike_df['weather_condition']=bike_df.weather_condition.astype('category')
bike_df.head(10)
bike_df.describe()
#Missing values in dataset
bike_df.isnull().sum()
# drop all those rows which  
# have any 'nan' value in it. 
bike_df.dropna(inplace = True) 
#load the required libraries
from sklearn import preprocessing,metrics,linear_model
from sklearn.model_selection import cross_val_score,cross_val_predict,train_test_split
bike_df.columns
bike_df.iloc[:,0:-3]
bike_df.iloc[:,-1]
#Split the dataset into the train and test data
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(bike_df.iloc[:,0:-3],bike_df.iloc[:,-1],test_size=0.3, random_state=42)

#Reset train index values
X_train.reset_index(inplace=True)
y_train=y_train.reset_index()

# Reset train index values
X_test.reset_index(inplace=True)
y_test=y_test.reset_index()

print(X_train.shape,X_test.shape,y_train.shape,y_test.shape)
print(y_train.head())
print(y_test.head())
#Create a new dataset for train attributes
train_attributes=X_train[['season','month','year','weekday','holiday','workingday','weather_condition','humidity','temp','windspeed']]
#Create a new dataset for test attributes
test_attributes=X_test[['season','month','year','weekday','holiday','workingday','humidity','temp','windspeed','weather_condition']]
#categorical attributes
cat_attributes=['season','holiday','workingday','weather_condition','year']
#numerical attributes
num_attributes=['temp','windspeed','humidity','month','weekday']
#To get dummy variables to encode the categorical features to numeric
train_encoded_attributes=pd.get_dummies(train_attributes,columns=cat_attributes)
print('Shape of transfomed dataframe::',train_encoded_attributes.shape)
train_encoded_attributes.head(5)
#Training dataset for modelling
X_train=train_encoded_attributes
y_train=y_train.total_count.values
#training model
lr_model=linear_model.LinearRegression()
lr_model
lr_model.fit(X_train,y_train)
#Accuracy of the model
lr=lr_model.score(X_train,y_train)
print('Accuracy of the model :',lr)
print('Model coefficients :',lr_model.coef_)
print('Model intercept value :',lr_model.intercept_)
#Cross validation prediction
predict=cross_val_predict(lr_model,X_train,y_train,cv=3)
predict
import numpy as np
#R-squared scores
r2_scores = cross_val_score(lr_model, X_train, y_train, cv=3)
print('R-squared scores :',np.average(r2_scores))<jupyter_output><empty_output><jupyter_text>**Model performance on test dataset**<jupyter_code>#To get dummy variables to encode the categorical features to numeric
test_encoded_attributes=pd.get_dummies(test_attributes,columns=cat_attributes)
print('Shape of transformed dataframe :',test_encoded_attributes.shape)
test_encoded_attributes.head(5)
#Test dataset for prediction
X_test=test_encoded_attributes
y_test=y_test.total_count.values
#predict the model
lr_pred=lr_model.predict(X_test)
lr_pred
import math
#Root mean square error 
rmse=math.sqrt(metrics.mean_squared_error(y_test,lr_pred))
#Mean absolute error
mae=metrics.mean_absolute_error(y_test,lr_pred)
print('Root mean square error :',rmse)
print('Mean absolute error :',mae)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12987619715019855, 'fraction_numerical': 0.005372576500817566, 'mean_word_length': 8.515555555555556, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '580596', 'license': 'Apache-2.0', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1487, 'n_words': 245, 'path': 'Bike_rental_count_prediction.ipynb', 'repo_name': 'Nikhitha-S-Pavan/ML-and-DL-Regression-model-examples', 'size': '4281'}