# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
<a id='top'></a>
<div class="list-group" id="list-tab" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
          text-align: center; font-size:250%;border-radius:20px 80px;">FIFA 19 Complete Players dataset</p>
"""
"""
![1_jgWmDxmnDmX2UBB7RzmI6A.jpeg](attachment:1_jgWmDxmnDmX2UBB7RzmI6A.jpeg)
"""
"""
<b>🙋‍♂️🙋‍♀️ Hi all !!! we are going to work with Fifa 19 players dataset,If you like this notebook then plz consider a upvote for me and if don't plz drop your advise or comment so that i improve my skill 😎😎
"""
"""
<a id=""></a>
<div class="list-group" id="list-tab" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:250%;border-radius:20px 80px;"> 🚢Data Story</p>
"""
"""
<b><u>This dataset includes lastest edition FIFA 2019 players attributes like:-</u>
    
Age, Nationality, Overall, Potential, Club, Value, Wage, Preferred Foot, International Reputation, Weak Foot, Skill Moves, Work Rate, Position, Jersey Number, Joined, Loaned From, Contract Valid Until, Height, Weight, LS, ST, RS, LW, LF, CF, RF, RW, LAM, CAM, RAM, LM, LCM, CM, RCM, RM, LWB, LDM, CDM, RDM, RWB, LB, LCB, CB, RCB, RB, Crossing, Finishing, Heading, Accuracy, ShortPassing, Volleys, Dribbling, Curve, FKAccuracy, LongPassing, BallControl, Acceleration, SprintSpeed, Agility, Reactions, Balance, ShotPower, Jumping, Stamina, Strength, LongShots, Aggression, Interceptions, Positioning, Vision, Penalties, Composure, Marking, StandingTackle, SlidingTackle, GKDiving, GKHandling, GKKicking, GKPositioning, GKReflexes, and Release Clause.
    
 
    
    

    
    
    
    
    
Reference : https://www.kaggle.com/karangadiya/fifa19    
"""
"""
<a id="1"></a>
<div class="list-group" id="1" role="tablist">
    
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:250%;border-radius:20px 80px;">🎰 Importing Libraries</p>
"""
#importing library
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
import seaborn as sns 
import datetime
import missingno as msno
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error
from sklearn.preprocessing import LabelEncoder,OneHotEncoder,OrdinalEncoder,StandardScaler,MinMaxScaler,PowerTransformer,FunctionTransformer
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import AdaBoostRegressor,GradientBoostingRegressor,RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from xgboost import XGBRegressor
from mlxtend.feature_selection import ExhaustiveFeatureSelector,SequentialFeatureSelector
from sklearn.feature_selection import mutual_info_regression,SelectKBest
import cufflinks as cf
import plotly.offline
cf.go_offline()
cf.set_config_file(offline=False, world_readable=True)
import plotly 
import plotly.express as px
import plotly.graph_objs as go
import plotly.offline as py
from plotly.offline import iplot
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
from plotly import tools
import warnings
warnings.filterwarnings("ignore")


%matplotlib inline 

"""
<a id="2"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:250%;border-radius:20px 80px;">🎰 Load the dataset</p>
"""
#Load the dataset and check the data head.
df=pd.read_csv("../input/fifa19/data.csv")
df.head().T
df.columns
"""
<a id="2"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;">📝 Meta information 
    about Dataframe</p>
"""
#check the rows and columns number.
print(f"number of rows:--> {df.shape[0]} and number of columns:--> {df.shape[1]}")
#chack the data information
df.info()
"""
<a id="3"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;">🛒Missing values </p>
"""
#check for missing values 
def checking_m(df):
    null_v = df.isnull().sum().sort_values(ascending=False)
    null_percent = (df.isnull().sum()/df.isnull().count()).sort_values(ascending=False)
    null_v = pd.concat([null_v, null_percent], axis=1, keys=['Missing_Number', 'Missing_Percent'])
    return null_v

checking_m(df)
msno.matrix(df,color=(0.100, 0.100, 0.100))
"""
<b>Too much missing values 😫😫,we will fix it later !
   
"""
"""
<b>we got some of the rows are almost empty or contain some data/values ,let's find their index and remove them .to find them we use the height and weight columns because these two columns are contain no missing values except those rows 😎
    
"""
miss_height = df[df['Height'].isnull()].index.tolist()
miss_weight = df[df['Weight'].isnull()].index.tolist()

#check if the dataset contain the completly empty rows 
if miss_height == miss_weight:
    print('Yes,empty rows exist😐😐')
else:
    print('Not found 😃😃')
#lets remove them 
df.drop(df.index[miss_height],inplace=True)
"""
<a id="3.1"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;">🤼‍♀️Duplicated rows </p>
"""
#check for dublicated rows
print("Numbers of duplicated rows :",df.duplicated().sum())
"""
<b>No duplicted rows found , great 😁
"""
"""
<a id="4"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;">📈 Statistical information of Dataframe </p>
"""
#check data description
df.describe().T.style.bar(subset=['mean'], color='#205ff2').background_gradient(subset=['std'], cmap='mako').background_gradient(subset=['50%'], cmap='coolwarm')
"""
<a id="2"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;">📝 Data Cleaning+Processing</p>
"""
df.drop(['Unnamed: 0',"ID",'Photo','Flag','Club Logo','Real Face','Jersey Number','Loaned From','Release Clause'],
          axis=1,inplace=True)
#cleaning the value and wage col
def m_and_k(Value):
    if isinstance(Value,str):
        out = Value.replace('€', '')
        if 'M' in out:
            out = float(out.replace('M', ''))*1000000
        elif 'K' in Value:
            out = float(out.replace('K', ''))*1000
        return float(out)
    
df["Value"]=df["Value"].apply(lambda x:m_and_k(x))
df["Wage"]=df["Wage"].apply(lambda x:m_and_k(x))
df[["Value","Wage"]].head(10).T
df.head()
#clean and preprocess the Joined columns
df["Joined"]=pd.to_datetime(df["Joined"])
df["Joined"]=pd.DatetimeIndex(df["Joined"]).year
df["Joined"]=df["Joined"].replace(np.nan,0)
df["Joined"]=df["Joined"].astype(str)
df["Joined"].head()
#clean and preprocess the Contract Valid Until columns
df["Contract Valid Until"]=pd.to_datetime(df["Contract Valid Until"])
df["Contract Valid Until"]=pd.DatetimeIndex(df["Contract Valid Until"]).year
df["Contract Valid Until"]=df["Contract Valid Until"].replace(np.nan,0)
df["Contract Valid Until"]=df["Contract Valid Until"].astype(str)
#clean and process the height columns 
def clean_height(val):
    test=[]
    value=[]
    if isinstance(val,str):
        test=val.split("'")
        i=test[0]
        j=test[1]
        value=(int(i*12)+int(j))/12
    return(value)   
df["Height"]=df["Height"].apply(clean_height) 

#clean and process the Weight columns 
def clean_weight(val):
    if isinstance(val,str):
        return(val.replace("lbs",""))   
df["Weight"]=df["Weight"].apply(clean_weight) 
df["Weight"]=df["Weight"].astype(float)
df.head()
"""
<a id="6"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:250%;border-radius:20px 80px;">🔥 EDA & Visualization</p>
"""
#top 30 contries with highset numbers of players 
y = df.Nationality.value_counts().head(30).index
x = df.Nationality.value_counts().head(30).values

# Athletes barplot with Discipline
plt.rcParams['figure.dpi'] = 600

fig = plt.figure(figsize=(2,3), facecolor='#f6f5f5')
gs = fig.add_gridspec(1, 1)
gs.update(wspace=1.5, hspace=0.05)

background_color = "#f6f5f5"
sns.set_palette(['#ff355d']*1200)
ax0 = fig.add_subplot(gs[0, 0])
for s in ["right", "top"]:
    ax0.spines[s].set_visible(False)
    
ax0.set_facecolor(background_color)


ax0_sns = sns.barplot(data=df,x=x,y=y,zorder=2) 
ax0_sns.set_xlabel('No of Players',fontsize=4, weight='bold')
ax0_sns.set_ylabel('Countries',fontsize=4, weight='bold')

ax0_sns.grid(which='major', axis='x', zorder=0, color='#EEEEEE', linewidth=0.4)
ax0_sns.grid(which='major', axis='y', zorder=0, color='#EEEEEE', linewidth=0.4)


ax0_sns.tick_params(labelsize=3, width=0.5, length=1.5)


for p in ax0_sns.patches:
            value = f'{p.get_width():.0f}'
            x = p.get_x() + p.get_width() + 20
            y = p.get_y() + p.get_height() / 2 
            ax0.text(x, y, value, ha='left', va='center', fontsize=3, 
                    bbox=dict(facecolor='none', edgecolor='black', boxstyle='round', linewidth=0.3))

plt.show()


"""
<b>European contries players are most here
    
"""
#analysis the club columns
print("Here total numbers of club are:",df["Club"].nunique())
#top 20 club with highest numbers of players 
y = df.Club.value_counts().head(20).index
x = df.Club.value_counts().head(20).values
plt.rcParams['figure.dpi'] = 600

fig = plt.figure(figsize=(2,2), facecolor='#f6f5f5')
gs = fig.add_gridspec(1, 1)
gs.update(wspace=1.5, hspace=0.05)

background_color = "#f6f5f5"
sns.set_palette(['#00FFFF'])
ax0 = fig.add_subplot(gs[0, 0])
for s in ["right", "top"]:
    ax0.spines[s].set_visible(False)
    
ax0.set_facecolor(background_color)


ax0_sns = sns.barplot(data=df,x=x,y=y,zorder=2) 
ax0_sns.set_xlabel('No of Players',fontsize=3, weight='bold')
ax0_sns.set_ylabel('Club',fontsize=3, weight='bold')

ax0_sns.grid(which='major', axis='x', zorder=0, color='#EEEEEE', linewidth=0.4)
ax0_sns.grid(which='major', axis='y', zorder=0, color='#EEEEEE', linewidth=0.4)


ax0_sns.tick_params(labelsize=2.5, width=0.5, length=1.5)


for p in ax0_sns.patches:
            value = f'{p.get_width():.0f}'
            x = p.get_x() + p.get_width() +2
            y = p.get_y() + p.get_height() / 2 
            ax0.text(x, y, value, ha='left', va='center', fontsize=2, 
                    bbox=dict(facecolor='none', edgecolor='black', boxstyle='round', linewidth=0.3))

plt.show()


#analysis the max and minimum potential and  overall performanced players 
print('Maximum Potential player :---> '+str(df.loc[df['Potential'].idxmax()][0]))
print('Maximum Overall Perforamnced player:---> '+str(df.loc[df['Overall'].idxmax()][0]))
print()
print('Minimum Potential player:---> '+str(df.loc[df['Potential'].idxmin()][0]))
print('Minimum Overall Perforamnced player:---> '+str(df.loc[df['Overall'].idxmin()][0]))
#top 10 maximum potential and overall performenced players 
top_potential=df.sort_values(by="Potential",ascending=False).head(10)

fig = go.Figure()
fig.add_trace(go.Bar(
    y=top_potential.Name,
    x=top_potential.Potential,
   orientation='h',
    name='potential',
    marker=dict(
        color='rgba(300, 200, 100, 50)',
        line=dict(color='rgba(246, 78, 139, 1.0)')
    )
    ))

template = dict(layout=go.Layout(title_font=dict(family="Rockwell", size=30)))

fig.update_layout(
                title = 'Top 10 potential players',template = template,barmode='stack', autosize=False,width=650,
                height=400,margin=dict( l=100,r=100, b=30,t=100, pad=4
                          )
)

fig.show()

       
top_overall=df.sort_values(by="Overall",ascending=False).head(10)

fig = go.Figure()
fig.add_trace(go.Bar(
    y=top_overall.Name,
    x=top_overall.Overall,
   orientation='h',
    name='potential',
    marker=dict(
        color='rgba(100, 200, 100, 50)',
        line=dict(color='rgba(246, 78, 139, 1.0)')
    )
    ))

template = dict(layout=go.Layout(title_font=dict(family="Rockwell", size=30)))

fig.update_layout(
                title = 'Top 10 Overall performanced players',template = template,barmode='stack', autosize=False,width=650,
                height=400,margin=dict( l=100,r=100, b=30,t=100, pad=4
                          )
)

fig.show()

       
#Best in others aspect 
best_players=['Crossing', 'Finishing', 'HeadingAccuracy', 'ShortPassing', 'Volleys',
       'Dribbling', 'Curve', 'FKAccuracy', 'LongPassing', 'BallControl',
       'Acceleration', 'SprintSpeed', 'Agility', 'Reactions', 'Balance',
       'ShotPower', 'Jumping', 'Stamina', 'Strength', 'LongShots',
       'Aggression', 'Interceptions', 'Positioning', 'Vision', 'Penalties',
       'Composure', 'Marking', 'StandingTackle', 'SlidingTackle', 'GKDiving',
       'GKHandling', 'GKKicking', 'GKPositioning', 'GKReflexes']
print("Best in others aspect :")
print("\n<----------------------------------------------------------------->\n")
for i in best_players:
    print('Best {0}:{1} '.format(i,df.loc[df[i].idxmax()][0]))
print("\n<----------------------------------------------------------------->\n")    
#Most valuable and highest earners players
print('Most valued player : '+str(df.loc[df['Value'].idxmax()][0]))
print('Highest earner : '+str(df.loc[df['Wage'].idxmax()][0]))
#Top 10 most valued players and highest earners players
top_values=df.sort_values(by="Value",ascending=False).head(10)

fig = go.Figure()
fig.add_trace(go.Bar(
    y=top_values.Name,
    x=top_values.Value,
   orientation='h',
    name='potential',
    marker=dict(
        color='rgba(30, 200, 100, 100)',
        line=dict(color='rgba(246, 78, 139, 1.0)')
    )
    ))

template = dict(layout=go.Layout(title_font=dict(family="Rockwell", size=30)))

fig.update_layout(
                title = 'Top 10 valuable players',template = template,barmode='stack', autosize=False,width=650,
                height=400,margin=dict( l=100,r=100, b=30,t=100, pad=4
                          )
)

fig.show()

       
#Top 10 most valued players and highest earners players
top_earners=df.sort_values(by="Wage",ascending=False).head(10)

fig = go.Figure()
fig.add_trace(go.Bar(
    y=top_earners.Name,
    x=top_earners.Wage,
   orientation='h',
    name='potential',
    marker=dict(
        color='rgba(70, 20, 100, 10)',
        line=dict(color='rgba(246, 78, 139, 1.0)')
    )
    ))

template = dict(layout=go.Layout(title_font=dict(family="Rockwell", size=30)))

fig.update_layout(
                title = 'Top 10 Earners player',template = template,barmode='stack', autosize=False,width=650,
                height=400,margin=dict( l=100,r=100, b=30,t=100, pad=4
                          )
)

fig.show()

       
#age columns analysis
plt.figure(figsize=(10,5))
sns.countplot(df["Age"])
plt.title("Players age distribution",fontsize=20)
plt.xlabel("Players age",fontsize=20)
plt.ylabel("Count",fontsize=20)
plt.show()
"""
<b>Most of the players age lies between 19 to 29
"""
#analysis age vs potential ,overall performance,Dribling,stamina,strength,Balance col
data=['Overall', 'Potential']
colors=["#00FFFF","#FFA500","#ADD8E6","#ED00D9","#ED1400"]
for col in data:
    fig = px.scatter(data_frame = df,
             x = "Age",
             y=col,color_discrete_sequence=colors,color_continuous_scale=colors
            ,template='plotly_dark', width=500,height=350)

    fig.show()
   
"""
<b>Analysis:
    As the age is increases ,overall performance,potential is decreases.
"""
sns.lmplot(x = 'BallControl', y = 'Dribbling', data = df,col = 'Preferred Foot',scatter_kws = {'alpha':0.5,'color':'green'},
           line_kws={'color':'blue'})
fig = px.pie(df, names = "Preferred Foot",
             title = "<b>Counts in Preferred Foot</b>",
             hole = 0.4, template = "plotly_dark",
            width=600,
            height=400)

fig.update_traces(textposition='inside',
                  textinfo='percent+label',
                  marker=dict(line=dict(color='#000000', width = 2)))


fig.show()
"""
<b>Most of the players are right in preferred foot
"""
fig = px.pie(df, names = "Work Rate",
             title = "<b>Counts in Work Rate</b>",
             hole = 0.4, template = "plotly_dark",
            width=600,
            height=500)

fig.update_traces(textposition='inside',
                  textinfo='percent+label',
                  marker=dict(line=dict(color='#000000', width = 2)))


fig.show()
#analysis overall performance and age with respect to preferred foot
#plt.rcParams['figure.figsize'] = (20, 7)
plt.figure(figsize=(20,12))
sns.boxenplot(df["Age"],df["Overall"],hue=df["Preferred Foot"])
plt.title("Players age vs overall performance wrt preferred foot",fontsize=40)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel("Players age",fontsize=20)
plt.ylabel("Overall",fontsize=20)
plt.show()
cols = ['Age','Overall','Potential','Acceleration','SprintSpeed',"Agility","Stamina",'Strength','Preferred Foot']
df_small = df[cols]
df_small.head()
#analysis these col wrt preferred foot
sns.pairplot(df_small,hue="Preferred Foot")
"""
<b>Analysis:Most of the players are right in preferred foot 
"""
"""
<a id="6"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:250%;border-radius:20px 80px;">🔥 Feature Engineering</p>
"""
#dropping the unnecceary columns 
df=df.drop(df.columns[20:46],axis=1)
col=["Name","Club","Contract Valid Until"]
df=df.drop(col,axis=1)
df.shape
#impute the missing values
#we have only 1 missing values column = (Position coluumns)
df["Position"].fillna("Unknown",inplace=True)
#get a count of nationalities in the dataset,make of list on those with over 250 players .
nationalities_count=df["Nationality"].value_counts()
nat_list=nationalities_count[nationalities_count>250].index.tolist()

def majorNation(data):
    if (data["Nationality"] in nat_list):
        return 1
    else:
        return 0
df["major_nation"]=df.apply(majorNation,axis=1)

#encode preferred foot col into numerical.
df=pd.get_dummies(df,columns=["Preferred Foot"], drop_first=True)

#Create a simplified position varaible to account for all player positions.
def simple_position(df):
    if (df['Position'] == 'GK'):
        return 'GK'
    elif ((df['Position'] == 'RB') | (df['Position'] == 'LB') | (df['Position'] == 'CB') | (df['Position'] == 'LCB') | (df['Position'] == 'RCB') | (df['Position'] == 'RWB') | (df['Position'] == 'LWB') ):
        return 'DF'
    elif ((df['Position'] == 'LDM') | (df['Position'] == 'CDM') | (df['Position'] == 'RDM')):
        return 'DM'
    elif ((df['Position'] == 'LM') | (df['Position'] == 'LCM') | (df['Position'] == 'CM') | (df['Position'] == 'RCM') | (df['Position'] == 'RM')):
        return 'MF'
    elif ((df['Position'] == 'LAM') | (df['Position'] == 'CAM') | (df['Position'] == 'RAM') | (df['Position'] == 'LW') | (df['Position'] == 'RW')):
        return 'AM'
    elif ((df['Position'] == 'RS') | (df['Position'] == 'ST') | (df['Position'] == 'LS') | (df['Position'] == 'CF') | (df['Position'] == 'LF') | (df['Position'] == 'RF')):
        return 'ST'
    else:
        return df.Position
    
df['Simple_Position'] = df.apply(simple_position,axis = 1)

#encode simple position col into numerical.
df=pd.get_dummies(df,columns=["Simple_Position"], drop_first=True)
#Split the Work Rate Column in two
tempwork = df["Work Rate"].str.split("/ ",expand=True) 

#Create new columns for first and secoend work rate
df["WorkRate1"]= tempwork[0]   
df["WorkRate2"]= tempwork[1]
#encode workrate1 and workRate2 columns into numerical.
df=pd.get_dummies(df,columns=["WorkRate1"], drop_first=True)
df=pd.get_dummies(df,columns=["WorkRate2"], drop_first=True)

df=df.drop(["Nationality",'Body Type','Position',"Work Rate"],axis=1)
df.head()
#get the features and terget columns 
y=df.Value
X=df.drop(["Value"],axis=1)
"""
<a id="7.2"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;"> 🐱‍🏍Train test split</p>
"""
#train test split  
x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)
print("X_train shape: ",x_train.shape)
print("X_test shape: ",x_test.shape)
print()
print("Y_train shape: ",y_train.shape)
print("Y_test shape: ",y_test.shape)

"""
<a id="7.3"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;"> 🎯Scaling the features</p>
"""
# Using standard scaler
sc=StandardScaler()
x_train_scaled=sc.fit_transform(x_train)
x_test_scaled=sc.transform(x_test)

x_train = pd.DataFrame(x_train_scaled, columns = x_train.columns[:])
x_test = pd.DataFrame(x_test_scaled, columns = x_test.columns[:])
x_train.head()
"""
<a id="6"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:250%;border-radius:20px 80px;">🔥 Feature Selection</p>
"""
#Mutual information Gain
mutual=SelectKBest(mutual_info_regression,k=15).fit(x_train,y_train)
mutual
#convert into dataframe and check the top 15 features
fetures=pd.DataFrame({"features":list(x_train.columns),"Score":mutual.scores_})
new=fetures.sort_values("Score",ascending=False)
new.head(15)

#get the top features using the mutual information gain.
xtrain=mutual.transform(x_train)
xtest=mutual.transform(x_test)
xtrain[0:3]
"""
<a id="8"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;"> 🤖 Create & Train Model</p>
"""
#this function used to evalute the models with features and terget.
def models_score(models, x_train, x_test, y_train, y_test):    
    scores = {}
    for name, model in models.items():
        model.fit(x_train, y_train)
        y_pred=model.predict(x_test) 
        scores[name] =r2_score(y_test,y_pred)
        #printing the model name and accuracy !!!!!
        print("Model name: ",model)
        print("R2 score :--->>",r2_score(y_test,y_pred))
        print("MSE",mean_squared_error(y_test,y_pred))
        print("MAE",mean_absolute_error(y_test,y_pred))
        print("\n<<<<------------------------------------------------------------->>>>\n")
           
   
    model_scores = pd.DataFrame(scores, index=['Score']).transpose()
    model_scores = model_scores.sort_values('Score',ascending=False)
    return model_scores
#initialize the models 
models = {"GradiantBoost":GradientBoostingRegressor(random_state=42),
         "RandomForest":RandomForestRegressor(random_state=42),
         "XGBboost":XGBRegressor()}
"""
<a id="9"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;">🧮 Models Score</p>
"""
#Calling the function
model_scores = models_score(models, x_train, x_test, y_train, y_test)
#printing the model score
model_scores
"""
<a id="10"></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;">📊 Visualize Model Score</p>
"""
model_scores = model_scores.reset_index().rename({"index":"Algorithms"}, axis = 1)

model_scores.style.bar()
#pie plot 
label = model_scores['Algorithms']
value = model_scores['Score']

fig = go.Figure(data=[go.Pie(labels = label, values = value, rotation = 90)])

fig.update_traces(textposition='inside',
                  textinfo='percent+label',
                  marker=dict(line=dict(color='#000000', width = 1.5)))

fig.update_layout(title_x=0.5,
                  title_font=dict(size=20),
                  uniformtext_minsize=15)

fig.show()
fig = px.bar(data_frame = model_scores,
             x="Algorithms",
             y="Score",
             color="Algorithms", title = "<b>Models Score</b>", template = 'plotly_dark')

fig.update_layout(bargap=0.2)

fig.show()
"""
<a id=""></a>
<div class="list-group" id="1" role="tablist">
<p style="padding: 20px; background-color:#7FDBFF;color: Black;font-weight: bold;
              text-align: center; font-size:220%;border-radius:20px 80px;">🌈Thank you 🎃 Happy kaggling👅</p>
"""