About Lesson
Join the conversation
df['age']=df['age'].fillna(df['age'].mean())
Reply
# Importing Libraries
import pandas as pd
import numpy as np
import seaborn as sns# Loading DataSet
df=sns.load_dataset('titanic')
df['age']=df['age'].fillna(df['age'].mean())
x=df[['age','fare','pclass','parch','sex','sibsp']]
y=df['survived']
x=pd.get_dummies(x,columns=['sex'])# Importing ML Algorithm
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.75, random_state=42)# Random ForestPrameter
param_cv={'n_estimators':[10,50 ,100],
'min_samples_split':[2,3,4],
'max_depth':[3,5,7, None],
'max_features': ['log2','sqrt']}# Decision Tree Parameter
param_cvv={ 'max_depth':[3,5,7,None],'min_samples_split':[ 2,3,4]}# KNN Neighbour Parameter
param_cvvv={'n_neighbors':[5,7],'weights':['uniform','distance']}models = [(RandomForestClassifier(),param_cv,'Random Forest Classifier'),
(DecisionTreeClassifier(),param_cvv,'Decision Tree Classifier'),
(KNeighborsClassifier(),param_cvvv,'KNeighbour')]
# model_names = ['Random Forest Classifier','Decision Tree Classifer','KNN Classifier']
for model,param_grid,model_names in models:
grid_search=GridSearchCV(model,param_grid,cv=5,scoring='accuracy')
grid_search.fit(x,y)
print(f"Model: {model_names}")
print('Best Parameters are: ',grid_search.best_params_)
print("Best Score:",grid_search.best_score_*100)
best_model=grid_search.best_estimator_
y_predict=best_model.predict(x_test)
accurac=accuracy_score(y_test,y_predict)
print('Accuracy Score for ',model_names,'is',accurac)
print("n")
Reply
Thanks for so comprehensive Leactures
Reply
X = pd.get_dummies(X, columns=['sex'])
Reply
df['age'].fillna(df['age'].mean(),inplace=True)
Reply
age columns main nan values hain
Reply
how we share my notebook in github
Reply
github ka link please share kar dain
Reply
X.age.fillna(value = X['age'].mean(), inplace=True)
Reply