从C#客户端使用Python脚本(包括绘图和图像)

c# python脚本Download sample project - 711.5 KB 下载示例项目-711.5 KB 介绍 (Introduction)This article presents a class which lets you run Python scripts from a C# client (PythonRunner). The scripts can prod...
摘要由CSDN通过智能技术生成

Sample Image

介绍 (Introduction)

This article presents a class which lets you run Python scripts from a C# client (PythonRunner). The scripts can produce textual output as well as images which will be converted to C# Images. This way, the PythonRunner class gives C# applications not only access to the world of Data Science, Machine Learning, and Artificial Intelligence, but also makes Python's exhaustive libraries for charting, plotting, and data visualization (for e.g., matplotlib and seaborn) available for C#.

本文介绍了一个类,该类使您可以从C#客户端( PythonRunner )运行Python脚本。 这些脚本可以产生文本输出以及图像,这些图像将被转换为C# Image 。 这样, PythonRunner类不仅使C#应用程序可以访问数据科学,机器学习和人工智能领域,而且还使Python的详尽图表,绘图和数据可视化库(例如matplotlibseaborn )可用于C#。

背景 (Background)

一般注意事项 (General Considerations)

I'm a C# developer for more than ten years now - and what can I say: Over the years, I deeply fell in love with this language. It gives me great architectural flexibility, it has large community support, and a wealth of third-party tools (both free and commercial, much of it is open-source) supporting almost every thinkable use case. C# is an all-purpose language and the number one choice for business application development.

我是C#开发人员已有十多年了,我能说什么:多年来,我深深地爱上了这种语言。 它为我提供了极大的体系结构灵活性,它得到了广泛的社区支持,并提供了支持几乎所有可想到的用例的大量第三方工具(包括免费和商业的,其中大部分是开源的)。 C#是一种通用语言,是业务应用程序开发的第一选择。

In the last months, I started to learn Python for Data Science, Machine Learning, Artificial Intelligence, and Data Visualization - mostly because I thought that this skill will push my career as a freelance software developer. I soon realized that Python is great for the above tasks (far better than C# ever will be), but it is completely unsuitable for developing and maintaining large-scale business applications. So the question C# vs. Python (a widely discussed topic in the internet) completely misses the point. C# is for developers who do the job of business-scale application development - Python is for data scientists who do - well - data science, machine learning, and data visualization. There's not much that these two jobs have in common.

在过去的几个月中,我开始学习用于数据科学,机器学习,人工智能和数据可视化的Python-主要是因为我认为该技能将推动我作为自由软件开发人员的职业。 我很快意识到Python非常适合上述任务(远胜过C#),但是它完全不适合开发和维护大型业务应用程序。 因此, C#与Python (在互联网上广泛讨论的话题)这个问题完全没有抓住重点。 C#适用于从事业务规模应用程序开发的开发人员-Python适用于擅长数据科学,机器学习和数据可视化的数据科学家。 这两个工作没有太多共同之处。

The task is not that C# developers additionally become data scientists or ML specialists (or the other way round). It would simply be too much to be an expert in both domains. This is IMHO the main reason why components like Microsoft's ML.NET or the SciSharp STACK won't become widely used. The average C# developer won't become a data scientist, and data scientists won't learn C#. Why would they? They already have a great programming language which is very well suited for their needs, and has a large ecosystem of scientific third-party libraries.

任务不是让C#开发人员另外成为数据科学家或ML专家(反之亦然)。 要成为这两个领域的专家,简直太过分了。 这是恕我直言,微软的ML.NETSciSharp STACK之类的组件不会得到广泛使用的主要原因 。 一般的C#开发人员不会成为数据科学家,数据科学家也不会学习C#。 他们为什么会呢? 他们已经拥有了很好的编程语言,非常适合他们的需求,并且拥有庞大的科学第三方图书馆生态系统。

With these considerations in mind, I started searching for an easier and more 'natural' way to bring the Python world and the C# world together. Here is one possible solution ...

考虑到这些考虑因素,我开始寻找一种更简单,更“自然”的方式来将Python世界和C#世界融合在一起。 这是一种可能的解决方案...

示例应用 (The Example App)

Before we dive into details, a short preliminary note: I wrote this sample app for the sole purpose of demonstrating C#/Python integration, using only some mildly sophisticated Python code. I didn't care too much about the question if the ML code in itself is useful, so please be forgiving in that respect.

在深入探讨之前,有一个简短的初步注释:我编写此示例应用程序的唯一目的是演示C#/ Python集成,仅使用一些稍微复杂的Python代码。 我不太关心ML代码本身是否有用的问题,因此请在这方面原谅。

Having that said, let me shortly describe the sample application. Basically, it:

话虽如此,让我简短地描述示例应用程序。 基本上,它:

  1. gives you a list of stocks that you can select from (6-30),

    提供您可以选择的股票清单(6-30),
  2. draws a summary line chart of the (normalized) monthly stock prices,

    绘制(标准化的)每月股票价格的摘要折线图,
  3. performs a so-called 'k-Means Clustering Analysis' based on their price movements and shows the results in a treeview.

    根据价格走势执行所谓的“ k均值聚类分析”,并在treeview显示结果。

Let's shortly step through the three parts of the application one by one...

让我们简短地一步一步地介绍应用程序的三个部分...

选股 (Stock Selection)

The DataGrid on the left side of the application's window presents you a list of available stocks that you can select from. You need at least six items to be selected before further action is possible (the maximum number of selected stocks is 30). You may use the controls on the top to filter the list. Also, the list is sortable by clicking the column headers. The Check Random Sample button randomly selects 18 stocks from the list.

应用程序窗口左侧的DataGrid为您提供了可供选择的可用库存列表。 您需要至少选择六个项目,然后才能采取进一步措施(所选库存的最大数量为30)。 您可以使用顶部的控件来过滤列表。 此外,可通过单击列标题对列表进行排序。 “ 检查随机样本”按钮从列表中随机选择18只股票。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值