reinforcement

از kaggle

Connect Four is a game where two players alternate turns dropping colored discs into a vertical grid. Each player uses a different color (usually red or yellow), and the objective of the game is to be the first player to get four discs in a row.

Connect Four یک بازی است که در آن دو بازیکن به طور متناوب دیسک های رنگی را به یک شبکه عمودی تبدیل می کنند. هر بازیکن از رنگ متفاوتی (معمولا قرمز یا زرد) استفاده می کند و هدف از بازی این است که اولین بازیکنی باشد که چهار دیسک پشت سر هم به دست می آورد.

In this course, you will build your own intelligent agents to play the game.

  • In the first lesson, you’ll learn how to set up the game environment and create your first agent.
  • نحوه ایجاد محیط بازی و first Agent

  • The next two lessons focus on traditional methods for building game AI.
  • تمرکز بر روی روش های قدیمی ایجاد بازی
  • These agents will be smart enough to defeat many novice players!

بازی به حدی قوی است که میتواند از تازه کار ببرد.

  • In the final lesson, you’ll experiment with cutting-edge algorithms from the field of reinforcement learning.
  • مفاد درس آخر
  • The agents that you build will come up with gameplay strategies much like humans do:
  • gradually, and with experience.

Join the competition

Throughout the course, you’ll test your agents’ performance by competing against agents that other users have created.

در دوره شما با agent رقابت میکنید که یوزر دیگر ایجاد کرده است.

To join the competition, open a new window with the competition page, and click on the “Join Competition” button.

انجام شد.

(If you see a “Submit Agent” button instead of a “Join Competition” button, you have already joined the competition, and don’t need to do so again.)

This takes you to the rules acceptance page.

صفحه قوانین

You must accept the competition rules in order to participate. These rules govern how many submissions you can make per day, the maximum team size, and other competition-specific details.

Then, click on “I Understand and Accept” to indicate that you will abide by the competition rules.

انجام شد.

Introduction

Connect Four is a game where two players alternate turns dropping colored discs into a vertical grid. Each player uses a different color (usually red or yellow), and the objective of the game is to be the first player to get four discs in a row.

In this course, you will build your own intelligent agents to play the game.

  • In the first lesson, you’ll learn how to set up the game environment and create your first agent.
  • The next two lessons focus on traditional methods for building game AI. These agents will be smart enough to defeat many novice players!
  • In the final lesson, you’ll experiment with cutting-edge algorithms from the field of reinforcement learning. The agents that you build will come up with gameplay strategies much like humans do: gradually, and with experience.

Join the competition

Throughout the course, you’ll test your agents’ performance by competing against agents that other users have created.

To join the competition, open a new window with the competition page, and click on the “Join Competition” button. (If you see a “Submit Agent” button instead of a “Join Competition” button, you have already joined the competition, and don’t need to do so again.)

This takes you to the rules acceptance page. You must accept the competition rules in order to participate. These rules govern how many submissions you can make per day, the maximum team size, and other competition-specific details. Then, click on “I Understand and Accept” to indicate that you will abide by the competition rules.

Getting started

The game environment comes equipped with agents that have already been implemented for you.

To see a list of these default agents, run:

انجام شد.

The "random" agent selects (uniformly) at random from the set of valid moves.

In Connect Four, a move is considered valid if there’s still space in the column to place a disc (i.e., if the board has seven rows, the column has fewer than seven discs).

در این بازی یک حرکت زمانی معتبر است که هنوز جایی برای گذاشتن دیسک وجود داشته باشد. برای مثال اگر صفحه دارای 7 ردیف باشد، ستون دارای کمتر از 7 دیسک باشد.

In the code cell below, this agent plays one game round against a copy of itself.

در کد زیر یک بازی در مقابل خودش انجام میشود.

کد جرا شد

You can use the player above to view the game in detail: every move is captured and can be replayed. Try this now!

As you’ll soon see, this information will prove incredibly useful for brainstorming ways to improve our agents.

Defining agents

To participate in the competition, you’ll create your own agents.

برای شرکت در مسابقه باید agent خود را بسازید.

Your agent should be implemented as a Python function that accepts two arguments: obs and config.

agent شما یک تابع پایتون دارد که دو آرگومان میگیرد: obs و config

It returns an integer with the selected column, where indexing starts at zero. So, the returned value is one of 0-6, inclusive.

چیزی که این تابع برمیگرداند، یک عدد صحیح و یک ستون انتخاب شده است. که index آن از صفر شروع میشود. بنابراین عدد برگردانده شده عددی بین 0 تا 6 است. (که شش را هم شامل میشود.)

We’ll start with a few examples, to provide some context. In the code cell below:

با چند مثال شروع میکنیم

  • The first agent behaves identically to the "random" agent above.
    • عامل اول دقیقا مانند عامل رندوم بالا عمل میکند
  • The second agent always selects the middle column, whether it’s valid or not!
  • عامل دوم ، همیشه ستون وسط را انتخاب میکند، فارق از اینکه درست باشد یا غلط
  • Note that if any agent selects an invalid move, it loses the game.
  • اگر عامل حرکت اشتباهی را انتخاب کند، بازی را میبازد.
  • The third agent selects the leftmost valid column.
  • عامل سوم،چپ ترین ستون معتبر را انتخاب میکند.

کد مربوطه ران شد.

So, what are obs and config, exactly?

این دو مورد دقیقا چی هستند؟

obs

obs contains two pieces of information:

این مورد دارای دو اطلاعات مهم است

  • obs.board – the game board (a Python list with one item for each grid location)

صفحه بازی- لیست پایتونی که هر آیتم آن مربوط به یک نقطه از شبکه است.

مثلا در مورد زیر6 ردیف و 7 ستون داریم ، پس میشود 42 تا لوکیشن.

  • obs.mark – the piece assigned to the agent (either 1 or 2)

مهره ای که در هر لوکیشن موجود است. 1 برای یک تیم و 2 برای تیم حریف

obs.board is a Python list that shows the locations of the discs, where the first row appears first, followed by the second row, and so on.

We use 1 to track player 1’s discs, and 2 to track player 2’s discs. For instance, for this game board:

در زیر یک نمونه مشاهده میشود.

obs.board would be [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 2, 1, 2, 0, 2, 0]

config

config contains three pieces of information:

سه داده مهم وجود دارد

  • config.columns – number of columns in the game board (7 for Connect Four)

تعداد ستون ها در بازی که 7 تاست

  • config.rows – number of rows in the game board (6 for Connect Four)
  • تعداد سطرها که شش تاست
  • config.inarow – number of pieces a player needs to get in a row in order to win (4 for Connect Four)
  • تعداد مهرهای مورد نیازیک تیم که اگر ردیف شوند، برنده خواهد شد. که در این بازی 4 تاست.

Take the time now to investigate the three agents we’ve defined above.

Make sure that the code makes sense to you!

حالا یکبار دیگر کد را بررسی کنید و مطمئن شوید که آن را فهمیده اید.

Evaluating agents

To have the custom agents play one game round, we use the same env.run() method as before.

The outcome of a single game is usually not enough information to figure out how well our agents are likely to perform.

To get a better idea, we’ll calculate the win percentages for each agent, averaged over multiple games.

For fairness, each agent goes first half of the time.

To do this, we’ll use the get_win_percentages() function (defined in a hidden code cell). To view the details of this function, click on the “Code” button below.

تسک bit and

تسک بعد از مصاحبه با hr به شرح زیر است:

1- تفاوت بین شی و موجودیت

An entity is something that exists in itself, actually or potentially, concretely or abstractly, physically or not. It needs not be of material existence. In computer science, an object is a location in memory having a value and possibly referenced by an identifier.

موجودیت چیزی است که به خودی خود، بالفعل یا بالقوه، به طور ملموس یا انتزاعی، به صورت فیزیکی یا غیر فیزیکی وجود دارد. لازم نیست از وجود مادی باشد. در علم کامپیوتر، یک شی مکانی در حافظه است که دارای یک مقدار است و احتمالاً توسط یک شناسه به آن ارجاع می شود.

2- what are the entity and object in software engineering

موجودیت ها اشیایی هستند که داده های سیستم را نشان می دهند: مشتری، تراکنش، سبد خرید و غیره. مرزها اشیایی هستند که با بازیگران سیستم ارتباط برقرار می کنند: رابط های کاربر، دروازه ها، پراکسی ها، و غیره. کنترل کننده ها اشیایی هستند که میان مرزها و موجودیت ها واسطه می شوند. آنها اجرای دستوراتی را که از مرز می آید هماهنگ می کنند.

Entities are objects representing system data: Customer, Transaction, Cart, etc. Boundaries are objects that interface with system actors: user interfaces, gateways, proxies, etc. Controllers are objects that mediate between boundaries and entities. They orchestrate the execution of commands coming from the boundary.

3- درک بیشتر: لینک

A database is a organized collection of logical related data stored in a software named Database Management Systems (DBMS).

دیتابیس مجموعه ای از روابط منطقی بین داده هاست که در یک نرم افزار به نام DBMS قرار گرفته اند.

Before inserting the data into the system, it needs to be in the form of a model for appropriate understanding of the database.

قبل از لود دیتا در دیتابیس باید به فرم مدل مناسب تغییر کند

Many models were introduced and Entity-Relationship model is one among them.

مدلهای متنوعی معرفی شده اند که entity relation ship model یکی از آنهاست.

It is a graphical representation of the data which gives the whole view of the data.

نمایش گرافیکی داده ها که ویوی کل داده ها را شامل میشود.

There are basic building units of this E-R model where entity and object are among them.

حال المان های اساسی در این مدل E-R وجود دارند که object و entity دو نمونه از آن هستند.

تعریف دارایی و ویژگی آن:
– متفاوت بودن از سایرین
– با توجه به ویژگی هایش تعریف میشود.
– دارایی میتواند در قالب pictorial نمایش داده شود.
– مجموعه ای از دارایی ها entity set هستند.

 1. Entity: An entity is a real time object that is different from others. An entity can defined using its attributes. They are a part of entity. Entities can be represented in pictorial form. Collection of similar entities is an entity set.

مثال:

2. Object: An entity that contains both attributes and actions is its object. It is defined by its life span, name and object identifier. Every object has two components: State and Behavior.

دارایی که شامل ویژگی و اکشن باشد را شی گویند. بر اساس 3 پارامتر (طول عمر، نام و object identifier ) تعریف میشود. هر شی دو جز دارد (state and behavior)

مقایسه:

1- دارایی همینجوری از بقیه قابل تمییز است—– شی نه.
2-دارایی شامل ویژگی است—– شی دارای (طول عمر، نام و object identifier) است.
3- دارایی به صورت یونیک قابل تمییز است.—– شی بر اساس identifier آن مشخص میشود
4- هر دارایی یک primary key دارد —–Object is a part of object oriented database.
5- دارایی بخشی از دیتابیسrealtional است —- شی بخشی از دیتابیس object oriented است.
6-

6- دارایی و نمایش در مستطیل و دیاگرام E-R —-اشیا به صورت گرافیکی نمایش داده نمیشوند
7-Attributes is a property of entity. —- ارث بری، انتزاع و… از ویژگی های شی هستند
8-مثال از دارایی: کامپیوتر————- مثال از شی : حداقل سن رای دادن 18 سال است.

ادامه از این رفرنس:

Databases are vital tools for storing, managing, and retrieving information.
They are also critical for building an e-commerce system.

دیتابیس در ایجاد یک سامانه ecommerce اهمیت زیادی دارد.

A well-structured database powers e-commerce and manages all the interactions within the system.

یک دیتا بیس خوب ، یک سامانه فروش الکترونیکی را قدرت میدهد و تمام تعاملات آن را مدیریت میکند.

سه ویژگی دیتا بیس خوب

یک دیتا بیس خوب شامل موارد زیر است:

  • 1- سادگی ساختار دیتابیس:
    ساده است اما تمام نیازهای مشتری را برطرف میسازد.
  • 2- پرفرومنس بالا:
    کوئری های دیتابیس سرعت بالایی دارند. و باید پرفورمنس دیتابیس طوری باشد که کاربر تجربه خوبی از خرید داشته باشد. بنابراین دیتابیس باید indexing خوبی داشته باشد و همچنین good performance optimization options.

  • 3- قابل اسکیل شدن و availability

قابلیت مدیریت کردن ترافیک بالا و همچنین قابلیت بزرگ شدن اشل کاری را داشته باشد.

براساس آنچه گفته شد، سه عامل کلیدی باید در یک سامانه e-commerce موجود باشد:

1- ساختار جداول دیتابیس – روابط بین آن – index های مناسب

2- نوع دیتابیس – این مورد باید بر اساس الزامات مورد نیاز و ساختار داده موجود تعیین شود.

3- زیرساخت دیتا بیس- مدیریت شده یا غیر آن –

In this article, we will review examples of e-commerce databases as they
relate to these three areas.

در این مقاله در مورد دیتابیس های ecommerce صحبت میکنیم که 3 مورد فوق را مربوط میشوند.

We will also explore database design
alternatives that simplifying data management for e-commerce, specifically
a product information manager

همچنین در مور روشهای alternative صحبت میکنیم که مدیریت دیتا در e-commerce را آسان کنند – بویژه مدیریت اطلاعات محصولات

Database Scope
The main consideration when designing the database is identifying the
functionalities offered by the e-commerce platform.

مهمترین عامل در طراحی دیتابیس، تشخیص قابلیت های مورد نیاز مشتری یا سیستم e-commerce است. یعنی اینکه چه میخواهیم.

These functionalities
can be further divided into core functions and additional functions.

قابلیت ها شامل اصلی ها و اضافی ها هستند. ساندویچ اصلیه، حالا سس اضافه هم ممکنه داشته باشیم. 🙂

Core functions

are the functions necessary for facilitating the day-to-day
operations of the e-commerce platform, including

  • user management,
  • product and inventory management,
  • shopping cart function,
  • payment management,
  • and shipping/logistics management.

قابلیت اصلی آن است که هر روز با آن سر و کار داریم. که شامل موارد زیر میشود:

  • مدیریت مشتریان
  • مدیریت فهرست محصولات
  • قابلیت کارت خرید shopping cart
  • مدیریت درگاه پرداخت
  • مدیریت ارسال کالا یا لاجستیک

Additional functions

are the nice-to-have functions for the e-commerce
platform that enhance the user experience for both end-users (customers)
and administrators (the business).

قابلیت های nice-to-have functions در یک پلتفرم e-Commerce باعث بهبود تجربه کاربری میشوند. چه تجربه مشتری (خریدار) و چه تجربه ادمین (فروشنده)

Additional functions include marketing
functions, help desk and support, advanced analytics, and third-party
integrations

قابلیت های غیر ضروری شامل موارد زیر است:

  • قابلیت های مارکتینگ– مثل تبلیغات بنری و …
  • پشتیبانی آنلاین
  • آنالیز پیشرفته
  • قابلیت حضور شخص سوم – تعامل با دیگری- مثلا فروش کالای دیجی کالا در سایت خودمان

Core functions
In this section, let’s see how to structure the database to facilitate the core
functions.

حالا بریم ببینیم چطور قابلیت اصلی رو پیاده سازی کنیم.

The following table structure is an example of a database design
that covers the core functionality of an e-commerce platform

ساختار جداول زیر مثالهایی از طراحی دیتابیس هستند که قابلیت اصلی پلتفرم e-commerce را پوشش میدهند.

تفسیر جدول 1

بخشی از آنچه در تصویر بالا مشاهده میشود:

  • یک جدول به نام product_category داریم. که یک id دارد که همین عامل ارتباط آن با جدول محصول شده است.
  • یک جدول به نام product داریم که عامل مشترک آن با product_id یک شی به نام category_id است. یعنی برای اینکه بدانیم یک product در چه category قرار دارد به سادگی از عامل مشترک آن باجدولcategory_id استفاده کرده ایم.
  • جدول لیست محصولات داریم به نام product_inventory که id آن در جدول product نیز وجود دارد. زیرا باید بدانیم هر محصول در فهرست محصولات در چه جایگاهیی قرار دارد.
  • جدول تخفیف یا discount – یک جدول داریم که تخفیفات در آن است . مثلا فرض کنیم تخفیف yalda که نام آن است و یک id که شماره آن است. حال این id در جدول محصول نیز وجود دارد. بنابراین محصول اگر تخفیف داشته باشد و با id شماره یک مشخص شده باشد، متوجه میشویم که آن محصول تخفیفyalda در موردش صدق میکند.
  • تمام 🙂

This example covers all the basics of an e-commerce platform.

این مثالها تمامی اصول پلتفرم های ecommerce را پوشش میدهند.

Here, the table fields and indexes depending on the design of the overall platform.

در اینجا ردیف و ستون جداول به طراحی جداول بستگی دارند.

It contains three separate sections for user management, product
management, and shopping process.

که شامل سکشن مجزا میشوند، 1- مدیریت یوزر – 2 مدیریت محصول – 3- فرآیند خرید

Let’s have a closer look at each

بریم دقیق تر بررسیشون کنیم.

مدیریت یوزر

We have created a user table that contains all the user details along

with user_payment and user_address tables to store multiple addresses and payment details of users.

در جدول user کاربر یک id دارد که این id در دو جدول دیگر نیز به کار رفته است1- پرداخت کاربر 2- آدرس کاربر

This structure offers more granular control over data while eliminating duplicate records.

این ساختار ضمن حذف رکوردهای تکراری، کنترل گرانول (موردی، دانه ای، تکی -) بیشتری بر روی داده ها ارائه می دهد.

Another way to manage users is by creating two separate user tables for end-users and administrators and assigning relationships according to their requirements, as shown below.

راه دیگر برای مدیریت کاربران، ایجاد دو جدول کاربری مجزا برای کاربران نهایی و مدیران و تخصیص روابط بر اساس نیاز آنها است، همانطور که در زیر نشان داده شده است.

Product management

Managing products is not simply about maintaining a list of products.

مدیریت محصول ، به تنهایی فقط شامل مدیریت لیست محصولات نمیشود.

You also have to manage the inventory, discounts, categories, and other attributes of the products.

زیرا باید سایر ویژگی های آنان نیز مدیریت شود. مثل (لیست محصول – تخفیفات – دسته مرتبط با آنها – )

So always focus on simplifying the data structure while reducing duplicates. In the following table structure, the main product table contains information about the products.

بنابراین همیشه سعی داریم ساختار داده را ساده کنیم، ضمن اینکه موارد duplicate را کم میکنیم. در ساختار جدول زیر جدول اصلی محصول، شامل اطلاعاتی در مورد محصول است:

There are two other separate tables called discountproduct_inventory, and product_category that are connected to it through database relationships. This approach provides the greatest level of flexibility to the database.

سه جدول دیگر به نام تخفیف discount و لیست محصول product inventory و دسته محصول product category وجود دارد که به جدول اصلی محصول از طریق روابط در DB متصل هستند. این روش باعث ایجاد نوعی flexibility در DB میشود.

For instance, we can simply query the product_inventory table to check for inventory without going through all the data associated with other related tables.

برای مثال میتوانیم به سادگی در جدول فهرست محصول کوئری بزنیم که فهرست را چک کرده باشیم. بدون اینکه نیاز باشد به سراغ تمام دیتا ها در جداول دیگر برویم (صرفه جویی زمانی و محاسباتی)

This is also a good place to utilize indexes to increase the performance of the database.

برای افزایش flexibility از index در جداول استفاده میشود.

Shopping process

This is the most critical and complex part when it comes to designing the database.

این بخش مهمترین بخش از طراحی DB است.

The shopping process will guide a user to search the products, add the desired products to the shopping cart, and finally complete the transaction using a payment provider.

پروسه خرید به کاربر کمک میکند که محصولات را سرچ کند و محصول دلخواه را به shopping card بیافزاید و در نهایت تراکنش را با سرویس دهنده payment service به انجام برساند.

The heart of the e-commerce process connects users with products.

A good chunk of design effort should be exhausted to streamline the shopping process.

قلب فرآیند تجارت الکترونیک، کاربران را با محصولات مرتبط می کند. بخش خوبی از تلاش طراحی باید برای ساده کردن فرآیند خرید تمام شود.

In the example above, there are shopping_session and cart_item as temporary data stores that only store the shopping session information of the current user until the order is confirmed and the data is moved to permanent storage tables with the payment details (order_detailsorder_items, and payment_details).

در مثال بالا، shopping_session و cart_item به عنوان انبار داده موقت وجود دارند که فقط اطلاعات shopping_session کاربر فعلی را تا زمانی که سفارش تأیید شود ، نگه میدارند. در صورت تایید نهایی داده‌ها در جداول ذخیره دائمی با جزئیات پرداخت (order_details، order_items و payment_details) ذخیره می‌شوند.

As shown in this section, the scope of the database structure is determined by the overall functionality of the platform.

همانطور که در این بخش نشان داده شده است، محدوده ساختار پایگاه داده با عملکرد کلی پلت فرم تعیین می شود.

Therefore, it is paramount that you properly define the required functionality before diving into designing the database.

بنابراین، بسیار مهم است که قبل از شروع به طراحی پایگاه داده، عملکرد مورد نیاز را به درستی تعریف کنید.

This way, you can create a clearly defined data structure with enough flexibility to support future expansions.

به این ترتیب، می توانید یک ساختار داده به وضوح تعریف شده با انعطاف کافی برای پشتیبانی از توسعه های آینده ایجاد کنید.

Database Type

The next consideration is to determine the type of database.

قدم بعدی تعیین نوع DB است.

To have the best e-commerce DB design, you must first consider two main database types:

دو نوع DB اصلی داریم:

RDBMS or NoSQL databases.

Relational e-commerce database example

مثال از نوع رابطه ای:

از جدول استفاده میشود و شبیه به spreadsheets است. که ردیف و ستون دارد که برای بازیابی دیتا از آن استفاده میشود.

This is similar to spreadsheets and uses tables, columns, and rows to organize and retrieve data.

از زبان SQL استفاده میشود و تمام داده ها به هم مرتبط هستند.

It is built using the standard query language (SQL) and all the data is related to each other. 

مثال هایی از DB رابطه ای در زیر آورده شده است:

  • MySQL
  • PostgreSQL
  • MariaDB
  • Microsoft SQL
  • Amazon RDS
  • Azure SQL

Examples include , , MariaDB, Microsoft SQL, Amazon RDS, and Azure SQL Database.

Many e-commerce sellers use a relational database design centered around the following tables:

بسیاری از افراد فعال در حوزه تجارت الکترونیک از جداول زیر استفاده میکنند:

  • customers table
  • orders table

products table, customers table, and orders table. Additional tables can be added as required to support shipping, categories, product reviews, and more.

جداول اضافی میتوانند اضافه شوند که خرید را تسهیل کنند، مانند:

  • categories
  • product reviews

We have covered the database structure in the previous section where the scope of the database was defined.

در مورد ساختار DB صحبت کردیم

Below, you can see a diagram of a simple e-commerce database design built using MySQL.


در زیر یک ساختار ساده از E-commerce تولید شده توسط mysql دیده میشود:

ادامه از این لینک قابل مطالعه است.

تمرین عملی:

با دستور زیر یک دیتا بیس تست میسازیم:

CREATE DATABASE testDB

و با کد زیر ازDB های موجود استعلام میگیریم:

SHOW DATABASES

تشخیص مهره شطرنج

ابتدا کتابخانه های مورد نیاز را فراخوانی میکنیم:

import cv2
import numpy as np
import pandas as pd
import glob
import os
import re
from tqdm import tqdm
import matplotlib as mpl
import matplotlib.pyplot as plt

با دستور زیر فولدری که تصاویرtrain در آن قرار دارند را یه یک متغیر نسبت میدهیم:

CHESS_BOARD_DIR = glob.glob('train/*.*')
CHESS_BOARD = []

تابع زیر نام یک فایل تصویری را میگیرد و باتوجه به کد fen آنها را تفکیک میکند.

def normalizeBoardName(boardName):
    mine_code=[]
    longName=boardName.split('-')
    for word in longName:
        for letter in word:
            if letter.isnumeric():
                for i in range(int(letter)):
                    mine_code.append(str(0))
            else:
                mine_code.append(letter)
                
    return mine_code

ما با یک الگوریتم هر خانه از صفحه شطرنج را جدا کرده و آن را در یک فایل csv ذخیره کرده ایم:

df = pd.DataFrame()
points=[5,19,  20,34,  35,49,  50,64,  64,78,  79,93,  94,108,  109,123]
my_counter=0
for idx, path in enumerate(CHESS_BOARD_DIR):
    my_counter_local=0
    if idx==1000:
        break
    
    image = cv2.imread(path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    CHESS_BOARD = [path[6:-4], image]
    PEACE_CODES=normalizeBoardName(CHESS_BOARD[0])
    #--------------------------------------------
    
    for j in range(0,len(points) , 2):# columns
        
        for i in range (0,len(points) , 2):# rows
            for board in CHESS_BOARD:
                img_small = CHESS_BOARD[1]
                img_small=img_small[points[j]:points[j+1],points[i]:points[i+1]] #first col , then row
                
#         cv2.imshow('s', img_small)
#         cv2.waitKey(0)
#         cv2.destroyAllWindows()
#         print(type(img_small))


            img_small = img_small.reshape(14*14)
            img_small=np.append(img_small,PEACE_CODES[my_counter_local])
            df[my_counter]=img_small
            my_counter+=1
            my_counter_local+=1
        
# df=df.T
# df_1=df[df[196]!='0']
# df_1.shape
# df_1.head()
# df_1.to_csv('all_images.csv', sep=',')

حال با کد زیر آن را فراخوانی میکنیم:

df_1=pd.read_csv('all_images.csv')
df_1= df_1.drop('Unnamed: 0',axis=1)

هر ردیف از csv فراخوانی شده یک تصویر را نشان میدهد.

با کد زیرتمام ستون ها به جز ستون آخر را در متغیر X میریزیم:

X = df_1.drop('196', axis=1)

ستون آخر را در متغیرY میریزیم:

y = df_1['196']

با دستور زیر مقادیر یکتایy را مشاهده میکنیم:

y.unique()

برای استفاده کردن از حروف در مدل ماشین لرنینگ با استفاده از label encoder آن را به کد تبدیل میکنیم:

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
label = le.fit_transform(df_1['196'])
label

حال دیتای خود را به تست و train تقسیم میکنیم:

X_train, X_test, \
y_train, y_test =\
X[:14000], X[14000:], y[:14000], y[14000:]

حال از sgdclassifier استفاده کرده و مدل خود train میکنیم:

from sklearn.linear_model import SGDClassifier
sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train)

مدل ما آموزش دیده است و با کد زیر یک نمونه به آن میدهیم تا برای ما پیش بینی کند:

sgd_clf.predict(pd.DataFrame(X.loc[0]).T)

106 سوال مصاحبه SQL

رفرنس- لینک

1- RDBMS چیست؟

RDBMS داده ها را در مجموعه ای از جدول ها ذخیره میکنند که بین آنها ارتباط وجود دارد.

RDBMS store data into a collection of tables, which is related
by common fields between the columns of the table. It also
provides relational operators to manipulate the data stored
into the tables.
Example: SQL Server.

2- SQL چیست؟

زبان ساختار یافته برای ارسال درخواست که برای ارتباط با دیتابیس مناسب است. یک زبان استاندارد است که برای بازیابی یا retrieval و به روز رسانی یا update و ایجاد یا insertion یا حذف یا delete داده از دیتابیس مناسب است

SQL stands for Structured Query Language , and it is used to
communicate with the Database. This is a standard
language used to perform tasks such as retrieval, updates,
insertion and deletion of data from a database.
Standard SQL Commands are Select.

3- دیتا بیس چیست؟

مجموعه ساختار یافته از دیتا. برای مثال دیتابیس مدیریت مدرسه یا دیتابیس اطلاعات بانک و …

A Database is an organized form of data for easy access,
storing, retrieval and managing of data. This is also known
as structured form of data which can be accessed in many
ways.
Example: School Management Database, Bank Management
Database.

4- primary key چیست؟

مجموعه ای از فیلدها (منظور شاخص یا index است) که به صورت یکتا هر ردیف را نشان میدهد. یک مشخصه الزامی آن این است که نباید null باشد.

A primary key is a combination of fields which uniquely
specify a row. This is a special kind of unique key, and it has
implicit NOT NULL constraint. This means, Primary key values
cannot be NULL.

5- unique key چیست؟

A Unique key constraint uniquely identifies each record in a
database. This provides uniqueness for the column or set of
columns. A Primary key constraint has automatic unique
constraint defined on it.

تفاوت آن با primary key چیست؟

There can be many unique
constraints defined per table, but only one Primary key
constraint defined per table.

6- foreign key چیست؟

یک جدول است که میتواند بعنوان primary key برای جدول دیگر باشد.

A foreign key is one table which can be related to the
primary key of another table. Relationships need to be
created between two tables by referencing the foreign key
with the primary key of another table

7- تفاوت بین spreadsheets و databases در چیست؟

spreadsheets

A file that exists of cells in rows and columns and can help
arrange, calculate and sort data.

It can have numeric
values, text, formulas and functions. It features columns and
rows to keep inserted information legible and simple to
understand. It is an electronic graph sheet

databases.

It is an organized collection of data arranged for ease and
speed of search and retrieval. It contains multiple tables. A
database engine can sort, change or serve the information
on the database. Basically, it is a set of information which is
held in a computer

8-What are table and fields

A table is a set of data that are organized in a model with
Columns and Rows. Columns can be categorized as vertical,
and Rows are horizontal.

تفاوت field و record

A table has a specified number of
column called fields but can have any number of rows
which are called records.


Example: مثال
Table: Employee.
Field: Emp ID, Emp Name, Date of Birth.
Data: 201456, David, 11/15/1960.

9- انواع زبان SQL

DDL

DDL changes the structure of the table like creating a table,
deleting a table, altering a table, etc. All the commands of
DDL are auto-committed which means that it permanently
saves all the changes in the database.
Some commands that come under DDL:
CREATE; ALTER; DROP; TRUNCATE

DML

DML commands are used to modify the database. It is
responsible for all forms of changes in the database. The
commands of DML are not auto-committed which means
that it can’t permanently save all the changes in the
database.
Some commands that come under DML:
INSERT; UPDATE; DELETE

DCL = data control language

DCL commands are used to grant and take back authority
from any database user.
Some commands that come under DCL:
Grant; Revoke

TCL=transaction control language

TCL commands can only be used with DML commands like
INSERT, DELETE and UPDATE. These operations are
automatically committed in the database, which is why they
cannot be used while creating tables or dropping them.
Some commands that come under TCL:
COMMIT; ROLLBACK; SAVEPOINT

DQL=data query language

DQL is used to fetch the data from the database.
It uses only one command:
SELECT

10-What is normalization

فرآیند کاهش ناهمواری در داده را گویند
Normalization is the process of minimizing redundancy and
dependency by organizing fields and tables of a database.
The main aim of Normalization is to add, delete or modify
fields that can be made in a single table

11- What is denormalization

اگر بخواهیم دیتای اصلی را بازیابی کنیم از این روش استفاده میکنیم.

Denormalization is a technique used to access the data
from higher to lower normal forms of database. It is also a
process of introducing redundancy into a table by
incorporating data from the related tables.

12- Explain the different types of normalization.

نوع اول: حذف تمامی مقادیریا ستونهای دابل

First Normal Form (1NF): This should remove all
the duplicate columns from the table. Creation
of tables for the related data and identification
of unique columns.

نوع دوم: اجرای تمام الزامات نوع اول+ …

Second Normal Form (2NF): Meeting all
requirements of the first normal form. Placing the
subsets of data in separate tables and Creation
of relationships between the tables using primary
keys.

نوع سوم:اجرای تمام الزامات نوع دوم + …

Third Normal Form (3NF): This should meet all
requirements of 2NF. Removing the columns
which are not dependent on primary key
constraints.

نوع چهارم: اجرای تمام الزامات نوع سوم + …

Fourth Normal Form (4NF): Meeting all the
requirements of third normal form and it should
not have multi- valued dependencies.

13-What are views in SQL

یک جدول مجازی که …

A view is a virtual table which consists of a subset
of data contained in a table. Views are not virtually
present, and it takes less space to store. View can
have data of one or more tables combined, and it is
depending on the relationship.

14-What is join? Explain the different types.

زمانی که بخواهیم کوئری های ترکیبی بزنیم.

This is a keyword used to query data from more tables
based on the relationship between the fields of the
tables. Keys play a major role when JOINs are used.

There are various types of joins which can be used to
retrieve data and it depends on the relationship between
tables.

Left Outer Join: If we want all the records from left
table and only matching records from right table then
will use left outer join/left join.

Right Outer Join: If we want to display all the records
from right table and only matching records from left
table then will right outer join/right join

Full Outer Join: If we want to display all the records
from both the tables then will use full outer join

Inner Join: If we want only the matching records from
both the tables then will use Inner join/Simple join

15- What are the different types of indexes?

An index is a performance tuning method of allowing
faster retrieval of records from the table. An index creates
an entry for each value and makes it faster to retrieve
data

3 نوع ایندکس داریم:

Unique Index: This indexing does not allow the field to
have duplicate values if the column is unique indexed.
Unique index can be applied automatically when
primary key is defined

Clustered Index: This type of index reorders the
physical order of the table and search based on the
key values. Each table can have only one clustered
index

Non-Clustered Index: Non-Clustered Index does not
alter the physical order of the table and maintains
logical order of data. Each table can have 999 nonclustered indexes

16-What is a cursor in SQL?

A database Cursor is a control which enables traversal
over the rows or records in the table. This can be viewed
as a pointer to one row in a set of rows. Cursor is very
much useful for traversing such as for retrieval, addition
and removal of database records

17-What is query?

A DB query is a code written in order to get the
information back from the database. Queries can be
designed in such a way that it matches with our
expectation of the result set.

18-What is a subquery?

A subquery is a query within another query. The outer
query is called as main query, and inner query is called
subquery. SubQuery is always executed first, and the
result of subquery is passed on to the main query.

دو نوع سابکوئری داریم:

There are two types of subquery – Correlated and NonCorrelated.

A correlated subquery cannot be considered as an
independent query, whereas a Non-Correlated sub query
can be considered as independent query and the output
of subquery are substituted in the main query.

19-What is a trigger?

A DB trigger is a code or programs that
automatically execute with response to some event
on a table or view in a database. Mainly, trigger
helps to maintain the integrity of the database.
مثال : زمانی که یک دانش آموز به جدول اضافه شود…

Example: When a new student is added to the
student database, new records should be created in
the related tables such as the Exam, Score and
Attendance tables

20-Differentiate between the DELETE and TRUNCATE commands.

DELETE command is used to remove rows from
the table, and WHERE clause can be used for
conditional set of parameters. Commit and
Rollback can be performed after delete
statement

TRUNCATE removes all rows from the table.
Truncate operation cannot be rolled back

BI

از فرادرس-لینک

برای تصمیم گیری مبتنی بر داده

کاهش هزینه کسب و کار

مراحل:

  • شناسایی نیازمندی، موجودیت ، ابزار مناسب و تشکیل تیم تجاری
  • انجام فرآیند ETL و ایجاد انباره داده
  • طراحی ساخت کیوب تبولار که بار محاسبات به سرور بره
  • مصور سازی با ابزارهایی مانند tableau وpower BI و Qlik view
  • تحویل و آموزش

انبار داده یا data warehouse

ویژگی ها :

  • time variant یعنی به مرور زمان تاریخ توش جمع میشه
  • nonvolatile دیتا حذف نمیشه و پایداره
  • integrated از منابع مختلف دیتا جمع آوری میشه، یکپارچه میشه و بعد توی اون قرار میگیره
  • subject-oriented – موضوع محور- قابلیت جداسازی دیتا ها بر اساس موضوع
  • مفاهیم موجود:
    • dimension برای مثال در مورد فروش کالا انواع dimention به شرح زیر هستند:
      • تاریخ فروش
      • خریدار
      • قیمت فروش
      • نام کالا
    • fact اطلاعات کمی در آن قرار میگیرد.
      • ستونهای کلیدی دارد که مقدار ان از یکی از dimention ها می آید

برای رابطه بین fact و dimension دو مدل معروف وجود دارد:

1- ستاره ای یا star schema که fact در وسط قرار میگیرد و سایرdimension ها در اطراف آن

2- مدل دانه برفی: که مشابه ستاره ای است با این تفاوت که dimension ها میتوانند باهم ارتباط داشته باشند.

  • مفهوم junk dimension
  • مفهوم roleplaying dimension
    • معروف ترین آن تاریخ است.
  • مفهوم degenerate dimension

هوش تجاری سلف سرویس:

نسخه ها:

معرفی انواع نسخه ها

پایان بخش اول

بخش دوم – لینک – نصب برنامه

power query برای تمیز کردن داده ها یا etl کردن داده ها کاربرد دارد.

  • آشنایی با power query و اتصال به منبع داده
  • مدیریت سطر و ستون در پاور کوئری
  • کار با تاریخ و ساعت در پاور کوئری
  • ساخت مدل داده و ستون محاسباتی
  • آشنایی با زیان dax
  • تمرین 1 زبان dax-
  • تا فصل 4 انواع نمودار

knn با w3school

knn به معنای K-nearest neighbors است.

  • نوعی از supervised است.
  • can be used for classification or regression tasks
  • ایده اصلی این روش به شرح زیر است:
    • مشاهداتی که نزدیک ترین فاصله را به یک داده نوعی دارند بعنوان the most similar شناخته میشوند و بر این اساس میتوان دسته بندی را انجام داد.
  • انتخاب مقدار k در الگوریتم به معنای انتخاب تعداد همسایگی در نظر گرفته شده برای یک نقطع نوعی برای تعیین مشابهت می باشد.

ابتدا کتابخانه های مورد نیاز را فراخوانی میکنیم:

import matplotlib.pyplot as plt

x = [4, 5, 10, 4, 3, 11, 14 , 8, 10, 12]
y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]
classes = [0, 0, 1, 0, 0, 1, 1, 0, 1, 1]

plt.scatter(x, y, c=classes)
plt.show()

و خروجی زیر را مشاهده میکنیم:

کتابخانه knn را فراخوانی میکنیم:

from sklearn.neighbors import KNeighborsClassifier
data = list(zip(x, y))
knn = KNeighborsClassifier(n_neighbors=1)

knn.fit(data, classes)

در خط دوم مختصات ها را لیست کرده ایم و در خط بعدی از knn خواسته ایم که داده ها را با k=1 دسته بندی کند. یعنی برای هر نونه تنها به یک مورد از نزدیک ترین موارد به آن نگاه کند. در خط آخر گفته ایم که روی دیتای ما fit شود.

نکته:

  • تعداد class در این مسئله دو مورد است. 0 و یک . بنابراین این مسئله به روش رگرسیون لاجستیک نیز قابل حل است.

در کد زیر یک نمونه جدید داریم و از knn میخواهیم که با شرط k=1 این نمونه را در دسته مناسب قرار دهد:

new_x = 8
new_y = 21
new_point = [(new_x, new_y)]

در نتیجه کد زیر مشاهده میشود که این نمونه در دسته 0 قرار گرفته است:

prediction = knn.predict(new_point)
prediction

که کد زیر این موضوع را به صورت مصور نشان میدهد:

plt.scatter(x + [new_x], y + [new_y], c=classes + [prediction[0]])
plt.text(x=new_x-1.7, y=new_y-0.7, s=f"new point, class: {prediction[0]}")
plt.show()

نتیجه:

حال مقدار k را به 5 تغییر میدهیم و میبینیم که نمونه داده شده در دسته 1 قرار میگیرد:

knn = KNeighborsClassifier(n_neighbors=5)

knn.fit(data, classes)

prediction = knn.predict(new_point)

plt.scatter(x + [new_x], y + [new_y], c=classes + [prediction[0]])
plt.text(x=new_x-1.7, y=new_y-0.7, s=f"new point, class: {prediction[0]}")
plt.show()

نتیجه:

اختصاصی logistic regression

مرجع لینک

تشخیص سریع:

زمانی که خروجی ما از نوه باینری (0-1 ) باشد.
آیا باران میبارد یا نه- آیا دانش آموز قبول میشود یا نه. آیا فرد بیمار سرطان دارد یا نه. آیا مسافر تایتانیک زنده میماند یا نه.

مثال: دیابت – دیتاست با سه فیچر- لینک (خط 175)

تفاوت با رگرسیون خطی:

بیان اول:

رگرسیون خطی برای تشخیص یا پیش بینی مقادیر پیوسته خروجی کاربرد دارد. اما logistic regression برای تشخیص یا پیش بینی خروجی در حالت categorical کاربرد دارد. اما هر دو مورد در زیرمجموعه supervised learning قرار میگیرند:

بیان دوم:

در روش رگرسیون، هدف ما عددی(و اکثرا پیوسته) است در صورتیکه در روش لاجستیک جنس هدف ما از نوع باینری (و گسسته)هست (بلی و خیر یا صفر و یک و …)

در نوع Linear حساسیت به outlier بسیار زیاد است.

فرمول زیر مربوط به logistic regression است که به آن Sigmoid گویند:

توضیحات:

تکرار از دروس قبلی:

در حل مسائل به روش رگرسیون لاجستیک از تابع Sigmoid کمک می‌گیریم. رفتار این تابع طوری است که مقادیر در بازه صفر تا یک قرار می‌گیرند.(بالای شکل زیر). همانطور که در شکل مشخص است داده‌هایی که در بازه صفر تا نیم قرار گیرند، صفر تلقی شده و مقادیر بالای نیم را یک در نظر می‌گیریم.

ادامه:

اگر مقدار خروجی با به تابع sigmoid بدهیم، این تابع عدد احتمال مربوطه را که عددی بین 0 تا 1 است را به ما برمیگرداند. اگر عدد زیر نیم باشد، نتیجه No/fail یا مواردی از این قبیل است. اگر خروجی بزرگتر از نیم باشد، خروجی yes یا pass خواهد بود.

If we feed an output value to the sigmoid function, it will return the probability of the outcome between 0 and 1. If the value is below 0.5, then the output is return as No/Fail/Deceased (above example). If the value is above 0.5, then the output is returned as Yes/Pass/Deceased.

فرضیه ها در رگرسیون لاجستیک:

  • Independent variables show a linear relationship with the log of output variables.
    • فیچرها رابطه خطی با لگاریتم خروجی داشته باشند
  • Non-Collinearity between independent variables. That is, independent variables are independent of each other.
    • خود فیچرها نسبت به هم مستقل باشند.
  • Output variable is binary.
    • متغیر خروجی دو حالتی باشد(بله یا خیر، صفر یا یک ، بالا یا پایین)

انتخاب مهمترین فیچر ها

زمانی که تعداد فیچرها زیاد باشد، انتخاب فیچرهای مهم اهمیت زیادی در بهبود دقت سیستم دارد.

در بند 3 از این منبع در مورد نحوه انتخاب مهمترین فیچرها توضیح داده شده است.

آخرین مرور:

14 فروردین 402

جلسه 21

  • ویدئو – لینک
  • در مورد confidence interval که به آن CI هم میگویند.
  • سوال مصاحبه: CI چیست و چرا مهم است؟
  • این سوال در مصاحبه ها زیاد می آید.
  • برای درک مفهوم CI ، ابتدا باید مفهوم point estimate را بدانیم.
  • میدانیم که پیدا کردن مقدارmean یک دیتاست بزرگ کار سختی است. برای مثال همان شوال میانگین اندازه کوسه های تمام اقیانوس ها.
  • اما میتوانیم اندازه mean در sample را به راحتی اندازه بگیریم. که با x_bar نمایش داده میشود.
  • حال با داشتن X-bar سعی میکنیم مقدار میو که میانگین کل جمعیت است را بدست بیاوریم.
  • این x-bar همان point estimate است.
  • همچنین بر اساس central limit theorem میدانیم که از طریق محاسبه x-bar میتوانیم به مقدار میو برسیم.
  • حال CI به ما میگوید که با چه احتمالی مقدار میو در چه بازه ای (lower limit و upper limit) قرار میگیرد.
  • در واقع اگر سوال از ما بپرسد که CI برابر با 95 درصد باشد، در واقع منظورمان بازه نشان داده شده در شکل زیر است. که در واقع همان lower limit و higher limit را نشان میدهد.
  • دو روش برای حل این مسئله وجود دارد:
    • اگر standard deviation داده شده باشد. که در این حالت از z-test استفاده میکنیم.
    • اگر STD را نداشته باشیم. در این حالت از t-test استفاده میکنیم.
  • مسئله ما این است: اندازه متوسط کوسه ها چقدر است؟
  • یک روش حل با CI است
  • یک روش حل با central limit theory است.
  • فرض کنیم که std به ما داده شده و برابر با 100 باشد(sigma=100)
  • همچنین x-bar=500 و n=30 داده شده باشد.
  • فرمول CI به شرح زیر است:
  • C.I=Point Estimate+/- Margin Error
  • یا:

که برابر است با :

حروف کنار z یک دوم آلفا هست که مقدار و چیستی آن را الان توضیح میدهیم .

  • آلفا همان significance value ما می باشد. که در این جمع دو مقدار موجود در tail یعنی 2*2.5 و برابر با 5 درصد یا 0.05 می باشد. (همان چیزی که میگوییم P-value باید کمتر از 0.05 باشد)
  • حال به سادگی با توجه به داده های مسئله جایگذاری میکنیم:
  • n طبق داده مسئله برابر با 30 است.
  • برای پیدا کردن عدد( Z- 0.05/2 ) باید در گوگل عدد z table را سرچ کنیم. لینک

تا دقیقه 8:50

تکرار:

  • روش z-test زمانی به کار میرود که standard deviationجمعیت به ما داده شده باشد.(همان سیگما)
  • اگر این مقدار به ما داده نشده باشد، باید از t-test استفاده کنیم.

جلسه 20

ویدئو – لینک

کل ویدیو 2 دقیقه بود

سوال :

اگر بخواهیم سایز متوسط تمام کوسه های موجود را بدست بیاوریم چه باید کنیم؟

جواب: به کامنت های ویدئو مراجعه شود. همچنین پست بعدی

برخی از جواب ها :

  • 1- find size of 1000 sharks in the sea.
    • پیدا کردن سایز هزار کوسه
  • 2- using bootstrapping create 1000 subsample sets of 300 sharks. Do Sampling with replacement. to create those 1000 subsets.
    • با استفاده از روش بو ت استرپ به تعداد هزار نمونه داده 300 تایی تشکیل میدهیم
  • 3- محاسبه کردن میانگین هر دیتابیس 100 تایی
  • 4- Arrange them in ascending order مرتب کردن از زیاد به کم
  • 5- Take 25th mean value and 975th value.
  • 6-the values obtained is our 95 percentile range for mean size of sharks.

نمونه جواب2:

  • Define the confidence level (usually around 95%)
  • Use sample sharks to measure
  • Calculate the mean and standard deviation of the lengths
  • Determine t-statistics values
  • Determine the confidence interval in which the mean length lies