0% found this document useful (0 votes)
21 views13 pages

Untitled5 1

Uploaded by

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

Untitled5 1

Uploaded by

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

7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [2]: #getting familiar with numpy, pandas, seaborn, and matplotlibb

In [1]: import pandas as pd


import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

In [7]: import pandas as pd


iris=pd.read_csv(r'C:\Users\Admin\Documents\iris.csv')
iris

C:\Users\Admin\AppData\Local\Temp\ipykernel_8268\551919226.py:1: Deprecati
onWarning:
Pyarrow will become a required dependency of pandas in the next major rele
ase of pandas (pandas 3.0),
(to allow more performant data types, such as the Arrow string type, and b
etter interoperability with other libraries)
but was not found to be installed on your system.
If this would cause problems for you,
please provide us feedback at https://github.com/pandas-dev/pandas/issues/
54466 (https://github.com/pandas-dev/pandas/issues/54466)

import pandas as pd

Out[7]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 Iris-setosa

1 2 4.9 3.0 1.4 0.2 Iris-setosa

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

... ... ... ... ... ... ...

145 146 6.7 3.0 5.2 2.3 Iris-virginica

146 147 6.3 2.5 5.0 1.9 Iris-virginica

147 148 6.5 3.0 5.2 2.0 Iris-virginica

148 149 6.2 3.4 5.4 2.3 Iris-virginica

149 150 5.9 3.0 5.1 1.8 Iris-virginica

150 rows × 6 columns

In [8]: iris.head()

Out[8]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 Iris-setosa

1 2 4.9 3.0 1.4 0.2 Iris-setosa

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

localhost:8890/notebooks/Untitled5.ipynb 1/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [9]: iris.shape

Out[9]: (150, 6)

In [10]: iris.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Id 150 non-null int64
1 SepalLengthCm 150 non-null float64
2 SepalWidthCm 150 non-null float64
3 PetalLengthCm 150 non-null float64
4 PetalWidthCm 150 non-null float64
5 Species 150 non-null object
dtypes: float64(4), int64(1), object(1)
memory usage: 7.2+ KB

In [12]: iris['Species'].value_counts()

Out[12]: Species
Iris-setosa 50
Iris-versicolor 50
Iris-virginica 50
Name: count, dtype: int64

In [8]: iris.isnull()

Out[8]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 False False False False False False

1 False False False False False False

2 False False False False False False

3 False False False False False False

4 False False False False False False

... ... ... ... ... ... ...

145 False False False False False False

146 False False False False False False

147 False False False False False False

148 False False False False False False

149 False False False False False False

150 rows × 6 columns

localhost:8890/notebooks/Untitled5.ipynb 2/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [9]: iris.isnull().sum()

Out[9]: Id 0
SepalLengthCm 0
SepalWidthCm 0
PetalLengthCm 0
PetalWidthCm 0
Species 0
dtype: int64

In [10]: iris.describe()

Out[10]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm

count 150.000000 150.000000 150.000000 150.000000 150.000000

mean 75.500000 5.843333 3.054000 3.758667 1.198667

std 43.445368 0.828066 0.433594 1.764420 0.763161

min 1.000000 4.300000 2.000000 1.000000 0.100000

25% 38.250000 5.100000 2.800000 1.600000 0.300000

50% 75.500000 5.800000 3.000000 4.350000 1.300000

75% 112.750000 6.400000 3.300000 5.100000 1.800000

max 150.000000 7.900000 4.400000 6.900000 2.500000

localhost:8890/notebooks/Untitled5.ipynb 3/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [11]: sns.pairplot(iris)

Out[11]: <seaborn.axisgrid.PairGrid at 0x1b9ff2c44c0>

In [13]: features=list(iris.drop(['Species'],axis=1).columns)
target=['Species']

localhost:8890/notebooks/Untitled5.ipynb 4/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [18]: import seaborn as sns


import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
for i in (features):
c=features.index(i)+1
plt.subplot(2,2,c)
sns.boxplot(y=iris[i].values,x=iris['Species']).set(ylabel=i)

--------------------------------------------------------------------------
-
ValueError Traceback (most recent call las
t)
Cell In[18], line 6
4 for i in (features):
5 c=features.index(i)+1
----> 6 plt.subplot(2,2,c)
7 sns.boxplot(y=iris[i].values,x=iris['Species']).set(ylabel=i)

File c:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packa
ges\matplotlib\pyplot.py:1425, in subplot(*args, **kwargs)
1422 fig = gcf()
1424 # First, search for an existing subplot with a matching spec.
-> 1425 key = SubplotSpec._from_subplot_args(fig, args)
1427 for ax in fig.axes:
1428 # If we found an Axes at the position, we can re-use it if the
user passed no
1429 # kwargs or if the axes class and kwargs are identical.
1430 if (ax.get_subplotspec() == key
1431 and (kwargs == {}
1432 or (ax._projection_init
1433 == fig._process_projection_requirements(**kwarg
s)))):

File c:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packa
ges\matplotlib\gridspec.py:599, in SubplotSpec._from_subplot_args(figure,
args)
597 else:
598 if not isinstance(num, Integral) or num < 1 or num > rows*col
s:
--> 599 raise ValueError(
600 f"num must be an integer with 1 <= num <= {rows*cols},
"
601 f"not {num!r}"
602 )
603 i = j = num
604 return gs[i-1:j]

ValueError: num must be an integer with 1 <= num <= 4, not 5

localhost:8890/notebooks/Untitled5.ipynb 5/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

localhost:8890/notebooks/Untitled5.ipynb 6/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [21]: sns.boxplot(iris['SepalLengthCm'])

Out[21]: <Axes: ylabel='SepalLengthCm'>

localhost:8890/notebooks/Untitled5.ipynb 7/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [20]: sns.pairplot(iris)

Out[20]: <seaborn.axisgrid.PairGrid at 0x2197a6eb5b0>

localhost:8890/notebooks/Untitled5.ipynb 8/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [23]: plt.figure(figsize=(5,5))
sns.heatmap(iris[features].corr(),annot=True,cbar=False)
plt.xticks(rotation=45)
plt.yticks(rotation=45)
plt.show()

In [24]: iris[features].corr()

Out[24]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm

Id 1.000000 0.716676 -0.397729 0.882747 0.899759

SepalLengthCm 0.716676 1.000000 -0.109369 0.871754 0.817954

SepalWidthCm -0.397729 -0.109369 1.000000 -0.420516 -0.356544

PetalLengthCm 0.882747 0.871754 -0.420516 1.000000 0.962757

PetalWidthCm 0.899759 0.817954 -0.356544 0.962757 1.000000

localhost:8890/notebooks/Untitled5.ipynb 9/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [26]: sns.distplot(iris['SepalLengthCm'])

C:\Users\Admin\AppData\Local\Temp\ipykernel_8268\3880742695.py:1: UserWarn
ing:

`distplot` is a deprecated function and will be removed in seaborn v0.14.


0.

Please adapt your code to use either `displot` (a figure-level function wi


th
similar flexibility) or `histplot` (an axes-level function for histogram
s).

For a guide to updating your code to use the new functions, please see
https://gist.github.com/mwaskom/de44147ed2974457ad6372750bbe5751 (https://
gist.github.com/mwaskom/de44147ed2974457ad6372750bbe5751)

sns.distplot(iris['SepalLengthCm'])

Out[26]: <Axes: xlabel='SepalLengthCm', ylabel='Density'>

localhost:8890/notebooks/Untitled5.ipynb 10/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [40]: X=iris.drop(['Species'],axis=1)
Y=iris['Species']
print(X,"\n\n",Y)

Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm


0 1 5.1 3.5 1.4 0.2
1 2 4.9 3.0 1.4 0.2
2 3 4.7 3.2 1.3 0.2
3 4 4.6 3.1 1.5 0.2
4 5 5.0 3.6 1.4 0.2
.. ... ... ... ... ...
145 146 6.7 3.0 5.2 2.3
146 147 6.3 2.5 5.0 1.9
147 148 6.5 3.0 5.2 2.0
148 149 6.2 3.4 5.4 2.3
149 150 5.9 3.0 5.1 1.8

[150 rows x 5 columns]

0 Iris-setosa
1 Iris-setosa
2 Iris-setosa
3 Iris-setosa
4 Iris-setosa
...
145 Iris-virginica
146 Iris-virginica
147 Iris-virginica
148 Iris-virginica
149 Iris-virginica
Name: Species, Length: 150, dtype: object

In [42]: #converting the numerical data into scaler unit


from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
X=scaler.fit_transform(X)
X

Out[42]: array([[-1.72054204e+00, -9.00681170e-01, 1.03205722e+00,


-1.34127240e+00, -1.31297673e+00],
[-1.69744751e+00, -1.14301691e+00, -1.24957601e-01,
-1.34127240e+00, -1.31297673e+00],
[-1.67435299e+00, -1.38535265e+00, 3.37848329e-01,
-1.39813811e+00, -1.31297673e+00],
[-1.65125846e+00, -1.50652052e+00, 1.06445364e-01,
-1.28440670e+00, -1.31297673e+00],
[-1.62816394e+00, -1.02184904e+00, 1.26346019e+00,
-1.34127240e+00, -1.31297673e+00],
[-1.60506942e+00, -5.37177559e-01, 1.95766909e+00,
-1.17067529e+00, -1.05003079e+00],
[-1.58197489e+00, -1.50652052e+00, 8.00654259e-01,
-1.34127240e+00, -1.18150376e+00],
[-1.55888037e+00, -1.02184904e+00, 8.00654259e-01,
-1.28440670e+00, -1.31297673e+00],
[-1.53578584e+00, -1.74885626e+00, -3.56360566e-01,
-1.34127240e+00, -1.31297673e+00],
[-1.51269132e+00, -1.14301691e+00, 1.06445364e-01,
1 28440670e+00 1 44444970e+00]

localhost:8890/notebooks/Untitled5.ipynb 11/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [44]: #converting the text data into numerical


from sklearn.preprocessing import LabelEncoder
label_encoder=LabelEncoder()
y=label_encoder.fit_transform(Y)
y

Out[44]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

In [55]: #splitting the dataset


from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2,stratify=y,
X_train,X_test,Y_train,Y_test

Out[55]: (array([[ 4.27248694e-01, -1.02184904e+00, -1.74477836e+00,


-2.60824029e-01, -2.61192967e-01],
[ 5.77363100e-02, 1.03800476e+00, -1.24957601e-01,
7.05892939e-01, 6.59117823e-01],
[-1.21246251e+00, -1.50652052e+00, 1.26346019e+00,
-1.56873522e+00, -1.31297673e+00],
[ 1.48959680e+00, 1.28034050e+00, 1.06445364e-01,
9.33355755e-01, 1.18500970e+00],
[-9.58422745e-01, -4.16009689e-01, 2.65187798e+00,
-1.34127240e+00, -1.31297673e+00],
[ 1.73208930e-01, -5.25060772e-02, -8.19166497e-01,
8.03701950e-02, 1.75297293e-03],
[-1.25865156e+00, -5.37177559e-01, 8.00654259e-01,
-1.17067529e+00, -1.31297673e+00],
[ 9.81517269e-01, 2.24968346e+00, 1.72626612e+00,
1.67260991e+00, 1.31648267e+00],
[ 1.69744751e+00, 4.32165405e-01, 8.00654259e-01,
9.33355755e-01, 1.44795564e+00],
[-1.73208930e-01, -5.25060772e-02, -8.19166497e-01,
1 94101603e 01 2 61192967e 01]
In [56]: X_train.shape

Out[56]: (120, 5)

localhost:8890/notebooks/Untitled5.ipynb 12/13
7/4/24, 7:37 PM Untitled5 - Jupyter Notebook

In [58]: from sklearn.linear_model import LogisticRegression


from sklearn.metrics import classification_report
lr=LogisticRegression()
lr.fit(X_train,Y_train)
y_pred=lr.predict(X_test)
acc_lr=lr.score(X_test,Y_test)
print('The accurancy for Logistic Regression is:',acc_lr*100,"%")
print(classification_report(y_pred,Y_test))

The accurancy for Logistic Regression is: 100.0 %


precision recall f1-score support

Iris-setosa 1.00 1.00 1.00 10


Iris-versicolor 1.00 1.00 1.00 10
Iris-virginica 1.00 1.00 1.00 10

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

In [ ]: ​

In [ ]: ​

localhost:8890/notebooks/Untitled5.ipynb 13/13

You might also like