数据可视化 Assignment 1 Python实现矩阵映射与归一化

Posted Jing Sir

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据可视化 Assignment 1 Python实现矩阵映射与归一化相关的知识,希望对你有一定的参考价值。

Questionnaire: Map data points to positions

You are given a dataset (attached as assignment1_data.txt). Answer the following questions.

1. Figure out data coordinates of the dataset, i.e., min & max of each dimension (SATM, SATV, ACT, GPA).

2. Determine view volume coordinates, for SATM and SATV, that are suitable for visualization.

3. Suppose you would like to map the scores of SATM (x-axis) and SATV (y-axis) onto a viewport of [(100,100), (200,200)], what transformation matrix would you use? Describe how the matrix is derived.

4. Figure out position of the first data point (430, 470) in the viewport.

数据

实验过程

1. 导入相关所需扩展包

2. 从文件中读取数据

3. 将数据存到列表里

4. 提取列信息方便后续处理

5. 删除表头数据

6. 查看读入是否正确

7. 对读入的字符数据进行格式转换

8. 求各属性的最大值、最小值

9. 将用到的SATM和SATV合并到一起

10. 画图看一下效果

11. 原始数据映射到[(100,100),(200,200)]范围

12. 映射后画图看一下效果,不能说完全一致,只能说一模一样

完整代码:

import matplotlib.pyplot as plt
import numpy as np

#从文件中读取数据
with open ('assignment 1_data.txt') as file_object:
    lines = file_object.read().splitlines()#去除每行末尾的回车符

#将txt中的数据逐行存到列表lines里,lines的每一个元素对应于txt中的一行    
file1 = []
row = []
for line in lines:
    row = line.split(',')#指定,作为分隔符对line进行切片
    file1.append(row)    

#print(file1) #查看当前数据是否读取成功

#将每列的信息分别提取到一个数组中方便后续处理
SATM_0 = []
SATV_0 = []
ACT_0 = []
GPA_0 = []
for row1 in file1:
    SATM_0.append(row1[0])
    SATV_0.append(row1[1])
    ACT_0.append(row1[2])
    GPA_0.append(row1[3])

#读数据时将表头也读进去了,删除列表第一个元素
del SATM_0[0]
del SATV_0[0]
del ACT_0[0]
del GPA_0[0]

#读入数据为字符型数据,使用格式转换为所需格式
SATM=[]
for str_l in SATM_0:
    a=int(float(str_l))#强制转换
    SATM.append(a)
SATV=[]
for str_l in SATV_0:
    a=int(float(str_l))#强制转换
    SATV.append(a)   
ACT=[]
for str_l in ACT_0:
    a=int(float(str_l))#强制转换
    ACT.append(a)   
GPA=[]
for str_l in GPA_0:
    a=float(str_l)#强制转换
GPA.append(a)

#各属性最大值
a = max(SATM)
b = max(SATV)
c = max(ACT)
d = max(GPA)
print(a,b,c,d)

#各属性最小值
a = min(SATM)
b = min(SATV)
c = min(ACT)
d = min(GPA)
print(a,b,c,d)

#将list中数据两两合并为矩阵
array = list(zip(SATM,SATV))
print(array)

#线图
plt.plot(SATM,SATV)
plt.show()

#散点图
plt.scatter(SATM,SATV)
plt.show()

#直方图
plt.hist(SATM,bins = 10)
plt.show()

#将原始数据映射到[(100,100),(200,200)]
y_max = 200.0
y_min = 100.0
array = (y_max-y_min)*(array-np.min(array))/(np.max(array)-np.min(array))
print(array)

#拆分数据
x = []
y = []
x = np.hsplit(array,2)[0]
y = np.hsplit(array,2)[1]

#线图
plt.plot(x,y)
plt.show()

#散点图
plt.scatter(x,y)
plt.show()

#直方图
plt.hist(x,bins = 10)
plt.show()

 

以上是关于数据可视化 Assignment 1 Python实现矩阵映射与归一化的主要内容,如果未能解决你的问题,请参考以下文章

数据可视化 Assignment 1 Python实现矩阵映射与归一化

数据可视化 Assignment 1 Python实现矩阵映射与归一化

可视化设计Assignment01

可视化设计Assignment01

python 数据争夺ASsignment 2第2部分

Python 基础 - Day 1 Assignment - Three tier menu 三级菜单