Advertisement
mikolajmki

si_lab03

Oct 27th, 2022
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. from sklearn.neighbors import KNeighborsClassifier as kNN
  4. from sklearn.svm import SVC as SVM
  5. from sklearn.metrics import confusion_matrix
  6. from sklearn.model_selection import train_test_split
  7. col_names = ['Gender', 'Married', 'Education', 'Self_Employed', 'Loan_Status']
  8. col_values = ['Male', 'Yes', 'Graduate', 'Yes', 'Y']
  9. pd.options.mode.chained_assignment = None
  10. data = pd.read_excel('loan_data.xlsx');
  11. for i in range(len(col_names)):
  12.     mask = data[col_names[i]].values == col_values[i]
  13.     data[col_names[i]][mask] = 0
  14.     data[col_names[i]][~mask] = 1
  15. columns = list(data.columns);
  16. # mask = data ['Gender'].values == 'Female'
  17. # data['Gender'][mask] = 1
  18. # data['Gender'][~mask] = 0
  19. cat_feature = pd.Categorical(data.Property_Area)
  20. one_hot = pd.get_dummies(cat_feature)
  21. data = pd.concat([data, one_hot], axis = 1)
  22. data = data.drop(columns = ['Property_Area'])
  23. features = data.columns
  24. vals = data.values.astype(np.float64)
  25. X = vals[:, :-1]
  26. y = vals[:,-1]
  27. models = [kNN(), SVM()]
  28. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  29. for model in models:
  30.     model.fit(X_train,y_train)
  31.     y_pred = model.predict(X_test)
  32.     print(confusion_matrix(y_test, y_pred))
  33. from sklearn.tree import DecisionTreeClassifier as DT
  34. from sklearn.tree import plot_tree
  35. model = DT(max_depth=3)
  36. model.fit(X_train, y_train)
  37. y_pred = model.predict(X_test)
  38. cm = confusion_matrix(y_test, y_pred)
  39. print(cm)
  40. from matplotlib import pyplot as plt
  41. plt.figure(figsize=(20,10))
  42. tree_vis = plot_tree(model,feature_names=
  43.  data.columns[:-1],class_names=['N', 'Y'], fontsize = 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement