Vous pouvez d'abord vérifier dtypes
de sortie df
:
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data'
df = pd.read_csv(url, header=None)
print (df.dtypes)
0 int64
1 object
2 float64
3 float64
...
...
29 float64
30 float64
31 float64
dtype: object
Toutes les colonnes sont numériques, seule la seconde colonne est object
- évidemment string
, donc une solution possible est set_index
pour convertir toutes les colonnes de chaînes à l'index:
df = df.set_index(1)
print (df.head())
0 2 3 4 5 6 7 8 9 \
1
M 842302 17.99 10.38 122.80 1001.0 0.11840 0.27760 0.3001 0.14710
M 842517 20.57 17.77 132.90 1326.0 0.08474 0.07864 0.0869 0.07017
M 84300903 19.69 21.25 130.00 1203.0 0.10960 0.15990 0.1974 0.12790
M 84348301 11.42 20.38 77.58 386.1 0.14250 0.28390 0.2414 0.10520
M 84358402 20.29 14.34 135.10 1297.0 0.10030 0.13280 0.1980 0.10430
10 ... 22 23 24 25 26 27 28 \
1 ...
M 0.2419 ... 25.38 17.33 184.60 2019.0 0.1622 0.6656 0.7119
M 0.1812 ... 24.99 23.41 158.80 1956.0 0.1238 0.1866 0.2416
M 0.2069 ... 23.57 25.53 152.50 1709.0 0.1444 0.4245 0.4504
M 0.2597 ... 14.91 26.50 98.87 567.7 0.2098 0.8663 0.6869
M 0.1809 ... 22.54 16.67 152.20 1575.0 0.1374 0.2050 0.4000
29 30 31
1
M 0.2654 0.4601 0.11890
M 0.1860 0.2750 0.08902
M 0.2430 0.3613 0.08758
M 0.2575 0.6638 0.17300
M 0.1625 0.2364 0.07678
[5 rows x 31 columns]
Ensuite, tout fonctionne bien et dernier ajouter reset_index
:
def normalize(df):
result = df.copy()
for feature_name in df.columns:
max_value = df[feature_name].max()
min_value = df[feature_name].min()
result[feature_name] = (df[feature_name] - min_value)/(max_value - min_value)
return result
df_normalized = normalize(df).reset_index().sort_index(axis=1)
print (df_normalized.head())
0 1 2 3 4 5 6 7 \
0 0.000915 M 0.521037 0.022658 0.545989 0.363733 0.593753 0.792037
1 0.000915 M 0.643144 0.272574 0.615783 0.501591 0.289880 0.181768
2 0.092495 M 0.601496 0.390260 0.595743 0.449417 0.514309 0.431017
3 0.092547 M 0.210090 0.360839 0.233501 0.102906 0.811321 0.811361
4 0.092559 M 0.629893 0.156578 0.630986 0.489290 0.430351 0.347893
8 9 ... 22 23 24 25 \
0 0.703140 0.731113 ... 0.620776 0.141525 0.668310 0.450698
1 0.203608 0.348757 ... 0.606901 0.303571 0.539818 0.435214
2 0.462512 0.635686 ... 0.556386 0.360075 0.508442 0.374508
3 0.565604 0.522863 ... 0.248310 0.385928 0.241347 0.094008
4 0.463918 0.518390 ... 0.519744 0.123934 0.506948 0.341575
26 27 28 29 30 31
0 0.601136 0.619292 0.568610 0.912027 0.598462 0.418864
1 0.347553 0.154563 0.192971 0.639175 0.233590 0.222878
2 0.483590 0.385375 0.359744 0.835052 0.403706 0.213433
3 0.915472 0.814012 0.548642 0.884880 1.000000 0.773711
4 0.437364 0.172415 0.319489 0.558419 0.157500 0.142595
[5 rows x 32 columns]