<jupyter_start><jupyter_text>The Above Graph shows the outliers and none of them exceeds above 0.25, so we need to eliminate the outliers increase the performance of our model.<jupyter_code>df = df[df['Salary'] <= 250000]
df = df[df['Salary'] >= 10000]
df = df[df['Country'] != 'Other']
df.shape<jupyter_output><empty_output><jupyter_text>Now let's check again, how our data looks<jupyter_code>fig, ax = plt.subplots(1,1,figsize=(12,7))
df.boxplot('Salary','Country',ax=ax)
plt.suptitle('Salary (US$) vs Country')
plt.title('')
plt.ylabel('salary')
plt.xticks(rotation=90)
plt.show()
df['YearsCodePro'].unique()
#Let's Clean the data
def clean_experience(x):
  if x == 'More than 50 years':
    return 50
  if x == 'Less than 1 year':
    return 0.5
  return float(x)
df['YearsCodePro'] = df['YearsCodePro'].apply(clean_experience)
df['YearsCodePro'].dtype
df['EdLevel'].unique()
# Lets Clean Education Column
def clean_education(x):
  if 'Bachelor’s degree' in x:
    return 'Bachelor’s degree'
  if 'Master’s degree' in x:
    return 'Master’s degree'
  if 'Professional degree' in x or 'Other doctoral degree' in x:
    return 'Post grad'
  return 'Less than a Bachelor'
df['EdLevel'] = df['EdLevel'].apply(clean_education)
df['EdLevel'].unique()
df['EdLevel'].value_counts()
from sklearn.preprocessing import LabelEncoder
le_education = LabelEncoder()
df['EdLevel'] = le_education.fit_transform(df['EdLevel'])
df['EdLevel'].unique()
le_country = LabelEncoder()
df['Country'] = le_country.fit_transform(df['Country'])
df['Country'].unique()
df.columns<jupyter_output><empty_output><jupyter_text>Now the data Cleaning and transforming  has been ended, let's build the model<jupyter_code>x = df.drop('Salary',axis=1)
y = df['Salary']
from sklearn.linear_model import LinearRegression
linear_reg = LinearRegression()
linear_reg.fit(x,y.values)
y_pred = linear_reg.predict(x)
from sklearn.metrics import mean_squared_error, mean_absolute_error
import numpy as np
error = np.sqrt(mean_squared_error(y,y_pred))
error
from sklearn.tree import DecisionTreeRegressor
dec_tree_reg = DecisionTreeRegressor(random_state=0)
dec_tree_reg.fit(x,y.values)
y_pred = dec_tree_reg.predict(x)
error = np.sqrt(mean_squared_error(y,y_pred))
print('${:,.02f}'.format(error))
from sklearn.ensemble import RandomForestRegressor
random_forest_reg = RandomForestRegressor(random_state=0)
random_forest_reg.fit(x, y.values)
y_pred = random_forest_reg.predict(x)
error = np.sqrt(mean_squared_error(y,y_pred))
print('${:,.02f}'.format(error))<jupyter_output>$29,487.31<jupyter_text>We can use GridSearchCV for finding the best fit!<jupyter_code>from sklearn.model_selection import GridSearchCV

max_depth = [None,2,4,6,8,10,12]
parameters = {'max_depth': max_depth}

regressor = DecisionTreeRegressor(random_state=0)
gs = GridSearchCV(regressor, parameters, scoring='neg_mean_squared_error')
gs.fit(x,y.values)
regressor = gs.best_estimator_

regressor.fit(x,y.values)
y_pred = regressor.predict(x)
error = np.sqrt(mean_squared_error(y,y_pred))
print('${:,.02f}'.format(error))
x
X = np.array([["United States", 'Master’s degree', 15 ]])
X
X[:, 0] = le_country.transform(X[:,0])
X[:, 1] = le_education.transform(X[:,1])
X = X.astype(float)
X
y_pred = regressor.predict(X)
y_pred
import pickle
data = {"model": regressor, "le_country": le_country, "le_education": le_education}
with open('saved_steps.pkl', 'wb') as file:
    pickle.dump(data, file)
with open('saved_steps.pkl', 'rb') as file:
    data = pickle.load(file)

regressor_loaded = data["model"]
le_country = data["le_country"]
le_education = data["le_education"]
y_pred = regressor_loaded.predict(X)
y_pred<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14473331546502277, 'fraction_numerical': 0.015813454837845083, 'mean_word_length': 6.375494071146245, 'pattern_counts': {'":': 3, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '471113', 'license': 'MIT', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1357, 'n_words': 286, 'path': 'SalaryPrediction/SalaryPrediction (1)-Copy1.ipynb', 'repo_name': 'niranjan-1/Data_Science_Projects', 'size': '3731'}