Team Ai
Apppublic

AlainDeLong/End-To-End-Machine-Learning-Project

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
model_trainer.cpython-38.pyc30 linesDownload Raw Back to __pycache__
1U

2�@�f��@s�ddlZddlZddlmZddlmZddlmZddlm	Z	ddl3mZddlm
Z
mZmZddlmZdd	lmZdd4lmZddlmZddlmZmZeGd
d�d��ZGdd�d�ZdS)�N)�	dataclass)�r2_score)�LinearRegression)�KNeighborsRegressor)�DecisionTreeRegressor)�RandomForestRegressor�AdaBoostRegressor�GradientBoostingRegressor)�XGBRegressor)�CatBoostRegressor)�logging)�CustomException)�save_object�evaluate_modelsc@seZdZej�dd�ZdS)�ModelTrainerConfig�	artifactsz	model.pklN)�__name__�5__module__�__qualname__�os�path�join�trained_model_file_path�rr�L/home/long/Projects/machine-learning-project/src/components/model_trainer.pyrsrc@s"eZdZdd�dd�Zdd�ZdS)�ModelTrainerN)�returncCst�|_dS)N)r�model_trainer_config)�selfrrr�__init__szModelTrainer.__init__c
Cs��z�t�d�|dd�dd�f|dd�df|dd�dd�f|dd�dff\}}}}t�t�t�t�t�t�t�t	dd�d�}iidddd	d6giddd
ddddgiddddgdd
ddddgd�ddddgddddddgdd
ddddgd�ddddgdd
ddddgd�ddd gdddgd!d"d#gd$�d�}t7||||||d%�}	tt|	�
���}8t|	���t|	�
���|9�}||}|10dk�r�td&t��t�d'�t|jj|d(�t|�|�|�}
t||
�}|WStk11�r�}zt|t��W5d}~XYnXdS))Nz%Split training and testing input data�����F)�verbose)zLinear RegressionzK-Neighbors RegressorzDecision Tree RegressorzRandom Forest RegressorzAdaBoost RegressorzGradient Boosting Regressorr12zCatBoosting Regressor�	criterion�
squared_errorZfriedman_mse�absolute_error�poisson�n_estimators��� �@��g�������?g{�G�z�?g�?g����MbP?)�
learning_rater&g�������?g333333�?gffffff�?g�?g�������?g333333�?g�������?)r-�	subsampler&��13��2�d)�depthr-�14iterations)�X_train�y_train�X_test�y_test�models�params_gridzNo best model foundz5Best found model on both training and testing dataset)�	file_path�obj)r�inforrrrrr	r15rr�max�sorted�values�list�keys�indexr
�sysrrr�print�predictr�	Exception)rZtrain_arrayZ16test_arrayr6r7r8r9r:r;Zmodel_reportZbest_model_scoreZbest_model_nameZ17best_model�	predictedZ	r2_square�errr�initiate_model_trainers�18����
�19�20�	21���*�2223�242526�2728z#ModelTrainer.initiate_model_trainer)rrrrrKrrrrrsr)rrE�dataclassesrZsklearn.metricsrZsklearn.linear_modelrZsklearn.neighborsrZsklearn.treerZsklearn.ensemblerrr	Zxgboostr29Zcatboostr�30src.loggerr�
src.exceptionr
�	src.utilsrrrrrrrr�<module>s