✿ import
import numpy as np
import pandas as pd
import seaborn as sns
import datetime
import os
import scipy.signal as signal
import warnings
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.signal as signal # 中值滤波
#%matplotlib inline
# %config InlineBackend.figure_format = 'svg'#使图高清 报错原因:可能是pycharm画出来的本来就是矢量图
pd.set_option('display.max_rows', None) #显示所有的行
pd.set_option('display.max_columns', None) #显示所有的列
pd.set_option('max_colwidth',100) # #设置value的显示长度为100,默认为50
warnings.filterwarnings("ignore")
plt.rcParams['font.sans-serif']=['SimHei'] # 显示中文
plt.rcParams['axes.unicode_minus']=False # 解决坐标轴不显示负号
# 在接下来的cell中所有单独的变量都会被直接输出 - jupyter-lab
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
# 线性回归
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import r2_score
✿ 模型拟合
# 线性回归
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import r2_score
def model(x, y):
def mape(y_true, y_pred):
return np.mean(np.abs((y_pred - y_true) / y_true)) * 100
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=3)
model = LinearRegression().fit(x_train, y_train)
y_pred = model.predict(x_test)
y_test = np.array(y_test)
print("截距为{},系数为{}。".format(model.intercept_, model.coef_))
print("模型R2得分为{}".format(model.score(x_test, y_test)))
print("模型平均绝对误差(MAPE)得分为{}".format(mape(y_test, y_pred)))
return
✿ 创建头
def create_df_head(feature):
'''
创建头
:param feature:
:return:
'''
df = pd.DataFrame(columns=feature) # 创建总的df的头
df.loc[0] = [1,1,1,2] # 一行行增加
return df
feature = ["gender", "age", "height", "weight", "BMI"]
data = [[1,2,3,4,5],[10,11,12,13,14]]
df = pd.DataFrame(data,columns=feature)
df
✿ 中值滤波
def median_filter(lst):
'''
对一个list进行中值滤波
:param lst:
:return:
'''
if len(lst) <=2:
return lst
lst = list(signal.medfilt(lst)) # 返回的是array所以需要list
return lst
✿ 求TP TN FP FN
sklearn.metrics.confusion_matrix文档
y_test = array([1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0,
1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1,
1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0,
1, 1, 0, 1])
pred = array([1., 0., 0., 0., 0., 1., 0., 0., 0., 1., 1., 0., 0., 1., 1., 1., 1.,
1., 1., 1., 0., 0., 1., 0., 1., 1., 1., 0., 1., 1., 0., 1., 1., 0.,
0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 1., 1., 1., 1., 0., 1., 1.,
1., 0., 1., 0., 1., 1., 1., 1., 1., 1., 1., 0., 1., 0., 0., 1., 1.,
0., 1., 0., 1., 1., 0., 1., 1., 0., 1., 1., 0., 1., 1., 1., 1., 1.,
1., 1., 0., 1., 1., 0., 0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 1.,
1., 0., 0., 1., 1., 1., 1., 0., 1., 1., 0., 1.])
# 方法1:
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, pred)
array([[37, 4],
[ 3, 70]], dtype=int64)
tn, fp, fn, tp = confusion_matrix(y_test, pred).ravel()
tn, fp, fn, tp =
(37, 4, 3, 70)
# 方法2:
def perf_measure(y_actual, y_hat):
TP = 0
FP = 0
TN = 0
FN = 0
for i in range(len(y_hat)):
if y_actual[i]==y_hat[i]==1:
TP += 1
if y_hat[i]==1 and y_actual[i]!=y_hat[i]:
FP += 1
if y_actual[i]==y_hat[i]==0:
TN += 1
if y_hat[i]==0 and y_actual[i]!=y_hat[i]:
FN += 1
return(TP, FP, TN, FN)
TP, FP, TN, FN = perf_measure(y_test, pred)
# (70, 4, 37, 3)
✿ 获取python 项目目录结构
参考链接:使用 Python 生成文件夹目录结构
替换下面direction_path
即可:
import re
from pathlib import Path
from pathlib import WindowsPath
from typing import Optional, List
class DirectionTree:
def __init__(self,
direction_name: str = 'WorkingDirection',
direction_path: str = '.',
ignore_list: Optional[List[str]] = None):
self.owner: WindowsPath = Path(direction_path)
self.tree: str = direction_name + '/\n'
self.ignore_list = ignore_list
if ignore_list is None:
self.ignore_list = []
self.direction_ergodic(path_object=self.owner, n=0)
def tree_add(self, path_object: WindowsPath, n=0, last=False):
if n > 0:
if last:
self.tree += '│' + (' │' * (n - 1)) + ' └────' + path_object.name
else:
self.tree += '│' + (' │' * (n - 1)) + ' ├────' + path_object.name
else:
if last:
self.tree += '└' + ('──' * 2) + path_object.name
else:
self.tree += '├' + ('──' * 2) + path_object.name
if path_object.is_file():
self.tree += '\n'
return False
elif path_object.is_dir():
self.tree += '/\n'
return True
def filter_file(self, file):
for item in self.ignore_list:
if re.fullmatch(item, file.name):
return False
return True
def direction_ergodic(self, path_object: WindowsPath, n=0):
dir_file: list = list(path_object.iterdir())
dir_file.sort(key=lambda x: x.name.lower())
dir_file = [f for f in filter(self.filter_file, dir_file)]
for i, item in enumerate(dir_file):
if i + 1 == len(dir_file):
if self.tree_add(item, n, last=True):
self.direction_ergodic(item, n + 1)
else:
if self.tree_add(item, n, last=False):
self.direction_ergodic(item, n + 1)
if __name__ == '__main__':
i_l = [
'\.git', '__pycache__', 'test.+', 'venv', '.+\.whl', '\.idea', '.+\.jpg', '.+\.png',
'image', 'css', 'admin', 'tool.py', 'db.sqlite3'
]
tree = DirectionTree(ignore_list=i_l, direction_path='/your_project_path/')
print(tree.tree)
结果类似于:
├────.gitattributes
├────.gitignore
├────app/
│ ├────__init__.py
│ ├────admin.py
│ ├────apps.py
│ ├────migrations/
├────README.md
├────requirements.txt
├────static/
├────templates/
└────uwsgi_params
✿ python项目描述
'''
_oo0oo_
o8888888o
88" . "88
(| -_- |)
0\ = /0
___/`---'\___
.' \\| |// '.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' ___/-. /
___'. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' >' "".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `_. \_ __\ /__ _/ .-` / /
=====`-.____`.___ \_____/___.-`___.-'=====
`=---='
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
佛祖保佑 永不宕机 永无BUG
'''
'''
┏┓ ┏┓+ +
┏┛┻━━━┛┻┓ + +
┃ ┃
┃ ━ ┃ ++ + + +
████━████ ┃+
┃ ┃ +
┃ ┻ ┃
┃ ┃ + +
┗━┓ ┏━┛
┃ ┃
┃ ┃ + + + +
┃ ┃
┃ ┃ + 神兽保佑
┃ ┃ 代码无bug
┃ ┃ +
┃ ┗━━━┓ + +
┃ ┣┓
┃ ┏┛
┗┓┓┏━┳┓┏┛ + + + +
┃┫┫ ┃┫┫
┗┻┛ ┗┻┛+ + + +
'''
'''
┌───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
│Esc│ │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│ ┌┐ ┌┐ ┌┐
└───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘ └┘ └┘ └┘
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
│~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
│ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │ │
├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
│ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter │ │ 4 │ 5 │ 6 │ │
├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤ ┌───┐ ├───┼───┼───┼───┤
│ Shift │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│ Shift │ │ ↑ │ │ 1 │ 2 │ 3 │ │
├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
│ Ctrl│ │Alt │ Space │ Alt│ │ │Ctrl│ │ ← │ ↓ │ → │ │ 0 │ . │←─┘│
└─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
'''
✿ 去标点/中文/数字
def re_str(desstr):
"""
function:处理字符串只留下英文
input: 字符串
output: 字符串
"""
# 去除网址
results = re.compile(r'[http|https]*://[a-zA-Z0-9.?/&=:]*', re.S)
desstr = re.sub(results, '', desstr)
s = desstr.replace(r"\n", " ") # 去\n
s = s.replace("\n", " ") # 去\n
s = re.sub('[{}]'.format(all_str),"",s) # 去中英文标点
s = re.sub('[\u4e00-\u9fa5]','',s) # 去中文
s = re.sub('[\d]','',s) # 去数字
s = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","",s) # 去表情
return s
测试
python | 字符串去除(中文、英文、数字、标点符号)
python正则过滤字母、数字及特殊字符
s = 'SMS charged at N 4.00.Main Bal: N175.20.Dial *123*1# for bonus bal. Call ¡ 11k/s flat rate; N7 first call fee applies. Dial *315# to join'
s = '(FastCent)Dear, AWONGE\nThis is our official offline repayment A/C:\nA/C Name:MONNIFY/Fast Pay-RES\nBank:Moniepoint Microfinance Bank\nA/C:6199529255\nPlease contact us after transfer'
s = "无I hope it's not dead people that FUNDY has been speaking to since. What's all this nonsense. Do u think we were employed to talk to U and u look away. Don't let me help you to design your obituary poster with your picture before u even die oo. Make your payment now or its your obituary poster that will be trending soon. I don't care about what u are passing through. Make your payment now. Don't push me oo. Abi u think say u snap picture for fun. FUNDY does not care about your situation oo. This is not a charity organization."
s = 'etc❌❌ scammed ‼️‼️ appreciated manageme chasing 😎😎 easeloan✅'
print(r"去\n:")
s = s.replace(r"\n", " ")
s = s.replace("\n", " ")
s
print(r"去符号:")
s = re.sub('[{}]'.format(all_str),"",s)
s
print(r"中文:")
s = re.sub('[\u4e00-\u9fa5]','',s)
s
print(r"去数字:")
s = re.sub('[\d]','',s)
s