<jupyter_start><jupyter_text>Python Project Template  1. Prepare Problem<jupyter_code># Load libraries
from pandas import read_csv
from pandas.plotting import scatter_matrix
from matplotlib import pyplot
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
# Load dataset
filename = '../datasets/iris-dataset/iris_data.csv'
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = read_csv(filename, names=names)<jupyter_output><empty_output><jupyter_text>2. Summarize Data  Descriptive statistics<jupyter_code># shape
print(dataset.shape)
# head
dataset.head(20)
# descriptions
dataset.describe()
# class distribution
print(dataset.groupby('class').size())<jupyter_output>class
Iris-setosa        50
Iris-versicolor    50
Iris-virginica     50
dtype: int64<jupyter_text>Data Visualization<jupyter_code>#Univariate Plots
# box and whisker plots
dataset.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)
pyplot.show()
# histograms
dataset.hist()
pyplot.show()
#Multivariate Plots
# scatter plot matrix
scatter_matrix(dataset)
pyplot.show()<jupyter_output><empty_output><jupyter_text>4. Evaluate Algorithms<jupyter_code># Split-out validation dataset
array = dataset.values
X = array[:,0:4]
Y = array[:,4]
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = train_test_split(X, Y,
test_size=validation_size, random_state=seed)<jupyter_output><empty_output><jupyter_text>Evaluate a suite of algorithms on the dataset.<jupyter_code># Spot-Check Algorithms
models = []
models.append(('LR', LogisticRegression(solver='newton-cg',multi_class='auto')))
#models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC(gamma='auto')))
# evaluate each model in turn
results = []
names = []
for name, model in models:
    kfold = KFold(n_splits=10, random_state=seed)
    cv_results = cross_val_score(model, X_train, Y_train, cv=kfold, scoring='accuracy')
    results.append(cv_results)
    names.append(name)
    msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)<jupyter_output>LR: 0.983333 (0.033333)
LDA: 0.975000 (0.038188)
KNN: 0.983333 (0.033333)
CART: 0.975000 (0.038188)
NB: 0.975000 (0.053359)
SVM: 0.991667 (0.025000)<jupyter_text>Select The Best Model<jupyter_code># Compare Algorithms
fig = pyplot.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
pyplot.boxplot(results)
ax.set_xticklabels(names)
pyplot.show()<jupyter_output><empty_output><jupyter_text>6. Finalize Model  Make Predictions on the Validation Dataset.<jupyter_code># Make predictions on validation dataset
knn = KNeighborsClassifier()
knn.fit(X_train, Y_train)
predictions = knn.predict(X_validation)
print(accuracy_score(Y_validation, predictions))
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))
# Make predictions on validation dataset
#lr = LogisticRegression()
lr = LogisticRegression(solver='newton-cg',multi_class='auto')
lr.fit(X_train, Y_train)
predictions = lr.predict(X_validation)
print(accuracy_score(Y_validation, predictions))
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))<jupyter_output>0.8666666666666667
[[ 7  0  0]
 [ 0 10  2]
 [ 0  2  9]]
                 precision    recall  f1-score   support

    Iris-setosa       1.00      1.00      1.00         7
Iris-versicolor       0.83      0.83      0.83        12
 Iris-virginica       0.82      0.82      0.82        11

       accuracy                           0.87        30
      macro avg       0.88      0.88      0.88        30
   weighted avg       0.87      0.87      0.87        30
{'fraction_non_alphanumeric': 0.11141427921782629, 'fraction_numerical': 0.04524783992723965, 'mean_word_length': 5.248579545454546, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '170090', 'license': 'MIT', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1470, 'n_words': 320, 'path': 'Linear Regression/Project Template iris_data.ipynb', 'repo_name': 'byionline/Machine-Learning', 'size': '4398'}