Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. # Load libraries
  2.  
  3. from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
  4. from sklearn.model_selection import train_test_split # Import train_test_split function
  5. from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
  6.  
  7.  
  8. col_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age', 'label']
  9. # load dataset
  10. pima = pd.read_csv("diabetes.csv", header=None, names=col_names)
  11.  
  12. #split dataset in features and target variable
  13. feature_cols = ['pregnant', 'insulin', 'bmi', 'age','glucose','bp','pedigree']
  14. X = pima[feature_cols] # Features
  15. y = pima.label # Target variable
  16.  
  17. # Split dataset into training set and test set
  18. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # 70% training and 30% test
  19.  
  20.  
  21. # Create Decision Tree classifer object
  22. clf = DecisionTreeClassifier()
  23.  
  24. # Train Decision Tree Classifer
  25. clf = clf.fit(X_train,y_train)
  26.  
  27. #Predict the response for test dataset
  28. y_pred = clf.predict(X_train)
  29.  
  30. # Predict and print the label for the new data point X_new
  31. new_prediction = clf.predict(X_test)
  32. print("Prediction: {}".format(new_prediction))
  33.  
  34.  
  35. # Model Accuracy, how often is the classifier correct?
  36. print("Accuracy:",metrics.accuracy_score(y_train, y_pred))
  37.  
  38.  
  39.  
  40.  
  41. And
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. # Load libraries
  49. import pandas as pd
  50. from sklearn.model_selection import train_test_split # Import train_test_split function
  51. from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
  52. #Import Gaussian Naive Bayes model
  53. from sklearn.naive_bayes import GaussianNB
  54.  
  55. col_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age', 'label']
  56. # load dataset
  57. pima = pd.read_csv("diabetes.csv", header=None, names=col_names)
  58.  
  59. #split dataset in features and target variable
  60. feature_cols = ['pregnant', 'insulin', 'bmi', 'age','glucose','bp','pedigree']
  61. X = pima[feature_cols] # Features
  62. y = pima.label # Target variable
  63.  
  64. # Split dataset into training set and test set
  65. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # 70% training and 30% test
  66.  
  67. #Create a Gaussian Classifier
  68. model = GaussianNB()
  69. # Train DGaussian Classifier
  70. model = model.fit(X_train,y_train)
  71. #Predict the response for test dataset
  72. y_pred = model.predict(X_train)
  73.  
  74. # Predict and print the label for the new data point X_new
  75. new_prediction = model.predict(X_test)
  76. print("Prediction: {}".format(new_prediction))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement