python数据分析与机器学习实战(1.1)

科学计算库Numpy(一)

1、导入numpy库函数

    文件名字;用“,”分开的文件内容;类型是string类型的

2、打印world_alcohol的数据类型,应该是numpy.ndarray

打印出world_alcoholnumpy矩阵和genfromtxt函数的帮助文档

import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt",delimiter=",",dtype=str)
print(type(world_alcohol))
print("-----------------------")
print(world_alcohol)
print("-----------------------")
print(help(numpy.genfromtxt))
3、利用numpy.array构造一维数组和二位数组
vector = numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print (vector)
print("------------------------------")
print (matrix)

结果为:

[ 5 10 15 20]
------------------------------
[[ 5 10 15]
 [20 25 30]
 [35 40 45]]

4、利用numpy.shape打印数组格式

vector = numpy.array([1,2,3,4])
print(vector.shape)
print("-------------------------------")
matrix = numpy.array([[5,10,15],[20,25,30]])
print (matrix.shape)

    结果为:

(4,)
-------------------------------
(2, 3)

5、数组的数据类型会根据内部元素改变

import numpy
numbers = numpy.array([1,2,3,4])
print(numbers)
print(numbers.dtype)
print("--------------------------------")
number = numpy.array([1,2,3,4.1])
print(number)
print(number.dtype)
print("--------------------------------")
numberstr = numpy.array([1,2,3,'4'])
print(numberstr)
print(numberstr.dtype)

结果为:

[1 2 3 4]
int64
--------------------------------
[ 1.   2.   3.   4.1]
float64
--------------------------------
['1' '2' '3' '4']
<U21

6、利用numpy.genfromtxt读取外部数据,并且打印出数组

world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype=str, skip_header=1)
print(world_alcohol)

结果为:

[['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
 ['1986' 'Americas' 'Uruguay' 'Other' '0.5']
 ['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62']
 ..., 
 ['1987' 'Africa' 'Malawi' 'Other' '0.75']
 ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
 ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]

注意:
delimiter=","
     - genfromtxt分割每个非空行成一个字符串序列。空的行或注释行跳过。
     - 分割符并不局限于单个字符,任何字符串就可以了。
     - 分割一个固定宽度的文件,列的宽度被定义为一个给定的字符数。在这种情况下,我们需要将分隔符设置成一个整数(如果所有的列有相同的大小)或一个整数序列(如果列可以有不同的大小)。

dtype=str
我们从文件读取的字符串序列要转换为其他类型数据时需设置dtype参数。默认是float类型。 一个特殊的值None,在这种情况下,每个列的类型有自身数据决定。将参数设置成None效率较低。因为它会从布尔值开始检验,然后是整型,浮点型,复数最后是字符串,直到满足条件为止。

skip_header和skip_footer
一个文件的页眉可能会阻碍文件的处理。在这种情况下,我们需要使用skip_header可选参数。这个参数的值必须是一个整数,跳过文件开头的对应的行数,然后再执行任何其他操作。同样的,我们通过使用skip_footer属性和n的值可以跳过文件的最后n行。默认值都为0.
7、numpy可以参考切片的方式选取矩阵内的元素进行打印

例一:

Uruguay_other_1986 = world_alcohol[1,4]
third_country = world_alcohol[2,2]
print (Uruguay_other_1986)
print (third_country)

结果为:

0.5
Cte d'Ivoire

例二:

vector = numpy.array([5,10,15,20])
print(vector[1:3])

结果为:

[10 15]

例三:

matrix = numpy.array([
                    [5,10,15],
                    [20,25,30],
                    [35,40,45],
                ])
print(matrix[:,1])
print("------------------------")
print(matrix[:,0:2])
print("------------------------")
print(matrix[1:3,:2])

结果为:

[10 25 40]
------------------------
[[ 5 10]
 [20 25]
 [35 40]]
------------------------
[[20 25]
 [35 40]]




猜你喜欢

转载自blog.csdn.net/Echo_dat/article/details/80855387