1. 程式人生 > >高階程式設計技術 資料處理課後作業

高階程式設計技術 資料處理課後作業

給定一個csv檔案,完成:

Part 1: For each of the four datasets:

Compute the mean and the variance of both x and y

Compute the correlation coefficient between x and y

Compute the linear regression line: y = β0 + β1x + ε

Part 2: Use seaborn to visualize all four datasets.

import random
import numpy as np
import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import statsmodels.api as sm
import statsmodels.formula.api as smf

anascombe = pd.read_csv('anscombe.csv')

print(anascombe.groupby('dataset')['x'].mean())
print(anascombe.groupby('dataset')['y'].mean())
print(anascombe.groupby('dataset')['x'].var())
print(anascombe.groupby('dataset')['y'].var())
print(anascombe.groupby('dataset').corr())

dataset_names = ['I', 'II', 'III', 'IV']
for i in dataset_names:
    n = len(anascombe[anascombe.dataset == i])
    is_train = np.random.rand(n) < 0.7
    train = anascombe[anascombe.dataset == i][is_train].reset_index(drop=True)
    test = anascombe[anascombe.dataset == i][~is_train].reset_index(drop=True)
    
    lin_model = smf.ols('y ~ x', train).fit()
    print(lin_model.summary())

g = sns.FacetGrid(anascombe, col='dataset')
g.map(plt.scatter, 'x', 'y')
plt.show()

最終輸出如下:


相關推薦

高階程式設計技術 資料處理課後作業

給定一個csv檔案,完成:Part 1: For each of the four datasets:Compute the mean and the variance of both x and yCompute the correlation coefficient be

高階程式設計技術(Python)作業11

11-1 城市和國家:編寫一個函式,它接受兩個形參:一個城市名和一個國家名。這個函式返回一個格式為City, Country的字串,如Santiago, Chile 。將這個函式儲存在一個名為city_functions.py的模組中。建立一個名為test_ci

高階程式設計技術(Python)作業4

4-9 立方解析:使用列表解析生成一個列表,其中包含前10個整數的立方。 Solution: cubes = [num ** 3 for num in range(1, 11)] for cube in cubes: print(cube, en

高階程式設計技術(Python)作業8

8-8 使用者的專輯:在為完成練習8-7編寫的程式中,編寫一個while 迴圈,讓使用者輸入一個專輯的歌手和名稱。獲取這些資訊後,使用它們來呼叫函式make_album(),並將建立的字典打印出來。在這個while 迴圈中,務必要提供退出途徑。 Solutio

高階程式設計技術(Python)作業3

3-7 縮減名單:你剛得知新購買的餐桌無法及時送達,因此只能邀請兩位嘉賓。 - 以完成練習3-6時編寫的程式為基礎,在程式末尾新增一行程式碼,列印一條你只能邀請兩位嘉賓共進晚餐的訊息。 - 使用pop() 不斷地刪除名單中的嘉賓,直到只有兩位嘉賓為止。每

高階程式設計技術 課後作業十四(第9周第2次)

題目來源:題目大意:    給定一個用非負數填充的m * n維方格,每個數字表示從當前方格移動到下一方格的距離,找到一條從左上角到右下角的最短路徑,使其路徑和最短。每次移動只能向右或向下。解題思路:    因為只能向右或向下移動,所以題目簡單很多,步驟如下:(1)對第一行方格

高階程式設計技術 numpy課後作業

Exercise 9:NumpyGenerate matrices A, with random Gaussian entries, B, a Toeplitz matrix,where A∈Rn×m and B∈Rm×m, for n = 200, m = 500.impo

高階程式設計技術課後作業 第七章練習

7-3 10的整數倍程式碼:num = int(input("請輸入一個數字:")) if num % 10 == 0: print("這個數字是10的整數倍") else: print("這個數字不是10的整數倍")結果:請輸入一個數字:100這個數字是10

高階程式設計技術 課後作業九(第5周)

9-2 三家餐館 :建立一個名為Restaurant 的類,其方法__init__() 設定兩個屬性:restaurant_name 和cuisine_type 。建立一個名為describe_restaurant() 的方法和一個名為open_restaurant() 的方

高階程式設計技術_課後作業(十四)

Numpy下面為作業文件中Numpy這一章的內容題目解答Exercise 9.1高斯隨機分佈矩陣的生成:numpy.random.normal(loc=0.0, scale=1.0, size=None

高階程式設計技術課後作業 第九章練習

9-1 餐館程式碼:class Restaurant(): def __init__(self, name, type): self.restaurant_name = name self.cuisine_type = type

高階程式設計技術課後作業 第四章練習

4-2 動物程式碼:animals = ['dog', 'cat', 'squirrel'] for animal in animals: print("A " + animal + " would make a great pet.") print("Any of

高階程式設計技術課後作業 第十章練習

10-1 python學習筆記程式碼:with open('input.txt') as input: print(input.read()) with open('input.txt') as input: file_in = input prin

java高階程式設計技術和API:異常處理<1>

1.關於java的異常處理首先會提到Throwable這個頂層父類,因為Exception和Error都是繼承自throwable。 Error一般是因為jvm本身的錯誤。error是說程式設計師不能通過程式碼去解決,一般很少出現。我們應該更多的去關注excep

高階程式設計技術作業_7

pets = { 'dog': { 'Momiji': 'Aya', 'Aun': 'Reimu', 'Kyouko': 'Hijiri', }, 'cat': { 'Orin': 'Satori',

高階程式設計技術 Python 第八週作業

Maximum Subarray (#53)來源:https://leetcode.com/problems/maximum-subarray/description/題意:給定一個整數陣列 nums ,找到一個具有最大和的連續子陣列(子陣列最少包含一個元素),返回其最大和。

高階程式設計技術】第12周作業

import numpy as np import matplotlib.pyplot as plt import scipy.stats as sts #exercise 1 f, ax = plt.subplots(1, 1, figsize=(5,4)) x=np

高階程式設計技術】第15周作業

from sklearn import datasets, cross_validation from sklearn.naive_bayes import GaussianNB from sklearn.svm import SVC from sklearn.ensembl

高階程式設計技術 Python 第九周作業

Move Zeroes (#283)來源:https://leetcode.com/problems/move-zeroes/description/題意:給定一個數組 nums,編寫一個函式將所有 0 移動到陣列的末尾,同時保持非零元素的相對順序。示例:輸入: [0,1,0

第6周作業 #高階程式設計技術

第11章 11-1 城市和國家 編寫一個函式,它接受兩個形參:一個城市名和一個國家名。這個函式返回一個格式為City, Country 的字串,如Santiago, Chile 。將這個函式儲存在一個名為city_functions.py的模組