fix: converted single to triple backticks11 (#36238)
This commit is contained in:
@@ -60,44 +60,45 @@ localeTitle: خوارزميات التجميع
|
||||
|
||||
في ما يلي مثال تجميع في Python يستخدم " [مجموعة بيانات Iris"](https://www.kaggle.com/uciml/iris)
|
||||
|
||||
`import pandas as pd
|
||||
import numpy as np
|
||||
iris = pd.read_csv('Iris.csv')
|
||||
del iris['Id']
|
||||
del iris['SepalLengthCm']
|
||||
del iris['SepalWidthCm']
|
||||
|
||||
from matplotlib import pyplot as plt
|
||||
# k is the input parameter set to the number of species
|
||||
k = len(iris['Species'].unique())
|
||||
for i in iris['Species'].unique():
|
||||
# select only the applicable rows
|
||||
ds = iris[iris['Species'] == i]
|
||||
# plot the points
|
||||
plt.plot(ds[['PetalLengthCm']],ds[['PetalWidthCm']],'o')
|
||||
plt.title("Original Iris by Species")
|
||||
plt.show()
|
||||
|
||||
from sklearn import cluster
|
||||
del iris['Species']
|
||||
kmeans = cluster.KMeans(n_clusters=k, n_init=10, max_iter=300, algorithm='auto')
|
||||
kmeans.fit(iris)
|
||||
labels = kmeans.labels_
|
||||
centroids = kmeans.cluster_centers_
|
||||
|
||||
for i in range(k):
|
||||
# select only data observations from the applicable cluster
|
||||
ds = iris.iloc[np.where(labels==i)]
|
||||
# plot the data observations
|
||||
plt.plot(ds['PetalLengthCm'],ds['PetalWidthCm'],'o')
|
||||
# plot the centroids
|
||||
lines = plt.plot(centroids[i,0],centroids[i,1],'kx')
|
||||
# make the centroid x's bigger
|
||||
plt.setp(lines,ms=15.0)
|
||||
plt.setp(lines,mew=2.0)
|
||||
plt.title("Iris by K-Means Clustering")
|
||||
plt.show()
|
||||
`
|
||||
```python
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
iris = pd.read_csv('Iris.csv')
|
||||
del iris['Id']
|
||||
del iris['SepalLengthCm']
|
||||
del iris['SepalWidthCm']
|
||||
|
||||
from matplotlib import pyplot as plt
|
||||
# k is the input parameter set to the number of species
|
||||
k = len(iris['Species'].unique())
|
||||
for i in iris['Species'].unique():
|
||||
# select only the applicable rows
|
||||
ds = iris[iris['Species'] == i]
|
||||
# plot the points
|
||||
plt.plot(ds[['PetalLengthCm']],ds[['PetalWidthCm']],'o')
|
||||
plt.title("Original Iris by Species")
|
||||
plt.show()
|
||||
|
||||
from sklearn import cluster
|
||||
del iris['Species']
|
||||
kmeans = cluster.KMeans(n_clusters=k, n_init=10, max_iter=300, algorithm='auto')
|
||||
kmeans.fit(iris)
|
||||
labels = kmeans.labels_
|
||||
centroids = kmeans.cluster_centers_
|
||||
|
||||
for i in range(k):
|
||||
# select only data observations from the applicable cluster
|
||||
ds = iris.iloc[np.where(labels==i)]
|
||||
# plot the data observations
|
||||
plt.plot(ds['PetalLengthCm'],ds['PetalWidthCm'],'o')
|
||||
# plot the centroids
|
||||
lines = plt.plot(centroids[i,0],centroids[i,1],'kx')
|
||||
# make the centroid x's bigger
|
||||
plt.setp(lines,ms=15.0)
|
||||
plt.setp(lines,mew=2.0)
|
||||
plt.title("Iris by K-Means Clustering")
|
||||
plt.show()
|
||||
```
|
||||
|
||||
بما أن نقاط البيانات تنتمي عادة إلى مساحة عالية الأبعاد ، فإن مقياس التشابه غالباً ما يتم تعريفه على أنه مسافة بين متجهين (Euclidean ، Manhathan ، Cosine ، Mahalanobis…)
|
||||
|
||||
|
||||
@@ -10,31 +10,32 @@ localeTitle: الانحدارالخطي
|
||||
|
||||
في بايثون:
|
||||
|
||||
`#Price of wheat/kg and the average price of bread
|
||||
wheat_and_bread = [[0.5,5],[0.6,5.5],[0.8,6],[1.1,6.8],[1.4,7]]
|
||||
|
||||
def step_gradient(b_current, m_current, points, learningRate):
|
||||
b_gradient = 0
|
||||
m_gradient = 0
|
||||
N = float(len(points))
|
||||
for i in range(0, len(points)):
|
||||
x = points[i][0]
|
||||
y = points[i][1]
|
||||
b_gradient += -(2/N) * (y - ((m_current * x) + b_current))
|
||||
m_gradient += -(2/N) * x * (y - ((m_current * x) + b_current))
|
||||
new_b = b_current - (learningRate * b_gradient)
|
||||
new_m = m_current - (learningRate * m_gradient)
|
||||
return [new_b, new_m]
|
||||
|
||||
def gradient_descent_runner(points, starting_b, starting_m, learning_rate, num_iterations):
|
||||
b = starting_b
|
||||
m = starting_m
|
||||
for i in range(num_iterations):
|
||||
b, m = step_gradient(b, m, points, learning_rate)
|
||||
return [b, m]
|
||||
|
||||
gradient_descent_runner(wheat_and_bread, 1, 1, 0.01, 100)
|
||||
`
|
||||
```py
|
||||
#Price of wheat/kg and the average price of bread
|
||||
wheat_and_bread = [[0.5,5],[0.6,5.5],[0.8,6],[1.1,6.8],[1.4,7]]
|
||||
|
||||
def step_gradient(b_current, m_current, points, learningRate):
|
||||
b_gradient = 0
|
||||
m_gradient = 0
|
||||
N = float(len(points))
|
||||
for i in range(0, len(points)):
|
||||
x = points[i][0]
|
||||
y = points[i][1]
|
||||
b_gradient += -(2/N) * (y - ((m_current * x) + b_current))
|
||||
m_gradient += -(2/N) * x * (y - ((m_current * x) + b_current))
|
||||
new_b = b_current - (learningRate * b_gradient)
|
||||
new_m = m_current - (learningRate * m_gradient)
|
||||
return [new_b, new_m]
|
||||
|
||||
def gradient_descent_runner(points, starting_b, starting_m, learning_rate, num_iterations):
|
||||
b = starting_b
|
||||
m = starting_m
|
||||
for i in range(num_iterations):
|
||||
b, m = step_gradient(b, m, points, learning_rate)
|
||||
return [b, m]
|
||||
|
||||
gradient_descent_runner(wheat_and_bread, 1, 1, 0.01, 100)
|
||||
```
|
||||
|
||||
المثال رمز من [هذه المقالة](http://blog.floydhub.com/coding-the-history-of-deep-learning/) . كما يشرح نزول التدرج والمفاهيم الأساسية الأخرى للتعلم العميق.
|
||||
|
||||
@@ -42,23 +43,24 @@ localeTitle: الانحدارالخطي
|
||||
|
||||
في بايثون: تطبيق مباشرة باستخدام مكتبة scikit ، مما يجعل من السهل استخدام الانحدار الخطي حتى على مجموعات البيانات الكبيرة.
|
||||
|
||||
`import pandas as pd
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.linear_model import LinearRegression as lr
|
||||
train = pd.read_csv('../input/train.csv')
|
||||
test = pd.read_csv('../input/test.csv')
|
||||
X = train.iloc[:, 0:4].values
|
||||
y = train.iloc[:, 4].values
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
|
||||
X_train
|
||||
model = lr()
|
||||
model.fit(X_train, y_train)
|
||||
print(model.score(X_train,y_train))
|
||||
y_pred_class = model.predict(X_test)
|
||||
model.score(X_train,y_train)
|
||||
print(model.coef_)
|
||||
print(model.intercept_)
|
||||
# calculate accuracy
|
||||
from sklearn import metrics
|
||||
print(metrics.accuracy_score(y_test, y_pred_class))
|
||||
`
|
||||
```py
|
||||
import pandas as pd
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.linear_model import LinearRegression as lr
|
||||
train = pd.read_csv('../input/train.csv')
|
||||
test = pd.read_csv('../input/test.csv')
|
||||
X = train.iloc[:, 0:4].values
|
||||
y = train.iloc[:, 4].values
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
|
||||
X_train
|
||||
model = lr()
|
||||
model.fit(X_train, y_train)
|
||||
print(model.score(X_train,y_train))
|
||||
y_pred_class = model.predict(X_test)
|
||||
model.score(X_train,y_train)
|
||||
print(model.coef_)
|
||||
print(model.intercept_)
|
||||
# calculate accuracy
|
||||
from sklearn import metrics
|
||||
print(metrics.accuracy_score(y_test, y_pred_class))
|
||||
```
|
||||
Reference in New Issue
Block a user