<jupyter_start><jupyter_text>Read data<jupyter_code>iris=load_iris()<jupyter_output><empty_output><jupyter_text>iris<jupyter_code>diris=pd.DataFrame(data=iris['data'],columns=['sepal length (cm)',
  'sepal width (cm)',
  'petal length (cm)',
  'petal width (cm)'])
diris.head()
dtarget=pd.DataFrame(data=iris['target'],columns=['species'])
dtarget=dtarget.apply(lambda x: iris['target_names'][x])

diris['target']=dtarget
diris['species']=dtarget
diris=diris.drop(columns='target',axis=1)<jupyter_output><empty_output><jupyter_text>Explore data<jupyter_code>diris.head()
diris.info()
diris.shape
diris.describe()
diris.isnull().sum()
diris.corr()
sns.pairplot(diris)
plt.show()
sns.heatmap(diris.corr(),annot=True)
plt.show()
sns.lmplot(x='sepal length (cm)',y='sepal width (cm)', fit_reg=False,data=diris, hue='species');
plt.show()
from mlxtend.plotting import category_scatter

fix = category_scatter(x='sepal length (cm)',y='sepal width (cm)', label_col=4, 
                       data=diris, legend_loc='upper left')
# Plot
diris=diris.rename(columns={'sepal length (cm)':'sepal_length','sepal width (cm)':'sepal_width'})
diris.head()

groups=diris.groupby(['species'])
fig, ax = plt.subplots()
ax.margins(0.05) # Optional, just adds 5% padding to the autoscaling
for species, group in groups:
    ax.plot(group.sepal_length, group.sepal_width, marker='o', linestyle='', ms=12, label=species);
ax.legend()
plt.show()
print(diris.groupby(['species']).corr())
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>diris.head()
y=diris.species
x_train,x_test,y_train,y_test=train_test_split(diris[['sepal_length',
  'sepal_width',
  'petal length (cm)',
  'petal width (cm)']],y,random_state=123,test_size=.3)

y_train.head()
model=LogisticRegression().fit(x_train,y_train)<jupyter_output><empty_output><jupyter_text>Compute the error<jupyter_code>y_pred=model.predict(x_test)
accuracy=accuracy_score(y_test,y_pred)
print('accuracy',accuracy)
cm=confusion_matrix(y_test,y_pred)
print(cm)
accuracy2=model.score(x_test,y_test)
print(accuracy2)
print(classification_report(y_pred,y_test))<jupyter_output>precision    recall  f1-score   support

     setosa       1.00      1.00      1.00        18
 versicolor       1.00      0.91      0.95        11
  virginica       0.94      1.00      0.97        16

avg / total       0.98      0.98      0.98        45<jupyter_text>f1 id 2*precision*recall/(precision+recall)  now we  use grid search to find good parameters<jupyter_code>from sklearn.model_selection import GridSearchCV
params={'C':[.01,.05,.1,.5,1,5,10],'penalty':['l1','l2']}
estimator=LogisticRegression()
optimizer=GridSearchCV(estimator, params, cv=10)#cv is for cross validation
optimizer.fit(x_test,y_test)
y_pred2=optimizer.best_estimator_.predict(x_test)
accuracy3=accuracy_score(y_pred2,y_test)
print(accuracy3)
print('best result %f using %s' % (optimizer.best_score_, optimizer.best_params_))
means=optimizer.cv_results_['mean_test_score']
stdvs=optimizer.cv_results_['std_test_score']
params=optimizer.cv_results_['params']
for mean, stdv, param in zip(means, stdv, params): 
    print('%f (%f) with %s' % (mean, stdv, param))<jupyter_output>0.400000 (0.038490) with {'C': 0.01, 'penalty': 'l1'}
0.777778 (0.045812) with {'C': 0.01, 'penalty': 'l2'}
0.377778 (0.045812) with {'C': 0.05, 'penalty': 'l1'}
0.777778 (0.045812) with {'C': 0.05, 'penalty': 'l2'}
0.777778 (0.045812) with {'C': 0.1, 'penalty': 'l1'}
0.777778 (0.045812) with {'C': 0.1, 'penalty': 'l2'}
0.800000 (0.083887) with {'C': 0.5, 'penalty': 'l1'}
0.800000 (0.083887) with {'C': 0.5, 'penalty': 'l2'}
0.866667 (0.108866) with {'C': 1, 'penalty': 'l1'}
0.866667 (0.108866) with {'C': 1, 'penalty': 'l2'}
0.911111 (0.099381) with {'C': 5, 'penalty': 'l1'}
0.911111 (0.099381) with {'C': 5, 'penalty': 'l2'}
0.933333 (0.094281) with {'C': 10, 'penalty': 'l1'}
0.911111 (0.099381) with {'C': 10, 'penalty': 'l2'}
{'fraction_non_alphanumeric': 0.18179525970751387, 'fraction_numerical': 0.07992939989914272, 'mean_word_length': 6.122082585278276, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '663086', 'license': 'MIT', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1624, 'n_words': 286, 'path': 'Classification_1_iris_Logistic.ipynb', 'repo_name': 'MC-Franco-de-Leon/ML_and_DL', 'size': '3966'}