Skip to content

Instantly share code, notes, and snippets.

@ajoydas
Last active November 2, 2017 08:41
Show Gist options
  • Save ajoydas/65502c5c20b88a4fb604bc97f22005b8 to your computer and use it in GitHub Desktop.
Save ajoydas/65502c5c20b88a4fb604bc97f22005b8 to your computer and use it in GitHub Desktop.
Added Feature Scaling to y_test and y_train
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("histBrightness.csv", header=None)
X = dataset.iloc[:, 1:].values
y = dataset.iloc[:, 0].values
labelDict = createDict('inputBrightness.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 = []
cms = []
for i in range(0,1):
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