<jupyter_start><jupyter_text>NATURAL GAS PRICE PRDICTION USING IBM WATSON MACHINE LAERNING SERVICE  Importing the Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Importing the Data into a Pandas DataFrame for further analysis<jupyter_code>import os, types
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0

# @hidden_cell
# The following code accesses a file in your IBM Cloud Object Storage. It includes your credentials.
# You might want to remove those credentials before you share the notebook.

if os.environ.get('RUNTIME_ENV_LOCATION_TYPE') == 'external':
    endpoint_e54a7051722a4e6abc7b50d84e1c4ecc = 'https://s3.us.cloud-object-storage.appdomain.cloud'
else:
    endpoint_e54a7051722a4e6abc7b50d84e1c4ecc = 'https://s3.private.us.cloud-object-storage.appdomain.cloud'

client_e54a7051722a4e6abc7b50d84e1c4ecc = ibm_boto3.client(service_name='s3',
    ibm_api_key_id='<KEY>',
    ibm_auth_endpoint="https://iam.cloud.ibm.com/oidc/token",
    config=Config(signature_version='oauth'),
    endpoint_url=endpoint_e54a7051722a4e6abc7b50d84e1c4ecc)

body = client_e54a7051722a4e6abc7b50d84e1c4ecc.get_object(Bucket='naturalgaspriceprediction-donotdelete-pr-e5kuh2lgmj7nro',Key='naturalgas.csv')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(body, "__iter__"): body.__iter__ = types.MethodType( __iter__, body )

data = pd.read_csv(body)
data.head()<jupyter_output><empty_output><jupyter_text>First 5 entries of the data Splitting the Date into 3 columns<jupyter_code>data['year'] = pd.DatetimeIndex(data['Date']).year
data['month'] = pd.DatetimeIndex(data['Date']).month
data['day'] = pd.DatetimeIndex(data['Date']).day
data.drop('Date',axis=1,inplace=True)
data<jupyter_output><empty_output><jupyter_text>Handling Null Values<jupyter_code>data.isnull().any()<jupyter_output><empty_output><jupyter_text>As it is continuous data so we are checking it is normal distribution or skewed distribution?<jupyter_code>sns.distplot(data['Price'].dropna())<jupyter_output><empty_output><jupyter_text>As we got normal distribution curve so we are filling NULL values with mean<jupyter_code>data['Price'].fillna(data['Price'].mean(),inplace=True)
data.isnull().sum()
data.describe()<jupyter_output><empty_output><jupyter_text>Finding and Removing Outliers<jupyter_code>sns.boxplot(data['Price'])
from scipy import stats
z=np.abs(stats.zscore(data))
z
threshold=3
np.where(z>threshold)
df_no_outliers=data[(z<=3).all(axis=1)]
df_no_outliers
df_no_outliers.shape<jupyter_output><empty_output><jupyter_text>Data Visualization Scatter Plot<jupyter_code>fig=plt.figure(figsize=(5,5))
plt.scatter(data['day'],data['Price'],color='pink')
plt.xlabel('day')
plt.ylabel('Price')
plt.title('PRICE OF NATURAL GAS ON THE BASIS OF DAYS OF A MONTH')
plt.legend()
fig=plt.figure(figsize=(5,5))
plt.scatter(data['year'],data['Price'],color='green')
plt.xlabel('month')
plt.ylabel('Price')
plt.title('PRICE OF NATURAL GAS ON THE BASIS OF MONTHS OF A YEAR')
plt.legend()<jupyter_output>No handles with labels found to put in legend.<jupyter_text>HEAT MAP<jupyter_code>sns.heatmap(data.corr())<jupyter_output><empty_output><jupyter_text>BAR GRAPH<jupyter_code>plt.bar(data['month'],data['Price'],color='blue')
plt.xlabel('month')
plt.ylabel('Price')
plt.title('PRICE OF NATURAL GAS ON THE BASIS OF MONTHS OF A YEAR')
plt.legend()<jupyter_output>No handles with labels found to put in legend.<jupyter_text>LINE GRAPH<jupyter_code>sns.lineplot(x='year',y='Price',data=data,color='red')
sns.lineplot(x='month',y='Price',data=data,color='red')
sns.lineplot(x='day',y='Price',data=data,color='red')<jupyter_output><empty_output><jupyter_text>HISTOGRAM<jupyter_code>data['Price'].hist(bins=90)<jupyter_output><empty_output><jupyter_text>BOX PLOT<jupyter_code>data.boxplot(column='Price',by='month')
sns.pairplot(data)
plt.show()<jupyter_output><empty_output><jupyter_text>separating dependent and independent variables<jupyter_code>x=data.iloc[:,1:4].values
y=data.iloc[:,0].values
x
y
x
# Encoding
from sklearn.preprocessing import StandardScaler
sc= StandardScaler()
x=sc.fit_transform(x)
x<jupyter_output><empty_output><jupyter_text>Train and Test<jupyter_code>from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)
x_train.shape
y_train.shape<jupyter_output><empty_output><jupyter_text>Model Building<jupyter_code>from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeRegressor
DecisionTreeRegressor()
df_grid = GridSearchCV(DecisionTreeRegressor(),param_grid = {'criterion':['mse', 'friedman_mse', 'mae', 'poisson'],'splitter': ['best', 'random'],'max_depth': range(1, 11),'min_samples_split': range(10, 60, 10),},cv=5,n_jobs=1,scoring='neg_mean_squared_error')
df_grid.fit(x_train, y_train)
print(df_grid.best_params_)
df=DecisionTreeRegressor(criterion='mse',max_depth=10,min_samples_split=10,splitter='best')
df.fit(x_train,y_train)
y_pred_df=df.predict(x_test)
y_pred_df
y_test
from sklearn.metrics import r2_score
accur_df=r2_score(y_test,y_pred_df)
print(accur_df)
!pip install ibm_watson_machine_learning
from ibm_watson_machine_learning import APIClient
wml_credentials = {
    "url":"https://us-south.ml.cloud.ibm.com",
    "apikey":"<KEY>"
}
client = APIClient(wml_credentials)
def guid_from_space_name(client,space_name):
    space = client.spaces.get_details()
    return(next(item for item in space['resources'] if item['entity']['name'] == space_name)['metadata']['id'])
space_uid = guid_from_space_name(client, 'Models')
print("Space UID = " + space_uid)
client.set.default_space(space_uid)
client.software_specifications.list()
software_spec_uid = client.software_specifications.get_uid_by_name("default_py3.7")
software_spec_uid
model_details = client.repository.store_model(model=df,meta_props={
    client.repository.ModelMetaNames.NAME:"DecisionTreeRegressor",
    client.repository.ModelMetaNames.TYPE:"scikit-learn_0.23",
    client.repository.ModelMetaNames.SOFTWARE_SPEC_UID:software_spec_uid })
model_id = client.repository.get_model_uid(model_details)
model_id
from six import StringIO
from IPython.display import Image
from sklearn.tree import export_graphviz
import pydotplus
dot_data = StringIO()
export_graphviz(df, out_file=dot_data,
                filled=True, rounded=True,
                special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())<jupyter_output><empty_output><jupyter_text>Random Forest Regerssor<jupyter_code>from sklearn.ensemble import RandomForestRegressor
rfr = RandomForestRegressor(n_estimators=1, random_state=10)
rfr.fit(x_train,y_train)
y_pred_rfr=rfr.predict(x_test)
y_pred_rfr
y_test
accur_rfr=r2_score(y_test,y_pred_rfr)
print(accur_rfr)
model_details_2 = client.repository.store_model(model=rfr,meta_props={
    client.repository.ModelMetaNames.NAME:"RandomForestRegressor",
    client.repository.ModelMetaNames.TYPE:"scikit-learn_0.23",
    client.repository.ModelMetaNames.SOFTWARE_SPEC_UID:software_spec_uid })
model_id_2 = client.repository.get_model_uid(model_details_2)
model_id_2<jupyter_output><empty_output><jupyter_text>Model Evaluation for Decision Tree Regressor<jupyter_code>from sklearn.metrics import mean_absolute_error,mean_squared_error
mean_absolute_error(y_test,y_pred_df)
mean_squared_error(y_test,y_pred_df)
from math import sqrt
sqrt(mean_squared_error(y_test,y_pred_df))<jupyter_output><empty_output><jupyter_text>Model Evaluation for Random Forest Regressor<jupyter_code>mean_absolute_error(y_test,y_pred_rfr)
mean_squared_error(y_test,y_pred_rfr)
sqrt(mean_squared_error(y_test,y_pred_rfr))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12321428571428572, 'fraction_numerical': 0.01913265306122449, 'mean_word_length': 9.71174863387978, 'pattern_counts': {'":': 2, '<': 78, '<?xml version=': 0, '>': 78, 'https://': 4, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '514949', 'license': 'MIT', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2888, 'n_words': 427, 'path': 'notebooks/Natural Gas Price Prediction -IBM.ipynb', 'repo_name': 'smartinternz02/SI-GuidedProject-4885-1627462179', 'size': '7918'}