我正在尝试与plotly
,特别是ploty express
,构建一些可视化。
我正在构建的一件事是散点图
我在下面有一些代码,它产生了一个很好的散点图:
import plotly.graph_objs as go, pandas as pd, plotly.express as px
df = pd.read_csv('iris.csv')
fig = px.scatter(df, x='sepal_length', y='sepal_width',
color='species', marker_colorscale=px.colors.sequential.Viridis)
fig.show()
Run Code Online (Sandbox Code Playgroud)
但是,我想尝试更改配色方案,即为每个物种呈现的颜色。
我读过了:
但是不能让颜色改变。
试:
fig = px.scatter(df, x='sepal_length', y='sepal_width',
color='species', marker_colorscale=px.colors.sequential.Viridis)
Run Code Online (Sandbox Code Playgroud)
产量:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-78a9d58dce23> in <module>
2 # https://plotly.com/python/line-and-scatter/
3 fig = px.scatter(df, x='sepal_length', y='sepal_width',
----> 4 color='species', marker_colorscale=px.colors.sequential.Viridis)
5 fig.show()
TypeError: scatter() got an unexpected keyword argument 'marker_colorscale'
Run Code Online (Sandbox Code Playgroud)
试
试: …
这是在内部理解如何使用 预测类的概率LightGBM
。
其他包,如sklearn
,为其分类器提供了详尽的细节。例如:
概率估计。
所有类的返回估计值按类的标签排序。
对于 multi_class 问题,如果 multi_class 设置为“多项式”,则使用 softmax 函数来查找每个类的预测概率。否则使用一对一的方法,即使用逻辑函数计算每个类的概率,假设它是正的。并在所有类中标准化这些值。
RandomForest
返回:预测 X 的类别概率。
输入样本的预测类别概率计算为森林中树木的平均预测类别概率。单棵树的类概率是叶子中同一类样本的分数。
还有其他 Stack Overflow 问题提供了更多详细信息,例如:
我正在尝试为 LightGBM 的predict_proba
功能揭示相同的细节。该文档未列出有关如何计算概率的详细信息。
该文件只是说明:
返回每个样本的每个类的预测概率。
源代码如下:
def predict_proba(self, X, raw_score=False, start_iteration=0, num_iteration=None,
pred_leaf=False, pred_contrib=False, **kwargs):
"""Return the predicted probability for each class for each sample.
Parameters
----------
X : array-like or sparse matrix of shape = [n_samples, n_features]
Input features matrix.
raw_score : …
Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法:
但是,我想不出一种方法来遍历一列并获取这些值,并将它们存储在一个数组中。我已经浏览了 Stack Overflow 和谷歌,但没有找到成功的解决方案。
在此先感谢您的帮助。
Sub collectNums()
Dim eNumStorage() As String ' initial storage array to take values
Dim i as Integer
Dim j as Integer
Dim lrow As Integer
lrow = Cells(Rows.Count, "B").End(xlUp).Row ' The amount of stuff in the column
For i = lrow To 2 Step -1
If (Not IsEmpty(Cells(i, 2).Value)) Then ' checks to make sure the value isn't empty
i = eNumStorage ' I know this isn't right
Next i …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个如下所示的Pandas数据框:
| user_name | followers | following | retweets | likes | tweet date | tweet |
|:---------:|:---------:|:---------:|:--------:|:-----:|:-----------:|:------------:|
| user1 | 50 | 100 | 25 | 10 | Oct-1-2019 | lorem ipsum… |
| user1 | 50 | 100 | 25 | 10 | Oct-6-2019 | lorem ipsum… |
| user1 | 50 | 100 | 25 | 10 | Oct-19-2019 | lorem ipsum… |
| user1 | 50 | 100 | 25 | 10 | Oct-4-2019 | lorem …
Run Code Online (Sandbox Code Playgroud) 我期待创建一个通用功能的输出转换decision_scores
的sklearn's IsolationForest
成真概率[0.0, 1.0]
。
我知道并阅读了原始论文,并且我在数学上理解该函数的输出不是概率,而是每个基估计器构建的路径长度的平均值,以隔离异常。
我想将该输出转换为tuple (x,y)
wherex=P(anomaly)
和形式的概率y=1-x
。
def convert_probabilities(predictions, scores):
from sklearn.preprocessing import MinMaxScaler
new_scores = [(1,1) for _ in range(len(scores))]
anomalous_idxs = [i for i in (range(len(predictions))) if predictions[i] == -1]
regular_idxs = [i for i in (range(len(predictions))) if predictions[i] == 1]
anomalous_scores = np.asarray(np.abs([scores[i] for i in anomalous_idxs]))
regular_scores = np.asarray(np.abs([scores[i] for i in regular_idxs]))
scaler = MinMaxScaler()
anomalous_scores_scaled = scaler.fit_transform(anomalous_scores.reshape(-1,1))
regular_scores_scaled = …
Run Code Online (Sandbox Code Playgroud) python machine-learning probability scikit-learn anomaly-detection
我正在尝试利用 MAX() OVER PARTITION BY 函数来评估我公司购买的特定部件的最新收据。下面是去年一些零件的信息示例表:
| VEND_NUM | VEND_NAME | RECEIPT_NUM | RECEIPT_ITEM | RECEIPT_DATE |
|----------|--------------|-------------|----------|--------------|
| 100 | SmallTech | 2001 | 5844HAJ | 11/22/2017 |
| 100 | SmallTech | 3188 | 5521LRO | 12/31/2017 |
| 200 | RealSolution | 5109 | 8715JUI | 05/01/2017 |
| 100 | SmallTech | 3232 | 8715JUI | 11/01/2017 |
| 200 | RealSolution | 2101 | 4715TEN | 01/01/2017 |
Run Code Online (Sandbox Code Playgroud)
如您所见,第三行和第四行显示同一部件号的两个不同供应商。
这是我当前的查询:
WITH
-- various other subqueries …
Run Code Online (Sandbox Code Playgroud) 我正在尝试评估多个评分指标以确定模型性能的最佳参数。即,说:
为了最大化 F1,我应该使用这些参数。为了最大限度地提高精度,我应该使用这些参数。
我正在从这个 sklearn 页面处理以下示例
import numpy as np
from sklearn.datasets import make_hastie_10_2
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import make_scorer
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
X, y = make_hastie_10_2(n_samples=5000, random_state=42)
scoring = {'PRECISION': 'precision', 'F1': 'f1'}
gs = GridSearchCV(DecisionTreeClassifier(random_state=42),
param_grid={'min_samples_split': range(2, 403, 10)},
scoring=scoring, refit='F1', return_train_score=True)
gs.fit(X, y)
best_params = gs.best_params_
best_estimator = gs.best_estimator_
print(best_params)
print(best_estimator)
Run Code Online (Sandbox Code Playgroud)
其产量:
{'min_samples_split': 62}
DecisionTreeClassifier(min_samples_split=62, random_state=42)
Run Code Online (Sandbox Code Playgroud)
但是,我要寻找的是为每个 metric找到这些结果,因此在这种情况下,对于F1和precision
如何才能获得每种评分指标的最佳参数GridSearchCV
?
注意 - 我相信这与我的使用有关 …
python machine-learning scikit-learn cross-validation grid-search
我有两个字典:
length = {1: 3, 2: 9, 3: 1, 4: 1, 5: 0, 6: 0, 7: 5, 8: 0}
result = {1: 3.0, 2: 3.7416573867739413, 3: 7.874007874011811, 4: 6.4031242374328485, 5: 4.0, 6: 0.0, 7: 5.0, 8: 1.0}
Run Code Online (Sandbox Code Playgroud)
当键匹配时,我需要将结果值除以长度值。因此,例如:
Key = 1: 3.0 / 3
Key = 2: 3.7416573867739413 / 9
etc
Run Code Online (Sandbox Code Playgroud)
对于结果中的所有键。我尝试使用以下字典理解:
sims = {k:lengths[k] / v for k, v in result.items()}
Run Code Online (Sandbox Code Playgroud)
但是我得到一个错误 ZeroDivisionError: float division by zero
然后,我尝试避免使用难看的代码来理解,并使用:
for dot_key, dot_val in result.items():
for length_key, length_val in lengths.items():
if …
Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的数据集:
{'prediction': {5: 'c1',
4: 'c1',
3: 'c1',
2: 'c1',
0: 'c1',
1: 'c1',
7: 'c1',
6: 'c1'},
'variable': {5: 'ft1',
4: 'ft2',
3: 'ft3',
2: 'ft4',
0: 'ft5',
1: 'ft6',
7: 'ft7',
6: 'ft8'},
'value': {5: 0.020915912763961077,
4: 0.020388363414781133,
3: 0.007204373035913109,
2: 0.0035298765062560817,
0: -0.002049702058734183,
1: -0.004283512505036808,
7: -0.01882610282871816,
6: -0.022324439779323434}}
Run Code Online (Sandbox Code Playgroud)
我正在尝试制作一个可行的条形图,如下所示:
sns.barplot(data=x, x='value', y='variable',
hue='prediction', orient="h")
Run Code Online (Sandbox Code Playgroud)
但是,我希望条形是红色的。以下:
sns.barplot(data=x, x='value', y='variable',
hue='prediction', color='red', orient="h")
Run Code Online (Sandbox Code Playgroud)
为什么会变成灰色?我也尝试过多种其他颜色,但结果相同。如何将条形变为橙色?
我正在使用dataclass
装饰器。
对于我的一个变量,我希望它是 astr
或int
类型
from dataclasses import dataclass
@dataclass
class Foo:
test_var: str
# test_var: int
def get_example(self):
return type(self.test_var)
Run Code Online (Sandbox Code Playgroud)
当对象被构造时,我希望 fortest_var
是 astr
或 an ;如何为我的类的属性指定两种类型?int
Foo
python ×8
scikit-learn ×3
pandas ×2
probability ×2
dataframe ×1
dictionary ×1
excel ×1
grid-search ×1
json ×1
lightgbm ×1
matplotlib ×1
max ×1
object ×1
oop ×1
oracle ×1
partition-by ×1
plot ×1
plotly ×1
python-3.x ×1
seaborn ×1
sql ×1
tweepy ×1
vba ×1