Jovian
⭐️
Sign In
In [2]:
from sklearn import datasets
import numpy as np
from sklearn.tree import DecisionTreeClassifier
In [3]:
import pandas as pd
In [4]:
data=pd.read_csv("train_ctrUa4K.csv")
data
Out[4]:
In [5]:
y=data.iloc[:,-1:]
y
Out[5]:
In [6]:
data=data.drop(columns="Loan_Status")
data
Out[6]:
In [7]:
x=pd.get_dummies(data["Property_Area"],drop_first=1)
x
Out[7]:
In [8]:
data
Out[8]:
In [9]:
data=data.drop(columns=['Property_Area'])
In [10]:
data1=pd.concat([data,x],axis=1)
In [11]:
data1
Out[11]:
In [12]:
data2=data1['Gender'].fillna(method='ffill')
In [13]:
data1=data1.drop(columns='Gender')
In [14]:
data1=pd.concat([data1,data2],axis=1)
In [15]:
data1
Out[15]:
In [16]:
data3=data1['Self_Employed'].fillna(method='ffill')
In [17]:
data1=data1.drop(columns='Self_Employed')
In [18]:
data1=pd.concat([data1,data3],axis=1)
In [19]:
data1
Out[19]:
In [20]:
data4=data1['Loan_Amount_Term'].fillna(method='bfill')
In [21]:
data1=data1.drop(columns='Loan_Amount_Term')
In [22]:
data1=pd.concat([data1,data4],axis=1)
In [23]:
data1
Out[23]:
In [24]:
data4=data1['Credit_History'].fillna(method='bfill')
In [25]:
data1=data1.drop(columns='Credit_History')
In [26]:
data1=pd.concat([data1,data4],axis=1)
In [27]:
data1
Out[27]:
In [28]:
data4=data1['Dependents'].fillna(method='bfill')
In [29]:
data1=data1.drop(columns='Dependents')
In [30]:
data1=pd.concat([data1,data4],axis=1)
In [31]:
data1=data1.replace('3+',3)
In [32]:
data1
Out[32]:
In [33]:
data4=data1['LoanAmount'].fillna(method='bfill')
In [34]:
data1=data1.drop(columns='LoanAmount')
In [35]:
data1=pd.concat([data1,data4],axis=1)
In [36]:
x1=pd.get_dummies(data1["Married"],drop_first=1)
In [37]:
data1=pd.concat([x1,data1],axis=1)
In [38]:
data1=data1.drop(columns='Married')
In [39]:
from sklearn.preprocessing import LabelEncoder
In [40]:
x1=pd.get_dummies(data1["Self_Employed"],drop_first=1)
In [41]:
data1=pd.concat([x1,data1],axis=1)
In [42]:
data1=data1.drop(columns='Self_Employed')
In [43]:
data1
Out[43]:
In [44]:
x1=pd.get_dummies(data1["Gender"],drop_first=1)
In [45]:
data1=pd.concat([x1,data1],axis=1)
In [46]:
data1=data1.drop(columns='Gender')
In [47]:
data1
Out[47]:
In [48]:
x1=pd.get_dummies(data1["Education"],drop_first=1)
In [49]:
data1=pd.concat([x1,data1],axis=1)
In [50]:
data1=data1.drop(columns='Education')
In [51]:
data1
Out[51]:
In [52]:
a=data1.drop(columns="Loan_ID")
In [53]:
a
Out[53]:
In [ ]:
 
In [54]:
from sklearn.model_selection import train_test_split
In [55]:
x_train,x_test,y_train,y_test = train_test_split(a,y,test_size=0.3,random_state=7)
In [ ]:
 
In [56]:
from sklearn.neighbors import KNeighborsClassifier
In [57]:
model = KNeighborsClassifier(n_neighbors=2)
In [58]:
# Train the model using the training sets
model=model.fit(x_train,y_train)
E:\ml\anaconda\lib\site-packages\ipykernel_launcher.py:2: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
In [59]:
pred= model.predict(x_test)
In [60]:
pred=pd.DataFrame(pred)

In [61]:
from sklearn.metrics import mean_squared_error,r2_score
In [62]:
model.score(x_train,y_train)
Out[62]:
0.7855477855477856
In [63]:
model.score(x_test,y_test)
Out[63]:
0.5621621621621622
In [ ]:
 
In [64]:
from sklearn.linear_model import LogisticRegression
In [65]:
model2=LogisticRegression(random_state=0, solver='warn',multi_class='warn').fit(x_train, y_train)
E:\ml\anaconda\lib\site-packages\sklearn\linear_model\logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning. FutureWarning) E:\ml\anaconda\lib\site-packages\sklearn\utils\validation.py:724: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, warn=True)
In [66]:
pred2=model2.predict(x_test)
In [67]:
model2.score(x_train,y_train)
Out[67]:
0.8018648018648019
In [68]:
model2.score(x_test,y_test)
Out[68]:
0.8108108108108109
In [ ]:
 
In [69]:
from sklearn.tree import DecisionTreeClassifier
In [70]:
model3=DecisionTreeClassifier(criterion='entropy',splitter='random').fit(x_train,y_train)
In [71]:
pred3=model3.predict(x_test)
In [72]:
model3.score(x_train,y_train)
Out[72]:
1.0
In [73]:
model3.score(x_test,y_test)
Out[73]:
0.7189189189189189
In [ ]:
 
In [74]:
from sklearn.svm import SVC
In [ ]:
model4=SVC(kernel='poly',decision_function_shape='ovo').fit(x_train,y_train)
In [ ]:
pred4=model4.predict(x_test)
In [ ]:
model4.score(x_train,y_train)
In [ ]:
model4.score(x_test,y_test)
In [ ]:
 
In [1]:
import jovian
In [ ]:
jovian.commit()
[jovian] Saving notebook..
In [ ]:
 
In [ ]:
 
In [ ]: