






import logging

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format="""%(message)s""")






def   UpperCamelCase  (       snake_case__     :   np.ndarray  ):




     '''simple docstring'''






     return input_array.reshape((input_array.size, 1)  )






def   UpperCamelCase  (       snake_case__     :   np.ndarray	,snake_case__     :   np.ndarray	,snake_case__     :   int  ):




     '''simple docstring'''






     __snake_case			:Optional[Any]          =						np.nan
     for i in range(snake_case__  ):
          __snake_case			:int          =						features[:, labels == i]
          __snake_case			:Union[str, Any]          =						data.mean(1  )
          # Centralize the data of class i
          __snake_case			:Tuple          =						data - column_reshape(snake_case__  )
          if i > 0:
               # If covariance_sum is not None
               covariance_sum += np.dot(snake_case__	,centered_data.T  )
          else:
               # If covariance_sum is np.nan (i.e. first loop)
               __snake_case			:List[Any]          =						np.dot(snake_case__	,centered_data.T  )

     return covariance_sum / features.shape[1]






def   UpperCamelCase  (       snake_case__     :   np.ndarray	,snake_case__     :   np.ndarray	,snake_case__     :   int  ):




     '''simple docstring'''






     __snake_case			:int          =						features.mean(1  )
     __snake_case			:Optional[int]          =						np.nan
     for i in range(snake_case__  ):
          __snake_case			:Tuple          =						features[:, labels == i]
          __snake_case			:str          =						data.shape[1]
          __snake_case			:Tuple          =						data.mean(1  )
          if i > 0:
               # If covariance_sum is not None
               covariance_sum += device_data * np.dot(
                   column_reshape(snake_case__  ) - column_reshape(snake_case__  )	,(column_reshape(snake_case__  ) - column_reshape(snake_case__  )).T	,)
          else:
               # If covariance_sum is np.nan (i.e. first loop)
               __snake_case			:Dict          =						device_data * np.dot(
                   column_reshape(snake_case__  ) - column_reshape(snake_case__  )	,(column_reshape(snake_case__  ) - column_reshape(snake_case__  )).T	,)

     return covariance_sum / features.shape[1]






def   UpperCamelCase  (       snake_case__     :   np.ndarray	,snake_case__     :   int  ):




     '''simple docstring'''






     if features.any():
          __snake_case			:Optional[Any]          =						features.mean(1  )
          # Center the dataset
          __snake_case			:Optional[Any]          =						features - np.reshape(snake_case__	,(data_mean.size, 1)  )
          __snake_case			:Optional[int]          =						np.dot(snake_case__	,centered_data.T  ) / features.shape[1]
          __snake_case			:List[Any]          =						np.linalg.eigh(snake_case__  )
          # Take all the columns in the reverse order (-1), and then takes only the first
          __snake_case			:Optional[int]          =						eigenvectors[:, ::-1][:, 0:dimensions]
          # Project the database on the new space
          __snake_case			:Union[str, Any]          =						np.dot(filtered_eigenvectors.T	,snake_case__  )
          logging.info("""Principal Component Analysis computed"""  )

          return projected_data
     else:
          logging.basicConfig(level=logging.ERROR	,format="""%(message)s"""	,force=snake_case__  )
          logging.error("""Dataset empty"""  )
          raise AssertionError






def   UpperCamelCase  (       snake_case__     :   np.ndarray	,snake_case__     :   np.ndarray	,snake_case__     :   int	,snake_case__     :   int  ):




     '''simple docstring'''






     assert classes > dimensions

     # Check if features have been already loaded
     if features.any:
          __snake_case			:Optional[Any]          =						eigh(
              covariance_between_classes(snake_case__	,snake_case__	,snake_case__  )	,covariance_within_classes(snake_case__	,snake_case__	,snake_case__  )	,)
          __snake_case			:Optional[int]          =						eigenvectors[:, ::-1][:, :dimensions]
          __snake_case			:Union[str, Any]          =						np.linalg.svd(snake_case__  )
          __snake_case			:Any          =						svd_matrix[:, 0:dimensions]
          __snake_case			:List[str]          =						np.dot(filtered_svd_matrix.T	,snake_case__  )
          logging.info("""Linear Discriminant Analysis computed"""  )

          return projected_data
     else:
          logging.basicConfig(level=logging.ERROR	,format="""%(message)s"""	,force=snake_case__  )
          logging.error("""Dataset empty"""  )
          raise AssertionError






def   UpperCamelCase  (       ):




     '''simple docstring'''






     __snake_case			:str          =						np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]  )
     __snake_case			:str          =						np.array([0, 0, 0, 1, 1]  )
     __snake_case			:List[str]          =						2
     __snake_case			:Tuple          =						2

     # Assert that the function raises an AssertionError if dimensions > classes
     with pytest.raises(snake_case__  ) as error_info:
          __snake_case			:Dict          =						linear_discriminant_analysis(
              snake_case__	,snake_case__	,snake_case__	,snake_case__  )
          if isinstance(snake_case__	,np.ndarray  ):
               raise AssertionError(
                   """Did not raise AssertionError for dimensions > classes"""  )
          assert error_info.type is AssertionError






def   UpperCamelCase  (       ):




     '''simple docstring'''






     __snake_case			:List[Any]          =						np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]  )
     __snake_case			:List[Any]          =						2
     __snake_case			:Dict          =						np.array([[6.9_2_8_2_0_3_2_3, 8.6_6_0_2_5_4_0_4, 10.3923_0485], [3.0, 3.0, 3.0]]  )

     with pytest.raises(snake_case__  ) as error_info:
          __snake_case			:Tuple          =						principal_component_analysis(snake_case__	,snake_case__  )
          if not np.allclose(snake_case__	,snake_case__  ):
               raise AssertionError
          assert error_info.type is AssertionError


if __name__ == "__main__":
     import doctest

     doctest.testmod()










import math

lowerCamelCase__							=     10
lowerCamelCase__							=     7
lowerCamelCase__							=     BALLS_PER_COLOUR * NUM_COLOURS






def   UpperCamelCase  (       snake_case__     :   int = 20  ):




     '''simple docstring'''






     __snake_case			:Dict          =						math.comb(snake_case__	,snake_case__  )
     __snake_case			:int          =						math.comb(NUM_BALLS - BALLS_PER_COLOUR	,snake_case__  )

     __snake_case			:List[str]          =						NUM_COLOURS * (1 - missing_colour / total)

     return f'''{result:.9f}'''


if __name__ == "__main__":
     print(solution(20))


