<jupyter_start><jupyter_text>Import the Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Import Dataset<jupyter_code>#Read files:
df = pd.read_csv("clean_2_train.csv")
df.head()
df.shape
df.describe()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 8519 entries, 0 to 8518
Data columns (total 29 columns):
 #   Column                     Non-Null Count  Dtype  
---  ------                     --------------  -----  
 0   Item_Identifier            8519 non-null   object 
 1   Item_Weight                8519 non-null   float64
 2   Item_Visibility            8519 non-null   float64
 3   Item_Type                  8519 non-null   object 
 4   Item_MRP                   8519 non-null   float64
 5   Outlet_Identifier          8519 non-null   object 
 6   Outlet_Establishment_Year  8519 non-null   int64  
 7   Item_Outlet_Sales          8519 non-null   float64
 8   Outlet_Years               8519 non-null   int64  
 9   Item_Fat_Content_1         8519 non-null   int64  
 10  Item_Fat_Content_2         8519 non-null   int64  
 11  Outlet_Location_Type_1     8519 non-null   int64  
 12  Outlet_Location_Type_2     8519 non-null   int64  
 13  Outlet_Size_1              8519 non-null   int64[...]<jupyter_text>Created a list remove_cols to remove those columns which doesn't required for Model Building<jupyter_code>remove_cols = [
    'Item_Identifier',
    'Item_Type',
    'Outlet_Identifier',
    'Outlet_Establishment_Year'
]
df = df.drop(remove_cols,axis =1)
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>Correlation Matrix<jupyter_code>plt.figure(figsize = (20,20))
sns.heatmap(df.corr(),annot = True)<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>y = df.Item_Outlet_Sales.values
X = df.drop('Item_Outlet_Sales',axis = 1)
print(X.shape,y.shape)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
cols = [ 
    'Item_Weight',
    'Item_Visibility',
    'Item_MRP',
    'Outlet_Years'
]
X[cols]
X[cols]  =  sc.fit_transform(X[cols])
X.head()<jupyter_output><empty_output><jupyter_text>Trained a Linear Regression model<jupyter_code>from sklearn.linear_model import LinearRegression
#Training the Simple Linear Regression model on the Training set
model = LinearRegression(normalize=True)<jupyter_output><empty_output><jupyter_text>______________________________________________________________________**Now, train on the training data and obtain predicted classes, as well as class probabilities, using the testing data.**<jupyter_code>model.fit(X,y)
#Predicting the Test set results
y_pred = model.predict(X)
from sklearn.metrics import r2_score,mean_squared_error

score = r2_score(y,y_pred)
print("Score of Training:",100*score)
print("RMSE : %.4g" % np.sqrt(mean_squared_error(y,y_pred)))
from sklearn.metrics import mean_squared_error,make_scorer,mean_absolute_error
from sklearn.model_selection import cross_val_score

#Perform cross-validation:
cv_score = cross_val_score(model,X, y, cv=20, scoring = make_scorer(mean_squared_error))
cv_score = np.sqrt(np.abs(cv_score))
    
#Print model report:
print("\nModel Report")
print("MAE : %.4g" % np.sqrt(mean_absolute_error(y,y_pred)))
print("RMSE : %.4g" % np.sqrt(mean_squared_error(y,y_pred)))
print("CV Score : Mean - %.4g | Std - %.4g | Min - %.4g | Max - %.4g" % (np.mean(cv_score),np.std(cv_score),np.min(cv_score),np.max(cv_score)))
model.coef_
coef2 = pd.Series(model.coef_ ,X.columns).sort_values()
plt.figure(figsize = (10,10))
coef2.plot(kind='bar', title='Model Coefficients')<jupyter_output><empty_output><jupyter_text>Regularized Linear Regression___________________________________________________________________________________________________________________________ Ridge Regression<jupyter_code>from sklearn.linear_model import Ridge
model_ridge = Ridge(alpha=0.05,normalize=True)
model_ridge.fit(X, y)
#Predicting the Test set results
y_pred = model_ridge.predict(X)
score = r2_score(y,y_pred)
print("Score of Training:",100*score)
print("RMSE : %.4g" % np.sqrt(mean_squared_error(y,y_pred)))
from sklearn.metrics import mean_squared_error,make_scorer,mean_absolute_error
from sklearn.model_selection import cross_val_score

#Perform cross-validation:
cv_score = cross_val_score(model,X, y, cv=20, scoring = make_scorer(mean_squared_error))
cv_score = np.sqrt(np.abs(cv_score))
    
#Print model report:
print("\nModel Report")
print("MAE : %.4g" % np.sqrt(mean_absolute_error(y,y_pred)))
print("RMSE : %.4g" % np.sqrt(mean_squared_error(y,y_pred)))
print("CV Score : Mean - %.4g | Std - %.4g | Min - %.4g | Max - %.4g" % (np.mean(cv_score),np.std(cv_score),np.min(cv_score),np.max(cv_score)))
coef2 = pd.Series(model_ridge.coef_ ,X.columns).sort_values()
plt.figure(figsize = (10,10))
coef2.plot(kind='bar', title='Model Coefficients')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11567089116877892, 'fraction_numerical': 0.032387849527258095, 'mean_word_length': 4.734717416378316, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '105911', 'license': 'MIT', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1704, 'n_words': 340, 'path': 'Data Analytics/Fitting_a_Linear_Regression_model_04.ipynb', 'repo_name': 'arghya013/BigMart-Sales-Prediction-1', 'size': '4971'}