Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
wanganlin00 committed Mar 28, 2024
1 parent 4c04bc9 commit e8ed336
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 4 deletions.
3 changes: 3 additions & 0 deletions datascience/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.quarto/
/*cache/
/data/
1 change: 1 addition & 0 deletions datascience/01basic_data_type.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ number-sections: true
code-fold: show
code-link: true
code-tools: true
format: html
---

# 基本数据类型
Expand Down
2 changes: 1 addition & 1 deletion datascience/R-Python.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ py$n
## **Python 与 R 对象相互转换**

| R | Python ||
|------------------|------------------|-----------------------------------|
|------------------------|-------------------|--------------------------------------------------|
| 单元素向量 | 标量Scalar | `1`、 `1L``TRUE``"foo"` |
| 未命名列表或多元素向量 | List | `c(1.0, 2.0, 3.0)``c(1L, 2L, 3L)` |
| 命名列表 | Dict | `list(a = 1L, b = 2.0)``dict(x = x_data)` |
Expand Down
2 changes: 2 additions & 0 deletions datascience/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ book:
- control_structure.qmd
- function.qmd
- class.qmd
- file_RW.qmd
- visualization.qmd



Expand Down
71 changes: 68 additions & 3 deletions datascience/class.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ code-tools: true

面向对象编程(object-oriented programming,OOP):编写表示现实世界中的事物和情景的**类(class)并**基于这些类创建**对象(object)。**在编写类时,你要定义所有对象都具备的通用行为。根据类创建对象称为**实例化****实例(instance)**

## 定义类和方法
## 创建类

特殊方法用行话说叫作***magic method***,如\_\_getitem\_\_***dunder***-getitem,**双下划线方法**

类中的函数称为**方法**`_init_(self,...)` 是一个特殊方法,其中`self` 形参指向实例,必不可少且必须位于首位。其他形参是基于类创建的实例的初始化属性。可通过实例访问的变量称为**属性(attribute)**

Expand Down Expand Up @@ -70,6 +72,69 @@ my_new_car.odometer_reading
my_new_car.update_odometer(24)
```

## 继承inheritance
## 继承类

**inheritance** ,原有的类称为**父类(parent class)****超类(super class)**,新类称为**子类(child class)**

### 子类

```{python}
class ElectricCar(Car): # 括号内指定父类名称
'''电动车'''
def __init__(self,make,model,year):
'''先初始化父类的属性,
再初始化电动车的特有属性'''
super().__init__(make,model,year) # 调用父类的__init__()方法
self.battery_size = 80
def battery_capacity(self):
'''打印电池容量'''
print(f'电池容量为 {self.battery_size} kWh。')
def read_odometer(self): # 重写父类中的方法
"""打印电动车里程"""
print(f"电动车已经行驶了{self.odometer_reading} 千米。")
# 创建对象
my_ECar = ElectricCar('nissan','leaf',2024)
print(my_ECar.get_descriptive_name())
my_ECar.battery_capacity()
my_ECar.read_odometer()
```

### 类的组合(composition)

```{python}
class Battery:
'''电池信息'''
def __init__(self,manufacture="BYD",material="Lithium_plomers",capacity=100):
self.manufacture = manufacture
self.material = material
self.capacity = capacity
def get_descriptive_name(self):
"""返回格式规范的描述性信息"""
long_name = f"{self.manufacture} {self.material} {self.capacity}"
return long_name.title()
def print_battery_capacity(self):
'''打印电池容量'''
print(f'电池容量为 {self.capacity} kWh。')
class ElectricCar2(Car):
'''电动车'''
def __init__(self,make,model,year,):
super().__init__(make,model,year) # 调用父类的__init__()方法
self.battery = Battery()
my_Ecar2 = ElectricCar2("nissan","leaf",2024)
my_Ecar2.battery.print_battery_capacity()
my_Ecar2.battery.get_descriptive_name()
```

## 导入类

```{from Module import class1,class2 as 别名}
from Module import *
```

原有的类称为父类(parent class),新类称为子类(child class)
## Python标准库
73 changes: 73 additions & 0 deletions datascience/file_RW.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
knitr:
opts_chunk:
comment: "#>>>"
collapse: TRUE
number-sections: true
code-fold: show
code-link: true
code-tools: true
---

# 文件处理

## 读取文件

```{python}
f = open("data/pi_digits.txt",mode="r")
contents = f.read()
print(contents)
contents = contents.rstrip()
print(contents)
f.close()
```

```{python}
with open("data/pi_digits.txt",mode="r") as g:
for i in g:
print(i)
```

```{python}
from pathlib import Path
path = Path("data/pi_digits.txt")
f = path.read_text()
f = f.splitlines()
for i in f:
print(i)
```

## 写入文件

```{python}
with open("data/write.txt",mode="a") as w:
w.write("Π是无限不循环小数。")
with open("data/write.txt",mode="a") as w:
w.write("fafhaofhaohfo")
```

## 异常处理

exception

```{python}
def division(dividend,divisor):
answer = dividend/divisor
return(answer)
division(4,2)
division(4,0)

def division_(dividend,divisor):
try:
answer = dividend/divisor
except ZeroDivisionError:
print("You can't divide by 0!")
else:
return(answer)
finally:
print("OK")
division_(4,2)
division_(4,0)
```

## 存储数据
17 changes: 17 additions & 0 deletions datascience/visualization.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: "visualization.qmd"
knitr:
opts_chunk:
comment: "#>>>"
collapse: TRUE
number-sections: true
code-fold: show
code-link: true
code-tools: true
format: html
editor: visual
---

# 数据可视化

Matplotlib

0 comments on commit e8ed336

Please sign in to comment.