-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing .mlpy
More file actions
208 lines (146 loc) · 4.4 KB
/
preprocessing .mlpy
File metadata and controls
208 lines (146 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df=pd.read_csv("data.csv")
df.head()
country age salary puchased
0 France NaN 7200 NO
1 Spain 27.0 4800 yes
2 Germany 30.0 5400 yes
3 UK 49.0 98000 NO
from google.colab import files
uploaded = files.upload()
Upload widget is only available when the cell has been executed in the current browser session. Please rerun this cell to enable.
Saving data.csv to data.csv
df.tail()
country age salary puchased
0 France 44.0 7200.0 NO
1 Spain 27.0 4800.0 yes
2 Germany 30.0 5400.0 yes
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 country 3 non-null object
1 age 3 non-null float64
2 salary 3 non-null float64
3 puchased 3 non-null object
dtypes: float64(2), object(2)
memory usage: 224.0+ bytes
df.describe()
age salary
count 3.000000 3.0000
mean 33.666667 5800.0000
std 9.073772 1248.9996
min 27.000000 4800.0000
25% 28.500000 5100.0000
50% 30.000000 5400.0000
75% 37.000000 6300.0000
max 44.000000 7200.0000
df.isnull().sum()
country 0
age 1
salary 0
puchased 0
dtype: int64
df["age"].fillna(df["age"].mean(),inplace=True)
df["salary"].fillna(df["salary"].mean(),inplace=True)
df.isnull().sum()
country 0
age 0
salary 0
puchased 0
dtype: int64
from sklearn.impute import SimpleImputer
x=df.iloc[:,:-1].values
x
array([['France', 35.333333333333336, 7200],
['Spain', 27.0, 4800],
['Germany', 30.0, 5400],
['UK', 49.0, 98000]], dtype=object)
y=df.iloc[:,3:].values
y
array([['NO'],
['yes'],
['yes'],
['NO']], dtype=object)
imp=SimpleImputer(missing_values=np.nan,strategy="mean")
x[:,1:3]=imp.fit_transform(x[:,1:3])
x
array([['France', 35.333333333333336, 7200.0],
['Spain', 27.0, 4800.0],
['Germany', 30.0, 5400.0],
['UK', 49.0, 98000.0]], dtype=object)
y
array([['NO'],
['yes'],
['yes'],
['NO']], dtype=object)
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
h=le.fit_transform(x[:,0])
h
array([0, 2, 1, 3])
y=le.fit_transform(y)
y
/usr/local/lib/python3.10/dist-packages/sklearn/preprocessing/_label.py:116: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
array([0, 1, 1, 0])
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
transform=ColumnTransformer([("norm1",OneHotEncoder(),[0])],remainder='passthrough')
x=transform.fit_transform(x)
x
array([[1.0, 0.0, 0.0, 0.0, 44.0, 7200.0],
[0.0, 0.0, 1.0, 0.0, 27.0, 4800.0],
[0.0, 1.0, 0.0, 0.0, 30.0, 5400.0],
[0.0, 0.0, 0.0, 1.0, 49.0, 98000.0]], dtype=object)
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,random_state=0)
from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
x_train[:,4:6]=sc.fit_transform(x_train[:,4:6])
x_train
array([[0.0, 0.0, 0.0, 1.0, 0.9557896392964647, 1.4138527953056175],
[0.0, 0.0, 1.0, 0.0, -1.3805850345393378, -0.7345887349522664],
[1.0, 0.0, 0.0, 0.0, 0.4247953952428732, -0.679264060353351]],
dtype=object)
plt.bar(df["country"],df["salary"])
plt.xlabel("country")
plt.ylabel("salary")
plt.show()
import seaborn as sns
sns.pairplot(df)
<seaborn.axisgrid.PairGrid at 0x7e40d00ff1c0>
df
country age salary puchased
0 France 44 7200 NO
1 Spain 27 4800 yes
2 Germany 30 5400 yes
3 UK 49 98000 NO
from sklearn.preprocessing import OneHotEncoder
import numpy as np
# Sample data
colors = np.array(['red', 'green', 'blue', 'red', 'blue']).reshape(-1, 1)
colors.shape
colors
array([['red'],
['green'],
['blue'],
['red'],
['blue']], dtype='<U5')
encoder = OneHotEncoder()
# Fit and transform the data
one_hot_encoded = encoder.fit_transform(colors)
one_hot_encoded
# Convert the sparse matrix to a dense array for visualization
one_hot_encoded_array = one_hot_encoded.toarray()
# Display the one-hot encoded array
print(one_hot_encoded_array)
[[0. 0. 1.]
[0. 1. 0.]
[1. 0. 0.]
[0. 0. 1.]
[1. 0. 0.]]