C3_W1_KMeans_Assignment || 吴恩达可选实验室

练习一

# UNQ_C1
# GRADED FUNCTION: find_closest_centroids

def find_closest_centroids(X, centroids):
    """
    Computes the centroid memberships for every example
    
    Args:
        X (ndarray): (m, n) Input values      
        centroids (ndarray): k centroids
    
    Returns:
        idx (array_like): (m,) closest centroids
    
    """

    # Set K
    K = centroids.shape[0]

    # You need to return the following variables correctly
    idx = np.zeros(X.shape[0], dtype=int)

    ### START CODE HERE ###
    for i in range(X.shape[0]):
        distance = []
        for j in range(centroids.shape[0]):
            norm_ij = np.linalg.norm(X[i] - centroids[j])
            distance.append(norm_ij)
        idx[i] = np.argmin(distance)

    ### END CODE HERE ###
    
    return idx

练习二:

# UNQ_C2
# GRADED FUNCTION: compute_centpods

def compute_centroids(X, idx, K):
    """
    Returns the new centroids by computing the means of the 
    data points assigned to each centroid.
    
    Args:
        X (ndarray):   (m, n) Data points
        idx (ndarray): (m,) Array containing index of closest centroid for each 
                       example in X. Concretely, idx[i] contains the index of 
                       the centroid closest to example i
        K (int):       number of centroids
    
    Returns:
        centroids (ndarray): (K, n) New centroids computed
    """
    
    # Useful variables
    m, n = X.shape
    
    # You need to return the following variables correctly
    centroids = np.zeros((K, n))
    
    ### START CODE HERE ###
    for k in range(K):
        points = X[idx == k]
        if(len(points) > 0):
            centroids[k] = np.mean(points, axis = 0)
        else:
            centroids[k] = X[np.random.choice(m, 1)].flatten()
    ### END CODE HERE ## 
    
    return centroids

K-means应用:图片压缩,通过改变颜色的数量颜色的数量就是K,以下是K= 16时,对图片进行压缩:

当K = 32时,

K越大,图片越清晰。

想要资料可以私信。

### 实现 iOS WebView 中集成 Sign in with Apple 为了实现在 iOS 的 `WKWebView` 中集成苹果登录 (Sign in with Apple),可以采用以下方法: #### 使用 ASWebAuthenticationSession 进行 OAuth 登录流程 由于 HTML、CSS 和 JavaScript 可以在 Web 视图中提供交互体验,而无需使用 Swift 或 Objective-C 来处理前端逻辑[^1]。然而,在 iOS 应用程序内实现 Sign in with Apple 功能则需要一些原生代码来启动认证过程。 对于现代 iOS 开发而言,推荐的方式是利用 `ASWebAuthenticationSession` API 完成 OAuth 流程中的重定向 URI 处理部分。这允许应用程序安全地执行第三方网站的身份验证操作,并返回授权码给调用者。 具体来说,可以在 AppDelegate 文件里配置好 URL Scheme 并注册回调地址;之后通过创建并展示一个基于 `ASWebAuthenticationSession` 的实例发起请求到 Apple ID 认证服务器获取用户的同意和凭证信息[^2]。 一旦获得了有效的身份令牌,则可以通过后端服务完成最终的账户关联或会话建立工作。需要注意的是,如果遇到类似 "503 Service Temporarily Unavailable" 错误提示时,可能是由于网络连接不稳定或者是临时性的服务中断所引起的,建议稍后再试或者检查是否有其他环境因素影响到了正常的通信链路[^3]。 另外,考虑到安全性方面的要求,务必妥善保管从客户端收到的所有敏感数据项,比如刷新令牌 (`remind_in`) 等参数应该被加密存储起来用于后续可能发生的自动续期场景下再次激活已存在的访问权限[^4]。 ```swift import AuthenticationServices func startSignInWithAppleFlow() { let appleIDProvider = ASAuthorizationAppleIDProvider() let request = appleIDProvider.createRequest() request.requestedScopes = [.fullName, .email] let authController = ASAuthorizationController(authorizationRequests: [request]) // 设置代理对象以接收回调通知 authController.delegate = self as? ASAuthorizationControllerDelegate // 启动授权控制器显示界面供用户输入凭证 authController.performRequests() } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值