Jovian
⭐️
Sign In
In [89]:
from sklearn import datasets
import numpy as np
from sklearn.tree import DecisionTreeClassifier
In [90]:
import pandas as pd
In [107]:
data=pd.read_csv("train_ctrUa4K.csv")
data
Out[107]:
In [92]:
y=data.iloc[:,-1:]
y
Out[92]:
In [109]:
data=data.drop(columns="Loan_Status")
data
Out[109]:
In [94]:
x=pd.get_dummies(data["Property_Area"],drop_first=1)
x
Out[94]:
In [110]:
data
Out[110]:
In [111]:
data=data.drop(columns=['Property_Area'])
In [132]:
data1=pd.concat([data,x],axis=1)
In [143]:
data1
Out[143]:
In [134]:
data2=data1['Gender'].fillna(method='ffill')
In [145]:
data1=data1.drop(columns='Gender')
In [147]:
data1=pd.concat([data1,data2],axis=1)
In [148]:
data1
Out[148]:
In [167]:
data3=data1['Self_Employed'].fillna(method='ffill')
In [155]:
data1=data1.drop(columns='Self_Employed')
In [157]:
data1=pd.concat([data1,data3],axis=1)
In [178]:
data1
Out[178]:
In [186]:
data4=data1['Loan_Amount_Term'].fillna(method='bfill')
In [188]:
data1=data1.drop(columns='Loan_Amount_Term')
In [189]:
data1=pd.concat([data1,data4],axis=1)
In [190]:
data1
Out[190]:
In [191]:
data4=data1['Credit_History'].fillna(method='bfill')
In [193]:
data1=data1.drop(columns='Credit_History')
In [195]:
data1=pd.concat([data1,data4],axis=1)
In [196]:
data1
Out[196]:
In [205]:
data4=data1['Dependents'].fillna(method='bfill')
In [206]:
data1=data1.drop(columns='Dependents')
In [207]:
data1=pd.concat([data1,data4],axis=1)
In [213]:
data1=data1.replace('3+',3)
In [215]:
data1
Out[215]:
In [ ]:
 
In [ ]:
 
In [198]:
from sklearn.preprocessing import LabelEncoder
In [ ]:
 
In [203]:
labelencoder=LabelEncoder()
In [204]:
data1[:,2:]=labelencoder.fit_transform(data1[:,2:])

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-204-5375c030ce9b> in <module> ----> 1 data1[:,2:]=labelencoder.fit_transform(data1[:,2:]) E:\ml\anaconda\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 2925 if self.columns.nlevels > 1: 2926 return self._getitem_multilevel(key) -> 2927 indexer = self.columns.get_loc(key) 2928 if is_integer(indexer): 2929 indexer = [indexer] E:\ml\anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2655 'backfill or nearest lookups') 2656 try: -> 2657 return self._engine.get_loc(key) 2658 except KeyError: 2659 return self._engine.get_loc(self._maybe_cast_indexer(key)) pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() TypeError: '(slice(None, None, None), slice(2, None, None))' is an invalid key
In [9]:
y=pd.DataFrame(y)
In [10]:
x
Out[10]:
array([['LP001002', 'Male', 'No', ..., 360.0, 1.0, 'Urban'],
       ['LP001003', 'Male', 'Yes', ..., 360.0, 1.0, 'Rural'],
       ['LP001005', 'Male', 'Yes', ..., 360.0, 1.0, 'Urban'],
       ...,
       ['LP002983', 'Male', 'Yes', ..., 360.0, 1.0, 'Urban'],
       ['LP002984', 'Male', 'Yes', ..., 360.0, 1.0, 'Urban'],
       ['LP002990', 'Female', 'No', ..., 360.0, 0.0, 'Semiurban']],
      dtype=object)
In [11]:
labelencoder_X_1 = LabelEncoder()
x[:, -1] = labelencoder_X_1.fit_transform(x[:, -1])
x
Out[11]:
array([['LP001002', 'Male', 'No', ..., 360.0, 1.0, 2],
       ['LP001003', 'Male', 'Yes', ..., 360.0, 1.0, 0],
       ['LP001005', 'Male', 'Yes', ..., 360.0, 1.0, 2],
       ...,
       ['LP002983', 'Male', 'Yes', ..., 360.0, 1.0, 2],
       ['LP002984', 'Male', 'Yes', ..., 360.0, 1.0, 2],
       ['LP002990', 'Female', 'No', ..., 360.0, 0.0, 1]], dtype=object)
In [19]:
onehotencoder = OneHotEncoder(categorical_features = [0])
x= onehotencoder.fit_transform(x).toarray()
x 
E:\ml\anaconda\lib\site-packages\sklearn\preprocessing\_encoders.py:451: DeprecationWarning: The 'categorical_features' keyword is deprecated in version 0.20 and will be removed in 0.22. You can use the ColumnTransformer instead. "use the ColumnTransformer instead.", DeprecationWarning)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-19-7958a9a2201d> in <module> 1 onehotencoder = OneHotEncoder(categorical_features = [0]) ----> 2 x= onehotencoder.fit_transform(x).toarray() 3 x E:\ml\anaconda\lib\site-packages\sklearn\preprocessing\_encoders.py in fit_transform(self, X, y) 627 return _transform_selected( 628 X, self._legacy_fit_transform, self.dtype, --> 629 self._categorical_features, copy=True) 630 else: 631 return self.fit(X).transform(X) E:\ml\anaconda\lib\site-packages\sklearn\preprocessing\base.py in _transform_selected(X, transform, dtype, selected, copy, retain_order) 43 Xt : array or sparse matrix, shape=(n_samples, n_features_new) 44 """ ---> 45 X = check_array(X, accept_sparse='csc', copy=copy, dtype=FLOAT_DTYPES) 46 47 if sparse.issparse(X) and retain_order: E:\ml\anaconda\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 494 try: 495 warnings.simplefilter('error', ComplexWarning) --> 496 array = np.asarray(array, dtype=dtype, order=order) 497 except ComplexWarning: 498 raise ValueError("Complex data not supported\n" E:\ml\anaconda\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order) 536 537 """ --> 538 return array(a, dtype, copy=False, order=order) 539 540 ValueError: could not convert string to float: 'LP001002'
In [216]:
import jovian
In [ ]:
jovian.commit()
[jovian] Saving notebook..
In [ ]: