河南網(wǎng)站優(yōu)化推廣/免費域名的網(wǎng)站
1. 實驗目的
①掌握Matplotlib繪圖基礎;
②運用Matplotlib,實現(xiàn)數(shù)據(jù)集的可視化;
③運用Pandas訪問csv數(shù)據(jù)集。
2. 實驗內(nèi)容
①繪制散點圖、直方圖和折線圖,對數(shù)據(jù)進行可視化;
②下載波士頓數(shù)房價據(jù)集,并繪制數(shù)據(jù)集中各個屬性與房價之間的散點圖,實現(xiàn)數(shù)據(jù)集可視化;
③使用Pandas訪問鳶尾花數(shù)據(jù)集,對數(shù)據(jù)進行設置列標題、讀取數(shù)據(jù)、顯示統(tǒng)計信息、轉化為Numpy數(shù)組等操作;并使用Matpoltlib對數(shù)據(jù)集進行可視化。
3. 實驗過程
題目一:
這是一個商品房銷售記錄表,請根據(jù)表中的數(shù)據(jù),按下列要求繪制散點圖。其中橫坐標為商品房面積,縱坐標為商品房價格。
要求:
(1)繪制散點圖,數(shù)據(jù)點為紅色圓點;
(2)標題為:“商品房銷售記錄”,字體顏色為藍色,大小為16;
(3)橫坐標標簽為:“面積(平方米)”,縱坐標標簽為“價格(萬元)”,字體大小為14。
源代碼
import numpy as np
import matplotlib.pyplot as plt#設置rc參數(shù)
plt.rcParams["font.family"] = "SimHei"#設置默認字體為中文黑體
plt.rcParams['axes.unicode_minus'] = False #坐標軸上負號的顯示可能會出錯
area = np.array([137.97,104.50,100.00,124.32,79.20,99.00,124.00,114.00,106.69,138.05,53.75,46.91,68.00,63.02,81.26,86.21])
price = np.array([145.00,110.00,93.00,116.00,65.32,104.00,118.00,91.00,62.00,133.00,51.00,45.00,78.50,69.65,75.69,95.30])plt.scatter(area,price,color = 'red')
plt.title("商品房銷售記錄",fontsize = "16",color = "blue")
plt.xlabel("面積(平方米)",fontsize = '14')
plt.ylabel("價格(萬元)",fontsize = '14')plt.show()
題目二:
按下列要求完成程序。
(1)下載波士頓數(shù)據(jù)集,讀取全部506條數(shù)據(jù),放在NumPy數(shù)組x、y中(x:屬性,y:標記);
(2)使用全部506條數(shù)據(jù),實現(xiàn)波士頓房價數(shù)據(jù)集可視化,如圖1所示;
(3)要求用戶選擇屬性,如圖2所示,根據(jù)用戶的選擇,輸出對應屬性的散點圖,如圖3所示
請用戶輸入屬性:
運行結果:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as npboston_housing = tf.keras.datasets.boston_housing
(train_x, train_y), (test_x, test_y) = boston_housing.load_data(test_split=0)plt.rcParams['font.sans-serif'] = "SimHei"
plt.rcParams['axes.unicode_minus'] = Falsetitles = ["CRIM", "ZN", "INDUS", "CHAS", "NOX", "RM", "AGE", "DIS", "RAD", "TAX","PTRATIO", "B-1000", "LSTAT", "MEDV"
]plt.figure(figsize=(14, 14))for i in range(13):plt.subplot(4, 4, i + 1)plt.scatter(train_x[:, i], train_y)plt.xlabel(titles[i])plt.ylabel("Price($1000's)")plt.title(str(i + 1) + "." + titles[i] + " - Price")plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.suptitle("各個屬性與房價的關系", x=0.5, y=0.98, fontsize=20)
plt.show()plt.close()print("請輸入所選擇的屬性")
print( "1--CRIM\n", "2--ZN\n", "3--INDUS\n", "4--CHAS\n", "5--NOX\n", "6--RM\n", "7--AGE\n", "8--DIS\n", "9--RAD\n", "10--TAX\n","11--PTRATIO\n", "12--B-1000\n", "13--LSTAT\n", "14--MEDV")
n = int(input())
sc = titles[i - 1] + "Price($1000's)"
plt.figure(figsize=(5,5))
plt.scatter(train_x[:,i-1],train_y)
plt.xlabel(titles[i - 1])
plt.ylabel("Price($1000's)")
plt.title(sc)
plt.show()
題目三:
使用鳶尾花數(shù)據(jù)集,繪制如下圖形,其中對角線為屬性的直方圖。
提示:繪制直方圖函數(shù) plt.hist(x, align= ‘mid’, color, edgecolor)
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pdTRAIN_URL = "http://download.tensorflow.org/data/iris_training.csv"
train_path = tf.keras.utils.get_file(TRAIN_URL.split('/')[-1],TRAIN_URL)#設置列標題
COLUMN_NAMES = ['SepalLEngth', 'SePalWidth', 'PetalLength', 'PetalWidth', 'Species'
]
#下載鳶尾花數(shù)據(jù)集,并設置列標題
dr_iris = pd.read_csv(train_path, names=COLUMN_NAMES, header=0)iris = np.array(dr_iris)fig = plt.figure(figsize=(15, 15))fig.suptitle("Anderson's Iris Data Set\n(Bule->Setosa | Red->Versicolor | Green->Virginica)"
)
for i in range(4):for j in range(4):plt.subplot(4, 4, 4 * i + (j + 1))if (i == j):plt.hist(iris[:, j], align='mid')else:plt.scatter(iris[:, j], iris[:, i], c=iris[:, 4], cmap='brg')plt.title(COLUMN_NAMES[j]) # 橫坐標標簽使用子圖標題來實現(xiàn)plt.ylabel(COLUMN_NAMES[i])plt.tight_layout(rect=[0, 0, 1, 0.93])plt.show()
4.實驗小結&討論題
① 實驗過程中遇到了哪些問題,你是如何解決的?
沒有熟悉使用pycharm,詢問了同學。
② 根據(jù)題目二的數(shù)據(jù)進行可視化結果,分析波士頓數(shù)據(jù)集中各個屬性對房價的影響。
占地面積與房價大致呈線性相關。
③ Numpy和Pandas各有什么特點和優(yōu)勢?在應用中應如何選擇?
Pandas擁有Numpy一些沒有的方法,例如describe函數(shù)。其主要區(qū)別是:Numpy就像增強版的List,而Pandas就像列表和字典的合集,Pandas有索引。Pandas有兩種結構,分別是Series和DataFrame。其中Series擁有Numpy的所有功能,可以認為是簡單的一維數(shù)組;而DataFrame是將多個Series按列合并而成的二維數(shù)據(jù)結構,每一列單獨取出來是一個Series。
④ 在題目基本要求的基礎上,你對每個題目做了那些擴展和提升?或者你覺得在編程實現(xiàn)過程中,還有哪些地方可以進行優(yōu)化?
完全按照題目的要求來做的。