0% found this document useful (0 votes)
4 views6 pages

HW0 Script

Uploaded by

duttaprayati
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

HW0 Script

Uploaded by

duttaprayati
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

(base) C:\Users\Prayati Dutta>python

Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] ::
Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # Load libraries
>>> from pandas import read_csv
>>> from [Link] import scatter_matrix
>>> from matplotlib import pyplot
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.model_selection import cross_val_score
>>> from sklearn.model_selection import StratifiedKFold
>>> from [Link] import classification_report
>>> from [Link] import confusion_matrix
>>> from [Link] import accuracy_score
>>> from sklearn.linear_model import LogisticRegression
>>> from [Link] import DecisionTreeClassifier
>>> from [Link] import KNeighborsClassifier
>>> from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
>>> from sklearn.naive_bayes import GaussianNB
>>> from [Link] import SVC
>>> ...
Ellipsis
>>> ...
Ellipsis
>>> # Load dataset
>>> url = "[Link]
>>> names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
>>> dataset = read_csv(url, names=names)
>>> ...
Ellipsis
>>> # shape
>>> print([Link])
(150, 5)
>>> ...
Ellipsis
>>> # head
>>> print([Link](20))
sepal-length sepal-width petal-length petal-width class
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
5 5.4 3.9 1.7 0.4 Iris-setosa
6 4.6 3.4 1.4 0.3 Iris-setosa
7 5.0 3.4 1.5 0.2 Iris-setosa
8 4.4 2.9 1.4 0.2 Iris-setosa
9 4.9 3.1 1.5 0.1 Iris-setosa
10 5.4 3.7 1.5 0.2 Iris-setosa
11 4.8 3.4 1.6 0.2 Iris-setosa
12 4.8 3.0 1.4 0.1 Iris-setosa
13 4.3 3.0 1.1 0.1 Iris-setosa
14 5.8 4.0 1.2 0.2 Iris-setosa
15 5.7 4.4 1.5 0.4 Iris-setosa
16 5.4 3.9 1.3 0.4 Iris-setosa
17 5.1 3.5 1.4 0.3 Iris-setosa
18 5.7 3.8 1.7 0.3 Iris-setosa
19 5.1 3.8 1.5 0.3 Iris-setosa
>>> ...
Ellipsis
>>> # descriptions
>>> print([Link]())
sepal-length sepal-width petal-length petal-width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.054000 3.758667 1.198667
std 0.828066 0.433594 1.764420 0.763161
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000
>>> ...
Ellipsis
>>> # class distribution
>>> print([Link]('class').size())
class
Iris-setosa 50
Iris-versicolor 50
Iris-virginica 50
dtype: int64
>>> ...
Ellipsis
>>> # box and whisker plots
>>> [Link](kind='box', subplots=True, layout=(2,2), sharex=False,
sharey=False)
sepal-length AxesSubplot(0.125,0.53;0.352273x0.35)
sepal-width AxesSubplot(0.547727,0.53;0.352273x0.35)
petal-length AxesSubplot(0.125,0.11;0.352273x0.35)
petal-width AxesSubplot(0.547727,0.11;0.352273x0.35)
dtype: object
>>> [Link]()
>>> ...
Ellipsis
>>> # histograms
>>> [Link]()
array([[<AxesSubplot: title={'center': 'sepal-length'}>,
<AxesSubplot: title={'center': 'sepal-width'}>],
[<AxesSubplot: title={'center': 'petal-length'}>,
<AxesSubplot: title={'center': 'petal-width'}>]], dtype=object)
>>> [Link]()
>>> ...
Ellipsis
>>> # scatter plot matrix
>>> scatter_matrix(dataset)
array([[<AxesSubplot: xlabel='sepal-length', ylabel='sepal-length'>,
<AxesSubplot: xlabel='sepal-width', ylabel='sepal-length'>,
<AxesSubplot: xlabel='petal-length', ylabel='sepal-length'>,
<AxesSubplot: xlabel='petal-width', ylabel='sepal-length'>],
[<AxesSubplot: xlabel='sepal-length', ylabel='sepal-width'>,
<AxesSubplot: xlabel='sepal-width', ylabel='sepal-width'>,
<AxesSubplot: xlabel='petal-length', ylabel='sepal-width'>,
<AxesSubplot: xlabel='petal-width', ylabel='sepal-width'>],
[<AxesSubplot: xlabel='sepal-length', ylabel='petal-length'>,
<AxesSubplot: xlabel='sepal-width', ylabel='petal-length'>,
<AxesSubplot: xlabel='petal-length', ylabel='petal-length'>,
<AxesSubplot: xlabel='petal-width', ylabel='petal-length'>],
[<AxesSubplot: xlabel='sepal-length', ylabel='petal-width'>,
<AxesSubplot: xlabel='sepal-width', ylabel='petal-width'>,
<AxesSubplot: xlabel='petal-length', ylabel='petal-width'>,
<AxesSubplot: xlabel='petal-width', ylabel='petal-width'>]],
dtype=object)
>>> [Link]()
>>> ...
Ellipsis
>>> # Split-out validation dataset
>>> array = [Link]
>>> X = array[:,0:4]
>>> y = array[:,4]
>>> X_train, X_validation, Y_train, Y_validation = train_test_split(X, y,
test_size=0.20, random_state=1)
>>> ...
Ellipsis
>>> # Spot Check Algorithms
>>> models = []
>>> [Link](('LR', LogisticRegression(solver='liblinear',
multi_class='ovr')))
>>> [Link](('LDA', LinearDiscriminantAnalysis()))
>>> [Link](('KNN', KNeighborsClassifier()))
>>> [Link](('CART', DecisionTreeClassifier()))
>>> [Link](('NB', GaussianNB()))
>>> [Link](('SVM', SVC(gamma='auto')))
>>> # evaluate each model in turn
>>> results = []
>>> names = []
>>> for name, model in models:
... kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)
File "<stdin>", line 2
kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)
^
IndentationError: expected an indented block
>>> cv_results = cross_val_score(model, X_train, Y_train, cv=kfold,
scoring='accuracy')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'model' is not defined
>>> [Link](cv_results)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'cv_results' is not defined
>>> [Link](name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>> print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>>
>>> ...
Ellipsis
>>> # Spot Check Algorithms
>>> models = []
>>> [Link](('LR', LogisticRegression(solver='liblinear',
multi_class='ovr')))
>>> [Link](('LDA', LinearDiscriminantAnalysis()))
>>> [Link](('KNN', KNeighborsClassifier()))
>>> [Link](('CART', DecisionTreeClassifier()))
>>> [Link](('NB', GaussianNB()))
>>> [Link](('SVM', SVC(gamma='auto')))
>>> # evaluate each model in turn
>>> results = []
>>> names = []
>>> for name, model in models:
... kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)
File "<stdin>", line 2
kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)
^
IndentationError: expected an indented block
>>> cv_results = cross_val_score(model, X_train, Y_train, cv=kfold,
scoring='accuracy')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'model' is not defined
>>> [Link](cv_results)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'cv_results' is not defined
>>> [Link](name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>> print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>>
>>> # Spot Check Algorithms
>>> models = []
>>> [Link](('LR', LogisticRegression(solver='liblinear',
multi_class='ovr')))
>>> [Link](('LDA', LinearDiscriminantAnalysis()))
>>> [Link](('KNN', KNeighborsClassifier()))
>>> [Link](('CART', DecisionTreeClassifier()))
>>> [Link](('NB', GaussianNB()))
>>> [Link](('SVM', SVC(gamma='auto')))
>>> # evaluate each model in turn
>>> results = []
>>> names = []
>>> for name, model in models:
... kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)
... cv_results = cross_val_score(model, X_train, Y_train, cv=kfold,
scoring='accuracy')
File "<stdin>", line 3
cv_results = cross_val_score(model, X_train, Y_train, cv=kfold,
scoring='accuracy')

^
IndentationError: unindent does not match any outer indentation level
>>> [Link](cv_results)
File "<stdin>", line 1
[Link](cv_results)
IndentationError: unexpected indent
>>> [Link](name)
File "<stdin>", line 1
[Link](name)
IndentationError: unexpected indent
>>> print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))
File "<stdin>", line 1
print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))
IndentationError: unexpected indent
>>> for name, model in models:
... kfold = StratifiedKFold(n_splits=10, random_state=1, shuffle=True)
... cv_results = cross_val_score(model, X_train, Y_train, cv=kfold,
scoring='accuracy')
... [Link](cv_results)
... [Link](name)
... print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))
...
LR: 0.941667 (0.065085)
LDA: 0.975000 (0.038188)
KNN: 0.958333 (0.041667)
CART: 0.958333 (0.041667)
NB: 0.950000 (0.055277)
SVM: 0.983333 (0.033333)
>>> ...
Ellipsis
>>> # Compare Algorithms
>>> [Link](results, labels=names)
{'whiskers': [<[Link].Line2D object at 0x000001DB0132EBB0>,
<[Link].Line2D object at 0x000001DB0132EE50>, <[Link].Line2D
object at 0x000001DB0133FDC0>, <[Link].Line2D object at
0x000001DB0134B0A0>, <[Link].Line2D object at 0x000001DB0137A0A0>,
<[Link].Line2D object at 0x000001DB0137A340>, <[Link].Line2D
object at 0x000001DB01388340>, <[Link].Line2D object at
0x000001DB013885E0>, <[Link].Line2D object at 0x000001DB013955E0>,
<[Link].Line2D object at 0x000001DB01395880>, <[Link].Line2D
object at 0x000001DB013A1880>, <[Link].Line2D object at
0x000001DB013A1B20>], 'caps': [<[Link].Line2D object at
0x000001DB0133F130>, <[Link].Line2D object at 0x000001DB0133F3D0>,
<[Link].Line2D object at 0x000001DB0134B340>, <[Link].Line2D
object at 0x000001DB0134B5E0>, <[Link].Line2D object at
0x000001DB0137A5E0>, <[Link].Line2D object at 0x000001DB0137A880>,
<[Link].Line2D object at 0x000001DB01388880>, <[Link].Line2D
object at 0x000001DB01388B20>, <[Link].Line2D object at
0x000001DB01395B20>, <[Link].Line2D object at 0x000001DB01395DC0>,
<[Link].Line2D object at 0x000001DB013A1DC0>, <[Link].Line2D
object at 0x000001DB013B00A0>], 'boxes': [<[Link].Line2D object at
0x000001DB0132E910>, <[Link].Line2D object at 0x000001DB0133FB20>,
<[Link].Line2D object at 0x000001DB0134BDC0>, <[Link].Line2D
object at 0x000001DB013880A0>, <[Link].Line2D object at
0x000001DB01395340>, <[Link].Line2D object at 0x000001DB013A15E0>],
'medians': [<[Link].Line2D object at 0x000001DB0133F5E0>,
<[Link].Line2D object at 0x000001DB0134B880>, <[Link].Line2D
object at 0x000001DB0137AB20>, <[Link].Line2D object at
0x000001DB01388DC0>, <[Link].Line2D object at 0x000001DB013A10A0>,
<[Link].Line2D object at 0x000001DB013B0340>], 'fliers':
[<[Link].Line2D object at 0x000001DB0133F880>, <[Link].Line2D
object at 0x000001DB0134BB20>, <[Link].Line2D object at
0x000001DB0137ADC0>, <[Link].Line2D object at 0x000001DB013950A0>,
<[Link].Line2D object at 0x000001DB013A1340>, <[Link].Line2D
object at 0x000001DB013B05E0>], 'means': []}
>>> [Link]('Algorithm Comparison')
Text(0.5, 1.0, 'Algorithm Comparison')
>>> [Link]()
>>>
>>> ...
Ellipsis
>>> # Make predictions on validation dataset
>>> model = SVC(gamma='auto')
>>> [Link](X_train, Y_train)
SVC(gamma='auto')
>>> predictions = [Link](X_validation)
>>> ....
File "<stdin>", line 1
....
^
SyntaxError: invalid syntax
>>> # Evaluate predictions
>>> print(accuracy_score(Y_validation, predictions))
0.9666666666666667
>>> print(confusion_matrix(Y_validation, predictions))
[[11 0 0]
[ 0 12 1]
[ 0 0 6]]
>>> print(classification_report(Y_validation, predictions))
precision recall f1-score support

Iris-setosa 1.00 1.00 1.00 11


Iris-versicolor 1.00 0.92 0.96 13
Iris-virginica 0.86 1.00 0.92 6

accuracy 0.97 30
macro avg 0.95 0.97 0.96 30
weighted avg 0.97 0.97 0.97 30

Common questions

Powered by AI

The confusion matrix for the SVM model on the validation dataset shows that the model perfectly classified 'Iris-setosa' with all predictions correct. For 'Iris-versicolor', one instance was misclassified as 'Iris-virginica', while all 'Iris-virginica' instances were correctly identified, except for one misclassified 'Iris-versicolor'. These results reveal high precision and recall for each class, contributing to an overall accuracy of 96.67% .

The 'train_test_split' method is essential in machine learning as it divides the dataset into training and validation subsets. This approach allows for an independent evaluation of the model's performance, preventing overfitting by ensuring that the model generalizes well to unseen data. By splitting the data, we can test the model's accuracy on the validation set, which was not used during training, providing a realistic estimate of its effectiveness .

Scatter plot matrices allow for a visual inspection of relationships and patterns between pairs of features in the Iris dataset. They help identify potential correlations, clusters, or separations that aid in distinguishing different classes. For the Iris dataset, this visualization helps in understanding how features like petal length and width differentiate between species and supports exploratory data analysis by highlighting feature interactions .

'SVC' with 'gamma' set to 'auto' automatically adjusts the kernel coefficient to the inverse of the number of features. This is suitable for the Iris dataset because it efficiently captures the complex decision boundaries required to separate the species classes. Additionally, the high accuracy achieved in cross-validation (98.33%) further supports its selection, as 'SVC' effectively handles the intricacies of the dataset's structure .

Cross-validation, such as k-fold, offers several advantages over a single train/test split. It provides a more robust estimate of the model's performance by training and validating the model on multiple subsets of the data, reducing dependency on a specific data split. This approach helps in identifying overfitting and ensures consistency and reliability in performance metrics, making model evaluation more comprehensive .

'StratifiedKFold' ensures that each fold of the cross-validation set contains approximately the same percentage of samples of each target class as the complete set. This stratification leads to more reliable and robust model evaluation by maintaining the distribution of the classes, which is particularly important for imbalanced datasets. It prevents bias caused by uneven class distribution during training and validation phases, making it a preferred choice over a simple train/test split .

Histograms provide a visual representation of the frequency distribution of each feature in the Iris dataset. They help identify the range, distribution shape, central tendency, and outliers. For example, histograms reveal that features like 'petal length' vary more distinctly between species than 'sepal width', suggesting these might be more important for differentiating classes .

The Iris dataset is evenly distributed among its classes, with each class ('Iris-setosa', 'Iris-versicolor', 'Iris-virginica') containing 50 samples. This balanced distribution is significant in model training as it ensures that each class is equally represented, reducing bias and enhancing the model's ability to learn and predict each class with similar effectiveness .

On the Iris dataset, different algorithms exhibit varying accuracy levels. Logistic Regression achieved an accuracy of 94.17%, while K-Nearest Neighbors (KNN) reached 95.83%. Support Vector Machine (SVM) with a gamma of 'auto' outperformed the others with an accuracy of 98.33%, indicating its effectiveness in handling the dataset's characteristics .

The box plot of algorithm performance reveals the variability in cross-validation results for different models, showcasing the range, median, and potential outliers. For instance, a smaller interquartile range in 'SVM' compared to others indicates consistent performance, whereas variability in 'LR' suggests greater sensitivity to data variation. Such comparisons help in understanding not just average performance, but also the stability of each model .

You might also like