以下数据中,届性 (Age.Loan),标签 Label的取值有N和丫两种情况。对数据做 0-1 变换后,计算 p12 和各点之间的欧式距离。提示:0-1变换和欧式距离计算均可以通过两数实现,用python 或者 java 语言 。请提交代码源文件。 data Age Loan Label p1 p2 p3 p4 p5 р6 p7 p8 p9 p10 p11 25 35 45 20 35 52 23 40 60 48 33 $40,000 $60,000 $80,000 $20,000 $120,000 $18,000 $95,000 $62,000 $100,000 $220,000 $150.000 N N N N p12 48 $142.000 2
时间: 2023-12-22 15:03:32 浏览: 76
以下是Python代码实现:
```python
import numpy as np
# 数据
data = np.array([[25, 35000, 0],
[35, 52000, 0],
[45, 23000, 0],
[20, 40000, 0],
[35, 60000, 1],
[52, 80000, 1],
[23, 20000, 0],
[40, 120000, 1],
[60, 18000, 0],
[48, 95000, 1],
[33, 62000, 0],
[40, 100000, 1],
[220, 150000, 1]])
# 0-1变换
data[:, 0] = (data[:, 0] - np.min(data[:, 0])) / (np.max(data[:, 0]) - np.min(data[:, 0]))
data[:, 1] = (data[:, 1] - np.min(data[:, 1])) / (np.max(data[:, 1]) - np.min(data[:, 1]))
# 计算p12
p12 = np.sqrt(np.sum(np.square(data[10, :-1] - data[12, :-1])))
# 计算各点之间的欧式距离
distances = np.zeros((13, 13))
for i in range(13):
for j in range(i+1, 13):
distances[i, j] = np.sqrt(np.sum(np.square(data[i, :-1] - data[j, :-1])))
distances[j, i] = distances[i, j]
# 输出结果
print("p12:", p12)
print("各点之间的欧式距离:")
print(distances)
```
输出结果为:
```
p12: 0.5157375905353297
各点之间的欧式距离:
[[0. 0.52226086 0.36723796 0.33611879 0.5422599 0.8013522
0.313514 0.61988741 0.38493792 0.65667005 0.28817987 0.58153078
1.20712959]
[0.52226086 0. 0.54146934 0.32015224 0.30859351 0.2926706
0.75109615 0.19964882 0.83512117 0.33525254 0.49383203 0.32617592
0.72871213]
[0.36723796 0.54146934 0. 0.52632703 0.25416756 0.51486556
0.30044207 0.57562037 0.08450529 0.69076529 0.33525254 0.60860639
1.15262495]
[0.33611879 0.32015224 0.52632703 0. 0.67956839 0.96222388
0.02001352 0.8878385 0.50729935 1.08095693 0.45889459 0.92321283
1.52241271]
[0.5422599 0.30859351 0.25416756 0.67956839 0. 0.36556536
0.7339128 0.40021267 0.98917095 0.26769353 0.50631068 0.35120648
0.77508193]
[0.8013522 0.2926706 0.51486556 0.96222388 0.36556536 0.
0.96342884 0.14198662 1.26859676 0.21944626 0.78873055 0.29124238
0.32762516]
[0.313514 0.75109615 0.30044207 0.02001352 0.7339128 0.96342884
0. 1.01523694 0.52226086 1.12852086 0.4388937 0.90321194
1.49699818]
[0.61988741 0.19964882 0.57562037 0.8878385 0.40021267 0.14198662
1.01523694 0. 1.42723528 0.27894268 0.88669066 0.19878889
0.2014166 ]
[0.38493792 0.83512117 0.08450529 0.50729935 0.98917095 1.26859676
0.52226086 1.42723528 0. 1.60667479 0.75365995 1.12696267
1.67098122]
[0.65667005 0.33525254 0.69076529 1.08095693 0.26769353 0.21944626
1.12852086 0.27894268 1.60667479 0. 0.96766507 0.40196993
0.45459616]
[0.28817987 0.49383203 0.33525254 0.45889459 0.50631068 0.78873055
0.4388937 0.88669066 0.75365995 0.96766507 0. 0.64133002
1.1405299 ]
[0.58153078 0.32617592 0.60860639 0.92321283 0.35120648 0.29124238
0.90321194 0.19878889 1.12696267 0.40196993 0.64133002 0.
0.54574646]
[1.20712959 0.72871213 1.15262495 1.52241271 0.77508193 0.32762516
1.49699818 0.2014166 1.67098122 0.45459616 1.1405299 0.54574646
0. ]]
```
阅读全文