<jupyter_start><jupyter_text>Validacion de un Modelo de manera incorrecta<jupyter_code>from sklearn.datasets import load_iris
import numpy as np
import pandas as pd
iris=load_iris()
X=iris.data
Y=iris.target<jupyter_output><empty_output><jupyter_text>**Ahora tenemos que elegir el modelo y los hiperparametros**<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
model= KNeighborsClassifier(n_neighbors=1)
model.fit(X,Y)
y_model=model.predict(X)
from sklearn.metrics import accuracy_score

accuracy_score(Y,y_model)<jupyter_output><empty_output><jupyter_text>Los resultado obtenidos fueron con una presicion del 100% pero no siempre es asi  Validacion de Modelos de manera Correcta Conjunto de retensiones<jupyter_code>from sklearn.model_selection import train_test_split
#ahora partimos los datos de entrenamiento y testeo
Xtrain,Xtest,Ytrain,Ytest =train_test_split(X,Y,random_state=0,test_size=0.2)
model.fit(Xtrain,Ytrain)
#ahora evaluamos los modelos con los datos de prueba
y_model=model.predict(Xtest)
accuracy_score(Ytest,y_model)<jupyter_output><empty_output><jupyter_text>Metodo de validacion cruzada,es un metodo mucho mas eficiente ya que nos permite explorar el conjunto de datos que deseamos testear segun sea la puntuacion obtenida en la presicion de estos<jupyter_code>from sklearn.model_selection import cross_val_score

score=cross_val_score(model,X,Y,cv=5)
score<jupyter_output><empty_output><jupyter_text>Pero si en casos extremos deseamos explorar un determinado conjunto de datos igual aal total de estos usaremos, tendriamos que usar la **Validacion Cruzada de salida uno**<jupyter_code>from sklearn.model_selection import LeaveOneOut

loo=LeaveOneOut()
score=cross_val_score(model,X,Y,cv=loo)
score.mean()<jupyter_output><empty_output><jupyter_text>La compensacion SESGO-VARIANZA [Image](https://jakevdp.github.io/PythonDataScienceHandbook/figures/05.03-validation-curve.png)         *  Curvas de validacion<jupyter_code>from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt
def PolynomialRegression(degree=2,**kwargs):
    return make_pipeline(PolynomialFeatures(degree),LinearRegression(**kwargs))
def makedata(N=5, err=0.1):
    rng=np.random.RandomState(0)
    X=rng.rand(N,1)**2
    Y=1+X.ravel()**3
    if err>=0:
        Y+=err*rng.randn(N)
    return X,Y
import seaborn as sns; sns.set()
X,Y=makedata(N=200)

Xtest=np.linspace(-1,1,200)[:,np.newaxis]


plt.scatter(X.ravel(),Y,color='black',s=0.2)

axis=plt.axis()
for degree in [1,3,5,7]:
    Ytest=PolynomialRegression(degree).fit(X,Y).predict(Xtest)
    plt.plot(Xtest.ravel(),Ytest,label='Degree {0}'.format(degree))
plt.xlabel('X')
plt.ylabel('Y')
plt.ylim(0.5,2)
plt.xlim(-0.5,1)
plt.legend(loc='best')
    
from sklearn.model_selection import validation_curve
train_scores,test_scores=validation_curve(LinearRegression(),X,Y,param_name='fit_intercept',param_range=[True,False],cv=7)
train_scores.mean(axis=1)
test_scores
test_scores.mean(axis=1)
ylb=np.arange(0,2)
plt.plot(train_scores.mean(axis=1),ylb,color='yellow',label='Train score')
plt.plot(test_scores.mean(axis=1),ylb,color='brown',label='Test Score')
plt.legend(loc='best')
degree=np.arange(0,20)
X,Y=makedata(N=500)
train_scores,test_scores=validation_curve(PolynomialRegression(),X,Y,
                                          param_name='polynomialfeatures__degree',
                                          param_range=degree,
                                          cv=7
                                         )




plt.plot(degree,train_scores.mean(axis=1),color='green',label='Train Score')
plt.plot(degree,test_scores.mean(axis=1),color='red',label='Test Score')

plt.legend(loc='best')
plt.xlim(0,5)
plt.scatter(X.ravel(),Y,color='red',alpha=0.3)

Ytest=PolynomialRegression(degree=2).fit(X,Y).predict(Xtest)
axis=plt.axis()
plt.plot(Xtest.ravel(),Ytest,color='blue',label='Predict Ytest')

plt.axis(axis)
plt.legend(loc='best')
from sklearn.model_selection import learning_curve
train_sizes,train_scores,test_scores=learning_curve(PolynomialRegression(degree=4),X,Y,
                                                    cv=7,
                                                    train_sizes=np.linspace(0.3,1,25)
                                                   )
train_sizes
from sklearn.model_selection import GridSearchCV

X,Y=makedata(N=400)
Xtest=10*np.random.rand(200)
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
pipe=Pipeline(
    [('scaler',StandardScaler()),('linear',LinearRegression())]
)
intersection=[True,False]
normalize=[True,False]
grid=GridSearchCV(
    estimator=pipe,
    param_grid=dict(
        linear__fit_intercept=intersection
    ),
    cv=7
)
grid.fit(X,Y)
grid.best_params_
grid.best_estimator_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1180314478251991, 'fraction_numerical': 0.016336532570961813, 'mean_word_length': 5.130162703379224, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '508059', 'license': 'MIT', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1710, 'n_words': 279, 'path': 'Machine Learning/Hiperparametros y validacion de Modelos.ipynb', 'repo_name': 'Jovamih/PythonProyectos', 'size': '4897'}