1 对一个5x5的随机矩阵做归一化
(x - min) / (max - min)
import numpy as np
Z = np.random.randint(1,20, size = 25).reshape(5,5)
print(Z)
Zmax, Zmin = Z.max(), Z.min()
Z = (Z - Zmin)/(Zmax - Zmin)
print(Z)
#Solution Method 1:
rand_arr = np.random.randint(low=5, high=10, size=(5,3)) + np.random.random((5,3))
print(rand_arr)
#Solution Method 2:
rand_arr = np.random.uniform(5,10, size=(5,3))
print(rand_arr)
# > [[ 8.50061025 9.10531502 6.85867783]
# > [ 9.76262069 9.87717411 7.13466701]
# > [ 7.48966403 8.33409158 6.16808631]
# > [ 7.75010551 9.94535696 5.27373226]
# > [ 8.0850361 5.56165518 7.31244004]]
3. 一个5x3的矩阵与一个3x2的矩阵相乘,实矩阵乘积是什么?
np.dot
Z = np.dot(np.ones((5,3)), np.ones((3,2)))
print(Z)
#Input
url = '<https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data>'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')
'''
答案:
import numpy as np
# Input
url = '<https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data>'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')
#查看数据
iris.shape
# Bin petallength
petal_length_bin = np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10])
# Map it to respective category
label_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan}
petal_length_cat = [label_map[x] for x in petal_length_bin]
# View
petal_length_cat[:4]
# > ['small', 'small', 'small', 'small']
petal_length_cat
- 描述:在鸢尾属植物数据集中找到最常见的花瓣长度值(第3列)。
**给定:**答案:
# **给定:**
url = '<https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data>'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
# Solution:
vals, counts = np.unique(iris[:, 2], return_counts=True)
print(vals[np.argmax(counts)])
vals
# > b'1.5'
- 描述:在iris_2d中找出SepalLength(第1列)和PetalLength(第3列)之间的相关性
**给定:**答案:
# Input
url = '<https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data>'
iris = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
# Solution 1
#np.corrcoef : <https://blog.csdn.net/qq_39514033/article/details/88931639>
cor = np.corrcoef(iris[:, 0], iris[:, 2])
print(cor)
print(cor[0, 1])
# Solution 2
#输出:r: 相关系数 [-1,1]之间,p-value: p值。
# 注: p值越小,表示相关系数越显著,一般p值在500个样本以上时有较高的可靠性。
#说明:<https://www.osgeo.cn/scipy/reference/generated/scipy.stats.pearsonr.html>
from scipy.stats.stats import pearsonr
corr, p_value = pearsonr(iris[:, 0], iris[:, 2])
print(corr,p_value)
阅读量:2010
点赞量:0
收藏量:0