daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2. import numpy as np
  3. from sklearn import preprocessing
  4. from sklearn.feature_extraction import DictVectorizer
  5. from sklearn.ensemble import RandomForestClassifier
  6. from sklearn.datasets import make_classification
  7.  
  8. def ranfor_prediction(train, labels, test):
  9.     RandomForest = RandomForestClassifier(n_estimators=10, max_depth = 7)
  10.     RandomForest.fit(train, labels)
  11.     return RandomForest.predict(test)
  12.  
  13.  
  14. train  = pd.read_csv('train.csv', index_col=0)
  15. test  = pd.read_csv('test.csv', index_col=0)
  16.  
  17. labels = train.Hazard
  18.  
  19. train.drop('T2_V10', axis=1, inplace=True)
  20. train.drop('T2_V7', axis=1, inplace=True)
  21. train.drop('T1_V13', axis=1, inplace=True)
  22. train.drop('T1_V10', axis=1, inplace=True)
  23.  
  24. test.drop('T2_V10', axis=1, inplace=True)
  25. test.drop('T2_V7', axis=1, inplace=True)
  26. test.drop('T1_V13', axis=1, inplace=True)
  27. test.drop('T1_V10', axis=1, inplace=True)
  28.  
  29. columns = train.columns
  30. test_index = test.index
  31.  
  32. train_temp = np.array(train)
  33. test_temp = np.array(test)
  34.  
  35. for i in range(train_temp.shape[1]):
  36.     le = preprocessing.LabelEncoder()
  37.     le.fit(list(train_temp[:,i]) + list(test_temp[:,i]))
  38.     le.transform(train_temp[:,i])#при ошибке указывает на эту строку
  39.     train_temp[:,i] = le.transform(train_temp[:,i])
  40.     test_temp[:,i] = le.transform(test_temp[:,i])
  41.  
  42. train_temp = train_temp.astype(float)
  43. test_temp = test_temp.astype(float)
  44.  
  45. prediction = ranfor_prediction(train_temp, labels, test_temp)
  46.  
  47. prediction = pd.DataFrame({"Id": test_index, "Hazard": prediction})
  48. prediction = prediction.set_index('Id')
  49. prediction.to_csv('result.csv')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top