예제 #1
0
	def test_LinearDiscriminantAnalysis(self):
		helper = Helper()
		# Test with dataset type: sklearn DataBunch
		# Load up data
		iris = load_iris()
		X_train, y_train, X_test, y_test = iris.data[:120], iris.target[:120], iris.data[120:], iris.target[120:]

		# Bring in the default LinearDiscriminantAnalysis model
		model_name = 'lda'
		actual_model = helper.getBuiltModel(model_name)

		# Explicity create the model we expect to get from getBuiltModel call
		expected_model = LinearDiscriminantAnalysis()

		# Make sure the models are the same type before continuing
		self.assertEqual( type(actual_model), type(expected_model) )

		# Train this default model on the iris dataset
		actual_model.fit(X_train, y_train)
		
		# Get default model accuracy on testing set
		actual_accuracy = actual_model.score(X_test, y_test)

		# Complete the same process, however we make the model explicitly
		expected_model.fit(X_train, y_train)
		expected_accuracy = expected_model.score(X_test, y_test)

		# Make sure that the accuracy reported from both models is the same
		self.assertEqual( actual_accuracy, expected_accuracy )

		# Test with dataset type: pandas DataFrame
		# Load up data
		iris_df = load_iris(as_frame=True).frame
		X = iris_df.loc[:, iris_df.columns != 'target']
		y = iris_df['target']
		X_train, y_train = X.iloc[:120], y.iloc[:120]
		X_test, y_test =   X.iloc[120:], y.iloc[120:]

		# Bring in the default LinearDiscriminantAnalysis model
		model_name = 'lda'
		actual_model = helper.getBuiltModel(model_name)

		# Explicity create the model we expect to get from getBuiltModel call
		expected_model = LinearDiscriminantAnalysis()

		# Make sure the models are the same type before continuing
		self.assertEqual( type(actual_model), type(expected_model) )

		# Train this default model on the iris dataset
		actual_model.fit(X_train, y_train)
		
		# Get default model accuracy on testing set
		actual_accuracy = actual_model.score(X_test, y_test)

		# Complete the same process, however we make the model explicitly
		expected_model.fit(X_train, y_train)
		expected_accuracy = expected_model.score(X_test, y_test)

		# Make sure that the accuracy reported from both models is the same
		self.assertEqual( actual_accuracy, expected_accuracy )
예제 #2
0
	def test_getBuiltModel(self):
		helper = Helper()

		model_name = 'rf:classifier'
		actual = helper.getBuiltModel(model_name)
		expected = RandomForestClassifier(random_state=0)
		self.assertEqual( type(actual), type(expected) )

		try:
			model_name = 'jonathan'
			model = helper.getBuiltModel(model_name)

			fail(self)
		except ValueError as ve:
			self.assertEqual( str(ve), 'No default model found for jonathan')
예제 #3
0
	def test_AdaBoostRegressor(self):
		helper = Helper()

		# Load up data
		X, y = load_boston(return_X_y=True)
		X_train, y_train, X_test, y_test = X[:405], y[:405], X[405:], y[405:]

		# Bring in the default RandomForestClassifier model
		model_name = 'ab:regressor'
		actual_model = helper.getBuiltModel(model_name)

		# Explicity create the model we expect to get from getBuiltModel call
		expected_model = AdaBoostRegressor(random_state=0)

		# Make sure the models are the same type before continuing
		self.assertEqual( type(actual_model), type(expected_model) )

		# Train this default model on the iris dataset
		actual_model.fit(X_train, y_train)
		
		# Get default model accuracy on testing set
		actual_accuracy = actual_model.score(X_test, y_test)

		# Complete the same process, however we make the model explicitly
		expected_model.fit(X_train, y_train)
		expected_accuracy = expected_model.score(X_test, y_test)

		# Make sure that the accuracy reported from both models is the same
		self.assertEqual( actual_accuracy, expected_accuracy )
예제 #4
0
	def test_LogisticRegression(self):
		helper = Helper()

		# Load up data
		iris = load_iris()
		X_train, y_train, X_test, y_test = iris.data[:120], iris.target[:120], iris.data[120:], iris.target[120:]

		# Bring in the default RandomForestClassifier model
		model_name = 'lr'
		actual_model = helper.getBuiltModel(model_name)

		# Explicity create the model we expect to get from getBuiltModel call
		expected_model = LogisticRegression(random_state=0)

		# Make sure the models are the same type before continuing
		self.assertEqual( type(actual_model), type(expected_model) )

		# Train this default model on the iris dataset
		actual_model.fit(X_train, y_train)
		
		# Get default model accuracy on testing set
		actual_accuracy = actual_model.score(X_test, y_test)

		# Complete the same process, however we make the model explicitly
		expected_model.fit(X_train, y_train)
		expected_accuracy = expected_model.score(X_test, y_test)

		# Make sure that the accuracy reported from both models is the same
		self.assertEqual( actual_accuracy, expected_accuracy )