NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


df_manipulated_ms_export_model = df_manipulated[['O-Xylene','MS_export']]

X = df_manipulated_ms_export_model.drop(['MS_export'], axis = 1)
y = df_manipulated_ms_export_model['MS_export']


# In[293]:


df_manipulated_ms_export_model.columns


# In[294]:


from sklearn.model_selection import train_test_split

x_main,x_test,y_main,y_test = train_test_split(X,y,test_size = 1000, shuffle=False) # test seti için

x_train,x_val,y_train,y_val = train_test_split(x_main,y_main,test_size = 1000,shuffle=False) # validation seti için


# In[295]:


from sklearn.linear_model import Ridge, Lasso


# In[296]:


linreg_model = LinearRegression()
rf_model = RandomForestRegressor()
lasso = Lasso()
ridge = Ridge()


# In[297]:


val_mape = []


# In[298]:


from sklearn.utils.validation import check_consistent_length, check_array

def mean_absolute_percentage_error(y_true, y_pred,
sample_weight=None,
multioutput='uniform_average'):
"""Mean absolute percentage error regression loss.
Note here that we do not represent the output as a percentage in range
[0, 100]. Instead, we represent it in range [0, 1/eps]. Read more in the
:ref:`User Guide <mean_absolute_percentage_error>`.
.. versionadded:: 0.24
Parameters
----------
y_true : array-like of shape (n_samples,) or (n_samples, n_outputs)
Ground truth (correct) target values.
y_pred : array-like of shape (n_samples,) or (n_samples, n_outputs)
Estimated target values.
sample_weight : array-like of shape (n_samples,), default=None
Sample weights.
multioutput : {'raw_values', 'uniform_average'} or array-like
Defines aggregating of multiple output values.
Array-like value defines weights used to average errors.
If input is list then the shape must be (n_outputs,).
'raw_values' :
Returns a full set of errors in case of multioutput input.
'uniform_average' :
Errors of all outputs are averaged with uniform weight.
Returns
-------
loss : float or ndarray of floats in the range [0, 1/eps]
If multioutput is 'raw_values', then mean absolute percentage error
is returned for each output separately.
If multioutput is 'uniform_average' or an ndarray of weights, then the
weighted average of all output errors is returned.
MAPE output is non-negative floating point. The best value is 0.0.
But note the fact that bad predictions can lead to arbitarily large
MAPE values, especially if some y_true values are very close to zero.
Note that we return a large value instead of `inf` when y_true is zero.
Examples
--------
>>> from sklearn.metrics import mean_absolute_percentage_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_absolute_percentage_error(y_true, y_pred)
0.3273...
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> mean_absolute_percentage_error(y_true, y_pred)
0.5515...
>>> mean_absolute_percentage_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.6198...
"""
y_type, y_true, y_pred, multioutput = _check_reg_targets(
y_true, y_pred, multioutput)
check_consistent_length(y_true, y_pred, sample_weight)
epsilon = np.finfo(np.float64).eps
mape = np.abs(y_pred - y_true) / np.maximum(np.abs(y_true), epsilon)
output_errors = np.average(mape,
weights=sample_weight, axis=0)
if isinstance(multioutput, str):
if multioutput == 'raw_values':
return output_errors
elif multioutput == 'uniform_average':
# pass None as weights to np.average: uniform mean
multioutput = None

return np.average(output_errors, weights=multioutput)

def _check_reg_targets(y_true, y_pred, multioutput, dtype="numeric"):
"""Check that y_true and y_pred belong to the same regression task.
Parameters
----------
y_true : array-like
y_pred : array-like
multioutput : array-like or string in ['raw_values', uniform_average',
'variance_weighted'] or None
None is accepted due to backward compatibility of r2_score().
Returns
-------
type_true : one of {'continuous', continuous-multioutput'}
The type of the true target data, as output by
'utils.multiclass.type_of_target'.
y_true : array-like of shape (n_samples, n_outputs)
Ground truth (correct) target values.
y_pred : array-like of shape (n_samples, n_outputs)
Estimated target values.
multioutput : array-like of shape (n_outputs) or string in ['raw_values',
uniform_average', 'variance_weighted'] or None
Custom output weights if ``multioutput`` is array-like or
just the corresponding argument if ``multioutput`` is a
correct keyword.
dtype : str or list, default="numeric"
the dtype argument passed to check_array.
"""
check_consistent_length(y_true, y_pred)
y_true = check_array(y_true, ensure_2d=False, dtype=dtype)
y_pred = check_array(y_pred, ensure_2d=False, dtype=dtype)

if y_true.ndim == 1:
y_true = y_true.reshape((-1, 1))

if y_pred.ndim == 1:
y_pred = y_pred.reshape((-1, 1))

if y_true.shape[1] != y_pred.shape[1]:
raise ValueError("y_true and y_pred have different number of output "
"({0}!={1})".format(y_true.shape[1], y_pred.shape[1]))

n_outputs = y_true.shape[1]
allowed_multioutput_str = ('raw_values', 'uniform_average',
'variance_weighted')
if isinstance(multioutput, str):
if multioutput not in allowed_multioutput_str:
raise ValueError("Allowed 'multioutput' string values are {}. "
"You provided multioutput={!r}".format(
allowed_multioutput_str,
multioutput))
elif multioutput is not None:
multioutput = check_array(multioutput, ensure_2d=False)
if n_outputs == 1:
raise ValueError("Custom weights are useful only in "
"multi-output cases.")
elif n_outputs != len(multioutput):
raise ValueError(("There must be equally many custom weights "
"(%d) as outputs (%d).") %
(len(multioutput), n_outputs))
y_type = 'continuous' if n_outputs == 1 else 'continuous-multioutput'

return y_type, y_true, y_pred, multioutput


# In[299]:


#from sklearn.metrics import mean_absolute_percentage_error

linreg_model = LinearRegression().fit(x_train,y_train)
pred_linreg = linreg_model.predict(x_val)
val_mape_linreg = mean_absolute_percentage_error(pred_linreg,y_val)


# In[300]:


val_mape.append(val_mape_linreg)


# In[301]:


linreg_model.coef_


# In[302]:


rf_model = RandomForestRegressor().fit(x_train,y_train)
pred_rfmodel = rf_model.predict(x_val)
val_mape_rfmodel = mean_absolute_percentage_error(pred_rfmodel,y_val)


# In[303]:


rf_model.feature_importances_


# In[304]:


val_mape.append(val_mape_rfmodel)


# In[305]:


lasso_model = Lasso().fit(x_train,y_train)
pred_lassomodel = lasso_model.predict(x_val)
val_mape_lassomodel = mean_absolute_percentage_error(pred_lassomodel,y_val)


# In[306]:


val_mape.append(val_mape_lassomodel)


# In[307]:


ridge_model = Ridge().fit(x_train,y_train)
pred_ridgemodel = ridge_model.predict(x_val)
val_mape_ridgemodel = mean_absolute_percentage_error(pred_ridgemodel,y_val)


# In[308]:


val_mape.append(val_mape_ridgemodel)


# In[309]:


huber_model = HuberRegressor().fit(x_train,y_train)
pred_hubermodel = huber_model.predict(x_val)
val_mape_hubermodel = mean_absolute_percentage_error(pred_hubermodel,y_val)


# In[310]:


val_mape.append(val_mape_hubermodel)


# In[311]:


theilsen_model = TheilSenRegressor().fit(x_train,y_train)
pred_theilsenmodel = theilsen_model.predict(x_val)
val_mape_theilsenmodel = mean_absolute_percentage_error(pred_theilsenmodel,y_val)


# In[312]:


val_mape.append(val_mape_theilsenmodel)


# In[313]:


ransac_model = RANSACRegressor().fit(x_train,y_train)
pred_ransacmodel = ransac_model.predict(x_val)
val_mape_ransacmodel = mean_absolute_percentage_error(pred_ransacmodel,y_val)


# In[314]:


val_mape.append(val_mape_ransacmodel)


# ### Scatter Plot of RansacModelPreds and Actual Values - Validation Set
#

# In[315]:


# x and y given as array_like objects
import plotly.express as px
fig = px.scatter(x=pred_ransacmodel, y=y_val,labels=dict(pred_ransacmodel_test="Preds", y_test="Actuals"))
fig.show()


# In[316]:


import statsmodels.api as sm
import statsmodels.formula.api as smf
from sklearn.linear_model import HuberRegressor, LinearRegression, TheilSenRegressor, RANSACRegressor


# In[317]:


X = sm.add_constant(X)
model = sm.OLS(y,X).fit()

preds = model.predict(X)

print_model = model.summary()

print(print_model)


# In[318]:


val_results = pd.DataFrame()

val_results['y_val'] = y_val
val_results['pred_linreg_val'] = pred_linreg
val_results['pred_rfmodel_val'] = pred_rfmodel
val_results['pred_lassomodel_val'] = pred_lassomodel
val_results['pred_ridgemodel_val'] = pred_ridgemodel
val_results['pred_hubermodel_val'] = pred_hubermodel
val_results['pred_theilsenmodel_val'] = pred_theilsenmodel
val_results['pred_ransacmodel_val'] = pred_ransacmodel



# In[319]:


val_mape


# In[320]:


val_mape_dict = dict({val_mape[0]: 'Mape_of_Linreg', val_mape[1]: 'Mape_of_RF_Regressor',
val_mape[2]:'Mape_of_Lasso_Regression',val_mape[3]:'Mape_of_Ridge_Regression',
val_mape[4]:'Mape_of_Huber_Regression',val_mape[5]:'Mape_of_Theilsen_Regression',
val_mape[6]:'Mape_of_Ransac_Regression'})


# In[321]:


new_columns = ['Validation_Mape_Values','Results_of_Algorithms']


# In[322]:


val_mape_df = pd.DataFrame(list(val_mape_dict.items()),columns = new_columns)


# In[323]:


val_mape_df.sort_values(ascending=True,by='Validation_Mape_Values')


# In[324]:


val_results


# In[325]:


import plotly.graph_objects as go
from plotly.subplots import make_subplots



for i in val_results.columns:



# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])



# Add traces
fig.add_trace(
go.Scatter(x=val_results.index, y=val_results['y_val'], name='Ethylene_Fuelgas_to_ACN_kg/h'),
secondary_y=False,
)



fig.add_trace(
go.Scatter(x=val_results.index, y=val_results[i], name=i),
secondary_y=True,
)




fig.show()


# #### Trying the model on test set

# In[326]:


test_mape = []


# In[327]:


linreg_model_test = LinearRegression().fit(x_main,y_main)
pred_linreg_test = linreg_model.predict(x_test)
test_mape_linreg = mean_absolute_percentage_error(pred_linreg_test,y_test)


# In[328]:


test_mape.append(test_mape_linreg)


# In[329]:


linreg_model_test.coef_


# In[330]:


rf_model = RandomForestRegressor().fit(x_main,y_main)
pred_rfmodel_test = rf_model.predict(x_test)
test_mape_rfmodel = mean_absolute_percentage_error(pred_rfmodel_test,y_test)


# In[331]:


test_mape.append(test_mape_rfmodel)


# In[332]:


lasso_model = Lasso().fit(x_main,y_main)
pred_lassomodel_test = lasso_model.predict(x_test)
test_mape_lassomodel = mean_absolute_percentage_error(pred_lassomodel_test,y_test)


# In[333]:


test_mape.append(test_mape_lassomodel)


# In[334]:


ridge_model = Ridge().fit(x_main,y_main)
pred_ridgemodel_test = ridge_model.predict(x_test)
test_mape_ridgemodel = mean_absolute_percentage_error(pred_ridgemodel_test,y_test)


# In[335]:


test_mape.append(test_mape_ridgemodel)


# In[336]:


import statsmodels.api as sm
import statsmodels.formula.api as smf
from sklearn.linear_model import HuberRegressor, LinearRegression, TheilSenRegressor, RANSACRegressor


# In[337]:


huber_model = HuberRegressor().fit(x_main,y_main)
pred_hubermodel_test = huber_model.predict(x_test)
test_mape_hubermodel = mean_absolute_percentage_error(pred_hubermodel_test,y_test)


# In[338]:


test_mape.append(test_mape_hubermodel)


# In[339]:


theilsen_model = TheilSenRegressor().fit(x_main,y_main)
pred_theilsenmodel_test = theilsen_model.predict(x_test)
test_mape_theilsenmodel = mean_absolute_percentage_error(pred_theilsenmodel_test,y_test)


# In[340]:


test_mape.append(test_mape_theilsenmodel)


# In[341]:


ransac_model = RANSACRegressor().fit(x_main,y_main)
pred_ransacmodel_test = ransac_model.predict(x_test)
test_mape_ransacmodel = mean_absolute_percentage_error(pred_ransacmodel_test,y_test)


# In[342]:


test_mape.append(test_mape_ransacmodel)


# ### Scatter Plot of RansacModelPreds and Actual Values - Test Set

# In[343]:


# x and y given as array_like objects
import plotly.express as px
fig = px.scatter(x=pred_ransacmodel_test, y=y_test,labels=dict(pred_ransacmodel_test="Preds", y_test="Actuals"))
fig.show()


# In[344]:


test_results = pd.DataFrame()

test_results['y_test'] = y_test
test_results['pred_linreg_test'] = pred_linreg_test
test_results['pred_rfmodel_test'] = pred_rfmodel_test
test_results['pred_lassomodel_test'] = pred_lassomodel_test
test_results['pred_ridgemodel_test'] = pred_ridgemodel_test
test_results['pred_hubermodel_test'] = pred_hubermodel_test
test_results['pred_theilsenmodel_test'] = pred_theilsenmodel_test
test_results['pred_ransacmodel_test'] = pred_ransacmodel_test


# In[345]:


test_mape_dict = dict({test_mape[0]: 'Mape_of_Linreg', test_mape[1]: 'Mape_of_RF_Regressor',
test_mape[2]:'Mape_of_Lasso_Regression',test_mape[3]:'Mape_of_Ridge_Regression',
test_mape[4]:'Mape_of_Huber_Regression',test_mape[5]:'Mape_of_Theilsen_Regression',
test_mape[6]:'Mape_of_Ransac_Regression'})


# In[346]:


new_columns_test = ['Test_Mape_Values','Results_of_Algorithms']


# In[347]:


test_mape_df = pd.DataFrame(list(test_mape_dict.items()),columns = new_columns_test)


# In[348]:


test_mape_df.sort_values(ascending=True,by='Test_Mape_Values')


# In[349]:


test_results


# In[350]:


test_mape


# ### Test Sonucları ve Tahmin Edilen Değerler - İkili Grafik

# In[351]:


import plotly.graph_objects as go
from plotly.subplots import make_subplots



for i in test_results.columns:



# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])



# Add traces
fig.add_trace(
go.Scatter(x=test_results.index, y=test_results['y_test'], name='Ethylene_Fuelgas_to_ACN_kg/h'),
secondary_y=False,
)



fig.add_trace(
go.Scatter(x=test_results.index, y=test_results[i], name=i),
secondary_y=True,
)




fig.show()
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.