我想预训练一个模型,然后用另一个模型训练它。
我有模型Decision Tree Classifer
,然后我想用 model 进一步训练它LGBM Classifier
。在 scikit learn 中是否有可能做到这一点?我已经读过这篇关于它的文章https://datascience.stackexchange.com/questions/28512/train-new-data-to-pre-trained-model。。帖子里说
根据官方文档,多次调用 fit() 将会覆盖之前的 fit() 学到的内容
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
# Train Decision Tree Classifer
clf = DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
lgbm = lgb.LGBMClassifier()
lgbm = lgbm.fit(X_train,y_train)
#Predict the response for test dataset
y_pred = lgbm.predict(X_test)
Run Code Online (Sandbox Code Playgroud) 我有个问题。我有一个巨大的dict
. 我想保存并加载这个巨大的字典。但不幸的是我得到了一个MemoryError
. 字典不应该太大。从数据库中读取的内容约为 4GB。我现在想保存这个字典并读出它。但是,它应该是高效的(不会消耗更多内存)并且不会花费太长时间。
目前有哪些选择?我无法进一步了解pickle
,出现内存错误。我还剩 200GB 可用磁盘空间。
我查看了用 Python 保存和加载大型字典的最快方法以及其他一些问题和博客。
import pickle
from pathlib import Path
def save_file_as_pickle(file, filename, path=os.path.join(os.getcwd(), 'dict')):
Path(path).mkdir(parents=True, exist_ok=True)
pickle.dump( file, open( os.path.join(path, str(filename+'.pickle')), "wb" ))
save_file_as_pickle(dict, "dict")
[OUT]
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<timed eval> in <module>
~\AppData\Local\Temp/ipykernel_1532/54965140.py in save_file_as_pickle(file, filename, path)
1 def save_file_as_pickle(file, filename, path=os.path.join(os.getcwd(), 'dict')):
2 Path(path).mkdir(parents=True, exist_ok=True)
----> 3 pickle.dump( file, open( os.path.join(path, str(filename+'.pickle')), "wb" ))
MemoryError:
Run Code Online (Sandbox Code Playgroud)
什么有效,但花了 1 小时并且使用了 26GB …
我有个问题。我正在合作k-means
并希望找到最佳的集群。不幸的是,我的数据集太大,无法应用silhouette
。是否可以选择调整此代码并将 替换silhouette
为Inertia
?
多维控制器
from sklearn.cluster import KMeans
import numpy as np
from sklearn.metrics import silhouette_score
import matplotlib as mpl
import matplotlib.pyplot as plt
X = np.array([[1, 2], [1, 4], [1, 0],
[10, 2], [10, 4], [10, 0],
[10, 2], [10, 4], [10, 0],
[1, 2], [1, 4], [1, 0],
[10, 2], [10, 4], [10, 0],
[10, 2], [10, 4], [10, 0],
[1, 2], [1, 4], [1, 0],
[10, 2], [10, 4], [10, …
Run Code Online (Sandbox Code Playgroud) python cluster-analysis machine-learning k-means unsupervised-learning
我有个问题。我想创建一个带有热图的流程。查看每个步骤花费了多长时间。我创建了该流程并为各个步骤PyDot
创建了一个。dataframe
如何为我的流程创建热图?
计算还应包括每一步的时间。因此,您可以计算边缘时间,例如task1_start - start
/task2_start - task1_end
并且您可以计算节点时间,例如task1_end - task1_start
/ task2_end - task2_start
。
我的 MVP 只改变边框的颜色。但我想创建一个真正的热图。
过程
import pydot
from IPython.display import SVG
graph = pydot.Dot(graph_type='digraph')
task_node1 = pydot.Node("Task1", shape="box",)
task_node2 = pydot.Node("Task2", shape="box",)
graph.add_node(task_node1)
graph.add_node(task_node2)
task1_to_task2_edge = pydot.Edge("Task1", "Task2",)
graph.add_edge(task1_to_task2_edge)
graph.write_svg("diagram.svg")
SVG('diagram.svg')
Run Code Online (Sandbox Code Playgroud)
数据框
id step timestamp
0 1 task1_start 2023-01-01
1 1 task1_End 2023-01-05
2 1 task2_start 2023-01-10
3 1 task2_end 2023-01-12
4 2 task1_start 2023-01-01
5 2 task1_End 2023-01-05 …
Run Code Online (Sandbox Code Playgroud) 我有个问题。我正在使用ArangoDB enterprise:3.8.6
通过Docker
. 但不幸的是我的查询花费的时间比30s
. 当失败时,错误是arangodb HTTPConnectionPool(host='127.0.0.1', port=8529): Read timed out. (read timeout=60)
。
我怎样才能获得包含所有文档的完整集合而不出现任何错误?
Python代码(在我的机器上本地运行)
from arango import ArangoClient
# Initialize the ArangoDB client.
client = ArangoClient()
# Connect to database as user.
db = client.db(<db>, username=<username>, password=<password>)
cursor = db.aql.execute(f'FOR doc IN students RETURN doc', batch_size=10000)
result = [doc for doc in cursor]
print(result[0])
[OUT]
arangodb HTTPConnectionPool(host='127.0.0.1', port=8529): Read timed out. (read timeout=60)
Run Code Online (Sandbox Code Playgroud)
ArangoDB …
我有个问题。我在这些列表中有一个列表myList
,还有一个字典。我想计算该字段是否dataOriginSystem
为空或不存在。不幸的是我得到了错误的结果。if(key_nested == 'dataOriginSystem'): ... else: count =+ 1
原因在于 if 查询。既然我问,这个领域存在吗?如果不是,则将其累加,由于我循环遍历所有嵌套键,因此错误之一就在这里。另外,有没有办法提高效率?
如何查询有多少字段dataOriginSystem
为空或不存在?
count = 0
for element in myList:
for key in element.keys():
if(key == 'metaData'):
for key_nested in element[key].keys():
if(key_nested == 'dataOriginSystem'):
if(key_nested == None):
count += 1
else:
count += 1
print(count)
Run Code Online (Sandbox Code Playgroud)
myList = [
{'_id': 'orders/213123',
'contactEditor': {'name': 'Max Power',
'phone': '1234567',
'email': 'max@power.com'},
'contactSoldToParty': {'name': 'Max Not',
'phone': '123456789',
'email': 'maxnot@power.com'},
'isCompleteDelivery': False,
'metaData': {'dataOriginSystem': 'Goods',
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': …
Run Code Online (Sandbox Code Playgroud) 我有个问题。我想使用StandardScaler()
,但我的数据集包含某些值和其他不应缩放的OneHotEncoding
值。但如果我正在运行,所有值都会缩放。那么是否可以选择仅对管道内的某些值运行此方法?StandardScaler()
我发现了这个问题:使用以下代码对分类变量进行 One-Hot-Encode 并同时缩放连续变量
columns = ['rank']
columns_to_scale = ['gre', 'gpa']
scaler = StandardScaler()
ohe = OneHotEncoder(sparse=False)
# Concatenate (Column-Bind) Processed Columns Back Together
processed_data = np.concatenate([scaled_columns, encoded_columns], axis=1)
Run Code Online (Sandbox Code Playgroud)
那么是否有一个选项可以仅在某些值上运行StandardScaler()
内部 a pipeline
,而其他值应该合并到缩放值中?因此管道应该只对值使用StandardScaler 'xy', 'xyz'
。
标准定标器类
from sklearn.base import BaseEstimator, TransformerMixin
class StandardScaler_with_certain_features(BaseEstimator, TransformerMixin):
def __init__(self, columns_to_scale):
scaler = StandardScaler()
def fit(self, X, y = None):
scaler.fit(X_train) # only std.fit on train set
X_train_nor = scaler.transform(X_train.values)
def transform(self, X, …
Run Code Online (Sandbox Code Playgroud) 我在 Node.js 中编写了一个小脚本,该脚本调用 Python 文件并拦截 Python 文件的输出。我首先使用 构建 Dockerfile,docker build -t backend_airbnb .
然后使用 运行 docker compose docker compose up -d
。之后我检查容器是否正在运行,但它直接关闭,没有错误消息。它只是说backend_airbnb exited with code 0
。
如何构建一个多阶段 Dockerfile,首先安装 python 要求,然后安装 Node(反之亦然)并运行npm start
?这样我就可以在 POST 请求进入时执行我的 Python 文件。
文件夹结构
|-- app.js
|-- requriments.txt
|-- test.js
|-- routes
|-- |-- model.py
|-- |-- post_price.js
Run Code Online (Sandbox Code Playgroud)
Dockerfile
FROM python:3.6.8
#RUN mkdir -p /usr/src/app
COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
FROM node:14
WORKDIR /opt/app
COPY package*.json ./ …
Run Code Online (Sandbox Code Playgroud) python ×8
dictionary ×2
docker ×2
scikit-learn ×2
arangodb ×1
dockerfile ×1
k-means ×1
list ×1
model ×1
node.js ×1
pandas ×1
pickle ×1
pipeline ×1
pydot ×1
svg ×1