Created
November 3, 2017 14:37
-
-
Save ajoydas/39a8da10e45f2f0fe8c2b3f1c2b8f752 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import csv | |
import numpy as np | |
def createDict(labelFile): | |
csvfile = open(labelFile, "r") | |
reader = csv.reader(csvfile) | |
labelDict = {} | |
for row in reader: | |
labelDict[row[0]] = row[1] | |
return labelDict | |
# Importing the dataset | |
dataset = pd.read_csv("histContrast.csv", header=None) | |
X = dataset.iloc[:, 1:].values | |
y = dataset.iloc[:, 0].values | |
labelDict = createDict('inputContrast.csv') | |
y = np.vectorize(labelDict.get)(y) | |
y = np.ndarray.tolist(y) | |
y = list(map(float, y)) | |
y = np.asarray(y) | |
# Encoding the y | |
#from sklearn.preprocessing import LabelEncoder | |
#from keras.utils import np_utils | |
#encoder = LabelEncoder() | |
#encoded_Y = encoder.fit_transform(y) | |
#dummy_y = np_utils.to_categorical(encoded_Y) | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.metrics import confusion_matrix,mean_absolute_error | |
from sklearn.ensemble import RandomForestRegressor | |
from sklearn.model_selection import cross_val_score | |
scores = [] | |
for i in range(0,100): | |
print(i) | |
# Splitting the dataset into the Training set and Test set | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, | |
stratify = y) | |
# Feature Scaling | |
sc = StandardScaler() | |
X_train = sc.fit_transform(X_train) | |
X_test = sc.transform(X_test) | |
# Feature Scaling | |
scy = StandardScaler() | |
y_train = scy.fit_transform(y_train) | |
y_test = scy.fit_transform(y_test) | |
# Fitting Random Forest Regression to the dataset | |
regressor = RandomForestRegressor(n_estimators = 100, random_state = 0) | |
regressor.fit(X_train, y_train) | |
# Predicting the Test set results | |
y_pred = regressor.predict(X_test) | |
# y_pred = scy.inverse_transform(y_pred) | |
# Check the score | |
scores.append(mean_absolute_error(y_test, y_pred)) | |
scores = np.asarray(scores) | |
scores.mean() | |
scores.std() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment