照葫芦画瓢
(战术捂脸~~~~~~~~)
这次用到了iris数据集。所以,运用了绝对位置进行索引。
sepallength:萼片长度
sepalwidth:萼片宽度
petallength:花瓣长度
petalwidth:花瓣宽度
首先导入鸢尾属植物数据集,保持文本不变。这里我用了绝对位置进行索引
因为我把它放在了桌面。。
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
按照题目的要求,求出鸢尾属植物萼片长度的平均值、中位数和标准差(第1列,sepallength)
这里面索引出了前十行数据的第一列数据。主要是熟练应用三个函数
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
sepalLength = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0])
print(sepalLength[0:10])
# [5.1 4.9 4.7 4.6 5. 5.4 4.6 5. 4.4 4.9]
print(np.mean(sepalLength))
# 5.843333333333334
print(np.median(sepalLength))
# 5.8
print(np.std(sepalLength))
# 0.8253012917851409
创建一种标准化形式的鸢尾属植物萼片长度,其值正好介于0和1之间,这样最小值为0,最大值为1(第1列,sepallength)
萼片长度也就是第一列数据
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
sepalLength = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0])
# 方法1
aMax = np.amax(sepalLength)
aMin = np.amin(sepalLength)
x = (sepalLength - aMin) / (aMax - aMin)
print(x[0:10])
# [0.22222222 0.16666667 0.11111111 0.08333333 0.19444444 0.30555556
# 0.08333333 0.19444444 0.02777778 0.16666667]
# 方法2
x = (sepalLength - aMin) / np.ptp(sepalLength)#这个函数起到直接推算最大值减最小值的作用。
print(x[0:10])
# [0.22222222 0.16666667 0.11111111 0.08333333 0.19444444 0.30555556
# 0.08333333 0.19444444 0.02777778 0.16666667]
找到鸢尾属植物萼片长度的第5和第95百分位数,也是第1列的数据,sepallength。
import numpy as np
outfile =r'C:\Users\liujinlong\Desktop\iris.csv'
sepalLength = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0])
x = np.percentile(sepalLength, [5, 95])
print(x) # [4.6 7.255]
把iris_data数据集中的20个随机位置修改为np.nan值。
这里,np.nan是数据类型
从网上嫖了一段解释。
np.nan是一个float类型的数据 None是一个NoneType类型
1、在ndarray中显示时 np.nan会显示nan,如果进行计算 结果会显示为NAN, None显示为None 并且对象为object类型,如果进行计算 结果会报错所以ndarray中无法对有缺失值的数据进行计算
2、 在Serise中显示的时候都会显示为NAN,均可以视作np.nan 进行计算时可以通过np.sum()得到结果,此时NAN默认为s1 + 10 对于空值得到的结果为NAN,如果使用加法 可以通过s1.add(参数,fill_value = 0)指定空值的默认值为0
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
# 方法1
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
i, j = iris_data.shape
np.random.seed(20200621)
iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan
print(iris_data[0:10])
# [['5.1' '3.5' '1.4' '0.2' 'Iris-setosa']
# ['4.9' '3.0' '1.4' '0.2' 'Iris-setosa']
# ['4.7' '3.2' '1.3' '0.2' 'Iris-setosa']
# ['4.6' '3.1' '1.5' '0.2' 'Iris-setosa']
# ['5.0' '3.6' '1.4' '0.2' 'Iris-setosa']
# ['5.4' nan '1.7' '0.4' 'Iris-setosa']
# ['4.6' '3.4' '1.4' '0.3' 'Iris-setosa']
# ['5.0' '3.4' '1.5' '0.2' 'Iris-setosa']
# ['4.4' '2.9' '1.4' '0.2' nan]
# ['4.9' '3.1' '1.5' '0.1' 'Iris-setosa']]
# 方法2
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
i, j = iris_data.shape
np.random.seed(20200620)
iris_data[np.random.choice(i, size=20), np.random.choice(j, size=20)] = np.nan
print(iris_data[0:10])
# [['5.1' '3.5' '1.4' '0.2' 'Iris-setosa']
# ['4.9' '3.0' '1.4' '0.2' 'Iris-setosa']
# ['4.7' '3.2' '1.3' '0.2' 'Iris-setosa']
# ['4.6' '3.1' '1.5' '0.2' 'Iris-setosa']
# [nan '3.6' '1.4' '0.2' 'Iris-setosa']
# ['5.4' '3.9' '1.7' '0.4' 'Iris-setosa']
# ['4.6' '3.4' '1.4' '0.3' 'Iris-setosa']
# ['5.0' '3.4' '1.5' '0.2' 'Iris-setosa']
# ['4.4' '2.9' '1.4' '0.2' 'Iris-setosa']
# ['4.9' '3.1' '1.5' nan 'Iris-setosa']]
在iris_data的sepallength中查找缺失值的个数和位置(第1列)。
如何在numpy数组中找到缺失值的位置
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
i, j = iris_data.shape
np.random.seed(20200621)
iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan
sepallength = iris_data[:, 0]
x = np.isnan(sepallength)
print(sum(x)) # 6
print(np.where(x))
# (array([ 26, 44, 55, 63, 90, 115], dtype=int64),)
筛选具有 sepallength(第1列)< 5.0 并且 petallength(第3列)> 1.5 的 iris_data行。考察的是如何根据两个条件或多个条件筛选numpy数组。
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
sepallength = iris_data[:, 0]
petallength = iris_data[:, 2]
index = np.where(np.logical_and(petallength > 1.5, sepallength < 5.0))
print(iris_data[index])
# [[4.8 3.4 1.6 0.2]
# [4.8 3.4 1.9 0.2]
# [4.7 3.2 1.6 0.2]
# [4.8 3.1 1.6 0.2]
# [4.9 2.4 3.3 1. ]
# [4.9 2.5 4.5 1.7]]
选择没有任何 nan 值的 iris_data行。
数据庞大,print前10行就行。
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
i, j = iris_data.shape
np.random.seed(20200621)
iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan
x = iris_data[np.sum(np.isnan(iris_data), axis=1) == 0]
print(x[0:10])
# [[5.1 3.5 1.4 0.2]
# [4.9 3. 1.4 0.2]
# [4.7 3.2 1.3 0.2]
# [4.6 3.1 1.5 0.2]
# [5. 3.6 1.4 0.2]
# [4.6 3.4 1.4 0.3]
# [5. 3.4 1.5 0.2]
# [4.9 3.1 1.5 0.1]
# [5.4 3.7 1.5 0.2]
# [4.8 3.4 1.6 0.2]]
计算 iris_data 中sepalLength(第1列)和petalLength(第3列)之间的相关系数。
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
sepalLength = iris_data[:, 0]
petalLength = iris_data[:, 2]
# 方法1
m1 = np.mean(sepalLength)
m2 = np.mean(petalLength)
cov = np.dot(sepalLength - m1, petalLength - m2)
std1 = np.sqrt(np.dot(sepalLength - m1, sepalLength - m1))
std2 = np.sqrt(np.dot(petalLength - m2, petalLength - m2))
print(cov / (std1 * std2)) # 0.8717541573048712
# 方法2
x = np.mean((sepalLength - m1) * (petalLength - m2))
y = np.std(sepalLength) * np.std(petalLength)
print(x / y) # 0.8717541573048712
# 方法3
x = np.cov(sepalLength, petalLength, ddof=False)
y = np.std(sepalLength) * np.std(petalLength)
print(x[0, 1] / y) # 0.8717541573048716
# 方法4
x = np.corrcoef(sepalLength, petalLength)
print(x)
# [[1. 0.87175416]
# [0.87175416 1. ]]
由于一个数据集,可能在某个地方没有记录东西。这就需要找出iris_data是否有任何缺失值。
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
x = np.isnan(iris_data)
print(np.any(x))
# False
显示false,就说明数据是全的。
nan在一定情况下和0相同,所以,为了操作的普适性,不出现一些小的差错,将numpy数组中将所有出现的nan替换为0。
import numpy as np
outfile =r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
i, j = iris_data.shape
np.random.seed(20200621)
iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan
iris_data[np.isnan(iris_data)] = 0
print(iris_data[0:10])
# [[5.1 3.5 1.4 0.2]
# [4.9 3. 1.4 0.2]
# [4.7 3.2 1.3 0.2]
# [4.6 3.1 1.5 0.2]
# [5. 3.6 1.4 0.2]
# [5.4 0. 1.7 0.4]
# [4.6 3.4 1.4 0.3]
# [5. 3.4 1.5 0.2]
# [4.4 2.9 0. 0.2]
# [4.9 3.1 1.5 0.1]]
数据太多,分不清哪种花的特点,为了区分长度问题 ,将 iris_data 的花瓣长度(第3列)以形成分类变量的形式显示。定义:Less than 3 --> ‘small’;3-5 --> ‘medium’;’>=5 --> ‘large’。
将数字转换成文本或者分类的数组。
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
petal_length_bin = np.digitize(iris_data[:, 2], [0, 3, 5, 10])
label_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan}
petal_length_cat = [label_map[x] for x in petal_length_bin]
print(petal_length_cat[0:10])
# ['small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small']
在 iris_data 中创建一个新列,其中 volume 是 (pi x petallength x sepallength ^ 2)/ 3。
考验的是如何从numpy数组的现有列创建新列。
import numpy as np
outfile = r'C:\Users\liujinlong\Desktop\iris.csv'
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
sepalLength = iris_data[:, 0].astype(float)
petalLength = iris_data[:, 2].astype(float)
volume = (np.pi * petalLength * sepalLength ** 2) / 3
volume = volume[:, np.newaxis]
iris_data = np.concatenate([iris_data, volume], axis=1)
print(iris_data[0:10])
# [['5.1' '3.5' '1.4' '0.2' 'Iris-setosa' 38.13265162927291]
# ['4.9' '3.0' '1.4' '0.2' 'Iris-setosa' 35.200498485922445]
# ['4.7' '3.2' '1.3' '0.2' 'Iris-setosa' 30.0723720777127]
# ['4.6' '3.1' '1.5' '0.2' 'Iris-setosa' 33.238050274980004]
# ['5.0' '3.6' '1.4' '0.2' 'Iris-setosa' 36.65191429188092]
# ['5.4' '3.9' '1.7' '0.4' 'Iris-setosa' 51.911677007917746]
# ['4.6' '3.4' '1.4' '0.3' 'Iris-setosa' 31.022180256648003]
# ['5.0' '3.4' '1.5' '0.2' 'Iris-setosa' 39.269908169872416]
# ['4.4' '2.9' '1.4' '0.2' 'Iris-setosa' 28.38324242763259]
# ['4.9' '3.1' '1.5' '0.1' 'Iris-setosa' 37.714819806345474]]
。。。。。。。。。。。。。。