<jupyter_start><jupyter_text>IMPORTING LIBRARIES<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
dataset= pd.read_csv('weather.csv')
dataset.head()<jupyter_output><empty_output><jupyter_text>HANDLING CATEGORICAL DATA<jupyter_code>import pandas as pd 
from sklearn.preprocessing import LabelEncoder
dataset = pd.read_csv('weather.csv') 
x = dataset.iloc[:, :-1].values 
y = dataset.iloc[:, -1].values 
labelencoder_X = LabelEncoder()
x[:,0] = labelencoder_X.fit_transform(x[:,0])
x[:,1] = labelencoder_X.fit_transform(x[:,1])
print(x)
#Here we are using LabelEncoder
x = dataset[['MinTemp','Rainfall','Evaporation','WindSpeed3pm','Humidity9am','Humidity3pm','Pressure9am','Pressure3pm','Cloud9am','Cloud3pm','Temp9am','Temp3pm','RISK_MM']]
#separate the other attributes from the predicting attribute
y = dataset[['MaxTemp']]
#separte the predicting attribute into Y for model training<jupyter_output><empty_output><jupyter_text>GRAPHS<jupyter_code>dataset.plot(x='MinTemp', y='MaxTemp' , style='o')
plt.title('min vs max')
plt.show()
plt.figure(figsize=(15,10))
plt.tight_layout()
seaborn.distplot(dataset['MaxTemp'])
plt.show()<jupyter_output><empty_output><jupyter_text>SPLITTING OF DATASET INTO TRAIN AND TEST SET<jupyter_code>x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2, random_state=0)<jupyter_output><empty_output><jupyter_text>NORMALIZING DATA<jupyter_code># data normalization with sklearn
from sklearn.preprocessing import MinMaxScaler

# fit scaler on training data
norm = MinMaxScaler().fit(x_train)

# transform training data
X_train_norm = norm.transform(x_train)

# transform testing data
X_test_norm = norm.transform(x_test)

print(X_train_norm)
print(X_test_norm)<jupyter_output>[[0.80916031 0.         0.91176471 ... 0.77642276 0.58503401 0.        ]
 [0.2480916  0.         0.08823529 ... 0.2601626  0.32312925 0.        ]
 [0.20610687 0.         0.26470588 ... 0.36178862 0.33333333 0.        ]
 ...
 [0.69847328 0.         0.35294118 ... 0.70325203 0.74489796 0.        ]
 [0.48854962 0.         0.47058824 ... 0.61788618 0.5        0.        ]
 [0.29389313 0.         0.19117647 ... 0.42682927 0.5        0.        ]]
[[ 0.61450382  0.          0.39705882  0.13461538  0.50819672  0.22891566
   0.60459184  0.5467033   0.125       0.125       0.60569106  0.65306122
   0.        ]
 [ 0.13358779  0.          0.02941176  0.46153846  1.          0.5060241
   0.59183673  0.46978022  0.875       0.375       0.04878049  0.20068027
   0.        ]
 [ 0.79389313  0.13567839  0.45588235  0.13461538  0.90163934  0.87951807
   0.34693878  0.32417582  1.          1.          0.67073171  0.50680272
   0.03517588]
 [ 0.70992366  0.00502513  0.47058824  0.32692308  0.52459016  0.506[...]<jupyter_text>ALGORITHM<jupyter_code>model =LinearRegression()
#loading the model constructor<jupyter_output><empty_output><jupyter_text>FITTING OF TRAINING DATA<jupyter_code>model.fit(x_train,y_train)
print('Intercept is :',model.intercept_)
print('Coefficient is :' ,model.coef_)<jupyter_output>Coefficient is : [[-2.24072168e-02 -1.15517900e-02  5.91958657e-02 -8.58077490e-03
   6.70848785e-04  1.80128608e-02  1.81056309e-02 -4.41630720e-02
  -2.94378789e-02  2.30080630e-02  9.33064920e-02  9.39000870e-01
  -1.40109918e-02]]<jupyter_text>PREDICTIONS FOR OUR PROBLEM STATEMENT WITH THE RELATED GRAPHS<jupyter_code>predictions = model.predict(x_test)
plt.scatter(y_test,predictions)
#to visualise the predictions and the test Y !!it is almost forming a linear line with less deviation
import seaborn as sns
sns.distplot((y_test-predictions))<jupyter_output><empty_output><jupyter_text>EVALUATION<jupyter_code>cutoff = 0.7                              # decide on a cutoff limit
y_pred_classes = np.zeros_like(predictions)    # initialise a matrix full with zeros
y_pred_classes[predictions > cutoff] = 1       # add a 1 if the cutoff was breached

y_test_classes = np.zeros_like(predictions)
y_test_classes[y_test > cutoff] = 1
print(metrics.accuracy_score(y_test_classes, y_pred_classes))<jupyter_output>1.0<jupyter_text>SAVING THE MODEL USING PICKLE LIBRARY<jupyter_code>import pickle 

# Save the trained model as a pickle string. 
saved_model = pickle.dumps(model) 

# Load the pickled model 
model_from_pickle = pickle.loads(saved_model) 

# Use the loaded pickled model to make predictions 
model_from_pickle.predict(x_test)<jupyter_output><empty_output><jupyter_text>ACCURACY w.r.t TRAINED DATA Confusion Matrix<jupyter_code>y_train = y_train[0:74]
from sklearn.metrics import confusion_matrix

cutoff = 0.7                                   # decide on a cutoff limit
y_pred_classes = np.zeros_like(predictions)    # initialise a matrix full with zeros
y_pred_classes[predictions > cutoff] = 1       # add a 1 if the cutoff was breached

y_train_classes = np.zeros_like(predictions)
y_train_classes[y_train > cutoff] = 1

results =confusion_matrix(y_train_classes, y_pred_classes)
print(results)<jupyter_output>[[74]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>import numpy
from sklearn.metrics import precision_recall_fscore_support

cutoff = 0.7                              # decide on a cutoff limit
y_pred_classes = np.zeros_like(predictions)    # initialise a matrix full with zeros
y_pred_classes[predictions > cutoff] = 1       # add a 1 if the cutoff was breached

y_train_classes = np.zeros_like(predictions)
y_train_classes[y_train > cutoff] = 1

precision_recall_fscore_support(y_train_classes, y_pred_classes, average='macro')<jupyter_output><empty_output><jupyter_text>ACCURACY w.r.t TEST DATA Confusion Matrix<jupyter_code>cutoff = 0.7                              # decide on a cutoff limit
y_pred_classes = np.zeros_like(predictions)    # initialise a matrix full with zeros
y_pred_classes[predictions > cutoff] = 1       # add a 1 if the cutoff was breached

y_test_classes = np.zeros_like(predictions)
y_test_classes[y_test > cutoff] = 1

results =confusion_matrix(y_test_classes, y_pred_classes)
print(results)<jupyter_output>[[74]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>import numpy
from sklearn.metrics import precision_recall_fscore_support

cutoff = 0.7                              # decide on a cutoff limit
y_pred_classes = np.zeros_like(predictions)    # initialise a matrix full with zeros
y_pred_classes[predictions > cutoff] = 1       # add a 1 if the cutoff was breached

y_test_classes = np.zeros_like(predictions)
y_test_classes[y_test > cutoff] = 1

precision_recall_fscore_support(y_test_classes, y_pred_classes, average='macro')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10127331951436186, 'fraction_numerical': 0.10852827953805153, 'mean_word_length': 4.302197802197802, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '339048', 'license': 'MIT', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2487, 'n_words': 499, 'path': 'Linear Regression/ML-LR/Assignment/ML-ASGN-LR-05/CODE/Weather_Prediction___LR.ipynb', 'repo_name': 'RASHI3004/Project-Internship---ML-DL', 'size': '6754'}